Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top