Skip to content

Commit

Permalink
Merge pull request #439 from spsanderson/development
Browse files Browse the repository at this point in the history
Fixes #419
  • Loading branch information
spsanderson authored Apr 25, 2024
2 parents 4628d68 + 67155f2 commit bad36cd
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 5 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export(util_lognormal_param_estimate)
export(util_lognormal_stats_tbl)
export(util_negative_binomial_param_estimate)
export(util_negative_binomial_stats_tbl)
export(util_normal_aic)
export(util_normal_param_estimate)
export(util_normal_stats_tbl)
export(util_pareto_param_estimate)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ None
3. Fix #414 - Add function `util_chisquare_param_estimate()` to estimate the parameters of the chi-square distribution.
4. Fix #417 - Add function `tidy_mcmc_sampling()` to sample from a distribution using MCMC.
This outputs the function sampled data and a diagnostic plot.
5. Fix #420 - Add functions `util_dist_aic()` functions to calculate the AIC for a distribution.
5. Fix #421 - Add functions `util_dist_aic()` functions to calculate the AIC for a distribution.

## Minor Fixes and Improvements
1. Fix #401 - Update `tidy_multi_single_dist()` to respect the `.return_tibble` parameter
Expand Down
58 changes: 58 additions & 0 deletions R/utils-aic-normal.R
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)
}
3 changes: 2 additions & 1 deletion man/check_duplicate_rows.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/convert_to_ts.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/quantile_normalize.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/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_normal_aic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bad36cd

Please sign in to comment.