add_pi.lm {ciTools}R Documentation

Prediction Intervals for Linear Model Predictions


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.


## S3 method for class 'lm'
  alpha = 0.05,
  names = NULL,
  yhatName = "pred",
  log_response = FALSE,



A data frame of new data.


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


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


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].


A string. Name of the predictions vector.


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.


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.


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.


# 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]