Skip to content

Commit

Permalink
Fixes #350
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Dec 7, 2023
1 parent 73f30cb commit 4b55837
Show file tree
Hide file tree
Showing 76 changed files with 418 additions and 233 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ None

## Minor Fixes and Improvements
1. Fix #372 and #373 - Update `cvar()` and `csd()` to a vectorized approach from @kokbent
which speeds these up by over 100x
2. Fix #350 - Update all `tidy_` distribution functions to generate data using `data.table`
this in many instances has resulted in a speed up of 30% or more.

# TidyDensity 1.2.6

Expand Down
2 changes: 1 addition & 1 deletion R/00_global_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ globalVariables(
"name", "prob", "rate", "sd_log", "shape1", "shape2", "size", "total", "total_deviance",
"abs_aic", "aic_value", "data", "dist_with_params", "ks", "lm", "lm_model", "mu",
"stan_dev", "tidy_ks", "prop", "dens_tbl", "cih", "cil", "mstat", "stat", ".",
"variable"
"variable",".N"
)
)
3 changes: 2 additions & 1 deletion R/autoplot-multi-dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
#' .n = 500,
#' .mean = c(-2, 0, 2),
#' .sd = 1,
#' .num_sims = 5
#' .num_sims = 5,
#' .return_tibble = TRUE
#' )
#' )
#'
Expand Down
3 changes: 2 additions & 1 deletion R/combine-multi-single-dist-tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#' .n = 50,
#' .mean = c(-1, 0, 1),
#' .sd = 1,
#' .num_sims = 3
#' .num_sims = 3,
#' .return_tibble = TRUE
#' )
#' )
#'
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-bernoulli.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#' @param .n The number of randomly generated points you want.
#' @param .prob The probability of success/failure.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_bernoulli()
Expand Down
5 changes: 3 additions & 2 deletions R/random-tidy-beta.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
#' @param .shape2 A non-negative parameter of the Beta distribution.
#' @param .ncp The `non-centrality parameter` of the Beta distribution.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_beta()
#'
#' @return
#' A tibble of randomly generated data.
#' @name tidy_beta
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-binomial.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#' @param .size Number of trials, zero or more.
#' @param .prob Probability of success on each trial.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_binomial()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-burr-inverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
#' @param .scale Must be strictly positive.
#' @param .rate An alternative way to specify the `.scale`.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_inverse_burr()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-burr.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#' @param .scale Must be strictly positive.
#' @param .rate An alternative way to specify the `.scale`.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_burr()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-cauchy.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#' @param .location The location parameter.
#' @param .scale The scale parameter, must be greater than or equal to 0.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_cauchy()
Expand Down
16 changes: 8 additions & 8 deletions R/random-tidy-chisquare.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#' @param .df Degrees of freedom (non-negative but can be non-integer)
#' @param .ncp Non-centrality parameter, must be non-negative.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_chisquare()
Expand All @@ -53,7 +53,7 @@ tidy_chisquare <- function(.n = 50, .df = 1, .ncp = 1, .num_sims = 1,
# Tidyeval ----
n <- as.integer(.n)
num_sims <- as.integer(.num_sims)
df <- as.numeric(.df)
deg_f <- as.numeric(.df)
ncp <- as.numeric(.ncp)
ret_tbl <- as.logical(.return_tibble)

Expand All @@ -72,13 +72,13 @@ tidy_chisquare <- function(.n = 50, .df = 1, .ncp = 1, .num_sims = 1,
)
}

if (!is.numeric(df) | !is.numeric(ncp)) {
if (!is.numeric(deg_f) | !is.numeric(ncp)) {
rlang::abort(
"The parameters of .df and .ncp must be of class numeric."
)
}

if (df < 0 | ncp < 0) {
if (deg_f < 0 | ncp < 0) {
rlang::abort("The parameters of .df and .ncp must be greater than or equal to 0.")
}

Expand All @@ -92,16 +92,16 @@ tidy_chisquare <- function(.n = 50, .df = 1, .ncp = 1, .num_sims = 1,
df <- data.table::CJ(sim_number = factor(1:num_sims), x = 1:n)

# Group the data by sim_number and add columns for x and y
df[, y := stats::rchisq(n = .N, df = df, ncp = ncp)]
df[, y := stats::rchisq(n = .N, df = deg_f, ncp = ncp)]

# Compute the density of the y values and add columns for dx and dy
df[, c("dx", "dy") := density(y, n = n)[c("x", "y")], by = sim_number]

# Compute the p-values for the y values and add a column for p
df[, p := stats::pchisq(y, df = df, ncp = ncp)]
df[, p := stats::pchisq(y, df = deg_f, ncp = ncp)]

# Compute the q-values for the p-values and add a column for q
df[, q := stats::qchisq(p, df = df, ncp = ncp)]
df[, q := stats::qchisq(p, df = deg_f, ncp = ncp)]

if(.return_tibble){
df <- dplyr::as_tibble(df)
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-exponential-inverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#' @param .scale Must be strictly positive.
#' @param .rate An alternative way to specify the `.scale`
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_inverse_exponential()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-exponential.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#' @param .n The number of randomly generated points you want.
#' @param .rate A vector of rates
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_exponential()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-f.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#' @param .df2 Degrees of freedom, Inf is allowed.
#' @param .ncp Non-centrality parameter.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_f()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-gamma-inverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#' @param .scale Must be strictly positive.
#' @param .rate An alternative way to specify the `.scale`
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_inverse_gamma()
Expand Down
10 changes: 5 additions & 5 deletions R/random-tidy-gamma.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#' @param .scale The standard deviation of the randomly generated data. This is
#' strictly from 0 to infinity.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_gamma()
Expand Down Expand Up @@ -97,16 +97,16 @@ tidy_gamma <- function(.n = 50, .shape = 1, .scale = 0.3, .num_sims = 1,
df <- data.table::CJ(sim_number = factor(1:num_sims), x = 1:n)

# Group the data by sim_number and add columns for x and y
df[, y := stats::rgamma(n = .N, shape = shape, scale = scle)]
df[, y := stats::rgamma(n = .N, shape = shp, scale = scle)]

# Compute the density of the y values and add columns for dx and dy
df[, c("dx", "dy") := density(y, n = n)[c("x", "y")], by = sim_number]

# Compute the p-values for the y values and add a column for p
df[, p := stats::pgamma(y, shape = shape, scale = scle)]
df[, p := stats::pgamma(y, shape = shp, scale = scle)]

# Compute the q-values for the p-values and add a column for q
df[, p := stats::qgamma(p, shape = shape, scale = scle)]
df[, p := stats::qgamma(p, shape = shp, scale = scle)]

if(.return_tibble){
df <- dplyr::as_tibble(df)
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-general-beta.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
#' @param .scale Must be strictly positive.
#' @param .rate An alternative way to specify the `.scale` parameter.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_generalized_beta()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-general-pareto.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
#' @param .scale Must be positive.
#' @param .rate An alternative way to specify the `.scale` argument
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_generalized_pareto()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-geom.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#' @param .n The number of randomly generated points you want.
#' @param .prob A probability of success in each trial 0 < prob <= 1.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_geometric()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-hypergeometric.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#' @param .nn The number of black balls in the urn
#' @param .k The number of balls drawn fro the urn.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_hypergeometric()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-logistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#' @param .location The location parameter
#' @param .scale The scale parameter
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_logistic()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-lognormal.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#' @param .meanlog Mean of the distribution on the log scale with default 0
#' @param .sdlog Standard deviation of the distribution on the log scale with default 1
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_lognormal()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-negative-binomial.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
#' positive, need not be integer.
#' @param .prob Probability of success on each trial where 0 < .prob <= 1.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_negative_binomial()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-normal-inverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#' @param .shape Must be strictly positive.
#' @param .dispersion An alternative way to specify the `.shape`.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_inverse_normal()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-normal.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#' @param .mean The mean of the randomly generated data.
#' @param .sd The standard deviation of the randomly generated data.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_normal()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-paralogistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#' @param .scale Must be strictly positive.
#' @param .rate An alternative way to specify the `.scale`
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_paralogistic()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-pareto-inverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#' @param .shape Must be positive.
#' @param .scale Must be positive.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_inverse_pareto()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-pareto-single-param.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#' @param .shape Must be positive.
#' @param .min The lower bound of the support of the distribution.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_pareto1()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-pareto.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#' @param .shape Must be positive.
#' @param .scale Must be positive.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_pareto()
Expand Down
4 changes: 2 additions & 2 deletions R/random-tidy-poisson.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#' @param .n The number of randomly generated points you want.
#' @param .lambda A vector of non-negative means.
#' @param .num_sims The number of randomly generated simulations you want.
#' @param .return_tibble Should the function return a tibble. FALSE will return
#' a data.table object.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_poisson()
Expand Down

0 comments on commit 4b55837

Please sign in to comment.