-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Fixes #420
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#' Calculate Akaike Information Criterion (AIC) for Chi-Square Distribution | ||
#' | ||
#' This function calculates the Akaike Information Criterion (AIC) for a chi-square distribution fitted to the provided data. | ||
#' | ||
#' @family Utility | ||
#' @author Steven P. Sanderson II, MPH | ||
#' | ||
#' @description | ||
#' This function estimates the parameters of a chi-square 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 chi-square distribution. | ||
#' | ||
#' @examples | ||
#' # Example 1: Calculate AIC for a sample dataset | ||
#' data <- c(1, 2, 3, 4, 5) | ||
#' util_chisq_aic(data) | ||
#' | ||
#' @return | ||
#' The AIC value calculated based on the fitted chi-square distribution to the provided data. | ||
#' | ||
#' @name util_chisq_aic | ||
#' | ||
#' @export | ||
#' @rdname util_chisq_aic | ||
util_chisq_aic <- function(.x) { | ||
# Tidyeval | ||
x <- as.numeric(.x) | ||
|
||
# Get parameters | ||
pe <- TidyDensity::util_chisquare_param_estimate(x)$parameter_tbl |> head(1) | ||
|
||
# Negative log-likelihood function for chi-square distribution | ||
neg_log_lik_chisq <- function(par, data) { | ||
df <- par[1] | ||
ncp <- par[2] | ||
n <- length(data) | ||
-sum(stats::dchisq(data, df = df, ncp = ncp, log = TRUE)) | ||
} | ||
|
||
# Fit chi-square distribution to sample data (rchisq) | ||
fit_chisq <- optim( | ||
c(pe$degrees_of_freedom, pe$ncp), | ||
neg_log_lik_chisq, | ||
data = x | ||
) | ||
|
||
# Extract log-likelihood and number of params | ||
logLik_chisq <- -fit_chisq$value | ||
k_chisq <- 2 # Number of parameters for chi-square distribution (degrees of freedom or df) | ||
|
||
# Calculate AIC | ||
AIC_chisq <- 2 * k_chisq - 2 * logLik_chisq | ||
|
||
# Return | ||
return(AIC_chisq) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.