What is the difference between lm(offense$R ~ offense$OBP) and lm(R ~ OBP)?

送分小仙女□ 提交于 2019-11-29 14:12:00

In the first case, you get this if you print the model:

Call:
lm(formula = offense$R ~ offense$OBP)

Coefficients:
(Intercept)  offense$OBP  
    -0.1102       0.5276 

But in the second, you get this:

Call:
lm(formula = R ~ OBP)

Coefficients:
(Intercept)          OBP  
    -0.1102       0.5276  

Look at the name of the coefficients. When you create your newdata with newdata=data.frame(OBP=0.5), that not really make sense for the first model, so newdata is ignored and you only get the predicted values with the training data. When you use offense$R ~ offense$OBP, the formula has just two vectors at each side, with no names associated to a data.frame.

The best way to do it is:

obp = lm(R ~ OBP, data=offense)
predict(obp, newdata=data.frame(OBP=0.5), interval="predict")

And you'll get the proper result, the prediction for OBP=0.5.

There is no difference---you get the same coefficients.

But some programming styles are better than others -- and attach is to be avoided, as is the more verbose first form.

Most experienced users do

 lm(R ~ OBP, offense)

instead.

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