Compute Confidence/Credible/Compatibility Intervals (CI) or Support Intervals (SI) for Bayesian and frequentist models. The Documentation is accessible for:
Usage
ci(x, ...)
# S3 method for numeric
ci(x, ci = 0.95, method = "ETI", verbose = TRUE, BF = 1, ...)
# S3 method for data.frame
ci(x, ci = 0.95, method = "ETI", verbose = TRUE, BF = 1, ...)
# S3 method for sim.merMod
ci(
x,
ci = 0.95,
method = "ETI",
effects = c("fixed", "random", "all"),
parameters = NULL,
verbose = TRUE,
...
)
# S3 method for sim
ci(x, ci = 0.95, method = "ETI", parameters = NULL, verbose = TRUE, ...)
# S3 method for stanreg
ci(
x,
ci = 0.95,
method = "ETI",
effects = c("fixed", "random", "all"),
component = c("location", "all", "conditional", "smooth_terms", "sigma",
"distributional", "auxiliary"),
parameters = NULL,
verbose = TRUE,
BF = 1,
...
)
# S3 method for brmsfit
ci(
x,
ci = 0.95,
method = "ETI",
effects = c("fixed", "random", "all"),
component = c("conditional", "zi", "zero_inflated", "all"),
parameters = NULL,
verbose = TRUE,
BF = 1,
...
)
# S3 method for BFBayesFactor
ci(x, ci = 0.95, method = "ETI", verbose = TRUE, BF = 1, ...)
# S3 method for MCMCglmm
ci(x, ci = 0.95, method = "ETI", verbose = TRUE, ...)
Arguments
- x
A
stanreg
orbrmsfit
model, or a vector representing a posterior distribution.- ...
Currently not used.
- ci
Value or vector of probability of the CI (between 0 and 1) to be estimated. Default to
.95
(95%
).- method
- verbose
Toggle off warnings.
- BF
The amount of support required to be included in the support interval.
- effects
Should results for fixed effects, random effects or both be returned? Only applies to mixed models. May be abbreviated.
- parameters
Regular expression pattern that describes the parameters that should be returned. Meta-parameters (like
lp__
orprior_
) are filtered by default, so only parameters that typically appear in thesummary()
are returned. Useparameters
to select specific parameters for the output.- component
Should results for all parameters, parameters for the conditional model or the zero-inflated part of the model be returned? May be abbreviated. Only applies to brms-models.
Value
A data frame with following columns:
Parameter
The model parameter(s), ifx
is a model-object. Ifx
is a vector, this column is missing.CI
The probability of the credible interval.CI_low
,CI_high
The lower and upper credible interval limits for the parameters.
Note
When it comes to interpretation, we recommend thinking of the CI in terms of
an "uncertainty" or "compatibility" interval, the latter being defined as
“Given any value in the interval and the background assumptions,
the data should not seem very surprising” (Gelman & Greenland 2019).
There is also a plot()
-method implemented in the see-package.
References
Gelman A, Greenland S. Are confidence intervals better termed "uncertainty intervals"? BMJ 2019;l5381. 10.1136/bmj.l5381
Examples
library(bayestestR)
posterior <- rnorm(1000)
ci(posterior, method = "ETI")
#> 95% ETI: [-1.91, 1.95]
ci(posterior, method = "HDI")
#> 95% HDI: [-1.98, 1.82]
df <- data.frame(replicate(4, rnorm(100)))
ci(df, method = "ETI", ci = c(0.80, 0.89, 0.95))
#> Equal-Tailed Interval
#>
#> Parameter | 80% ETI | 89% ETI | 95% ETI
#> ---------------------------------------------------------
#> X1 | [-1.09, 1.23] | [-1.38, 1.65] | [-1.58, 1.95]
#> X2 | [-1.56, 1.02] | [-1.82, 1.15] | [-2.11, 1.35]
#> X3 | [-1.21, 0.97] | [-1.28, 1.12] | [-1.52, 1.71]
#> X4 | [-1.06, 1.54] | [-1.50, 1.74] | [-1.93, 2.01]
ci(df, method = "HDI", ci = c(0.80, 0.89, 0.95))
#> Highest Density Interval
#>
#> Parameter | 80% HDI | 89% HDI | 95% HDI
#> ---------------------------------------------------------
#> X1 | [-1.04, 1.29] | [-1.60, 1.26] | [-1.60, 1.91]
#> X2 | [-1.18, 1.18] | [-1.67, 1.22] | [-2.39, 1.22]
#> X3 | [-1.00, 1.06] | [-1.28, 1.16] | [-1.89, 1.30]
#> X4 | [-0.70, 1.74] | [-1.21, 1.92] | [-2.02, 2.08]
model <- stan_glm(mpg ~ wt, data = mtcars, chains = 2, iter = 200, refresh = 0)
#> Warning: 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
#> Warning: 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
ci(model, method = "ETI", ci = c(0.80, 0.89))
#> Equal-Tailed Interval
#>
#> Parameter | 80% ETI | 89% ETI | Effects | Component
#> ---------------------------------------------------------------------
#> (Intercept) | [35.16, 39.35] | [34.66, 40.09] | fixed | conditional
#> wt | [-6.01, -4.66] | [-6.23, -4.49] | fixed | conditional
ci(model, method = "HDI", ci = c(0.80, 0.89))
#> Highest Density Interval
#>
#> Parameter | 80% HDI | 89% HDI
#> ---------------------------------------------
#> (Intercept) | [35.08, 39.34] | [34.61, 39.97]
#> wt | [-5.92, -4.60] | [-6.27, -4.60]
model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
#> Compiling Stan program...
#> Start sampling
#>
#> SAMPLING FOR MODEL '2d19b3a372313df641edf05db5e9f303' NOW (CHAIN 1).
#> Chain 1:
#> Chain 1: Gradient evaluation took 1.7e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.17 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1:
#> Chain 1:
#> Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 1:
#> Chain 1: Elapsed Time: 0.031316 seconds (Warm-up)
#> Chain 1: 0.02732 seconds (Sampling)
#> Chain 1: 0.058636 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL '2d19b3a372313df641edf05db5e9f303' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 9e-06 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.09 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2:
#> Chain 2:
#> Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 2:
#> Chain 2: Elapsed Time: 0.034546 seconds (Warm-up)
#> Chain 2: 0.035686 seconds (Sampling)
#> Chain 2: 0.070232 seconds (Total)
#> Chain 2:
#>
#> SAMPLING FOR MODEL '2d19b3a372313df641edf05db5e9f303' NOW (CHAIN 3).
#> Chain 3:
#> Chain 3: Gradient evaluation took 9e-06 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.09 seconds.
#> Chain 3: Adjust your expectations accordingly!
#> Chain 3:
#> Chain 3:
#> Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 3:
#> Chain 3: Elapsed Time: 0.033807 seconds (Warm-up)
#> Chain 3: 0.037323 seconds (Sampling)
#> Chain 3: 0.07113 seconds (Total)
#> Chain 3:
#>
#> SAMPLING FOR MODEL '2d19b3a372313df641edf05db5e9f303' NOW (CHAIN 4).
#> Chain 4:
#> Chain 4: Gradient evaluation took 8e-06 seconds
#> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.08 seconds.
#> Chain 4: Adjust your expectations accordingly!
#> Chain 4:
#> Chain 4:
#> Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 4:
#> Chain 4: Elapsed Time: 0.033134 seconds (Warm-up)
#> Chain 4: 0.030532 seconds (Sampling)
#> Chain 4: 0.063666 seconds (Total)
#> Chain 4:
ci(model, method = "ETI")
#> Equal-Tailed Interval
#>
#> Parameter | 95% ETI | Effects | Component
#> ----------------------------------------------------
#> b_Intercept | [36.13, 43.22] | fixed | conditional
#> b_wt | [-4.83, -1.62] | fixed | conditional
#> b_cyl | [-2.39, -0.63] | fixed | conditional
ci(model, method = "HDI")
#> Highest Density Interval
#>
#> Parameter | 95% HDI
#> ----------------------------
#> (Intercept) | [36.25, 43.29]
#> wt | [-4.64, -1.47]
#> cyl | [-2.42, -0.67]
bf <- ttestBF(x = rnorm(100, 1, 1))
ci(bf, method = "ETI")
#> Equal-Tailed Interval
#>
#> Parameter | 95% ETI
#> -------------------------
#> Difference | [0.70, 1.12]
ci(bf, method = "HDI")
#> Highest Density Interval
#>
#> Parameter | 95% HDI
#> -------------------------
#> Difference | [0.73, 1.13]
model <- emtrends(model, ~1, "wt")
ci(model, method = "ETI")
#> Equal-Tailed Interval
#>
#> Parameter | 95% ETI
#> --------------------------
#> overall | [-4.83, -1.62]
ci(model, method = "HDI")
#> Highest Density Interval
#>
#> Parameter | 95% HDI
#> --------------------------
#> overall | [-4.64, -1.47]