You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#' 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_aicutil_chisq_aic<-function(.x) {
# Tidyevalx<- as.numeric(.x)
# Get parameterspe<-TidyDensity::util_chisquare_param_estimate(x)$parameter_tbl|> head(1)
# Negative log-likelihood function for chi-square distributionneg_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 paramslogLik_chisq<--fit_chisq$valuek_chisq<-2# Number of parameters for chi-square distribution (degrees of freedom or df)# Calculate AICAIC_chisq<-2*k_chisq-2*logLik_chisq# Returnreturn(AIC_chisq)
}
Function:
Example:
The text was updated successfully, but these errors were encountered: