'Efficiently extract fitted values from linear regression with many groups

How can I efficiently extract the fitted values from several linear regression models and append them to the original data used to build the models?

Example Data:

library(dplyr)

# Fit several (3 in this case) linear regression models 

fitted_models <- iris %>%
  group_by(Species) %>%
  do(model = lm(Petal.Length~Sepal.Length+Sepal.Width, data = .))

I can extract the fitted values for each group (see below) but this is cumbersome and would be inefficient if you have 10's or 100's of models. How can I more efficiently extract the fitted data from the models and append them back to the dataset used to build the models?

df2 <- iris[,c(5,3)]
df2$predicted <- NA
df2[1:50,3] <- fitted_models$model[[1]]$fitted.values
df2[51:100,3] <- fitted_models$model[[2]]$fitted.values 
df2[101:150,3] <- fitted_models$model[[3]]$fitted.values 
df2
r


Solution 1:[1]

With the model created, there is rowwise grouping, so we can directly extract in a list and unnest the list column

library(dplyr)
library(tidyr)
fitted_models %>%
    transmute(Species, fitted.values = list(model$fitted.values)) %>% 
    ungroup %>%
    unnest(fitted.values)

-output

# A tibble: 150 × 2
   Species fitted.values
   <fct>           <dbl>
 1 setosa           1.47
 2 setosa           1.46
 3 setosa           1.42
 4 setosa           1.41
 5 setosa           1.46
 6 setosa           1.51
 7 setosa           1.40
 8 setosa           1.46
 9 setosa           1.38
10 setosa           1.45
# … with 140 more rows

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akrun