Learn R Programming

ciTools (version 0.1.0)

add_pi.glm: Prediction Intervals for Generalized Linear Models

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

# S3 method for glm
add_pi(tb, fit, alpha = 0.05, names = NULL,
  yhatName = "pred", nSims = 1000, type = "sim", ...)

Arguments

tb

A tibble or 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.

type

A string. Currently type = "sim" is the only valid string.

...

Additional arguments.

Value

A tibble, tb, with predicted values, upper and lower prediction bounds attached.

Details

Prediction intervals are generated through simulation via arm::sim. At the moment, only prediction intervals for Poisson GLMs with the log link function are supported. Note that if the response is count data, prediction intervals are only approximate.

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

Run this code
# NOT RUN {
# 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"))

# }

Run the code above in your browser using DataLab