Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different result from the same regression

Tags:

r

glm

lm

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

like image 713
alien_plutone Avatar asked Sep 23 '17 15:09

alien_plutone


People also ask

Why the results of simple regression are different from multiple regression?

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.

What is multicollinearity in regression analysis?

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.

What does a regression test tell us about the relationship between two variables?

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.

How do you know if two regression lines are significantly different?

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.


1 Answers

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?

like image 140
G. Grothendieck Avatar answered Oct 18 '22 07:10

G. Grothendieck