Learn R Programming

ciTools (version 0.1.0)

add_pi: 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 tb with respect to the model fit. These intervals are then appended to tb and returned to the user as a tibble. fit can be a linear, log-linear, linear mixed, or generalized linear models (currently only Poisson models are supported).

Usage

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

Arguments

tb

A tibble or 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

Value

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

Details

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

  • add_pi.lm for linear regression prediction intervals

  • add_pi.glm for generalized linear regression prediction intervals

  • add_pi.lmerMod for linear mixed models prediction intervals

See Also

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

Examples

Run this code
# NOT RUN {
# 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")


# }

Run the code above in your browser using DataLab