Utility Function for Safe Prediction
A utility to help
model.frame.default create the right
matrices when predicting from models with terms like (univariate)
- A variable.
- The term in the formula, as a call.
This is a generic function with methods for
ns: the default method handles
model.frame.default encounters such a term when
creating a model frame, it modifies the
predvars attribute of
the terms supplied by replacing the term with one which will work for
predicting new data. For example
arguments for the knots and intercept.
To make use of this, have your model-fitting function return the
terms attribute of the model frame, or copy the
attribute of the
terms attribute of the model frame to your
To extend this, make sure the term creates variables with a class, and write a suitable method for that class.
- A replacement for
predvarsattribute of the terms.
cars for an example of prediction from a polynomial fit.
require(graphics) ## using poly: this did not work in R < 1.5.0 fm <- lm(weight ~ poly(height, 2), data = women) plot(women, xlab = "Height (in)", ylab = "Weight (lb)") ht <- seq(57, 73, len = 200) lines(ht, predict(fm, data.frame(height = ht))) ## see also example(cars) ## see bs and ns for spline examples.