-
Notifications
You must be signed in to change notification settings - Fork 17
/
simple_example.R
47 lines (35 loc) · 961 Bytes
/
simple_example.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
library(causalForest)
library(randomForestCI)
n = 500
ntree = 1000
sigma = 1
d = 10
n.test = 1000
#
# This example has no treatment effect, but has a treatment propensity
# that is correlated with outcomes
#
baseline = function(x) {
2 * (x[1] - 0.5)
}
propensity = function(x) {
0.25 + dbeta(x[1], 2, 4)/4
}
X = matrix(runif(n * d, 0, 1), n, d) # features
e = apply(X, 1, propensity)
W = rbinom(n, 1, e) #treatment condition
# no treatment effect
Y = apply(X, 1, baseline) + sigma * rnorm(n)
X.test = matrix(runif(n.test * d, 0, 1), n.test, d)
#
# random forest
#
forest = propensityForest(X, Y, W, num.trees = ntree, sample.size = n / 10, nodesize = 1)
predictions = predict(forest, X.test)
forest.ci = randomForestInfJack(forest, X.test, calibrate = TRUE)
se.hat = sqrt(forest.ci$var.hat)
up.lim = predictions + 1.96 * se.hat
down.lim = predictions - 1.96 * se.hat
# true tau is 0 here
coverage = mean(up.lim > 0 & down.lim < 0)
print(coverage)