Learn R Programming

ciTools (version 0.1.0)

add_ci.lm: Confidence Intervals for Linear Model Predictions

Description

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

Usage

# S3 method for lm
add_ci(tb, fit, alpha = 0.05, names = NULL,
  yhatName = "pred", log_response = FALSE, ...)

Arguments

tb

A tibble or data frame.

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, confidence bounds automatically will be named by add_ci, otherwise, the lower confidence bound will be named names[1] and the upper confidence bound will be named names[2].

yhatName

A string. Name of the vector of the predictions made for each observation in tb

log_response

Logical. Default is FALSE. If TRUE, confidence intervals will be generated for the response level of a log-linear model: \(\log(Y) = X\beta + \epsilon\).

...

Additional arguments.

Value

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

Details

Confidence intervals for lm objects are calculated parametrically. This function is essentially a wrapper for predict(fit, tb, interval = "confidence") if fit is a linear model. If log_response = TRUE, confidence intervals for the response are calculated using Wald's Method. See Meeker and Escobar (1998) for details.

See Also

add_pi.lm for prediction intervals for lm objects, add_probs.lm for conditional probabilities of lm objects, and add_quantile.lm for response quantiles of lm objects.

Examples

Run this code
# NOT RUN {
# Fit a linear model
fit <- lm(dist ~ speed, data = cars)
# Get fitted values for each observation in cars, and append
# confidence intervals
add_ci(cars, fit)
# Try a different confidence level
add_ci(cars, fit, alpha = 0.5)
# Try custom names for the confidence bounds 
add_ci(cars, fit, alpha = 0.5, names = c("lwr", "upr"))

# }

Run the code above in your browser using DataLab