predict_ci: Calculate Confidence Intervals for Predictions from...

Description Usage Arguments Details Examples

View source: R/predict_ci.R

Description

Calculate Confidence Intervals for Predictions from Regression Models

Usage

1
predict_ci(m = NULL, newdata = NULL, Sigma = NULL, level = 0.95, n_sim = 1000)

Arguments

m

model object (must be of class lm, glm, MASS::polr, or nnet::multinom)

newdata

dataset to make predictions

Sigma

variance-covariance matrix of estimator; if NULL, vcov(m) is used

level

confidence level; defaults to .95

n_sim

number of simulation draws when confidence intervals are calculated via simulation; defaults to 1000

Details

This function calculates confidence intervals for predictions based on fitted GLMs. The function works similarly to other predict methods. When the fitted model is of class lm or glm, endpoints of the confidence interval of the linear predictor are inverted to obtain the confidence intervals on the outcome scale. For MASS::polr and nnet::multinom objects, the function simulates n_sim draws from a N(x'b, S) distribution, where x is the covariate profile at which the predictions are made, b is the estimated regression coefficient vector, and S is the estimated variance-covariance matrix of b. Based on these simulations, the confidence interval for the prediction are calculated and then inverted, using the inverse-link function, into the outcome scale.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# binary logistic regression example
dat_bin = data.frame(y = c(rep(0, 5), rep(1, 5)), 
                     x = c(1:7, 3:5))
fit_bin = glm(y ~ x, 
              data = dat_bin, 
              family = binomial("logit"))

predict_ci(fit_bin, newdata = data.frame(x = c(3, 5)))

# ordered logistic regression example
dat_polr = data.frame(
    y = c(rep(0, 3), rep(1, 3), rep(2, 4)), 
    x = c(1:6, 2:5))
fit_polr = MASS::polr(factor(y) ~ x, data = dat_polr)

predict_ci(fit_polr, 
           newdata = data.frame(x = c(3, 5)),
           level = .90,
           n_sim = 3000)

baruuum/btoolbox documentation built on Aug. 17, 2020, 1:29 a.m.