Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

get_predicted_ci

Confidence and Prediction Interval for Model Predictions


Description

Returns the Confidence (or Prediction) Interval (CI) associated with predictions made by a model.

Usage

get_predicted_ci(
  x,
  predictions = NULL,
  data = NULL,
  ci = 0.95,
  ci_type = "confidence",
  vcov_estimation = NULL,
  vcov_type = NULL,
  vcov_args = NULL,
  dispersion_function = "sd",
  interval_function = "quantile",
  ...
)

Arguments

x

A statistical model (can also be a data.frame, in which case the second argument has to be a model).

predictions

A vector of predicted values (as obtained by stats::fitted(), stats::predict() or get_predicted).

data

An optional data frame in which to look for variables with which to predict. If omitted, the data used to fit the model is used.

ci

The interval level (default 0.95, i.e., 95% CI).

ci_type

Can be "prediction" or "confidence". Prediction intervals show the range that likely contains the value of a new observation (in what range it would fall), whereas confidence intervals reflect the uncertainty around the estimated parameters (and gives the range of the link; for instance of the regression line in a linear regressions). Prediction intervals account for both the uncertainty in the model's parameters, plus the random variation of the individual values. Thus, prediction intervals are always wider than confidence intervals. Moreover, prediction intervals will not necessarily become narrower as the sample size increases (as they do not reflect only the quality of the fit). This applies mostly for "simple" linear models (like lm), as for other models (e.g., glm), prediction intervals are somewhat useless (for instance, for a binomial model for which the dependent variable is a vector of 1s and 0s, the prediction interval is... [0, 1]).

vcov_estimation

String, indicating the suffix of the vcov*()-function from the sandwich or clubSandwich package, e.g. vcov_estimation = "CL" (which calls vcovCL to compute clustered covariance matrix estimators), or vcov_estimation = "HC" (which calls vcovHC() to compute heteroskedasticity-consistent covariance matrix estimators).

vcov_type

Character vector, specifying the estimation type for the robust covariance matrix estimation (see vcovHC() or clubSandwich::vcovCR() for details).

vcov_args

List of named vectors, used as additional arguments that are passed down to the sandwich-function specified in vcov_estimation.

dispersion_function, interval_function

These arguments are only used in the context of bootstrapped and Bayesian models. Possible values are dispersion_function = c("sd", "mad") and interval_function = c("quantile", "hdi", "eti"). For the latter, the bayestestR package is required.

...

Not used for now.

Value

The Confidence (or Prediction) Interval (CI).

Examples

data(mtcars)

# Linear model
x <- lm(mpg ~ cyl + hp, data = mtcars)
predictions <- predict(x)
ci_vals <- get_predicted_ci(x, predictions, ci_type = "prediction")
head(ci_vals)
ci_vals <- get_predicted_ci(x, predictions, ci_type = "confidence")
head(ci_vals)

predictions <- get_predicted(x, iterations = 500)
get_predicted_ci(x, predictions)

if (require("bayestestR")) {
  ci_vals <- get_predicted_ci(x, predictions, ci = c(0.80, 0.95))
  head(ci_vals)
  bayestestR::reshape_ci(ci_vals)

  ci_vals <- get_predicted_ci(x,
    predictions,
    dispersion_function = "MAD",
    interval_function = "HDI"
  )
  head(ci_vals)
}


# Logistic model
x <- glm(vs ~ wt, data = mtcars, family = "binomial")
predictions <- predict(x, type = "link")
ci_vals <- get_predicted_ci(x, predictions, ci_type = "prediction")
head(ci_vals)
ci_vals <- get_predicted_ci(x, predictions, ci_type = "confidence")
head(ci_vals)

insight

Easy Access to Model Information for Various Model Objects

v0.14.0
GPL-3
Authors
Daniel Lüdecke [aut, cre] (<https://orcid.org/0000-0002-8895-3206>, @strengejacke), Dominique Makowski [aut, ctb] (<https://orcid.org/0000-0001-5375-9967>, @Dom_Makowski), Indrajeet Patil [aut, ctb] (<https://orcid.org/0000-0003-1995-6531>, @patilindrajeets), Philip Waggoner [aut, ctb] (<https://orcid.org/0000-0002-7825-7573>), Mattan S. Ben-Shachar [aut, ctb] (<https://orcid.org/0000-0002-4287-4801>), Brenton M. Wiernik [aut] (<https://orcid.org/0000-0001-9560-6336>, @bmwiernik)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.