-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #439 from spsanderson/development
Fixes #419
- Loading branch information
Showing
8 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#' Calculate Akaike Information Criterion (AIC) for Normal Distribution | ||
#' | ||
#' This function calculates the Akaike Information Criterion (AIC) for a normal distribution fitted to the provided data. | ||
#' | ||
#' @family Utility | ||
#' @author Steven P. Sanderson II, MPH | ||
#' | ||
#' @description | ||
#' This function estimates the parameters of a normal 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 normal distribution. | ||
#' | ||
#' @examples | ||
#' # Example 1: Calculate AIC for a sample dataset | ||
#' set.seed(123) | ||
#' data <- rnorm(30) | ||
#' util_normal_aic(data) | ||
#' | ||
#' @return | ||
#' The AIC value calculated based on the fitted normal distribution to the provided data. | ||
#' | ||
#' @name util_normal_aic | ||
#' | ||
#' @export | ||
#' @rdname util_normal_aic | ||
util_normal_aic <- function(.x) { | ||
# Tidyeval | ||
x <- as.numeric(.x) | ||
|
||
# Get parameters | ||
pe <- TidyDensity::util_normal_param_estimate(x)$parameter_tbl |> head(1) | ||
|
||
# Negative log-likelihood function for normal distribution | ||
neg_log_lik_norm <- function(par, data) { | ||
mu <- par[1] | ||
sigma <- par[2] | ||
n <- length(data) | ||
-sum(stats::dnorm(data, mean = mu, sd = sigma, log = TRUE)) | ||
} | ||
|
||
# Fit normal distribution to population data (rnorm) | ||
fit_norm <- optim( | ||
c(pe$mu, pe$stan_dev), | ||
neg_log_lik_norm, | ||
data = x | ||
) | ||
|
||
# Extract log-likelihoods and number of parameters | ||
logLik_norm <- -fit_norm$value | ||
k_norm <- 2 # Number of parameters for normal distribution (mu and sigma) | ||
|
||
# Calculate AIC | ||
AIC_norm <- 2 * k_norm - 2 * logLik_norm | ||
|
||
# Return | ||
return(AIC_norm) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.