Learn R Programming

insight (version 0.16.0)

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, ...)

# S3 method for default get_predicted_ci( x, predictions = NULL, data = NULL, se = NULL, ci = 0.95, ci_type = "confidence", ci_method = "quantile", dispersion_method = "sd", vcov_estimation = NULL, vcov_type = NULL, vcov_args = NULL, ... )

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()).

...

Not used for now.

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.

se

Numeric vector of standard error of predicted values. If NULL, standard errors are calculated based on the variance-covariance matrix.

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]).

dispersion_method, ci_method

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

vcov_estimation

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

vcov_type

Character vector, specifying the estimation type for the robust covariance matrix estimation (see sandwich::vcovHC() or clubSandwich::vcovCR() for details). Only applies if vcov_estimation is a string, and not a matrix.

vcov_args

List of named vectors, used as additional arguments that are passed down to the sandwich-function specified in vcov_estimation. Only applies if vcov_estimation is a string, and not a matrix.

Value

The Confidence (or Prediction) Interval (CI).

Examples

Run this code
# NOT RUN {
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)
ci_vals <- get_predicted_ci(x, predictions, ci = c(0.8, 0.9, 0.95))
head(ci_vals)

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

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

  ci_vals <- get_predicted_ci(x,
    predictions,
    dispersion_method = "MAD",
    ci_method = "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)
# }

Run the code above in your browser using DataLab