Regression in R using poly() function

自闭症网瘾萝莉.ら 提交于 2021-02-20 10:11:20

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/50099312/regression-in-r-using-poly-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!