Skip to content

Commit 4367905

Browse files
authored
Merge pull request #493 from spsanderson/development
Fixes #481
2 parents 9b4f72d + 4d1f839 commit 4367905

28 files changed

+207
-0
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export(util_poisson_aic)
136136
export(util_poisson_param_estimate)
137137
export(util_poisson_stats_tbl)
138138
export(util_t_stats_tbl)
139+
export(util_triangular_aic)
139140
export(util_triangular_param_estimate)
140141
export(util_triangular_stats_tbl)
141142
export(util_uniform_aic)

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Add function `util_zero_truncated_poisson_stats_tbl()` to create a summary table
1717
5. #482 - Add function `util_zero_truncated_geometric_param_estimate()` to estimate the parameters of the zero-truncated geometric distribution.
1818
Add function `util_zero_truncated_geometric_aic()` to calculate the AIC for the zero-truncated geometric distribution.
1919
Add function `util_zero_truncated_geometric_stats_tbl()` to create a summary table of the zero-truncated geometric distribution.
20+
6. #481 - Add function `util_triangular_aic()` to calculate the AIC for the triangular distribution.
2021

2122
## Minor Improvements and Fixes
2223
1. Fix #468 - Update `util_negative_binomial_param_estimate()` to add the use of

R/utils-aic-triangular.R

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#' Calculate Akaike Information Criterion (AIC) for Triangular Distribution
2+
#'
3+
#' This function calculates the Akaike Information Criterion (AIC) for a
4+
#' triangular distribution fitted to the provided data.
5+
#'
6+
#' @family Utility
7+
#' @author Steven P. Sanderson II, MPH
8+
#'
9+
#' @description
10+
#' This function estimates the parameters of a triangular distribution
11+
#' (min, max, and mode) from the provided data and calculates the AIC value
12+
#' based on the fitted distribution.
13+
#'
14+
#' @details
15+
#' The function operates in several steps:
16+
#' 1. **Parameter Estimation**: The function extracts the minimum, maximum, and
17+
#' mode values from the data via the `TidyDensity::util_triangular_param_estimate`
18+
#' function. It returns these initial parameters as the starting point for
19+
#' optimization.
20+
#' 2. **Negative Log-Likelihood Calculation**: A custom function calculates the
21+
#' negative log-likelihood using the `EnvStats::dtri` function to obtain density
22+
#' values for each data point. The densities are logged manually to simulate the
23+
#' behavior of a `log` parameter.
24+
#' 3. **Parameter Validation**: During optimization, the function checks that the
25+
#' constraints `min <= mode <= max` are met, and returns an infinite loss if not.
26+
#' 4. **Optimization**: The optimization process utilizes the "SANN"
27+
#' (Simulated Annealing) method to minimize the negative log-likelihood and find
28+
#' optimal parameter values.
29+
#' 5. **AIC Calculation**: The Akaike Information Criterion (AIC) is calculated
30+
#' using the optimized negative log-likelihood and the total number of parameters
31+
#' (3).
32+
#'
33+
#' @param .x A numeric vector containing the data to be fitted to a triangular
34+
#' distribution.
35+
#'
36+
#' @examples
37+
#' # Example: Calculate AIC for a sample dataset
38+
#' set.seed(123)
39+
#' data <- tidy_triangular(.min = 0, .max = 1, .mode = 1/2)$y
40+
#' util_triangular_aic(data)
41+
#'
42+
#' @return
43+
#' The AIC value calculated based on the fitted triangular distribution to the provided data.
44+
#'
45+
#' @name util_triangular_aic
46+
NULL
47+
48+
#' @export
49+
#' @rdname util_triangular_aic
50+
util_triangular_aic <- function(.x) {
51+
# Convert the input to numeric
52+
x <- as.numeric(.x)
53+
54+
# Estimate triangular distribution parameters: min, max, and mode
55+
pe <- TidyDensity::util_triangular_param_estimate(x)$parameter_tbl
56+
min_val <- pe$min
57+
max_val <- pe$max
58+
mode_val <- pe$mode
59+
60+
# Ensure the initial guesses respect the constraints min <= mode <= max
61+
initial_par <- c(min_val, max_val, mode_val)
62+
63+
# Negative log-likelihood function for triangular distribution
64+
neg_log_lik_tri <- function(par, data) {
65+
min_val <- par[1]
66+
max_val <- par[2] + 1e-6 # Add a small value to avoid numerical issues
67+
mode_val <- par[3]
68+
69+
# Validate the parameter constraints
70+
if (min_val > mode_val || mode_val > max_val) {
71+
return(Inf) # Invalid parameter combination returns an infinite loss
72+
}
73+
74+
# Calculate the density using dtri and then take the log manually
75+
density_vals <- EnvStats::dtri(data, min = min_val, max = max_val, mode = mode_val)
76+
log_density_vals <- log(density_vals)
77+
78+
# Sum the negative log-density values
79+
-sum(log_density_vals, na.rm = TRUE)
80+
}
81+
82+
# Fit triangular distribution to the data
83+
fit_tri <- stats::optim(
84+
par = initial_par,
85+
fn = neg_log_lik_tri,
86+
data = x,
87+
method = "SANN"
88+
)
89+
90+
# Extract log-likelihoods and number of parameters
91+
logLik_tri <- -fit_tri$value
92+
k_tri <- 3 # Number of parameters for the triangular distribution (min, max, and mode)
93+
94+
# Calculate AIC
95+
AIC_tri <- 2 * k_tri - 2 * logLik_tri
96+
97+
# Return the calculated AIC value
98+
return(AIC_tri)
99+
}

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_beta_aic.Rd

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

man/util_binomial_aic.Rd

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

man/util_cauchy_aic.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

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

man/util_exponential_aic.Rd

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

man/util_f_aic.Rd

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

man/util_gamma_aic.Rd

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

man/util_geometric_aic.Rd

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

man/util_hypergeometric_aic.Rd

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

man/util_logistic_aic.Rd

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

man/util_lognormal_aic.Rd

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

man/util_negative_binomial_aic.Rd

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

man/util_normal_aic.Rd

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

man/util_pareto_aic.Rd

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

man/util_poisson_aic.Rd

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

man/util_triangular_aic.Rd

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

man/util_uniform_aic.Rd

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

man/util_weibull_aic.Rd

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

man/util_zero_truncated_geometric_aic.Rd

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

man/util_zero_truncated_negative_binomial_aic.Rd

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

man/util_zero_truncated_poisson_aic.Rd

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

0 commit comments

Comments
 (0)