【DM】在Tidyverse用estimatr
笔者邀请您,先思考:
1 如何使用estimatr包?
estimatetr以稳健的标准差进行快速的OLS和IV回归。 本文揭示estimatetr如何与RStudio的tidyverse软件包集成。
获得整洁
tidyverse的第一步是将模型输出转换为我们可以操作的数据。 tidy函数将lm_robust对象转换为data.frame。
1library(estimatr)
2fit <- lm_robust(Fertility ~ Agriculture + Catholic, data = swiss)
3tidy(fit)
4
数据处理与dplyr
一旦回归拟合为data.frame,您就可以使用dplyr的任何“动作”来进行数据操作,比如mutate、filter、select、summary、group_by和arrange(更多信息在这里)。
1library(tidyverse)
2
3# lm_robust and filter
4fit %>% tidy %>% filter(term == "Agriculture")
5
6# lm_robust and select
7fit %>% tidy %>% select(term, estimate, std.error)
8
9lm_robust and mutate
10fit %>% tidy %>% mutate(t_stat = estimate/std.error, significant = p.value <= 0.05)
ggplot2的数据可视化
ggplot2提供了许多与estimatr兼容的数据可视化工具
1 绘制系数图
1fit %>% tidy %>% filter(term != "(Intercept)") %>% ggplot(aes(y = term, x = estimate)) +
2 geom_vline(xintercept = 0, linetype = 2) + geom_point() + geom_errorbarh(aes(xmin = conf.low,
3 xmax = conf.high, height = 0.1)) + theme_bw()

使用geom_smooth函数和stat_smooth函数基于CIS健壮的方差估计(而不是“经典的”方差估计)。
1library(ggplot2)
2ggplot(swiss, aes(x = Agriculture, y = Fertility)) + geom_point() + geom_smooth(method = "lm_robust") +
3 theme_bw()

注意,函数形式可以包括多项式。例如,如果模型是

我们可以这样建模:
1library(ggplot2)
2ggplot(swiss, aes(x = Agriculture, y = Fertility)) + geom_point() + geom_smooth(method = "lm_robust",
3 formula = y ~ poly(x, 3, raw = TRUE)) + theme_bw()

Bootstrap 使用rsample
rsample pacakage提供了Bootstrap 工具:
1library(rsample)
2
3boot_out <- bootstraps(data = swiss, 500)$splits %>% map(~lm_robust(Fertility ~
4 Catholic + Agriculture, data = analysis(.))) %>% map(tidy) %>% bind_rows(.id = "bootstrap_replicate")
5kable(head(boot_out))
boot_out是一个data.frame,它包含来自每个boostrapped示例的估计。然后,我们可以使用dplyr函数来总结bootstrap,使用tidyr函数来重塑估计,使用GGally::ggpair来可视化它们。
1boot_out %>% group_by(term) %>% summarise(boot_se = sd(estimate))
1library(GGally)
2boot_out %>% select(bootstrap_replicate, term, estimate) %>% spread(key = term,
3 value = estimate) %>% select(-bootstrap_replicate) %>% ggpairs(lower = list(continuous = wrap("points",
4 alpha = 0.1))) + theme_bw()

多个模型使用purrr
purrr提供了对向量的每个元素执行相同操作的工具。例如,我们可能需要估计不同数据子集上的模型。我们可以使用map函数来做这件事。
1library(purrr)
2
3# Running the same model for highly educated and less educated cantons/districts
4
5two_subsets <-
6 swiss %>%
7 mutate(HighlyEducated = as.numeric(Education > 8)) %>%
8 split(.$HighlyEducated) %>%
9 map( ~ lm_robust(Fertility ~ Catholic, data = .)) %>%
10 map(tidy) %>%
11 bind_rows(.id = "HighlyEducated")
12
13kable(two_subsets, digits =2)
或者,我们可能想在同一个自变量上回归不同的因变量。map也可以与estimatr函数一起使用。
1three_outcomes <- c("Fertility", "Education", "Agriculture") %>% map(~formula(paste0(.,
2 " ~ Catholic"))) %>% map(~lm_robust(., data = swiss)) %>% map_df(tidy)
3
4kable(three_outcomes, digits = 2)
使用ggplot2,我们可以做一个系数图:
1three_outcomes %>% filter(term == "Catholic") %>% ggplot(aes(x = estimate, y = outcome)) +
2 geom_vline(xintercept = 0, linetype = 2) + geom_point() + geom_errorbarh(aes(xmin = conf.low,
3 xmax = conf.high, height = 0.1)) + ggtitle("Slopes with respect to `Catholic`") +
4 theme_bw()

最后的想法
一旦输出的模型变成数据框, 在tidyverse中使用estimatr函数很容易。我们用tidy的函数来完成这个任务。在那之后,许多总结和可视化的可能性出现了。整洁快乐!
原文链接:
https://declaredesign.org/r/estimatr/articles/estimatr-in-the-tidyverse.html
版权声明:作者保留权利,严禁修改,转载请注明原文链接。
数据人网是数据人学习、交流和分享的平台http://shujuren.org 。专注于从数据中学习到有用知识。
平台的理念:人人投稿,知识共享;人人分析,洞见驱动;智慧聚合,普惠人人。
您在数据人网平台,可以1)学习数据知识;2)创建数据博客;3)认识数据朋友;4)寻找数据工作;5)找到其它与数据相关的干货。
我们努力坚持做原创,聚合和分享优质的省时的数据知识!
我们都是数据人,数据是有价值的,坚定不移地实现从数据到商业价值的转换!
点击阅读原文,了解更多信息。
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!