在Tidyverse用estimatr

栏目: CSS · 发布时间: 7年前

内容简介:estimatetr以稳健的标准差进行快速的OLS和IV回归。 本文揭示estimatetr如何与RStudio的tidyverse软件包集成。tidyverse的第一步是将模型输出转换为我们可以操作的数据。 tidy函数将lm_robust对象转换为data.frame。一旦回归拟合为data.frame,您就可以使用dplyr的任何“动作”来进行数据操作,比如mutate、filter、select、summary、group_by和arrange(更多信息在

estimatetr以稳健的标准差进行快速的OLS和IV回归。 本文揭示estimatetr如何与RStudio的tidyverse软件包集成。

获得整洁

tidyverse的第一步是将模型输出转换为我们可以操作的数据。 tidy函数将lm_robust对象转换为data.frame。

library(estimatr)
fit <- lm_robust(Fertility ~ Agriculture + Catholic, data = swiss)
tidy(fit)

数据处理与dplyr

一旦回归拟合为data.frame,您就可以使用dplyr的任何“动作”来进行数据操作,比如mutate、filter、select、summary、group_by和arrange(更多信息在 这里 )。

library(tidyverse)

# lm_robust and filter
fit %>% tidy %>% filter(term == "Agriculture")

# lm_robust and select
fit %>% tidy %>% select(term, estimate, std.error)

lm_robust and mutate
fit %>% tidy %>% mutate(t_stat = estimate/std.error, significant = p.value <= 0.05)

ggplot2的数据可视化

ggplot2提供了许多与estimatr兼容的数据可视化工具

1 绘制系数图

fit %>% tidy %>% filter(term != "(Intercept)") %>% ggplot(aes(y = term, x = estimate)) + 
    geom_vline(xintercept = 0, linetype = 2) + geom_point() + geom_errorbarh(aes(xmin = conf.low, 
    xmax = conf.high, height = 0.1)) + theme_bw()

在Tidyverse用estimatr

使用geom_smooth函数和stat_smooth函数基于CIS健壮的方差估计(而不是“经典的”方差估计)。

library(ggplot2)
ggplot(swiss, aes(x = Agriculture, y = Fertility)) + geom_point() + geom_smooth(method = "lm_robust") + 
    theme_bw()

在Tidyverse用estimatr

注意,函数形式可以包括多项式。例如,如果模型是 在Tidyverse用estimatr 我们可以这样建模:

library(ggplot2)
ggplot(swiss, aes(x = Agriculture, y = Fertility)) + geom_point() + geom_smooth(method = "lm_robust", 
    formula = y ~ poly(x, 3, raw = TRUE)) + theme_bw()

在Tidyverse用estimatr

Bootstrap 使用rsample

rsample pacakage提供了Bootstrap 工具:

library(rsample)

boot_out <- bootstraps(data = swiss, 500)$splits %>% map(~lm_robust(Fertility ~ 
    Catholic + Agriculture, data = analysis(.))) %>% map(tidy) %>% bind_rows(.id = "bootstrap_replicate")
kable(head(boot_out))

boot_out是一个data.frame,它包含来自每个boostrapped示例的估计。然后,我们可以使用dplyr函数来总结bootstrap,使用tidyr函数来重塑估计,使用GGally::ggpair来可视化它们。

boot_out %>% group_by(term) %>% summarise(boot_se = sd(estimate))
library(GGally)
boot_out %>% select(bootstrap_replicate, term, estimate) %>% spread(key = term, 
    value = estimate) %>% select(-bootstrap_replicate) %>% ggpairs(lower = list(continuous = wrap("points", 
    alpha = 0.1))) + theme_bw()

在Tidyverse用estimatr

多个模型使用purrr

purrr提供了对向量的每个元素执行相同操作的工具。例如,我们可能需要估计不同数据子集上的模型。我们可以使用map函数来做这件事。

library(purrr)

# Running the same model for highly educated and less educated cantons/districts

two_subsets <- 
  swiss %>%
  mutate(HighlyEducated = as.numeric(Education > 8)) %>%
  split(.$HighlyEducated) %>%
  map( ~ lm_robust(Fertility ~ Catholic, data = .)) %>%
  map(tidy) %>%
  bind_rows(.id = "HighlyEducated")

kable(two_subsets, digits =2)

或者,我们可能想在同一个自变量上回归不同的因变量。map也可以与estimatr函数一起使用。

three_outcomes <- c("Fertility", "Education", "Agriculture") %>% map(~formula(paste0(., 
    " ~ Catholic"))) %>% map(~lm_robust(., data = swiss)) %>% map_df(tidy)

kable(three_outcomes, digits = 2)

使用ggplot2,我们可以做一个系数图:

three_outcomes %>% filter(term == "Catholic") %>% ggplot(aes(x = estimate, y = outcome)) + 
    geom_vline(xintercept = 0, linetype = 2) + geom_point() + geom_errorbarh(aes(xmin = conf.low, 
    xmax = conf.high, height = 0.1)) + ggtitle("Slopes with respect to `Catholic`") + 
    theme_bw()

在Tidyverse用estimatr

最后的想法

一旦输出的模型变成数据框, 在tidyverse中使用estimatr函数很容易。我们用tidy的函数来完成这个任务。在那之后,许多总结和可视化的可能性出现了。整洁快乐!

原文链接: https://declaredesign.org/r/estimatr/articles/estimatr-in-the-tidyverse.html

版权声明:作者保留权利,严禁修改,转载请注明原文链接。

数据人网是数据人学习、交流和分享的平台http://shujuren.org 。专注于从数据中学习到有用知识。 平台的理念:人人投稿,知识共享;人人分析,洞见驱动;智慧聚合,普惠人人。 您在数据人网平台,可以1)学习数据知识;2)创建数据博客;3)认识数据朋友;4)寻找数据工作;5)找到其它与数据相关的干货。 我们努力坚持做原创,聚合和分享优质的省时的数据知识! 我们都是数据人,数据是有价值的,坚定不移地实现从数据到商业价值的转换!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Nature of Code

The Nature of Code

Daniel Shiffman / The Nature of Code / 2012-12-13 / GBP 19.95

How can we capture the unpredictable evolutionary and emergent properties of nature in software? How can understanding the mathematical principles behind our physical world help us to create digital w......一起来看看 《The Nature of Code》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具