add_ci.lm: Confidence Intervals for Linear Model Predictions

Description Usage Arguments Details Value See Also Examples

View source: R/add_ci_lm.R

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## S3 method for class 'lm'
add_ci(
  df,
  fit,
  alpha = 0.05,
  names = NULL,
  yhatName = "pred",
  log_response = FALSE,
  ...
)

Arguments

df

A 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 df

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β + ε.

...

Additional arguments.

Details

Confidence intervals for lm objects are calculated parametrically. This function is essentially a wrapper for predict(fit, df, 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.

Value

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

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

1
2
3
4
5
6
7
8
9
# 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"))

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