add_pi.glm: Prediction Intervals for Generalized Linear Models

Description Usage Arguments Details Value See Also Examples

View source: R/add_pi_glm.R

Description

This function is one of the methods for add_pi, and is called automatically when add_pi is used on a fit of class glm.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## S3 method for class 'glm'
add_pi(
  df,
  fit,
  alpha = 0.05,
  names = NULL,
  yhatName = "pred",
  nSims = 2000,
  ...
)

Arguments

df

A data frame of new data.

fit

An object of class glm.

alpha

A real number between 0 and 1. Controls the confidence level of the interval estimates.

names

NULL or character vector of length two. If NULL, prediction bounds automatically will be named by add_pi, otherwise, the lower prediction bound will be named names[1] and the upper prediction bound will be named names[2].

yhatName

A string. Name of the predictions vector.

nSims

A positive integer. Determines the number of simulations to run.

...

Additional arguments.

Details

Prediction intervals are generated through simulation with the aid arm::sim, which simulates the uncertainty in the regression coefficients. At the moment, only prediction intervals for Poisson, Quasipoisson, Gaussian, and Gamma GLMs are supported. Note that if the response is count data, prediction intervals are only approximate. Simulation from the QuasiPoisson model is performed with the negative binomial distribution, see Gelman and Hill (2007).

Value

A dataframe, df, with predicted values, upper and lower prediction bounds attached.

See Also

add_ci.glm for confidence intervals for glm objects, add_probs.glm for conditional probabilities of glm objects, and add_quantile.glm for response quantiles of glm objects.

Examples

1
2
3
4
5
6
7
8
# Fit a Poisson model
fit <- glm(dist ~ speed, data = cars, family = "poisson")
# Add prediction intervals and fitted values to the original data frame
add_pi(cars, fit)
# Try a different confidence level
add_pi(cars, fit, alpha = 0.5)
# Try custom names for the prediction bounds (may be useful for plotting)
add_pi(cars, fit, alpha = 0.5, names = c("lwr", "upr"))

ciTools documentation built on Jan. 13, 2021, 7 a.m.