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
A multiple regression formula has multiple slopes (one for each variable) and one y-intercept. It is interpreted the same as a simple linear regression formula except there are multiple variables that all impact the slope of the relationship.
Multicollinearity occurs when two or more independent variables are highly correlated with one another in a regression model. This means that an independent variable can be predicted from another independent variable in a regression model.
Regression lines give us useful information about the data they are collected from. They show how one variable changes on average with another, and they can be used to find out what one variable is likely to be when we know the other – provided that we ask this question within the limits of the scatter diagram.
Use analysis of covariance (ancova) when you want to compare two or more regression lines to each other; ancova will tell you whether the regression lines are different from each other in either slope or intercept.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With