| 42333 |
ripley |
1 |
% File src/library/stats/man/makepredictcall.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 74690 |
maechler |
3 |
% Copyright 1995-2018 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 27442 |
ripley |
6 |
\name{makepredictcall}
|
| 56186 |
murdoch |
7 |
\alias{makepredictcall}
|
| 27442 |
ripley |
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
|
| 64864 |
ripley |
13 |
matrices when predicting from models with terms like (univariate)
|
|
|
14 |
\code{poly} or \code{ns}.
|
| 27442 |
ripley |
15 |
}
|
|
|
16 |
\usage{
|
|
|
17 |
makepredictcall(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
|
| 51111 |
ripley |
28 |
the terms supplied by replacing the term with one which will work for
|
| 27442 |
ripley |
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{
|
| 31094 |
ripley |
45 |
\code{\link{model.frame}}, \code{\link{poly}}, \code{\link{scale}};
|
| 51111 |
ripley |
46 |
\code{\link{bs}} and \code{\link{ns}} in package \pkg{splines}.
|
| 61433 |
ripley |
47 |
|
| 51111 |
ripley |
48 |
\code{\link{cars}} for an example of prediction from a polynomial fit.
|
| 27442 |
ripley |
49 |
}
|
|
|
50 |
\examples{
|
| 41508 |
ripley |
51 |
require(graphics)
|
|
|
52 |
|
| 27442 |
ripley |
53 |
## using poly: this did not work in R < 1.5.0
|
|
|
54 |
fm <- lm(weight ~ poly(height, 2), data = women)
|
|
|
55 |
plot(women, xlab = "Height (in)", ylab = "Weight (lb)")
|
| 80080 |
hornik |
56 |
ht <- seq(57, 73, length.out = 200)
|
| 74690 |
maechler |
57 |
nD <- data.frame(height = ht)
|
|
|
58 |
pfm <- predict(fm, nD)
|
|
|
59 |
lines(ht, pfm)
|
|
|
60 |
pf2 <- predict(update(fm, ~ stats::poly(height, 2)), nD)
|
|
|
61 |
stopifnot(all.equal(pfm, pf2)) ## was off (rel.diff. 0.0766) in R <= 3.5.0
|
| 27442 |
ripley |
62 |
|
|
|
63 |
## see also example(cars)
|
|
|
64 |
|
|
|
65 |
## see bs and ns for spline examples.
|
|
|
66 |
}
|
|
|
67 |
\keyword{models}
|