Skip to content

Commit

Permalink
Merge pull request #445 from spsanderson/development
Browse files Browse the repository at this point in the history
Fixes #428
  • Loading branch information
spsanderson authored Apr 25, 2024
2 parents fe89667 + f31ab66 commit 1531ec9
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export(util_geometric_param_estimate)
export(util_geometric_stats_tbl)
export(util_hypergeometric_param_estimate)
export(util_hypergeometric_stats_tbl)
export(util_logistic_aic)
export(util_logistic_param_estimate)
export(util_logistic_stats_tbl)
export(util_lognormal_param_estimate)
Expand Down
79 changes: 79 additions & 0 deletions R/utils-aic-logistic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' Calculate Akaike Information Criterion (AIC) for Logistic Distribution
#'
#' This function calculates the Akaike Information Criterion (AIC) for a logistic distribution fitted to the provided data.
#'
#' @family Utility
#' @author Steven P. Sanderson II, MPH
#'
#' @description
#' This function estimates the location and scale parameters of a logistic
#' distribution from the provided data using maximum likelihood estimation,
#' and then calculates the AIC value based on the fitted distribution.
#'
#' @param .x A numeric vector containing the data to be fitted to a logistic distribution.
#'
#' @details
#' This function fits a logistic distribution to the provided data using maximum
#' likelihood estimation. It estimates the location and scale parameters
#' of the logistic distribution using maximum likelihood estimation. Then, it
#' calculates the AIC value based on the fitted distribution.
#'
#' Initial parameter estimates: The function uses the method of moments estimates
#' as starting points for the location and scale parameters of the logistic
#' distribution.
#'
#' Optimization method: The function uses the optim function for optimization.
#' You might explore different optimization methods within optim for potentially
#' better performance.
#'
#' Goodness-of-fit: While AIC is a useful metric for model comparison, it's
#' recommended to also assess the goodness-of-fit of the chosen model using
#' visualization and other statistical tests.
#'
#' @examples
#' # Example 1: Calculate AIC for a sample dataset
#' set.seed(123)
#' x <- rlogis(30)
#' util_logistic_aic(data)
#'
#' @return
#' The AIC value calculated based on the fitted logistic distribution to the provided data.
#'
#' @name util_logistic_aic
NULL

#' @export
#' @rdname util_logistic_aic
util_logistic_aic <- function(.x) {
# Tidyeval
x <- as.numeric(.x)

# Negative log-likelihood function for logistic distribution
neg_log_lik_logistic <- function(par, data) {
location <- par[1]
scale <- par[2]
n <- length(data)
-sum(dlogis(data, location = location, scale = scale, log = TRUE))
}

# Get initial parameter estimates: method of moments
pe <- TidyDensity::util_logistic_param_estimate(x)$parameter_tbl |>
subset(method == "EnvStats_MLE")

# Fit logistic distribution using optim
fit_logistic <- optim(
c(pe$location, pe$scale),
neg_log_lik_logistic,
data = x
)

# Extract log-likelihood and number of parameters
logLik_logistic <- -fit_logistic$value
k_logistic <- 2 # Number of parameters for logistic distribution (location and scale)

# Calculate AIC
AIC_logistic <- 2 * k_logistic - 2 * logLik_logistic

# Return AIC
return(AIC_logistic)
}
1 change: 1 addition & 0 deletions man/check_duplicate_rows.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/convert_to_ts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/quantile_normalize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/tidy_mcmc_sampling.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/util_beta_aic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/util_cauchy_aic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/util_chisq_aic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/util_exponential_aic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/util_gamma_aic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions man/util_logistic_aic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/util_normal_aic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1531ec9

Please sign in to comment.