predFit: Predictions from a Fitted Model

View source: R/predFit.R

predFitR Documentation

Predictions from a Fitted Model

Description

Generic prediction method for various types of fitted models. predFit can be used to obtain standard errors of fitted values and adjusted/unadjusted confidence/prediction intervals for objects of class "lm", "nls", and "glm".

Usage

predFit(object, ...)

## Default S3 method:
predFit(object, ...)

## S3 method for class 'lm'
predFit(
  object,
  newdata,
  se.fit = FALSE,
  interval = c("none", "confidence", "prediction"),
  level = 0.95,
  adjust = c("none", "Bonferroni", "Scheffe"),
  k,
  ...
)

## S3 method for class 'glm'
predFit(
  object,
  newdata,
  type = c("link", "response"),
  se.fit = FALSE,
  interval = c("none", "confidence"),
  level = 0.95,
  ...
)

## S3 method for class 'nls'
predFit(
  object,
  newdata,
  se.fit = FALSE,
  interval = c("none", "confidence", "prediction"),
  level = 0.95,
  adjust = c("none", "Bonferroni", "Scheffe"),
  k,
  ...
)

## S3 method for class 'lme'
predFit(object, newdata, se.fit = FALSE, ...)

Arguments

object

An object that inherits from class "lm", "glm", "nls", or "lme".

...

Additional optional arguments. At present, no optional arguments are used.

newdata

An optional data frame in which to look for variables with which to predict. If omitted, the fitted values are used.

se.fit

A logical vaue indicating if standard errors are required. Default is FALSE.

interval

Type of interval to be calculated. Can be one of "none" (default), "confidence", or "prediction". Default is "none".

level

A numeric scalar between 0 and 1 giving the confidence level for the intervals (if any) to be calculated. Default is 0.95.

adjust

A logical value indicating if an adjustment should be made to the critical value used in calculating the confidence interval. This is useful for when the calibration curve is to be used multiple, say k, times. Default is FALSE.

k

The number times the calibration curve is to be used for computing a confidence/prediction interval. Only needed when adjust = "Bonferroni".

type

Character string specifying the type of prediction. Current options are type = "link" (the default) and type = "response".

Details

Confidence and prediction intervals for linear models (i.e., "lm" objects) are obtained according to the usual formulas. Nonlinear and generalized linear models (i.e., "nls" and "glm" objects), on the other hand, rely on Taylor-series approximations for the standard errors used in forming the intervals. Approximate standard errors for the fitted values in linear mixed-effects models (i.e., "lme" objects) can also be computed; however, these rely on the approximate variance-covariance matrix of the fixed-effects estimates and often under estimate the true standard error. More accurate standard errors can be obtained using the parametric bootstrap; see bootMer for details.

For linear and nonlinear models, it is possible to request adjusted confidence or prediction intervals using the Bonferroni method (adjust = "Bonferroni") or Scheffe's method (adjust = "Scheffe"). For the Bonferroni adjustment, you must specify a value for k, the number of intervals for which the coverage is to hold simultaneously. For the Scheffe adjustment, specifying a value for k is only required when interval = "prediction"; if interval = "confidence", k is set equal to p, the number of regression parameters. For example, calling plotFit on "lm" objects with interval = "confidence" and adjust = "Scheffe" will plot the Working-Hotelling band.

Value

If se.fit = FALSE, then predFit() returns a vector of predictions or a matrix of predictions and bounds with column names fit, lwr, and upr if interval is not "none". (This function is more so meant for internal use.)

If se.fit = TRUE, then a list with the following components is returned:

  • fit a vector or matrix as described above;

  • se.fit a vector containing the standard errors of the predicted means;

  • residual.scale the residual standard deviations;

  • df the residual degrees of freedom.

Examples

# A linear regression example (see ?datasets::cars)
cars.lm <- lm(dist ~ speed + I(speed^2), data = cars)
predFit(cars.lm, interval = "confidence")

# A nonlinear least squares example (see ?datasets::Puromycin)
data(Puromycin, package = "datasets")
Puromycin2 <- Puromycin[Puromycin$state == "treated", ][, 1:2]
Puro.nls <- nls(rate ~ Vm * conc/(K + conc), data = Puromycin2,
                start = c(Vm = 200, K = 0.05))
conc <- seq(from = 0.02, to = 1.10, length = 101)
pred <- predFit(Puro.nls, newdata = data.frame(conc), interval = "prediction")
plot(Puromycin2, ylim = c(min(pred[, "lwr"]), max(pred[, "upr"])))
lines(conc, pred[, "fit"], lwd = 2)
lines(conc, pred[, "lwr"], lty = 2)
lines(conc, pred[, "upr"], lty = 2)

investr documentation built on March 31, 2022, 9:06 a.m.