Skip to content

Commit 68c6db8

Browse files
authored
Merge pull request #440 from spsanderson/development
Fixes #420
2 parents bad36cd + fa2e52b commit 68c6db8

8 files changed

+103
-1
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export(util_burr_param_estimate)
9696
export(util_burr_stats_tbl)
9797
export(util_cauchy_param_estimate)
9898
export(util_cauchy_stats_tbl)
99+
export(util_chisq_aic)
99100
export(util_chisquare_param_estimate)
100101
export(util_chisquare_stats_tbl)
101102
export(util_exponential_param_estimate)

R/utils-aic-chisq.R

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#' Calculate Akaike Information Criterion (AIC) for Chi-Square Distribution
2+
#'
3+
#' This function calculates the Akaike Information Criterion (AIC) for a chi-square distribution fitted to the provided data.
4+
#'
5+
#' @family Utility
6+
#' @author Steven P. Sanderson II, MPH
7+
#'
8+
#' @description
9+
#' This function estimates the parameters of a chi-square distribution from the provided data using maximum likelihood estimation,
10+
#' and then calculates the AIC value based on the fitted distribution.
11+
#'
12+
#' @param .x A numeric vector containing the data to be fitted to a chi-square distribution.
13+
#'
14+
#' @examples
15+
#' # Example 1: Calculate AIC for a sample dataset
16+
#' data <- c(1, 2, 3, 4, 5)
17+
#' util_chisq_aic(data)
18+
#'
19+
#' @return
20+
#' The AIC value calculated based on the fitted chi-square distribution to the provided data.
21+
#'
22+
#' @name util_chisq_aic
23+
#'
24+
#' @export
25+
#' @rdname util_chisq_aic
26+
util_chisq_aic <- function(.x) {
27+
# Tidyeval
28+
x <- as.numeric(.x)
29+
30+
# Get parameters
31+
pe <- TidyDensity::util_chisquare_param_estimate(x)$parameter_tbl |> head(1)
32+
33+
# Negative log-likelihood function for chi-square distribution
34+
neg_log_lik_chisq <- function(par, data) {
35+
df <- par[1]
36+
ncp <- par[2]
37+
n <- length(data)
38+
-sum(stats::dchisq(data, df = df, ncp = ncp, log = TRUE))
39+
}
40+
41+
# Fit chi-square distribution to sample data (rchisq)
42+
fit_chisq <- optim(
43+
c(pe$degrees_of_freedom, pe$ncp),
44+
neg_log_lik_chisq,
45+
data = x
46+
)
47+
48+
# Extract log-likelihood and number of params
49+
logLik_chisq <- -fit_chisq$value
50+
k_chisq <- 2 # Number of parameters for chi-square distribution (degrees of freedom or df)
51+
52+
# Calculate AIC
53+
AIC_chisq <- 2 * k_chisq - 2 * logLik_chisq
54+
55+
# Return
56+
return(AIC_chisq)
57+
}

man/check_duplicate_rows.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/convert_to_ts.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/quantile_normalize.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tidy_mcmc_sampling.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/util_chisq_aic.Rd

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/util_normal_aic.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)