I am running regression on data by group/levels. so a reg for each level, i.e, my code looks very similar to this:
library('dplyr')
regressions <- mtcars %>%
group_by(cyl) %>%
do(mod = lm(mpg ~ wt, .))
How do I extract predictions on a smaller newdata set that has same group? i.e the levels are the same but with a smaller sample. I want to do the predictions all at once on a new data sets for all the levels. I know "augment" in broom gives you predictions. but I don't know how to do it for all levels all at once.
The code I used looks like this.
library('broom')
aa <- as.data.frame(augment(regressions, newdata=test))
I also tried
aa <- as.data.frame(augment(regressions, mod, newdata=test))
which gave fitted values but was duplicating my newdata to match the original one but the predictions for each observation were different. My data has 44 levels and I have to do more. Thanks a bunch for the help.