原始数据集中许多模型的增强预测

时间:2018-11-16 00:27:49

标签: r purrr broom

我想用x和ys的所有可能组合来运行许多模型。我创建了以下代码来做到这一点。

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)))

现在,我想从原始数据集中的所有模型(这里是mtcars)中获得所有预测。我怎样才能做到这一点?有没有办法使用扫帚的增强功能?

1 个答案:

答案 0 :(得分:1)

您可以使用mapaugment类似于将glm放入每一行的方式。

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))

预测位于.fitted列表中每个数据帧的Pred列中。

models2 <- models %>%
  select(Formula, Pred) %>%
  unnest() %>%
  select(`.rownames`, names(mtcars), Formula, `.fitted`) %>%
  spread(Formula, `.fitted`)