Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 671
Steve Rowe Avatar asked Mar 12 '14 21:03

Steve Rowe


2 Answers

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.

like image 50
Ricardo Oliveros-Ramos Avatar answered Nov 09 '22 09:11

Ricardo Oliveros-Ramos


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.

like image 1
Dirk Eddelbuettel Avatar answered Nov 09 '22 10:11

Dirk Eddelbuettel