Skip to content

Commit

Permalink
Merge pull request #440 from spsanderson/development
Browse files Browse the repository at this point in the history
Fixes #420
  • Loading branch information
spsanderson authored Apr 25, 2024
2 parents bad36cd + fa2e52b commit 68c6db8
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export(util_burr_param_estimate)
export(util_burr_stats_tbl)
export(util_cauchy_param_estimate)
export(util_cauchy_stats_tbl)
export(util_chisq_aic)
export(util_chisquare_param_estimate)
export(util_chisquare_stats_tbl)
export(util_exponential_param_estimate)
Expand Down
57 changes: 57 additions & 0 deletions R/utils-aic-chisq.R
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)
}
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.

39 changes: 39 additions & 0 deletions man/util_chisq_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 68c6db8

Please sign in to comment.