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.2e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.42 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.143 seconds (Warm-up)
#> Chain 1: 0.078 seconds (Sampling)
#> Chain 1: 0.221 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'dist_fit' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 3.8e-05 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.38 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.137 seconds (Warm-up)
#> Chain 2: 0.071 seconds (Sampling)
#> Chain 2: 0.208 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.21 0.02 0.32 1.67 1.99 2.18 2.40 2.91 320 1.01
#> lp__ -22.09 0.04 0.74 -24.21 -22.20 -21.82 -21.65 -21.60 297 1.00
#>
#> Samples were drawn using NUTS(diag_e) at Fri Dec 20 21:37: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.000311 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 3.11 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.494 seconds (Warm-up)
#> Chain 1: 1.102 seconds (Sampling)
#> Chain 1: 3.596 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'dist_fit' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 0.000307 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 3.07 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: 2.582 seconds (Warm-up)
#> Chain 2: 1.224 seconds (Sampling)
#> Chain 2: 3.806 seconds (Total)
#> Chain 2:
#> WARN [2024-12-20 21:37:40] 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-12-20 21:37:40] 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.84 0.03 0.53 0.04 0.45 0.74 1.16 2.03 240 1
#> beta_raw[1] 1.01 0.03 0.54 0.18 0.59 0.96 1.38 2.16 264 1
#> alpha[1] 6.21 0.03 0.53 5.40 5.81 6.10 6.52 7.39 240 1
#> beta[1] 6.46 0.03 0.54 5.63 6.04 6.41 6.83 7.60 264 1
#> lp__ -11.93 0.13 1.37 -15.78 -12.48 -11.55 -10.97 -10.52 120 1
#>
#> Samples were drawn using NUTS(diag_e) at Fri Dec 20 21:37:40 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 5.9e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.59 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.314 seconds (Warm-up)
#> Chain 1: 0.146 seconds (Sampling)
#> Chain 1: 0.46 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'dist_fit' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 5.4e-05 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.54 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.312 seconds (Warm-up)
#> Chain 2: 0.135 seconds (Sampling)
#> Chain 2: 0.447 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.61 0.00 0.02 1.57 1.59 1.61 1.62 1.65 744 1
#> sigma[1] 0.17 0.00 0.02 0.14 0.16 0.17 0.18 0.21 570 1
#> lp__ -75.88 0.04 0.90 -78.24 -76.23 -75.61 -75.22 -74.96 489 1
#>
#> Samples were drawn using NUTS(diag_e) at Fri Dec 20 21:37:41 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).
# }