Reputation: 103
Why do I get different results from
summary(lm(mpg~horsepower + I(horsepower^2),data = Auto))$coef
and
summary(lm(mpg∼poly(horsepower,2) ,data=Auto))$coef
PS: I'm practicing the labs of ISLR
Upvotes: 5
Views: 658
Reputation: 269481
poly
uses orthogonal polynomials by default. If you use poly(..., 2, raw = TRUE)
it will use raw polynomials in which case the results are the same.
If you use the default orthogonal polynomials then although it parameterizes the model differently the model still gives the same predictions. That is, fitted(lm(...))
will be the same for both your models.
library(ISLR)
fo1 <- mpg ~ horsepower + I(horsepower ^ 2)
fo2 <- mpg ~ poly(horsepower, 2)
fo3 <- mpg ~ poly(horsepower, 2, raw = TRUE)
fm1 <- lm(fo1, Auto)
fm2 <- lm(fo2, Auto)
fm3 <- lm(fo3, Auto)
all.equal(coef(summary(fm1)), coef(summary(fm3)), check.attributes = FALSE)
## [1] TRUE
all.equal(fitted(fm1), fitted(fm2))
## [1] TRUE
all.equal(fitted(fm1), fitted(fm3))
## [1] TRUE
The discussion here may be helpful: What does the R function `poly` really do?
Upvotes: 5