Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regression in R using poly() function

Tags:

r

lm

The function poly() in R is used in order to produce orthogonal vectors and can be helpful to interpret coefficient significance. However, I don't see the point of using it for prediction. To my view, the two following model (model_1 and model_2) should produce the same predictions.

q=1:11
v=c(3,5,7,9.2,14,20,26,34,50,59,80)
model_1=lm(v~poly(q,2))
model_2=lm(v~1+q+q^2)
predict(model_1)
predict(model_2)

But it doesn't. Why?

like image 600
Anthony Hauser Avatar asked Oct 16 '22 19:10

Anthony Hauser


1 Answers

Because they are not the same model. Your second one has one unique covariate, while the first has two.

> model_2

Call:
lm(formula = v ~ 1 + q + q^2)

Coefficients:
(Intercept)            q  
    -15.251        7.196  

You should use the I() function to modify one parameter inside your formula in order the regression to consider it as a covariate:

model_2=lm(v~1+q+I(q^2))

> model_2

Call:
lm(formula = v ~ 1 + q + I(q^2))

Coefficients:
(Intercept)            q       I(q^2)  
     7.5612      -3.3323       0.8774  

will give the same prediction

> predict(model_1)
        1         2         3         4         5         6         7         8         9        10        11 
 5.106294  4.406154  5.460793  8.270210 12.834406 19.153380 27.227133 37.055664 48.638974 61.977063 77.069930 
> predict(model_2)
        1         2         3         4         5         6         7         8         9        10        11 
 5.106294  4.406154  5.460793  8.270210 12.834406 19.153380 27.227133 37.055664 48.638974 61.977063 77.069930
like image 104
denis Avatar answered Nov 02 '22 04:11

denis