How do you predict outcomes from a new dataset using a model created from a different dataset in R?

StackOverflow https://stackoverflow.com/questions/23141479

  •  05-07-2023
  •  | 
  •  

Question

I could be missing something about prediction -- but my multiple linear regression is seemingly working as expected:

> bigmodel <- lm(score ~ lean + gender + age, data = mydata)
> summary(bigmodel)

Call:
lm(formula = score ~ lean + gender + age, data = mydata)

Residuals:
    Min      1Q  Median      3Q     Max 
-25.891  -4.354   0.892   6.240  18.537 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 70.96455    3.85275  18.419   <2e-16 ***
lean         0.62463    0.05938  10.518   <2e-16 ***
genderM     -2.24025    1.40362  -1.596   0.1121    
age          0.10783    0.06052   1.782   0.0764 .  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 9 on 195 degrees of freedom
Multiple R-squared:  0.4188,    Adjusted R-squared:  0.4098 
F-statistic: 46.83 on 3 and 195 DF,  p-value: < 2.2e-16

> head(predict(bigmodel),20)
       1        2        3        4        5        6        7        8        9       10 
75.36711 74.43743 77.02533 78.76903 79.95515 79.09251 80.38647 81.65807 80.14846 78.96234 
      11       12       13       14       15       16       17       18       19       20 
82.39052 82.04468 81.05187 81.26753 84.50240 81.80667 80.92169 82.40895 81.76197 82.94809

But I can't wrap my head around the prediction after reading ?predict.lm. This output looks good to me for my original dataset -- but what if I want to run the prediction against a different dataset than the one I used to create bigmodel?

For example, if I import a .csv file into R called newmodel with 200 people complete with leans, gender, and age -- how can I use the regression formula from bigmodel to produce predictions for newmodel?

Thanks!

Was it helpful?

Solution

If you read the documentation for predict.lm, you will see the following. So, use the newdata argument to pass the newmodel data you imported to get predictions.

predict(object, newdata, se.fit = FALSE, scale = NULL, df = Inf,
        interval = c("none", "confidence", "prediction"),
        level = 0.95, type = c("response", "terms"),
        terms = NULL, na.action = na.pass,
        pred.var = res.var/weights, weights = 1, ...)
Arguments

object  
Object of class inheriting from "lm"

newdata 
An optional data frame in which to look for variables with which to predict. 
If omitted, the fitted values are used.

UPDATE. On the question of exporting data with predictions, here is how you can do it.

predictions = cbind(newmodel, pred = predict(bigmodel, newdata = newmodel))
write.csv(predictions, 'predictions.csv', row.names = F)

UPDATE 2. A full minimally reproducible solution

bigmodel <- lm(mpg ~ wt, data = mtcars)
newdata = data.frame(wt = runif(20, min = 1.5, max = 6))

cbind(
  newdata,
  mpg = predict(bigmodel, newdata = newdata)
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top