倾向得分加权匹配分析方法的R实现

2017 年 11 月 16 日 数萃大数据 赵毅

声明:本文最早发表于公众号:每天进步一点点2015,已征得作者同意

1.1 PSW Package 简介

  • PSW : Propensity Score Weighting Methods for Dichotomous Treatments.

      该包由Huzhang Mao 和LiangLi两位作者贡献,首次发布于2017年10月;该包主要运用倾向得分加权分析方法实现因果效应的推断;主要由5个函数模块构成:

(1)倾向得分共同取值范围以及各匹配变量的标准化偏差的可视化图示

(2)检验匹配后数据是否平衡(平衡性假设检验);

(3)倾向值模型设定检验

(4)处理效应的加权估计

(5)处理效应的双重稳健性估计

      由于篇幅所限,本文仅关注对average treatment effect of the treated (ATT)的估计。

1.1.1 载入需要的程辑包

  
    
    
    
  1. # 加载第三方包

  2. # 为读入本文所需要用到的Stata数据集

  3. library(haven)

  4. library(RStata)

  5. library(readstata13)

  6. library(foreign)

  7. #同时载入需要的程辑包

  8. library(stats)  

  9. library(Hmisc)

  10. library(survival)

  11. library(Formula)

  12. library(ggplot2)

  13. library(gtools)

  14. library(graphics)

  15. library(lattice)

  16. library(PSW)

  17. # 读取ldw_exper.dta数据集ldw_exper <- read_dta(file = file.choose())

  18. # 强制转换为数据框结构

  19. ldwdf <- as.data.frame(ldw_exper) #转换为数据框格式,便于计算

  20. # 变量激活,便于直接使用变量名

  21. attach(ldwdf)

1.1.2 本文使用的数据集(ldw_exper.dta)简介

      该数据集介绍可参见《高级计量经济学与Stata应用第二版(陈强)》
re78为1978年实际收入,t是否参加就业培训,age年龄,educ教育年限,black是否为黑人,hisp是否为拉丁族,married是否结婚,re74、re75为1974和75年的实际收入,u74,u75为74和75年是否为失业状态;笔者选择该数据集,核心目的对照书中传统的倾向得分匹配分析方法与本文所介绍的用倾向得分加权匹配分析方法的异同。

1.2 Propensity score weighting in R

本节主要介绍倾向得分加权匹配分析的实现过程.

1.2.1 构建 Propensity score model

  
    
    
    
  1. # generate Propensity score model

  2. form.ps <- "t ~ age + educ + black + hisp+married+re74+re75+u74+u75"

  3. # calculate Standardized differnce with "ATT"

  4. tmp1 <- psw( data = ldwdf, form.ps = form.ps, weight = "ATT" )

  5. # display ps.model  

  6. M1<-tmp1$ps.model

  7. summary.glm(M1)

  8. #由于glm函数未给出logit模型的整体性是否显著P值,故需通过手动编制计算

  9. with(M1, pchisq(null.deviance - deviance, df.null - df.residual, lower.tail = FALSE))

结果输出:

1.2.2 平衡性检验 Balance checking using standardized mean difference

  
    
    
    
  1. # generate A vector of covariates

  2. V.name <- c("age","educ","black", "hisp","married","re74","re75","u74","u75")

  3. #  compute the standardized mean difference for balance diagnosis.

  4. tmp2 <- psw.balance( data = ldwdf, weight = "ATT", form.ps = form.ps,

  5. V.name = V.name )

  6. # 为方便演示,此处仅提取匹配前、后的标准化均值差异

  7. tmp2$std.diff.before;

  8. tmp2$std.diff.after;

输出结果:

我们可以进一步提取以下非常有用的信息,具体包括以下内容(fitted propensity score model, estimated propenstity scores, estimated propensity score weights, standardized mean difference before and after weighting adjustment).从tmp2列示的结果来看,我们发现,通过倾向得分加权法,匹配后偏差大幅度下降,通过了平衡性检验。

1.2.3 模型设定检验 Propensity score model specification test

      This test is a goodness-of-fit test of propensity score model. 备注:该检验原假设为倾向性得分模型被正确设定

  
    
    
    
  1. # generate A vector of transformation types for covariates in V.name.

  2. trans.type <- c( "identity","identity","identity","identity","identity","identity","identity","identity","identity")

  3. #  compute Propensity score model specification test.

  4. tmp3<-psw.spec.test( data = ldwdf, weight = "ATT", form.ps = form.ps,

  5. V.name = V.name, trans.type = trans.type )

  6. #  为方便演示,这里仅提取 Propensity score model specification test的P值信息

  7. tmp3$pvalue

      由此,我们可以得出模型设定不拒绝原假设,可以接受被正确设定的结论。

1.2.4 Propensity score weighting estimator

      This is used to estimate the weighted estimator, and make inference using the sandwich variance estimator that takes into account the sampling variability in the estimated propensity score.

备注:仍以估计ATT的值为例;需注意一下gaussian和binomial的用法

  
    
    
    
  1. # estimate the weighted treatment effect estimator.

  2. tmp4 <- psw.wt( data = ldwdf, weight = "ATT", form.ps = form.ps,

  3. out.var = "re78", family = "binomial" )

  4. # display tmp4

  5. head(tmp4)

结果输出:

可以根据自己需求提取相应信息,包括本部分所关心的weighted estimator for risk difference、standard error for est.risk.wt、weighted estimator for relative risk以及standard error  for est.rr.wt等内容.

小结:

用倾向得分加权分析方法得到的ATT值为1.7381,标准误为0.6887,与传统的PSM分析所用的匹配方法得出的结论基本一致。

感谢:

非常感谢各位网友向“每天进步一点点2015”公众号投递原创性文章,同时也欢迎各位网友一起参与知识分享和交流。

欢迎大家关注微信公众号:数萃大数据

课程公告

深度学习培训班【上海站】

时间:2017年12月23-24日

地点:上海创梦云实训创新中心

更多详情,请扫描下面二维码


登录查看更多
5

相关内容

【干货书】R语言书: 编程和统计的第一课程,
专知会员服务
107+阅读 · 2020年5月9日
【Amazon】使用预先训练的Transformer模型进行数据增强
专知会员服务
56+阅读 · 2020年3月6日
缺失数据统计分析,第三版,462页pdf
专知会员服务
103+阅读 · 2020年2月28日
【论文推荐】文本分析应用的NLP特征推荐
专知会员服务
33+阅读 · 2019年12月8日
ExBert — 可视化分析Transformer学到的表示
专知会员服务
30+阅读 · 2019年10月16日
R语言机器学习:xgboost的使用及其模型解释
R语言中文社区
10+阅读 · 2019年5月6日
基于R语言进行Box-Cox变换
R语言中文社区
45+阅读 · 2018年11月19日
时间序列深度学习:状态 LSTM 模型预测太阳黑子(上)
R语言中文社区
19+阅读 · 2018年6月15日
一文读懂因果推测、倾向模型(结合实例)
数据派THU
3+阅读 · 2018年3月26日
基于深度学习的文本分类?
数萃大数据
9+阅读 · 2018年3月4日
【干货】--基于Python的文本情感分类
R语言中文社区
5+阅读 · 2018年1月5日
教你用Python爬虫股票评论,简单分析股民用户情绪
数据派THU
10+阅读 · 2017年12月12日
用 Python 进行贝叶斯模型建模(1)
Python开发者
3+阅读 · 2017年7月11日
Arxiv
8+阅读 · 2019年5月20日
Universal Transformers
Arxiv
5+阅读 · 2019年3月5日
Arxiv
7+阅读 · 2018年3月21日
Arxiv
9+阅读 · 2018年1月30日
Arxiv
8+阅读 · 2018年1月25日
VIP会员
相关VIP内容
相关资讯
R语言机器学习:xgboost的使用及其模型解释
R语言中文社区
10+阅读 · 2019年5月6日
基于R语言进行Box-Cox变换
R语言中文社区
45+阅读 · 2018年11月19日
时间序列深度学习:状态 LSTM 模型预测太阳黑子(上)
R语言中文社区
19+阅读 · 2018年6月15日
一文读懂因果推测、倾向模型(结合实例)
数据派THU
3+阅读 · 2018年3月26日
基于深度学习的文本分类?
数萃大数据
9+阅读 · 2018年3月4日
【干货】--基于Python的文本情感分类
R语言中文社区
5+阅读 · 2018年1月5日
教你用Python爬虫股票评论,简单分析股民用户情绪
数据派THU
10+阅读 · 2017年12月12日
用 Python 进行贝叶斯模型建模(1)
Python开发者
3+阅读 · 2017年7月11日
Top
微信扫码咨询专知VIP会员