1% File src/library/stats/man/makepredictcall.Rd 2% Part of the R package, https://www.R-project.org 3% Copyright 1995-2018 R Core Team 4% Distributed under GPL 2 or later 5 6\name{makepredictcall} 7\alias{makepredictcall} 8\alias{makepredictcall.default} 9\alias{SafePrediction} 10\title{Utility Function for Safe Prediction} 11\description{ 12 A utility to help \code{\link{model.frame.default}} create the right 13 matrices when predicting from models with terms like (univariate) 14 \code{poly} or \code{ns}. 15} 16\usage{ 17makepredictcall(var, call) 18} 19\arguments{ 20 \item{var}{A variable.} 21 \item{call}{The term in the formula, as a call.} 22} 23\details{ 24 This is a generic function with methods for \code{poly}, \code{bs} and 25 \code{ns}: the default method handles \code{scale}. If 26 \code{model.frame.default} encounters such a term when 27 creating a model frame, it modifies the \code{predvars} attribute of 28 the terms supplied by replacing the term with one which will work for 29 predicting new data. For example \code{makepredictcall.ns} adds 30 arguments for the knots and intercept. 31 32 To make use of this, have your model-fitting function return the 33 \code{terms} attribute of the model frame, or copy the \code{predvars} 34 attribute of the \code{terms} attribute of the model frame to your 35 \code{terms} object. 36 37 To extend this, make sure the term creates variables with a class, 38 and write a suitable method for that class. 39} 40\value{ 41 A replacement for \code{call} for the \code{predvars} attribute of 42 the terms. 43} 44\seealso{ 45 \code{\link{model.frame}}, \code{\link{poly}}, \code{\link{scale}}; 46 \code{\link{bs}} and \code{\link{ns}} in package \pkg{splines}. 47 48 \code{\link{cars}} for an example of prediction from a polynomial fit. 49} 50\examples{ 51require(graphics) 52 53## using poly: this did not work in R < 1.5.0 54fm <- lm(weight ~ poly(height, 2), data = women) 55plot(women, xlab = "Height (in)", ylab = "Weight (lb)") 56ht <- seq(57, 73, length.out = 200) 57nD <- data.frame(height = ht) 58pfm <- predict(fm, nD) 59lines(ht, pfm) 60pf2 <- predict(update(fm, ~ stats::poly(height, 2)), nD) 61stopifnot(all.equal(pfm, pf2)) ## was off (rel.diff. 0.0766) in R <= 3.5.0 62 63## see also example(cars) 64 65## see bs and ns for spline examples. 66} 67\keyword{models} 68