Take a simple linear model based on the mtcars dataset.
lm1 = lm(mpg~disp,data=mtcars)
summary(lm1)
##
## Call:
## lm(formula = mpg ~ disp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.8922 -2.2022 -0.9631 1.6272 7.2305
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 29.599855 1.229720 24.070 < 2e-16 ***
## disp -0.041215 0.004712 -8.747 9.38e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.251 on 30 degrees of freedom
## Multiple R-squared: 0.7183, Adjusted R-squared: 0.709
## F-statistic: 76.51 on 1 and 30 DF, p-value: 9.38e-10
Suppose I want to use the model to predict the value of mpg when disp = 300.
One way to do this is to use the printed coefficients and write an expression by hand.
29.599855 -0.041215 * 300
## [1] 17.23536
To use the predict function you need to create a dataframe with the value of mpg you want to use. Then feed the dataframe into predict
mycar = data.frame(disp=300)
predict(lm1,newdata=mycar)
## 1
## 17.23532
This looks a little strange because mycar is a dataframe with only one vector, disp; and the vector disp only has one entry. You have to do this because predict needs its argument newdata to be a dataframe.