Skip to content

Commit a41ec6f

Browse files
authored
Merge pull request #486 from spsanderson/development
Add zero truncated negative binomial stats tbl
2 parents 5ed579a + 8c91507 commit a41ec6f

32 files changed

+224
-30
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export(util_weibull_param_estimate)
145145
export(util_weibull_stats_tbl)
146146
export(util_zero_truncated_poisson_param_estimate)
147147
export(util_ztn_binomial_param_estimate)
148+
export(util_ztn_binomial_stats_tbl)
148149
export(util_ztp_aic)
149150
importFrom(data.table,.SD)
150151
importFrom(data.table,as.data.table)

NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ None
55

66
## New Features
77
1. #468 - Add function `util_negative_binomial_aic()` to calculate the AIC for the negative binomial distribution.
8-
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.
8+
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()`
99
3. #467 - Add function `util_zero_truncated_poisson_param_estimate()` to estimate
1010
the parameters of the zero-truncated Poisson distribution.
1111

R/stats-ztn-binomial-tbl.R

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#' Distribution Statistics for Zero-Truncated Negative Binomial
2+
#'
3+
#' @family Binomial
4+
#' @family Negative Binomial
5+
#' @family Distribution Statistics
6+
#'
7+
#' @author Steven P. Sanderson II, MPH
8+
#'
9+
#' @details This function computes statistics for a zero-truncated negative
10+
#' binomial distribution.
11+
#'
12+
#' @description Computes distribution statistics for a zero-truncated negative
13+
#' binomial distribution.
14+
#'
15+
#' @param .data The data from a zero-truncated negative binomial distribution.
16+
#'
17+
#' @examples
18+
#' library(dplyr)
19+
#'
20+
#' tidy_zero_truncated_negative_binomial(.size = 1, .prob = 0.1) |>
21+
#' util_ztn_binomial_stats_tbl() |>
22+
#' glimpse()
23+
#'
24+
#'
25+
#' @return A tibble with distribution statistics.
26+
#'
27+
#' @name util_ztn_binomial_stats_tbl
28+
NULL
29+
30+
#' @export
31+
#' @rdname util_ztn_binomial_stats_tbl
32+
33+
util_ztn_binomial_stats_tbl <- function(.data) {
34+
35+
# Immediate check for tidy_ distribution function
36+
if (!"tibble_type" %in% names(attributes(.data))) {
37+
rlang::abort(
38+
message = "You must pass data from the 'tidy_dist' function.",
39+
use_cli_format = TRUE
40+
)
41+
}
42+
43+
if (attributes(.data)$tibble_type != "tidy_zero_truncated_negative_binomial") {
44+
rlang::abort(
45+
message = "You must use 'tidy_zero_truncated_negative_binomial()'",
46+
use_cli_format = TRUE
47+
)
48+
}
49+
50+
# Extract parameters from data
51+
data_tbl <- dplyr::as_tibble(.data)
52+
atb <- attributes(data_tbl)
53+
r <- atb$.size
54+
p <- atb$.prob
55+
56+
# Compute statistics
57+
mean_val <- (p * r) / (1 - p)
58+
mode_val <- ifelse(r > 1, floor((p * (r - 1)) / (1 - p)), 0)
59+
var_val <- (p * r) / ((1 - p)^2)
60+
sd_val <- sqrt(var_val)
61+
skewness_val <- (1 + p) / sqrt(p * r)
62+
kurtosis_val <- 6 / r + ((1 - p)^2) / (p * r)
63+
64+
# Create tibble of distribution statistics
65+
ret <- dplyr::tibble(
66+
tidy_function = atb$tibble_type,
67+
function_call = atb$dist_with_params,
68+
distribution = dist_type_extractor(atb$tibble_type),
69+
distribution_type = atb$distribution_family_type,
70+
points = atb$.n,
71+
simulations = atb$.num_sims,
72+
mean = mean_val,
73+
mode_lower = mode_val,
74+
range = "1 to Inf",
75+
std_dv = sd_val,
76+
coeff_var = var_val / mean_val,
77+
skewness = skewness_val,
78+
kurtosis = kurtosis_val,
79+
computed_std_skew = tidy_skewness_vec(data_tbl$y),
80+
computed_std_kurt = tidy_kurtosis_vec(data_tbl$y),
81+
ci_lo = ci_lo(data_tbl$y),
82+
ci_hi = ci_hi(data_tbl$y)
83+
)
84+
85+
# Return the tibble with distribution statistics
86+
return(ret)
87+
}

man/tidy_binomial.Rd

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

man/tidy_negative_binomial.Rd

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

man/tidy_zero_truncated_binomial.Rd

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

man/tidy_zero_truncated_negative_binomial.Rd

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

man/util_bernoulli_stats_tbl.Rd

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

man/util_beta_stats_tbl.Rd

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

man/util_binomial_param_estimate.Rd

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

man/util_binomial_stats_tbl.Rd

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

man/util_burr_stats_tbl.Rd

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

man/util_cauchy_stats_tbl.Rd

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

man/util_chisquare_stats_tbl.Rd

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

man/util_exponential_stats_tbl.Rd

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

man/util_f_stats_tbl.Rd

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

man/util_gamma_stats_tbl.Rd

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

man/util_geometric_stats_tbl.Rd

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

man/util_hypergeometric_stats_tbl.Rd

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

man/util_logistic_stats_tbl.Rd

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

man/util_lognormal_stats_tbl.Rd

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

man/util_negative_binomial_param_estimate.Rd

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

man/util_negative_binomial_stats_tbl.Rd

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

man/util_normal_stats_tbl.Rd

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

man/util_pareto_stats_tbl.Rd

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

man/util_poisson_stats_tbl.Rd

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

man/util_t_stats_tbl.Rd

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

man/util_triangular_stats_tbl.Rd

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

man/util_uniform_stats_tbl.Rd

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

man/util_weibull_stats_tbl.Rd

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

0 commit comments

Comments
 (0)