From 67155f2171ab2951d6b079142947c956411a1a9e Mon Sep 17 00:00:00 2001 From: "Steven Paul Sanderson II, MPH" Date: Thu, 25 Apr 2024 09:34:07 -0400 Subject: [PATCH] Fixes #419 Fixes #422 --- NAMESPACE | 1 + NEWS.md | 2 +- R/utils-aic-normal.R | 58 +++++++++++++++++++++++++++++++++++++ man/check_duplicate_rows.Rd | 3 +- man/convert_to_ts.Rd | 3 +- man/quantile_normalize.Rd | 3 +- man/tidy_mcmc_sampling.Rd | 3 +- man/util_normal_aic.Rd | 39 +++++++++++++++++++++++++ 8 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 R/utils-aic-normal.R create mode 100644 man/util_normal_aic.Rd diff --git a/NAMESPACE b/NAMESPACE index c32ab09a..f6cd80c4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/NEWS.md b/NEWS.md index 7323608f..e630f761 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/utils-aic-normal.R b/R/utils-aic-normal.R new file mode 100644 index 00000000..a9051748 --- /dev/null +++ b/R/utils-aic-normal.R @@ -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) +} diff --git a/man/check_duplicate_rows.Rd b/man/check_duplicate_rows.Rd index b64bc5c8..d4cc72b5 100644 --- a/man/check_duplicate_rows.Rd +++ b/man/check_duplicate_rows.Rd @@ -36,7 +36,8 @@ check_duplicate_rows(data) Other Utility: \code{\link{convert_to_ts}()}, \code{\link{quantile_normalize}()}, -\code{\link{tidy_mcmc_sampling}()} +\code{\link{tidy_mcmc_sampling}()}, +\code{\link{util_normal_aic}()} } \author{ Steven P. Sanderson II, MPH diff --git a/man/convert_to_ts.Rd b/man/convert_to_ts.Rd index 48b33fb3..445a5ed7 100644 --- a/man/convert_to_ts.Rd +++ b/man/convert_to_ts.Rd @@ -62,7 +62,8 @@ head(result) Other Utility: \code{\link{check_duplicate_rows}()}, \code{\link{quantile_normalize}()}, -\code{\link{tidy_mcmc_sampling}()} +\code{\link{tidy_mcmc_sampling}()}, +\code{\link{util_normal_aic}()} } \author{ Steven P. Sanderson II, MPH diff --git a/man/quantile_normalize.Rd b/man/quantile_normalize.Rd index c3eb1bc9..943cf932 100644 --- a/man/quantile_normalize.Rd +++ b/man/quantile_normalize.Rd @@ -62,7 +62,8 @@ data.frame(sample1 = rnorm(30), Other Utility: \code{\link{check_duplicate_rows}()}, \code{\link{convert_to_ts}()}, -\code{\link{tidy_mcmc_sampling}()} +\code{\link{tidy_mcmc_sampling}()}, +\code{\link{util_normal_aic}()} } \author{ Steven P. Sanderson II, MPH diff --git a/man/tidy_mcmc_sampling.Rd b/man/tidy_mcmc_sampling.Rd index a11b47a9..e9e4631f 100644 --- a/man/tidy_mcmc_sampling.Rd +++ b/man/tidy_mcmc_sampling.Rd @@ -43,7 +43,8 @@ result Other Utility: \code{\link{check_duplicate_rows}()}, \code{\link{convert_to_ts}()}, -\code{\link{quantile_normalize}()} +\code{\link{quantile_normalize}()}, +\code{\link{util_normal_aic}()} } \author{ Steven P. Sanderson II, MPH diff --git a/man/util_normal_aic.Rd b/man/util_normal_aic.Rd new file mode 100644 index 00000000..6c2e3728 --- /dev/null +++ b/man/util_normal_aic.Rd @@ -0,0 +1,39 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-aic-normal.R +\name{util_normal_aic} +\alias{util_normal_aic} +\title{Calculate Akaike Information Criterion (AIC) for Normal Distribution} +\usage{ +util_normal_aic(.x) +} +\arguments{ +\item{.x}{A numeric vector containing the data to be fitted to a normal distribution.} +} +\value{ +The AIC value calculated based on the fitted normal distribution to the provided data. +} +\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. +} +\details{ +This function calculates the Akaike Information Criterion (AIC) for a normal distribution fitted to the provided data. +} +\examples{ +# Example 1: Calculate AIC for a sample dataset +set.seed(123) +data <- rnorm(30) +util_normal_aic(data) + +} +\seealso{ +Other Utility: +\code{\link{check_duplicate_rows}()}, +\code{\link{convert_to_ts}()}, +\code{\link{quantile_normalize}()}, +\code{\link{tidy_mcmc_sampling}()} +} +\author{ +Steven P. Sanderson II, MPH +} +\concept{Utility}