Use augment
in mutate
and use map
to pass one model at a time in augment
.
library(broom)
library(dplyr)
library(purrr)
mtcars %>%
group_by(cyl) %>%
do(fit = lm(wt ~ mpg, .),
data = (.)) %>%
ungroup() %>%
mutate(col = map(fit, augment, newdata = data1))
Also since do
has been superseded you can fit the model in summarise
.
mtcars %>%
group_by(cyl) %>%
summarise(fit = list(lm(wt ~ mpg)),
data = list(cur_data())) %>%
mutate(col = map(fit, augment, newdata = data1))
# cyl fit data col
# <dbl> <list> <list> <list>
#1 4 <lm> <tibble [11 × 11]> <tibble [20 × 14]>
#2 6 <lm> <tibble [7 × 11]> <tibble [20 × 14]>
#3 8 <lm> <tibble [14 × 11]> <tibble [20 × 14]>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…