lm(进攻$ R $进攻$ OBP)和lm(R〜OBP)有什么区别? [英] What is the difference between lm(offense$R ~ offense$OBP) and lm(R ~ OBP)?

查看:149
本文介绍了lm(进攻$ R $进攻$ OBP)和lm(R〜OBP)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R创建一个线性模型,并用它来预测一些值。主题是棒球统计。如果我这样做:

  obp < -  lm(进攻$ R〜进攻$ OBP)
predict(obp ,newdata = data.frame(OBP = 0.5),interval =predict)

错误:警告消息:'newdata'有1行,但找到的变量有20行。



但是,如果我这样做:



pre> 附加(进攻)
obp < - lm(R〜OBP)
predict(obp,newdata = data.frame(OBP = 0.5) ,interval =predict)

它按预期工作,我得到一个结果。两者有什么区别?如果我打印OBP并且进攻$ OBP,他们看起来一样。

解决方案

在第一种情况下,如果你打印模型:

 致电:
lm(公式=进攻$ R〜进攻$ OBP)

系数:
(截取)进攻$ OBP
-0.1102 0.5276

但是在第二个,你得到这个:

 调用:
lm(formula = R〜OBP)

系数:
(截取)OBP
-0.1102 0.5276

看系数的名称。当您使用 newdata = data.frame(OBP = 0.5)创建新数据时,这对第一个模型并不真实,因此newdata将被忽略,您只能获得预测值与训练数据。当您使用进攻$ R〜进攻$ OBP 时,公式在每一边只有两个向量,没有与 data.frame



最好的方法是:

  obp < -  lm(R〜OBP,data =进攻)
predict(obp,newdata = data.frame(OBP = 0.5),interval =predict)

您将得到正确的结果, OBP = 0.5


I am trying to use R to create a linear model and use that to predict some values. The subject matter is baseball stats. If I do this:

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

I get the error: Warning message: 'newdata' had 1 row but variables found have 20 rows.

However, if I do this:

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

It works as expected and I get one result. What is the difference between the two? If I just print OBP and offense$OBP, they look the same.

解决方案

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.

这篇关于lm(进攻$ R $进攻$ OBP)和lm(R〜OBP)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆