0

I have a multivariate lm model in R for which I want to obtain predicated values/fitted values for each category of a factor variable, setting the control variables at their means.

So for example, using the mtcars dataset:

data(mtcars)
fit <- lm(mpg ~ gear + am + drat)

I would want to know what the predicted value of mpg is for every value that gear takes, while holding am and drat at their means.

I know I would use predict for this, but there does not seem to be an option in predict for holding variables at their means.

Ella Wind
  • 121
  • 12

1 Answers1

0
data(mtcars)
fit <- lm(mpg ~ gear + am + drat, data=mtcars)

We just create a new dataset with the desired attributes (I'm using dplyr functions here, but any solution will do as long as it produces a data.frame with correctly named variables of the correct type):

library(dplyr)
fixed_am_and_drat <- data.frame(mtcars) %>% 
  mutate(am = mean(am),
         drat = mean(drat)) %>% 
  select(gear, am, drat)

And pass this new dataset to predict using the newdata argument:

fixed_am_and_drat$predicted <- predict(fit, newdata = fixed_am_and_drat)

fixed_am_and_drat

Returns:

     gear      am     drat predicted
  1     4 0.40625 3.596563  19.59310
  2     4 0.40625 3.596563  19.59310
  3     4 0.40625 3.596563  19.59310
  4     3 0.40625 3.596563  21.18519
  5     3 0.40625 3.596563  21.18519
  6     3 0.40625 3.596563  21.18519
  7     3 0.40625 3.596563  21.18519
  8     4 0.40625 3.596563  19.59310
  9     4 0.40625 3.596563  19.59310
  10    4 0.40625 3.596563  19.59310
  11    4 0.40625 3.596563  19.59310
  12    3 0.40625 3.596563  21.18519
  13    3 0.40625 3.596563  21.18519
  14    3 0.40625 3.596563  21.18519
  15    3 0.40625 3.596563  21.18519
  16    3 0.40625 3.596563  21.18519
  17    3 0.40625 3.596563  21.18519
  18    4 0.40625 3.596563  19.59310
  19    4 0.40625 3.596563  19.59310
  20    4 0.40625 3.596563  19.59310
  21    3 0.40625 3.596563  21.18519
  22    3 0.40625 3.596563  21.18519
  23    3 0.40625 3.596563  21.18519
  24    3 0.40625 3.596563  21.18519
  25    3 0.40625 3.596563  21.18519
  26    4 0.40625 3.596563  19.59310
  27    5 0.40625 3.596563  18.00101
  28    5 0.40625 3.596563  18.00101
  29    5 0.40625 3.596563  18.00101
  30    5 0.40625 3.596563  18.00101
  31    5 0.40625 3.596563  18.00101
  32    4 0.40625 3.596563  19.59310
dario
  • 6,415
  • 2
  • 12
  • 26