attach(mtcars)
poly_model <- lm(mpg ~ poly(hp,degree=1), data = mtcars)
summary(poly_model)
lin_model <- lm(mpg ~ hp, data = mtcars)
summary(lin_model)
x <- with(mtcars, seq(min(hp), max(hp), length.out=2000))
y1 <- predict(poly_model, newdata = data.frame(hp = x))
y2 <- predict(lin_model, newdata = data.frame(hp = x))
plot(mpg ~ hp, data = mtcars)
lines(x, y1, col = "red")
lines(x, y2, col = "blue")
poly_model and lin_model produce different coefficients.
poly_model
Estimate Std. Error t value Pr(>|t|)
(Intercept) 20.0906 0.6829 29.420 < 2e-16 ***
poly(hp, degree = 1) -26.0456 3.8630 -6.742 1.79e-07 ***
lin_model
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.09886 1.63392 18.421 < 2e-16 ***
hp -0.06823 0.01012 -6.742 1.79e-07 ***
By inspection of the plot, it appears the lin_model coefficients are correct. Why does poly_model appear to produce different coefficients? The plots overlap and R2 is the same for both.