Learn R Programming

EnvStats (version 1.0.3)

predict.lm: Predict method for Linear Model Fits

Description

Predicted values based on linear model object. This function is a modified version of the built-in Rfunction predict.lm. The EnvStats function predict.lm returns a component called n.coefs when the argument se.fit=TRUE. The component n.coefs is used by the function pointwise to create simultaneous confidence or prediction limits.

Usage

## S3 method for class 'lm':
predict(object, newdata, se.fit = FALSE, scale = NULL, df = Inf, 
  interval = c("none", "confidence", "prediction"), 
  level = 0.95, type = c("response", "terms"), 
  terms = NULL, na.action = na.pass, 
  pred.var = NULL, weights = 1, ...)

Arguments

object
Object of class "lm" or a class that inherits from "lm"
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 scalar indicating whether to compute standard errors. The default value is se.fit=FALSE.
scale
Scale parameter for std.err. calculation.
df
Degrees of freedom for scale.
interval
Type of interval calculation. Possible values are "none" (the default), "confidence", and "prediction".
level
Tolerance/confidence level.
type
Type of prediction (response or model term).
terms
If type="terms", this argument determines which terms (the default is all terms).
na.action
A function determining what should be done with missing values in newdata. The default is to predict NA.
pred.var
The variance(s) for future observations to be assumed for prediction intervals. See Details.
weights
The variance weights for prediction. This can be a numeric vector or a one-sided model formula. In the latter case, it is interpreted as an expression evaluated in newdata.
...
Further arguments passed to or from other methods.

Value

  • predict.lm produces a vector of predictions or a matrix of predictions and bounds with column names fit, lwr, and upr if interval is set. If se.fit is TRUE, a list with the following components is returned:
  • fitvector or matrix as above
  • se.fitstandard error of predicted means
  • residual.scaleresidual standard deviations
  • dfdegrees of freedom for residual
  • n.coefsnumeric scalar denoting the number of predictor variables used in the model

Details

See the Rhelp file for predict.lm.

References

Chambers, J.M., and Hastie, T.J., eds. (1992). Statistical Models in S. Chapman and Hall/CRC, Boca Raton, FL. Draper, N., and H. Smith. (1998). Applied Regression Analysis. Third Edition. John Wiley and Sons, New York, Chapter 3. Millard, S.P., and N.K. Neerchal. (2001). Environmental Statistics with S-PLUS. CRC Press, Boca Raton, FL, pp.546-553. Miller, R.G. (1981a). Simultaneous Statistical Inference. Springer-Verlag, New York, pp.111, 124.

See Also

Rhelp file for predict.lm, predict, lm, calibrate, calibrate, inversePredictCalibrate, detectionLimitCalibrate. SafePrediction for prediction from polynomial and spline fits.

Examples

Run this code
# Using the data from the built-in data frame Air.df, 
  # fit the cube-root of ozone as a function of temperature, 
  # then compute predicted values for ozone at 70 and 90 degrees F,
  # along with the standard errors of these predicted values.

  # First look at the data
  #-----------------------
  attach(Air.df)

  plot(temperature, ozone, xlab = "Temperature (degrees F)", 
    ylab = "Cube-Root Ozone (ppb)")


  # Now create the lm object 
  #-------------------------
  ozone.fit <- lm(ozone ~ temperature, data = Air.df) 


  # Now get predicted values and CIs at 70 and 90 degrees.
  # Note the presence of the last component called n.coefs.
  #--------------------------------------------------------
  predict.list <- predict(ozone.fit, 
    newdata = data.frame(temperature = c(70, 90)), se.fit = TRUE) 

  predict.list
  #$fit
  #       1        2 
  #2.697810 4.101808 
  #
  #$se.fit
  #         1          2 
  #0.07134554 0.08921071 
  #
  #$df
  #[1] 114
  #
  #$residual.scale
  #[1] 0.5903046
  #
  #$n.coefs
  #[1] 2

 
  #----------

  #Continuing with the above example, create a scatterplot of 
  # cube-root ozone vs. temperature, and add the fitted line 
  # along with simultaneous 95\% confidence bands.

  plot(temperature, ozone, xlab = "Temperature (degrees F)", 
    ylab = "Cube-Root Ozone (ppb)")

  abline(ozone.fit, lwd = 3, col = "blue")

  new.temp <- seq(min(temperature), max(temperature), length = 100)

  predict.list <- predict(ozone.fit, 
    newdata = data.frame(temperature = new.temp), 
    se.fit = TRUE)

  ci.ozone <- pointwise(predict.list, coverage = 0.95, 
    simultaneous = TRUE)

  lines(new.temp, ci.ozone$lower, lty = 2, lwd = 3, col = "magenta") 

  lines(new.temp, ci.ozone$upper, lty = 2, lwd = 3, col = "magenta") 

  title(main=paste("Scatterplot of Cube-Root Ozone vs. Temperature", 
    "with Fitted Line and Simultaneous 95% Confidence Bands", 
    sep=""))


  #----------

  # Clean up

  rm(ozone.fit, predict.list, new.temp, ci.ozone)
  detach("Air.df") 

  #----------------------------------------------------------------

  # Examples from the R help file for predict.lm:

  require(graphics)

  ## Predictions
  x <- rnorm(15)
  y <- x + rnorm(15)
  predict(lm(y ~ x))
  new <- data.frame(x = seq(-3, 3, 0.5))
  predict(lm(y ~ x), new, se.fit = TRUE)
  pred.w.plim <- predict(lm(y ~ x), new, interval="prediction")
  pred.w.clim <- predict(lm(y ~ x), new, interval="confidence")
  matplot(new$x,cbind(pred.w.clim, pred.w.plim[,-1]),
          lty=c(1,2,2,3,3), type="l", ylab="predicted y")

  ## Prediction intervals, special cases
  ##  The first three of these throw warnings
  w <- 1 + x^2
  fit <- lm(y ~ x)
  wfit <- lm(y ~ x, weights = w)
  predict(fit, interval = "prediction")
  predict(wfit, interval = "prediction")
  predict(wfit, new, interval = "prediction")
  predict(wfit, new, interval = "prediction", weights = (new$x)^2)
  predict(wfit, new, interval = "prediction", weights = ~x^2)

Run the code above in your browser using DataLab