View source: R/get_predicted_ci.R
get_predicted_ci | R Documentation |
Confidence intervals around predicted values
get_predicted_ci(x, ...)
## Default S3 method:
get_predicted_ci(
x,
predictions = NULL,
data = NULL,
se = NULL,
ci = 0.95,
ci_type = "confidence",
ci_method = NULL,
dispersion_method = "sd",
vcov = NULL,
vcov_args = NULL,
verbose = TRUE,
...
)
x |
A statistical model (can also be a data.frame, in which case the second argument has to be a model). |
... |
Other argument to be passed, for instance to |
predictions |
A vector of predicted values (as obtained by
|
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. Visualization
matrices can be generated using |
se |
Numeric vector of standard error of predicted values. If |
ci |
The interval level. Default is |
ci_type |
Can be |
ci_method |
The method for computing p values and confidence intervals. Possible values depend on model type.
See |
dispersion_method |
Bootstrap dispersion and Bayesian posterior summary:
|
vcov |
Variance-covariance matrix used to compute uncertainty estimates (e.g., for robust standard errors). This argument accepts a covariance matrix, a function which returns a covariance matrix, or a string which identifies the function to be used to compute the covariance matrix.
|
vcov_args |
List of arguments to be passed to the function identified by
the |
verbose |
Toggle warnings. |
Typically, get_predicted()
returns confidence intervals based on the standard
errors as returned by the predict()
-function, assuming normal distribution
(+/- 1.96 * SE
) resp. a Student's t-distribution (if degrees of freedom are
available). If predict()
for a certain class does not return standard
errors (for example, merMod-objects), these are calculated manually, based
on following steps: matrix-multiply X
by the parameter vector B
to get the
predictions, then extract the variance-covariance matrix V
of the parameters
and compute XVX'
to get the variance-covariance matrix of the predictions.
The square-root of the diagonal of this matrix represent the standard errors
of the predictions, which are then multiplied by the critical test-statistic
value (e.g., ~1.96 for normal distribution) for the confidence intervals.
If ci_type = "prediction"
, prediction intervals are calculated. These are
wider than confidence intervals, because they also take into account the
uncertainty of the model itself. Before taking the square-root of the
diagonal of the variance-covariance matrix, get_predicted_ci()
adds the
residual variance to these values. For mixed models, get_variance_residual()
is used, while get_sigma()^2
is used for non-mixed models.
It is preferred to rely on standard errors returned by get_predicted()
(i.e.
returned by the predict()
-function), because these are more accurate than
manually calculated standard errors. Use get_predicted_ci()
only if standard
errors are not available otherwise. An exception are Bayesian models or
bootstrapped predictions, where get_predicted_ci()
returns quantiles of the
posterior distribution or bootstrapped samples of the predictions. These are
actually accurate standard errors resp. confidence (or uncertainty) intervals.
# Confidence Intervals for Model Predictions
# ------------------------------------------
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)
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.