Learn R Programming

s20x (version 3.4.0)

predict20x: Teaching Predictions with Confidence and Prediction Intervals

Description

predict20x() gives predictions and intervals for a fitted model. It is an S3 generic, so the same function works for linear models, generalised linear models and tslm fits.

Usage

predict20x(object, ...)

# S3 method for default predict20x(object, ...)

# S3 method for lm predict20x( object, newdata, conf.level = 0.95, digits = 3, print.out = TRUE, cilevel = conf.level, ... )

# S3 method for glm predict20x( object, newdata, conf.level = 0.95, digits = 3, print.out = TRUE, cilevel = conf.level, type = c("response", "link", "odds"), quasit = FALSE, ... )

# S3 method for tslm predict20x(object, newdata, ...)

Value

Invisibly returns an object of class s20xPrediction, which prints as the prediction table. It is a list with components

frame

data frame containing the predictions and intervals.

fit

prediction values, on the link scale for a glm fit.

se.fit

standard errors of predictions.

residual.scale

residual standard deviation, for an lm fit.

df

residual degrees of freedom, for an lm fit.

type

the scale of the results, for a glm fit.

cilevel

confidence level of the interval.

Arguments

object

a fitted model, such as the output from lm or glm.

...

optional arguments that are passed to the model's prediction method, or for other methods to predict20x().

newdata

prediction data frame.

conf.level

confidence level for the intervals.

digits

number of decimal places to print.

print.out

if TRUE, print the prediction table.

cilevel

the older name for conf.level.

type

for a glm fit, the scale of the results: "response" (the default), "link", or "odds" for a logit-link model.

quasit

if TRUE, use a t multiplier rather than a normal multiplier when object is a quasi model.

Details

For an lm fit the table holds fitted values, confidence intervals for the mean response, and prediction intervals for new observations. For a glm fit the intervals are confidence intervals, calculated on the link scale and then transformed according to type.

The columns of newdata are matched to the model variables by name when every variable is present. Otherwise they are taken in order, either one column per variable or one column per model term.

This is not a method for predict, so it does not change what predict() does for these models.

See Also

Examples

Run this code

# Zoo data
data(zoo.df)
zoo.df = within(zoo.df, {day.type = factor(day.type)})
zoo.fit = lm(log(attendance) ~ time + sun.yesterday + nice.day + day.type + tv.ads,
             data = zoo.df)
pred.zoo = data.frame(time = 8, sun.yesterday = 10.8, nice.day = 0,
                      day.type = factor(3), tv.ads = 1.181)
predict20x(zoo.fit, pred.zoo)

# Peruvian Indians data
data(peru.df)
peru.fit = lm(BP ~ age + years + I(years^2) + weight + height, data = peru.df)
pred.peru = data.frame(age = 21, years = 2, `I(years^2)` = 2, weight = 71, height = 1629)
predict20x(peru.fit, pred.peru)

# A generalised linear model: predictions on the response scale
peru.glm = glm(BP ~ log(weight), data = peru.df, family = poisson)
predict20x(peru.glm, data.frame(weight = c(60, 70)))
predict20x(peru.glm, data.frame(weight = c(60, 70)), type = "link")

Run the code above in your browser using DataLab