添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
【机器学习-因果推断】因果树 causalTree 估计 HTE R语言快速上手小案例

【机器学习-因果推断】因果树 causalTree 估计 HTE R语言快速上手小案例

1. 因果树的目标

根据Athey和Imbens (2016) 的论文 Recursive partitioning for heterogeneous causal effects 中描述,因果树的作用是 “estimating heterogeneity in causal effects in experimental and observational studies and for conducting hypothesis tests about the magnitude of differences in treatment effects across subsets of the population.” -- 基于实验或者观测研究,检验不同子组样本之间的干预差异,也就是所谓的 HTE heterogeneous treatment effect。

causaltree 基于回归树,其中作者创新式的提出了“诚实树”,并用来划分样本。一棵诚实树用来划分样本,另外一棵则用来进行组内 ATE 的估计。

在因果树的基础上,结合随机森林的思想,该团队还开发出了“因果森林 Causal Forest”。

2. 案例演示准备

这个官方自带的一个案例,简洁明了。

先安装相关的包。

## 官方的安装方法
install.packages("devtools")
library(devtools) 
install_github("susanathey/causalTree")
## 直接安装
install.packages("causalTree")

导入 causalTree 包:

library(causalTree)
## 查看该包自带的模拟数据集
> dim(simulation.1)
[1] 500  12 ## 500行12列,其中最后一列是干预变量,全部样本一半是干预组,一半对照组
simulation.1 
## btw,该包还带有另外一个模拟数据集:simulation.2,结构类似于 simulation.1
> summary(simulation.1)
     

顺便看下各个变量的标准差:

> sapply(simulation.1, sd)
  x1        x2        x3        x4        x5        x6        x7        x8        x9       x10         y treatment
1.0608268 1.0266664 0.9744904 0.9869265 0.9969123 1.0365745 1.0314448 0.9661240 0.9743713 0.9983310 1.8023988 0.5005008 

可见,各个自变量几乎服从标准正态分布,均值为0,sd为1。

3 建立因果树

先上模型参数解读:

  • 只用了4个自变量;
  • 样本切割的方式是 CT - 诚实树;
> tree <- causalTree(y~ x1 + x2 + x3 + x4, data = simulation.1, treatment = simulation.1$treatment,
                   split.Rule = "CT", cv.option = "CT", split.Honest = T, cv.Honest = T, split.Bucket = F, 
                   xval = 5, cp = 0, minsize = 20, propensity = 0.5)
[1] 2
[1] "CT"
opcp <- tree$cptable[, 1][which.min(tree$cptable[, 4])]