2

I want to run many models with all possible combinations of x and ys. I created the following code to do that.

library(tidyverse)

y <- names(mtcars)

xs <- map(y, ~setdiff(names(mtcars), .x)) %>% 
  map(~paste0(.x, collapse = "+")) %>%
  unlist()

ys <- names(mtcars)

models <- tibble(ys, xs) %>%
  mutate(Formula = paste0(ys, " ~ ", xs)) %>% 
  mutate(model = map(Formula, ~glm(as.formula(.x), data = mtcars)))

Now, I want to get all the predictions from all these models in the original dataset, here mtcars. How can I do that? Is there a way to use augment from broom?

Geet
  • 2,515
  • 2
  • 19
  • 42

1 Answers1

2

You can use map and augment similar to the way you fit glm to each row.

library(tidyverse)
library(broom)

y <- names(mtcars)

xs <- map(y, ~setdiff(names(mtcars), .x)) %>% 
  map(~paste0(.x, collapse = "+")) %>%
  unlist()

ys <- names(mtcars)

models <- tibble(ys, xs) %>%
  mutate(Formula = paste0(ys, " ~ ", xs)) %>% 
  mutate(model = map(Formula, ~glm(as.formula(.x), data = mtcars))) %>%
  mutate(Pred = map(model, augment))

The prediction is in the .fitted column in each dataframe from the Pred list.

models2 <- models %>%
  select(Formula, Pred) %>%
  unnest() %>%
  select(`.rownames`, names(mtcars), Formula, `.fitted`) %>%
  spread(Formula, `.fitted`)
www
  • 38,575
  • 12
  • 48
  • 84
  • Ok, Thanks! How can I pull those predictions from the Pred list then so that every prediction stays in the main mtcars dataset? – Geet Nov 16 '18 at 00:52
  • Almost! I actually want the predictions in columns eg. mpg_pred, cyl_pred, ...carb_pred with 32 rows. I guess, tidyr::spread could be leveraged to do that? – Geet Nov 16 '18 at 01:02
  • Wow...fantastic! To make the predicted variable names, I made one small change and used it: models2 <- models %>% select(ys, pred) %>% unnest() %>% select(ys, `.rownames`, names(mtcars), `.fitted`) %>% mutate(ys = paste0(ys, "_pred")) %>% spread(ys, `.fitted`) Can you put this in your answer for the benefit of other users? – Geet Nov 16 '18 at 01:28