我经常想要拆分-应用-组合回归模型。我找到了两种方法,“
purrr
”方法和“dplyr::do()
”方法。
purrr
方法的问题:我希望结果 data.frame 中的列指示完成分割的变量级别,就像正常的 group_by %>%
汇总操作一样。
dplyr::do()
方法的问题:do(tidy(lm_robust))
存在令人讨厌的混乱,这显然是不优雅的。但我把专栏拿回来了。
主要问题:有没有一种方法可以在
purrr
中进行拆分-应用-组合,从而很好地返回变量拆分?
下面的最小工作示例显示该问题与您分割的变量数量相互作用。
library(tidyverse)
library(estimatr) # for lm_robust
# spliting by one variable
# the purrr approach
mtcars %>%
split(.$am) %>%
map(~lm_robust(mpg ~ hp, data = .)) %>%
map_df(tidy, .id = "am") # annoying to have to type "am" again!
# the dplyr do() approach
mtcars %>%
group_by(am) %>%
do(tidy(lm_robust(mpg ~ hp, data = .))) # gross nesting
# Splitting by two variables
# the purr approach??
mtcars %>%
split(list(.$am, .$vs)) %>%
map(~lm_robust(mpg ~ hp, data = .)) %>%
map_df(tidy, .id = "OH NO") # the column encodes both am and vs info
# the dplyr do() approach works great
mtcars %>%
group_by(am, vs) %>%
do(tidy(lm_robust(mpg ~ hp, data = .))) # still nested up.
编辑
这里有一种使用nest()和unnest()的方法。笨重,但也许是最好的 purrr 方法?灵感来自 http://stat545.com/block024_group-nest-split-map.html
mtcars %>%
group_by(am, vs) %>%
nest() %>%
mutate(fit = map(data, ~lm_robust(mpg ~ hp, data = .)),
tidy = map(fit, tidy)) %>%
select(am, vs, tidy) %>%
unnest(tidy)
编辑2
这是一种使用
group_map
的方式,它和 do
一样丑陋,但也许这就是它的方式。
mtcars %>%
group_by(am, vs) %>%
group_map(~tidy(lm_robust(mpg ~ hp, data = .x)))
编辑3:
我想对我来说看起来很漂亮的就是每行做一件事,像这样,但我尊重下面的评论,说,天哪,组图非常接近。
# does not work
mtcars %>%
group_by(am, vs) %>%
map(~lm_robust(mpg ~ hp, data = .)) %>%
map_df(tidy)
一行:
mtcars %>% reframe(tidy(lm_robust(mpg ~ hp, .)), .by = c(am, vs))