Bug 16239 - Bug in predict(..., newdata) with poly() on more than one variable
Summary: Bug in predict(..., newdata) with poly() on more than one variable
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Models (show other bugs)
Version: R 3.1.2
Hardware: All All
: P5 major
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2015-03-05 18:34 UTC by Russ Lenth
Modified: 2015-03-05 18:34 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Russ Lenth 2015-03-05 18:34:20 UTC
The bug in poly, when predicting with newdata, remains when there is more than one predictor involved. Consider the following code:

# --- test code ---
set.seed(1234)
foo <- data.frame(x1 = runif(50), x2 = runif(50))
foo <- transform(foo, y = (x1 - .35)^2 + x2 + .05 * rnorm(50))
foo.lm1 <- lm(y ~ poly(x1, x2, degree = 2), data = foo)
foo.lm2 <- lm(y ~ poly(x1, x2, degree = 2, raw = TRUE), data = foo)
nd1 <- expand.grid(x1 = 0:4, x2 = 0:4)
nd2 <- expand.grid(x1 = -2:2, x2 = -2:2)

predict(foo.lm1, newdata = nd1)
predict(foo.lm1, newdata = nd2)
predict(foo.lm2, newdata = nd1)
predict(foo.lm2, newdata = nd2)
# --- end of code ---

The predict statements at the end produce the following results:
> predict(foo.lm1, newdata = nd1) 
          1           2           3           4           5           6 
-0.07528595 -0.18951330 -0.18890527 -0.07346185  0.15681696  0.19497183 ...

> predict(foo.lm1, newdata = nd2)
          1           2           3           4           5           6 
-0.07528595 -0.18951330 -0.18890527 -0.07346185  0.15681696  0.19497183 ...

> predict(foo.lm2, newdata = nd1)
         1          2          3          4          5          6
 0.1673247  0.4222482  2.7923988  7.2777768 13.8783820  1.1099921  ...

> predict(foo.lm2, newdata = nd2)
         1          2          3          4          5          6
 4.5005300  0.3455516 -1.6941995 -1.6187233  0.5719801  5.2478764  ... 

Note that the first two `predict` calls produce the same results even though `newdata` is different. The 3rd and 4th `predict` calls (using foo.lm2, which uses `raw = TRUE` produce the correct results.

I did do additional tests using models like lm(y ~ poly(x1, degree = 2), data = foo), and verified that for one-predictor models with `poly`, predictions on new data seem to work correctly.