我想用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)中获得所有预测。我怎样才能做到这一点?有没有办法使用扫帚的增强功能?
答案 0 :(得分:1)
您可以使用map
和augment
类似于将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`)