Possible bug in predict.lm when x is a poly

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Possible bug in predict.lm when x is a poly

Kenny Bell-2
Hi all,

As always, thank you all for your incredible work maintaining and improving
R.

mdl <- lm(data = mtcars,
          mpg ~ I(poly(disp, 2)))

predict(mdl, newdata = data.frame(disp = c(120, 120)))
#> Error in poly(disp, 2): 'degree' must be less than number of unique
points

predict(mdl, newdata = data.frame(disp = c(120, 121, 122)))
#>         1         2         3
#> 43.937856 12.617762  3.716257

The predict function seems to require a sufficiently high number of unique
values in newdata when the RHS is a poly. Of course, I would have expected
the output here to be:

#>         1         2
#> 43.937856 43.937856

If people agree, I can submit this to bugzilla.

Kind regards,
Kenny

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Possible bug in predict.lm when x is a poly

bbolker
   Any particular reason you're using I() around your poly()?  That
looks weird to me ... and it works fine if you don't do that ...  {AND,
I think your result is *incorrect* when you have 3 observations in your
response}.

   Basically, you have managed to short-circuit the (admittedly) obscure
machinery that R uses to generate the correct bases when predicting from
new data (see ?makepredictcall ...)

On 3/29/21 6:04 PM, Kenny Bell wrote:

> Hi all,
>
> As always, thank you all for your incredible work maintaining and improving
> R.
>
> mdl <- lm(data = mtcars,
>            mpg ~ I(poly(disp, 2)))
>
> predict(mdl, newdata = data.frame(disp = c(120, 120)))
> #> Error in poly(disp, 2): 'degree' must be less than number of unique
> points
>
> predict(mdl, newdata = data.frame(disp = c(120, 121, 122)))
> #>         1         2         3
> #> 43.937856 12.617762  3.716257
>
> The predict function seems to require a sufficiently high number of unique
> values in newdata when the RHS is a poly. Of course, I would have expected
> the output here to be:
>
> #>         1         2
> #> 43.937856 43.937856
>
> If people agree, I can submit this to bugzilla.
>
> Kind regards,
> Kenny
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel