Skip to content

Commit

Permalink
Merge pull request #491 from spsanderson/development
Browse files Browse the repository at this point in the history
update site
  • Loading branch information
spsanderson authored May 9, 2024
2 parents 3f1fa87 + 405f1a3 commit b589b9c
Show file tree
Hide file tree
Showing 169 changed files with 2,754 additions and 273 deletions.
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export(util_pareto_stats_tbl)
export(util_poisson_aic)
export(util_poisson_param_estimate)
export(util_poisson_stats_tbl)
export(util_rztnbinom_aic)
export(util_t_stats_tbl)
export(util_triangular_param_estimate)
export(util_triangular_stats_tbl)
Expand All @@ -145,11 +144,12 @@ export(util_uniform_stats_tbl)
export(util_weibull_aic)
export(util_weibull_param_estimate)
export(util_weibull_stats_tbl)
export(util_zero_truncated_negative_binomial_aic)
export(util_zero_truncated_negative_binomial_param_estimate)
export(util_zero_truncated_negative_binomial_stats_tbl)
export(util_zero_truncated_poisson_aic)
export(util_zero_truncated_poisson_param_estimate)
export(util_zero_truncated_poisson_stats_tbl)
export(util_ztn_binomial_param_estimate)
export(util_ztn_binomial_stats_tbl)
importFrom(data.table,.SD)
importFrom(data.table,as.data.table)
importFrom(data.table,melt)
Expand Down
9 changes: 7 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ None

## New Features
1. #468 - Add function `util_negative_binomial_aic()` to calculate the AIC for the negative binomial distribution.
2. #470 - Add function `util_ztn_binomial_param_estimate()` and `util_rztnbinom_aic()` to estimate the parameters and calculate the AIC for the zero-truncated negative binomial distribution. Also added `util_ztn_binomial_stats_tbl()`
2. #470 - Add function `util_zero_truncated_negative_binomial_param_estimate()` to
estimate the parameters of the zero-truncated negative binomial distribution.
Add function `util_zero_truncated_negative_binomial_aic()` to calculate the AIC for the zero-truncated negative binomial distribution.
Add function `util_zero_truncated_negative_binomial_stats_tbl()` to create a summary table of the zero-truncated negative binomial distribution.
3. #471 - Add function `util_zero_truncated_poisson_param_estimate()` to estimate
the parameters of the zero-truncated Poisson distribution. Add function `util_zero_truncated_poisson_aic()` to calculate the AIC for the zero-truncated Poisson distribution. Add function `util_zero_truncated_poisson_stats_tbl()` to create a summary table of the zero-truncated Poisson distribution.
the parameters of the zero-truncated Poisson distribution.
Add function `util_zero_truncated_poisson_aic()` to calculate the AIC for the zero-truncated Poisson distribution.
Add function `util_zero_truncated_poisson_stats_tbl()` to create a summary table of the zero-truncated Poisson distribution.
4. #472 - Add function `util_f_param_estimate()` and `util_f_aic()` to estimate the parameters and calculate the AIC for the F distribution.

## Minor Improvements and Fixes
Expand Down
4 changes: 2 additions & 2 deletions R/est-param-negative-binomial.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ util_negative_binomial_param_estimate <- function(.x, .size = 1,
nll_func <- function(params) {
size <- params[1]
mu <- params[2]
-sum(dnbinom(x, size = size, mu = mu, log = TRUE))
-sum(dnbinom(x_term, size = size, mu = mu, log = TRUE))
}

# Initial parameter guesses (you might need to adjust these based on your data)
initial_params <- c(size = 1, mu = mean(x))
initial_params <- c(size = 1, mu = mean(x_term))

# Optimize using optim()
optim_result <- optim(initial_params, nll_func)
Expand Down
10 changes: 7 additions & 3 deletions R/est-param-ztn-binmoial.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#' library(actuar)
#'
#' x <- as.integer(mtcars$mpg)
#' output <- util_ztn_binomial_param_estimate(x)
#' output <- util_zero_truncated_negative_binomial_param_estimate(x)
#'
#' output$parameter_tbl
#'
Expand All @@ -38,15 +38,19 @@
#'
#' set.seed(123)
#' t <- rztnbinom(100, 10, .1)
#' util_ztn_binomial_param_estimate(t)$parameter_tbl
#' util_zero_truncated_negative_binomial_param_estimate(t)$parameter_tbl
#'
#' @name util_zero_truncated_negative_binomial_param_estimate
NULL

#' @return
#' A tibble/list
#'
#' @rdname util_zero_truncated_negative_binomial_param_estimate
#' @export
#'

util_ztn_binomial_param_estimate <- function(.x, .auto_gen_empirical = TRUE) {
util_zero_truncated_negative_binomial_param_estimate <- function(.x, .auto_gen_empirical = TRUE) {

# Check if actuar library is installed
if (!requireNamespace("actuar", quietly = TRUE)) {
Expand Down
4 changes: 2 additions & 2 deletions R/est-param-ztpois.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ util_zero_truncated_poisson_param_estimate <- function(.x, .auto_gen_empirical =
# Optimize to find lambda that minimizes negative log-likelihood
optim_result <- stats::optim(par = 1, fn = neg_loglik, data = x_term,
method = "Brent",
lower = 0, upper = max(x))
lower = 0, upper = max(x_term))

# Extract estimated lambda
lambda_est <- optim_result$par
Expand All @@ -109,7 +109,7 @@ util_zero_truncated_poisson_param_estimate <- function(.x, .auto_gen_empirical =
# Return ----
attr(ret, "tibble_type") <- "parameter_estimation"
attr(ret, "family") <- "zero truncated poisson"
attr(ret, "x_term") <- .x
attr(ret, "x_term") <- x_term
attr(ret, "n") <- n

if (.auto_gen_empirical) {
Expand Down
8 changes: 4 additions & 4 deletions R/stats-ztn-binomial-tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
#' library(dplyr)
#'
#' tidy_zero_truncated_negative_binomial(.size = 1, .prob = 0.1) |>
#' util_ztn_binomial_stats_tbl() |>
#' util_zero_truncated_negative_binomial_stats_tbl() |>
#' glimpse()
#'
#'
#' @return A tibble with distribution statistics.
#'
#' @name util_ztn_binomial_stats_tbl
#' @name util_zero_truncated_negative_binomial_stats_tbl
NULL

#' @export
#' @rdname util_ztn_binomial_stats_tbl
#' @rdname util_zero_truncated_negative_binomial_stats_tbl

util_ztn_binomial_stats_tbl <- function(.data) {
util_zero_truncated_negative_binomial_stats_tbl <- function(.data) {

# Immediate check for tidy_ distribution function
if (!"tibble_type" %in% names(attributes(.data))) {
Expand Down
12 changes: 10 additions & 2 deletions R/utils-aic-f.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
#' This function calculates the Akaike Information Criterion (AIC) for an F
#' distribution fitted to the provided data.
#'
#' @family Utility
#' @author Steven P. Sanderson II, MPH
#'
#' @description
#' This function estimates the parameters of a F 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 an F
#' distribution.
#'
Expand Down Expand Up @@ -34,10 +42,10 @@ NULL

util_f_aic <- function(.x) {
# Tidyeval
x <- as.numeric(.x)
x_term <- as.numeric(.x)

# Get initial parameter estimates
pe <- TidyDensity::util_f_param_estimate(x)$parameter_tbl |>
pe <- TidyDensity::util_f_param_estimate(x_term)$parameter_tbl |>
dplyr::filter(method == "MLE")

# Negative log-likelihood function for the F-distribution
Expand Down
10 changes: 5 additions & 5 deletions R/utils-aic-ztn-binomial.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@
#' x <- rztnbinom(30, size = 2, prob = 0.4)
#'
#' # Calculate AIC
#' util_rztnbinom_aic(x)
#' util_zero_truncated_negative_binomial_aic(x)
#'
#' @return The AIC value calculated based on the fitted ZTNB distribution to
#' the provided data.
#'
#' @name util_rztnbinom_aic
#' @name util_zero_truncated_negative_binomial_aic
NULL

#' @export
#' @rdname util_rztnbinom_aic
#' @rdname util_zero_truncated_negative_binomial_aic

util_rztnbinom_aic <- function(.x) {
util_zero_truncated_negative_binomial_aic <- function(.x) {
# Check if actuar library is installed
if (!requireNamespace("actuar", quietly = TRUE)) {
stop("The 'actuar' package is needed for this function. Please install it with: install.packages('actuar')")
Expand All @@ -64,7 +64,7 @@ util_rztnbinom_aic <- function(.x) {
x <- as.numeric(.x)

# Get parameters
pe <- util_ztn_binomial_param_estimate(x)$parameter_tbl
pe <- TidyDensity::util_zero_truncated_negative_binomial_param_estimate(x)$parameter_tbl

# Negative log-likelihood function for zero-truncated negative binomial distribution
neg_log_lik_rztnbinom <- function(par, data) {
Expand Down
13 changes: 9 additions & 4 deletions docs/news/index.html

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

4 changes: 2 additions & 2 deletions docs/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pandoc: 3.1.1
pandoc: 3.1.11
pkgdown: 2.0.9
pkgdown_sha: ~
articles:
getting-started: getting-started.html
last_built: 2024-04-30T17:59Z
last_built: 2024-05-09T15:29Z
urls:
reference: https://www.spsanderson.com/TidyDensity/reference
article: https://www.spsanderson.com/TidyDensity/articles
Expand Down
6 changes: 5 additions & 1 deletion docs/reference/check_duplicate_rows.html

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

6 changes: 5 additions & 1 deletion docs/reference/convert_to_ts.html

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

45 changes: 45 additions & 0 deletions docs/reference/index.html

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

Loading

0 comments on commit b589b9c

Please sign in to comment.