• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    同等学历教学

    同等学历教学

    免费
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      同等学历教学

      同等学历教学

      免费
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      R语言

      • 首页
      • 博客
      • R语言
      • 【DM】在Tidyverse用estimatr

      【DM】在Tidyverse用estimatr

      • 发布者 weinfoadmin
      • 分类 R语言
      • 日期 2018年10月28日
      测试开头

      【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()
      【DM】在Tidyverse用estimatr

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

      1library(ggplot2)
      2ggplot(swiss, aes(x = Agriculture, y = Fertility)) + geom_point() + geom_smooth(method = "lm_robust") + 
      3    theme_bw()
      【DM】在Tidyverse用estimatr

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

      【DM】在Tidyverse用estimatr


      我们可以这样建模:


      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()
      【DM】在Tidyverse用estimatr

      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()
      【DM】在Tidyverse用estimatr

      多个模型使用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()
      【DM】在Tidyverse用estimatr

      最后的想法

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

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

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

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

      点击阅读原文,了解更多信息。

      测试结尾

      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      一套limma、edgeR的实战【上】
      2018年10月28日

      下一篇文章

      R数据科学--第十章 使用stringr处理字符串(下)
      2018年10月28日

      你可能也喜欢

      3-1665801675
      R语言学习:重读《R数据科学(中文版)》书籍
      28 9月, 2022
      6-1652833487
      经典铁死亡,再出新思路
      16 5月, 2022
      1-1651501980
      R语言学习:阅读《R For Everyone 》(第二版)
      1 5月, 2022

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?