Learn R Programming

bayestestR (version 0.2.0)

ci: Confidence/Credible Interval

Description

Compute Confidence/Credible Intervals (CI) for Bayesian (using quantiles) and frequentist models.

Usage

ci(x, ...)

# S3 method for numeric ci(x, ci = 0.89, verbose = TRUE, ...)

# S3 method for data.frame ci(x, ci = 0.89, verbose = TRUE, ...)

# S3 method for stanreg ci(x, ci = 0.89, effects = c("fixed", "random", "all"), parameters = NULL, verbose = TRUE, ...)

# S3 method for brmsfit ci(x, ci = 0.89, effects = c("fixed", "random", "all"), component = c("conditional", "zi", "zero_inflated", "all"), parameters = NULL, verbose = TRUE, ...)

# S3 method for BFBayesFactor ci(x, ci = 0.89, verbose = TRUE, ...)

Arguments

x

A stanreg or brmsfit model, or a vector representing a posterior distribution.

...

Currently not used.

ci

Value or vector of probability of the interval (between 0 and 1) to be estimated. Named Credible Interval (CI) for consistency.

verbose

Toggle off warnings.

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__ or prior_) are filtered by default, so only parameters that typically appear in the summary() are returned. Use parameters 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), if x is a model-object. If x 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.

Details

Documentation is accessible for:

Bayesian models

This functions returns, by default, the quantile interval, i.e., an equal-tailed interval (ETI). A 90% ETI has 5% of the distribution on either side of its limits. It indicates the 5th percentile and the 95h percentile. In symmetric distributions, the two methods of computing credible intervals, the ETI and the HDI, return similar results.

This is not the case for skewed distributions. Indeed, it is possible that parameter values in the ETI have lower credibility (are less probable) than parameter values outside the ETI. This property seems undesirable as a summary of the credible values in a distribution.

On the other hand, the ETI range does change when transformations are applied to the distribution (for instance, for a log odds scale to probabilities): the lower and higher bounds of the transformed distribution will correspond to the transformed lower and higher bounds of the original distribution. On the contrary, applying transformations to the distribution will change the resulting HDI.

Frequentist models

This function is implemented in the parameters package and attemps to retrieve, or compute, the Confidence Interval (default ci level: .95).

Examples

Run this code
# NOT RUN {
library(bayestestR)

posterior <- rnorm(1000)
ci(posterior)
ci(posterior, ci = c(.80, .89, .95))

df <- data.frame(replicate(4, rnorm(100)))
ci(df)
ci(df, ci = c(.80, .89, .95))

library(rstanarm)
model <- stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200)
ci(model)
ci(model, ci = c(.80, .89, .95))

# }
# NOT RUN {
library(brms)
model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
ci(model)
ci(model, ci = c(.80, .89, .95))

library(BayesFactor)
bf <- ttestBF(x = rnorm(100, 1, 1))
ci(bf)
ci(bf, ci = c(.80, .89, .95))
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab