add_pi {ciTools}R Documentation

Add Prediction Intervals to Data Frames

Description

This is a generic function to append prediction intervals to a data frame. A prediction interval is made for each observation in df with respect to the model fit. These intervals are then appended to df and returned to the user as a data frame. fit can be a linear, log-linear, linear mixed, generalized linear, generalized linear mixed, or accelerated failure time model.

Usage

add_pi(df, fit, alpha = 0.05, names = NULL, yhatName = "pred", ...)

Arguments

df

A data frame of new data.

fit

An object of class lm, glm, or lmerMod. 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 piNames[1] and the upper prediction bound will be named piNames[2].

yhatName

A string. Name of the predictions vector.

...

Additional arguments

Details

For more specific information about the arguments that are applicable in each method, consult:

Value

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

See Also

add_ci for confidence intervals, add_probs for response level probabilities, and add_quantile for quantiles of the conditional response distribution.

Examples

# Fit a linear model
fit <- lm(dist ~ speed, data = cars)
# Define some new data
new_data <- cars[sample(NROW(cars), 10), ]
# Add fitted values and prediction intervals to new_data
add_pi(new_data, fit)

# Fit a Poisson model
fit2 <- glm(dist ~ speed, family = "poisson", data = cars)
# Add approximate prediction intervals to the fitted values of
# new_data
add_pi(new_data, fit2)

# Fit a linear mixed model
fit3 <- lme4::lmer(Reaction ~ Days + (1|Subject), data = lme4::sleepstudy)
# Add parametric prediction intervals for the fitted values to the
# original data
add_pi(lme4::sleepstudy, fit3, type = "parametric")



[Package ciTools version 0.6.1 Index]