add_pi.lm {ciTools}R Documentation

Prediction Intervals for Linear Model Predictions

Description

This function is one of the methods for add_pi and is automatically called when an object of class lm is passed to to add_pi.

Usage

## S3 method for class 'lm'
add_pi(
  df,
  fit,
  alpha = 0.05,
  names = NULL,
  yhatName = "pred",
  log_response = FALSE,
  ...
)

Arguments

df

A data frame of new data.

fit

An object of class lm. Predictions are made with this object.

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.

log_response

A logical. If TRUE, prediction intervals will be generated at the response level of a log-linear model: \log(Y) = X\beta + \epsilon. Again, these intervals will be on the scale of the original response, Y.

...

Additional arguments.

Details

Prediction intervals for lm objects are calculated parametrically. This function is essentially just a wrapper for predict(fit, df, interval = "prediction") if fit is a linear model. If log_response = TRUE, prediction intervals for the response are calculated parametrically, then the exponential function is applied to transform them to the original scale.

Value

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

See Also

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

Examples

# Fit a linear model
fit <- lm(dist ~ speed, data = cars)
# Add prediction intervals and fitted values to the original data
add_pi(cars, fit)

# Try to add predictions to a data frame of new data
new_data <- cars[sample(NROW(cars), 10), ]
add_pi(new_data, fit)

# Try a different confidence level
add_pi(cars, fit, alpha = 0.5)

# Add custom names to the prediction bounds.
add_pi(cars, fit, alpha = 0.5, names = c("lwr", "upr"))

[Package ciTools version 0.6.1 Index]