Fit an Integer Adjusted Exponential, Gamma or Lognormal distributions
Source:R/estimate_delay.R
dist_fit.Rd
Usage
dist_fit(
values = NULL,
samples = 1000,
cores = 1,
chains = 2,
dist = "exp",
verbose = FALSE,
backend = "rstan"
)
Arguments
- values
Numeric vector of values
- samples
Numeric, number of samples to take. Must be >= 1000. Defaults to 1000.
- cores
Numeric, defaults to 1. Number of CPU cores to use (no effect if greater than the number of chains).
- chains
Numeric, defaults to 2. Number of MCMC chains to use. More is better with the minimum being two.
- dist
Character string, which distribution to fit. Defaults to exponential (
"exp"
) but gamma ("gamma"
) and lognormal ("lognormal"
) are also supported.- verbose
Logical, defaults to FALSE. Should verbose progress messages be printed.
- backend
Character string indicating the backend to use for fitting stan models. Supported arguments are "rstan" (default) or "cmdstanr".
Examples
# \donttest{
# integer adjusted exponential model
dist_fit(rexp(1:100, 2),
samples = 1000, dist = "exp",
cores = ifelse(interactive(), 4, 1), verbose = TRUE
)
#>
#> SAMPLING FOR MODEL 'dist_fit' NOW (CHAIN 1).
#> Chain 1:
#> Chain 1: Gradient evaluation took 4.9e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.49 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1:
#> Chain 1:
#> Chain 1: Iteration: 1 / 1500 [ 0%] (Warmup)
#> Chain 1: Iteration: 50 / 1500 [ 3%] (Warmup)
#> Chain 1: Iteration: 100 / 1500 [ 6%] (Warmup)
#> Chain 1: Iteration: 150 / 1500 [ 10%] (Warmup)
#> Chain 1: Iteration: 200 / 1500 [ 13%] (Warmup)
#> Chain 1: Iteration: 250 / 1500 [ 16%] (Warmup)
#> Chain 1: Iteration: 300 / 1500 [ 20%] (Warmup)
#> Chain 1: Iteration: 350 / 1500 [ 23%] (Warmup)
#> Chain 1: Iteration: 400 / 1500 [ 26%] (Warmup)
#> Chain 1: Iteration: 450 / 1500 [ 30%] (Warmup)
#> Chain 1: Iteration: 500 / 1500 [ 33%] (Warmup)
#> Chain 1: Iteration: 550 / 1500 [ 36%] (Warmup)
#> Chain 1: Iteration: 600 / 1500 [ 40%] (Warmup)
#> Chain 1: Iteration: 650 / 1500 [ 43%] (Warmup)
#> Chain 1: Iteration: 700 / 1500 [ 46%] (Warmup)
#> Chain 1: Iteration: 750 / 1500 [ 50%] (Warmup)
#> Chain 1: Iteration: 800 / 1500 [ 53%] (Warmup)
#> Chain 1: Iteration: 850 / 1500 [ 56%] (Warmup)
#> Chain 1: Iteration: 900 / 1500 [ 60%] (Warmup)
#> Chain 1: Iteration: 950 / 1500 [ 63%] (Warmup)
#> Chain 1: Iteration: 1000 / 1500 [ 66%] (Warmup)
#> Chain 1: Iteration: 1001 / 1500 [ 66%] (Sampling)
#> Chain 1: Iteration: 1050 / 1500 [ 70%] (Sampling)
#> Chain 1: Iteration: 1100 / 1500 [ 73%] (Sampling)
#> Chain 1: Iteration: 1150 / 1500 [ 76%] (Sampling)
#> Chain 1: Iteration: 1200 / 1500 [ 80%] (Sampling)
#> Chain 1: Iteration: 1250 / 1500 [ 83%] (Sampling)
#> Chain 1: Iteration: 1300 / 1500 [ 86%] (Sampling)
#> Chain 1: Iteration: 1350 / 1500 [ 90%] (Sampling)
#> Chain 1: Iteration: 1400 / 1500 [ 93%] (Sampling)
#> Chain 1: Iteration: 1450 / 1500 [ 96%] (Sampling)
#> Chain 1: Iteration: 1500 / 1500 [100%] (Sampling)
#> Chain 1:
#> Chain 1: Elapsed Time: 0.18 seconds (Warm-up)
#> Chain 1: 0.101 seconds (Sampling)
#> Chain 1: 0.281 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'dist_fit' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 4.7e-05 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.47 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2:
#> Chain 2:
#> Chain 2: Iteration: 1 / 1500 [ 0%] (Warmup)
#> Chain 2: Iteration: 50 / 1500 [ 3%] (Warmup)
#> Chain 2: Iteration: 100 / 1500 [ 6%] (Warmup)
#> Chain 2: Iteration: 150 / 1500 [ 10%] (Warmup)
#> Chain 2: Iteration: 200 / 1500 [ 13%] (Warmup)
#> Chain 2: Iteration: 250 / 1500 [ 16%] (Warmup)
#> Chain 2: Iteration: 300 / 1500 [ 20%] (Warmup)
#> Chain 2: Iteration: 350 / 1500 [ 23%] (Warmup)
#> Chain 2: Iteration: 400 / 1500 [ 26%] (Warmup)
#> Chain 2: Iteration: 450 / 1500 [ 30%] (Warmup)
#> Chain 2: Iteration: 500 / 1500 [ 33%] (Warmup)
#> Chain 2: Iteration: 550 / 1500 [ 36%] (Warmup)
#> Chain 2: Iteration: 600 / 1500 [ 40%] (Warmup)
#> Chain 2: Iteration: 650 / 1500 [ 43%] (Warmup)
#> Chain 2: Iteration: 700 / 1500 [ 46%] (Warmup)
#> Chain 2: Iteration: 750 / 1500 [ 50%] (Warmup)
#> Chain 2: Iteration: 800 / 1500 [ 53%] (Warmup)
#> Chain 2: Iteration: 850 / 1500 [ 56%] (Warmup)
#> Chain 2: Iteration: 900 / 1500 [ 60%] (Warmup)
#> Chain 2: Iteration: 950 / 1500 [ 63%] (Warmup)
#> Chain 2: Iteration: 1000 / 1500 [ 66%] (Warmup)
#> Chain 2: Iteration: 1001 / 1500 [ 66%] (Sampling)
#> Chain 2: Iteration: 1050 / 1500 [ 70%] (Sampling)
#> Chain 2: Iteration: 1100 / 1500 [ 73%] (Sampling)
#> Chain 2: Iteration: 1150 / 1500 [ 76%] (Sampling)
#> Chain 2: Iteration: 1200 / 1500 [ 80%] (Sampling)
#> Chain 2: Iteration: 1250 / 1500 [ 83%] (Sampling)
#> Chain 2: Iteration: 1300 / 1500 [ 86%] (Sampling)
#> Chain 2: Iteration: 1350 / 1500 [ 90%] (Sampling)
#> Chain 2: Iteration: 1400 / 1500 [ 93%] (Sampling)
#> Chain 2: Iteration: 1450 / 1500 [ 96%] (Sampling)
#> Chain 2: Iteration: 1500 / 1500 [100%] (Sampling)
#> Chain 2:
#> Chain 2: Elapsed Time: 0.182 seconds (Warm-up)
#> Chain 2: 0.078 seconds (Sampling)
#> Chain 2: 0.26 seconds (Total)
#> Chain 2:
#> Inference for Stan model: dist_fit.
#> 2 chains, each with iter=1500; warmup=1000; thin=1;
#> post-warmup draws per chain=500, total post-warmup draws=1000.
#>
#> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> lambda[1] 2.95 0.03 0.50 2.14 2.59 2.89 3.26 4.04 314 1
#> lp__ -13.35 0.04 0.73 -15.43 -13.50 -13.08 -12.89 -12.84 404 1
#>
#> Samples were drawn using NUTS(diag_e) at Fri May 17 06:40:33 2024.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at
#> convergence, Rhat=1).
# integer adjusted gamma model
dist_fit(rgamma(1:100, 5, 5),
samples = 1000, dist = "gamma",
cores = ifelse(interactive(), 4, 1), verbose = TRUE
)
#>
#> SAMPLING FOR MODEL 'dist_fit' NOW (CHAIN 1).
#> Chain 1:
#> Chain 1: Gradient evaluation took 0.000316 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 3.16 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1:
#> Chain 1:
#> Chain 1: Iteration: 1 / 1500 [ 0%] (Warmup)
#> Chain 1: Iteration: 50 / 1500 [ 3%] (Warmup)
#> Chain 1: Iteration: 100 / 1500 [ 6%] (Warmup)
#> Chain 1: Iteration: 150 / 1500 [ 10%] (Warmup)
#> Chain 1: Iteration: 200 / 1500 [ 13%] (Warmup)
#> Chain 1: Iteration: 250 / 1500 [ 16%] (Warmup)
#> Chain 1: Iteration: 300 / 1500 [ 20%] (Warmup)
#> Chain 1: Iteration: 350 / 1500 [ 23%] (Warmup)
#> Chain 1: Iteration: 400 / 1500 [ 26%] (Warmup)
#> Chain 1: Iteration: 450 / 1500 [ 30%] (Warmup)
#> Chain 1: Iteration: 500 / 1500 [ 33%] (Warmup)
#> Chain 1: Iteration: 550 / 1500 [ 36%] (Warmup)
#> Chain 1: Iteration: 600 / 1500 [ 40%] (Warmup)
#> Chain 1: Iteration: 650 / 1500 [ 43%] (Warmup)
#> Chain 1: Iteration: 700 / 1500 [ 46%] (Warmup)
#> Chain 1: Iteration: 750 / 1500 [ 50%] (Warmup)
#> Chain 1: Iteration: 800 / 1500 [ 53%] (Warmup)
#> Chain 1: Iteration: 850 / 1500 [ 56%] (Warmup)
#> Chain 1: Iteration: 900 / 1500 [ 60%] (Warmup)
#> Chain 1: Iteration: 950 / 1500 [ 63%] (Warmup)
#> Chain 1: Iteration: 1000 / 1500 [ 66%] (Warmup)
#> Chain 1: Iteration: 1001 / 1500 [ 66%] (Sampling)
#> Chain 1: Iteration: 1050 / 1500 [ 70%] (Sampling)
#> Chain 1: Iteration: 1100 / 1500 [ 73%] (Sampling)
#> Chain 1: Iteration: 1150 / 1500 [ 76%] (Sampling)
#> Chain 1: Iteration: 1200 / 1500 [ 80%] (Sampling)
#> Chain 1: Iteration: 1250 / 1500 [ 83%] (Sampling)
#> Chain 1: Iteration: 1300 / 1500 [ 86%] (Sampling)
#> Chain 1: Iteration: 1350 / 1500 [ 90%] (Sampling)
#> Chain 1: Iteration: 1400 / 1500 [ 93%] (Sampling)
#> Chain 1: Iteration: 1450 / 1500 [ 96%] (Sampling)
#> Chain 1: Iteration: 1500 / 1500 [100%] (Sampling)
#> Chain 1:
#> Chain 1: Elapsed Time: 2.889 seconds (Warm-up)
#> Chain 1: 1.32 seconds (Sampling)
#> Chain 1: 4.209 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'dist_fit' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 0.000274 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 2.74 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2:
#> Chain 2:
#> Chain 2: Iteration: 1 / 1500 [ 0%] (Warmup)
#> Chain 2: Iteration: 50 / 1500 [ 3%] (Warmup)
#> Chain 2: Iteration: 100 / 1500 [ 6%] (Warmup)
#> Chain 2: Iteration: 150 / 1500 [ 10%] (Warmup)
#> Chain 2: Iteration: 200 / 1500 [ 13%] (Warmup)
#> Chain 2: Iteration: 250 / 1500 [ 16%] (Warmup)
#> Chain 2: Iteration: 300 / 1500 [ 20%] (Warmup)
#> Chain 2: Iteration: 350 / 1500 [ 23%] (Warmup)
#> Chain 2: Iteration: 400 / 1500 [ 26%] (Warmup)
#> Chain 2: Iteration: 450 / 1500 [ 30%] (Warmup)
#> Chain 2: Iteration: 500 / 1500 [ 33%] (Warmup)
#> Chain 2: Iteration: 550 / 1500 [ 36%] (Warmup)
#> Chain 2: Iteration: 600 / 1500 [ 40%] (Warmup)
#> Chain 2: Iteration: 650 / 1500 [ 43%] (Warmup)
#> Chain 2: Iteration: 700 / 1500 [ 46%] (Warmup)
#> Chain 2: Iteration: 750 / 1500 [ 50%] (Warmup)
#> Chain 2: Iteration: 800 / 1500 [ 53%] (Warmup)
#> Chain 2: Iteration: 850 / 1500 [ 56%] (Warmup)
#> Chain 2: Iteration: 900 / 1500 [ 60%] (Warmup)
#> Chain 2: Iteration: 950 / 1500 [ 63%] (Warmup)
#> Chain 2: Iteration: 1000 / 1500 [ 66%] (Warmup)
#> Chain 2: Iteration: 1001 / 1500 [ 66%] (Sampling)
#> Chain 2: Iteration: 1050 / 1500 [ 70%] (Sampling)
#> Chain 2: Iteration: 1100 / 1500 [ 73%] (Sampling)
#> Chain 2: Iteration: 1150 / 1500 [ 76%] (Sampling)
#> Chain 2: Iteration: 1200 / 1500 [ 80%] (Sampling)
#> Chain 2: Iteration: 1250 / 1500 [ 83%] (Sampling)
#> Chain 2: Iteration: 1300 / 1500 [ 86%] (Sampling)
#> Chain 2: Iteration: 1350 / 1500 [ 90%] (Sampling)
#> Chain 2: Iteration: 1400 / 1500 [ 93%] (Sampling)
#> Chain 2: Iteration: 1450 / 1500 [ 96%] (Sampling)
#> Chain 2: Iteration: 1500 / 1500 [100%] (Sampling)
#> Chain 2:
#> Chain 2: Elapsed Time: 3.093 seconds (Warm-up)
#> Chain 2: 1.388 seconds (Sampling)
#> Chain 2: 4.481 seconds (Total)
#> Chain 2:
#> WARN [2024-05-17 06:40:42] dist_fit (chain: 1): Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess -
#> WARN [2024-05-17 06:40:42] dist_fit (chain: 1): Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess -
#> Inference for Stan model: dist_fit.
#> 2 chains, each with iter=1500; warmup=1000; thin=1;
#> post-warmup draws per chain=500, total post-warmup draws=1000.
#>
#> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> alpha_raw[1] 0.87 0.04 0.58 0.03 0.42 0.82 1.24 2.17 231 1.00
#> beta_raw[1] 0.88 0.04 0.57 0.06 0.42 0.82 1.24 2.18 230 1.01
#> alpha[1] 6.62 0.04 0.58 5.78 6.16 6.56 6.99 7.91 231 1.00
#> beta[1] 6.36 0.04 0.57 5.54 5.90 6.30 6.73 7.67 230 1.01
#> lp__ -14.99 0.16 1.69 -19.18 -15.78 -14.49 -13.71 -13.28 109 1.03
#>
#> Samples were drawn using NUTS(diag_e) at Fri May 17 06:40:42 2024.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at
#> convergence, Rhat=1).
# integer adjusted lognormal model
dist_fit(rlnorm(1:100, log(5), 0.2),
samples = 1000, dist = "lognormal",
cores = ifelse(interactive(), 4, 1), verbose = TRUE
)
#>
#> SAMPLING FOR MODEL 'dist_fit' NOW (CHAIN 1).
#> Chain 1:
#> Chain 1: Gradient evaluation took 7.5e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.75 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1:
#> Chain 1:
#> Chain 1: Iteration: 1 / 1500 [ 0%] (Warmup)
#> Chain 1: Iteration: 50 / 1500 [ 3%] (Warmup)
#> Chain 1: Iteration: 100 / 1500 [ 6%] (Warmup)
#> Chain 1: Iteration: 150 / 1500 [ 10%] (Warmup)
#> Chain 1: Iteration: 200 / 1500 [ 13%] (Warmup)
#> Chain 1: Iteration: 250 / 1500 [ 16%] (Warmup)
#> Chain 1: Iteration: 300 / 1500 [ 20%] (Warmup)
#> Chain 1: Iteration: 350 / 1500 [ 23%] (Warmup)
#> Chain 1: Iteration: 400 / 1500 [ 26%] (Warmup)
#> Chain 1: Iteration: 450 / 1500 [ 30%] (Warmup)
#> Chain 1: Iteration: 500 / 1500 [ 33%] (Warmup)
#> Chain 1: Iteration: 550 / 1500 [ 36%] (Warmup)
#> Chain 1: Iteration: 600 / 1500 [ 40%] (Warmup)
#> Chain 1: Iteration: 650 / 1500 [ 43%] (Warmup)
#> Chain 1: Iteration: 700 / 1500 [ 46%] (Warmup)
#> Chain 1: Iteration: 750 / 1500 [ 50%] (Warmup)
#> Chain 1: Iteration: 800 / 1500 [ 53%] (Warmup)
#> Chain 1: Iteration: 850 / 1500 [ 56%] (Warmup)
#> Chain 1: Iteration: 900 / 1500 [ 60%] (Warmup)
#> Chain 1: Iteration: 950 / 1500 [ 63%] (Warmup)
#> Chain 1: Iteration: 1000 / 1500 [ 66%] (Warmup)
#> Chain 1: Iteration: 1001 / 1500 [ 66%] (Sampling)
#> Chain 1: Iteration: 1050 / 1500 [ 70%] (Sampling)
#> Chain 1: Iteration: 1100 / 1500 [ 73%] (Sampling)
#> Chain 1: Iteration: 1150 / 1500 [ 76%] (Sampling)
#> Chain 1: Iteration: 1200 / 1500 [ 80%] (Sampling)
#> Chain 1: Iteration: 1250 / 1500 [ 83%] (Sampling)
#> Chain 1: Iteration: 1300 / 1500 [ 86%] (Sampling)
#> Chain 1: Iteration: 1350 / 1500 [ 90%] (Sampling)
#> Chain 1: Iteration: 1400 / 1500 [ 93%] (Sampling)
#> Chain 1: Iteration: 1450 / 1500 [ 96%] (Sampling)
#> Chain 1: Iteration: 1500 / 1500 [100%] (Sampling)
#> Chain 1:
#> Chain 1: Elapsed Time: 0.4 seconds (Warm-up)
#> Chain 1: 0.191 seconds (Sampling)
#> Chain 1: 0.591 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'dist_fit' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 7e-05 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.7 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2:
#> Chain 2:
#> Chain 2: Iteration: 1 / 1500 [ 0%] (Warmup)
#> Chain 2: Iteration: 50 / 1500 [ 3%] (Warmup)
#> Chain 2: Iteration: 100 / 1500 [ 6%] (Warmup)
#> Chain 2: Iteration: 150 / 1500 [ 10%] (Warmup)
#> Chain 2: Iteration: 200 / 1500 [ 13%] (Warmup)
#> Chain 2: Iteration: 250 / 1500 [ 16%] (Warmup)
#> Chain 2: Iteration: 300 / 1500 [ 20%] (Warmup)
#> Chain 2: Iteration: 350 / 1500 [ 23%] (Warmup)
#> Chain 2: Iteration: 400 / 1500 [ 26%] (Warmup)
#> Chain 2: Iteration: 450 / 1500 [ 30%] (Warmup)
#> Chain 2: Iteration: 500 / 1500 [ 33%] (Warmup)
#> Chain 2: Iteration: 550 / 1500 [ 36%] (Warmup)
#> Chain 2: Iteration: 600 / 1500 [ 40%] (Warmup)
#> Chain 2: Iteration: 650 / 1500 [ 43%] (Warmup)
#> Chain 2: Iteration: 700 / 1500 [ 46%] (Warmup)
#> Chain 2: Iteration: 750 / 1500 [ 50%] (Warmup)
#> Chain 2: Iteration: 800 / 1500 [ 53%] (Warmup)
#> Chain 2: Iteration: 850 / 1500 [ 56%] (Warmup)
#> Chain 2: Iteration: 900 / 1500 [ 60%] (Warmup)
#> Chain 2: Iteration: 950 / 1500 [ 63%] (Warmup)
#> Chain 2: Iteration: 1000 / 1500 [ 66%] (Warmup)
#> Chain 2: Iteration: 1001 / 1500 [ 66%] (Sampling)
#> Chain 2: Iteration: 1050 / 1500 [ 70%] (Sampling)
#> Chain 2: Iteration: 1100 / 1500 [ 73%] (Sampling)
#> Chain 2: Iteration: 1150 / 1500 [ 76%] (Sampling)
#> Chain 2: Iteration: 1200 / 1500 [ 80%] (Sampling)
#> Chain 2: Iteration: 1250 / 1500 [ 83%] (Sampling)
#> Chain 2: Iteration: 1300 / 1500 [ 86%] (Sampling)
#> Chain 2: Iteration: 1350 / 1500 [ 90%] (Sampling)
#> Chain 2: Iteration: 1400 / 1500 [ 93%] (Sampling)
#> Chain 2: Iteration: 1450 / 1500 [ 96%] (Sampling)
#> Chain 2: Iteration: 1500 / 1500 [100%] (Sampling)
#> Chain 2:
#> Chain 2: Elapsed Time: 0.406 seconds (Warm-up)
#> Chain 2: 0.18 seconds (Sampling)
#> Chain 2: 0.586 seconds (Total)
#> Chain 2:
#> Inference for Stan model: dist_fit.
#> 2 chains, each with iter=1500; warmup=1000; thin=1;
#> post-warmup draws per chain=500, total post-warmup draws=1000.
#>
#> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> mu[1] 1.63 0.00 0.02 1.60 1.62 1.64 1.65 1.68 417 1.00
#> sigma[1] 0.16 0.00 0.02 0.13 0.15 0.16 0.17 0.20 665 1.00
#> lp__ -75.22 0.07 1.08 -78.32 -75.65 -74.88 -74.44 -74.12 277 1.01
#>
#> Samples were drawn using NUTS(diag_e) at Fri May 17 06:40:43 2024.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at
#> convergence, Rhat=1).
# }