forked from swager/causalForest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_test.R
72 lines (54 loc) · 1.6 KB
/
simple_test.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
library(causalForest)
n = 500
p = 10
X = matrix(rnorm(n * p), n, p)
colnames(X) = sample.int(p)
Y = rnorm(n)
DF = data.frame(X=X, Y=Y)
W = rbinom(n, 1, 0.5)
#
# Test basic tree functionality
#
ct = causalTree(Y ~ ., data = DF, treatment=W, split.option="TOT", cv.option="TOT")
#summary(ct)
min(predict(ct))
max(predict(ct))
class(ct) == "causalTree"
rct = refit.causalTree(ct, newx=DF, newy=Y, treatment=W, propensity=rep(0.5, n))
min(predict(rct))
max(predict(rct))
class(rct) == "causalTree"
ct = causalTree(Y ~ ., data = DF, treatment=W, split.option="CT", cv.option="TOT")
#summary(ct)
min(predict(ct))
max(predict(ct))
class(ct) == "causalTree"
rct = refit.causalTree(ct, newx=DF, newy=Y, treatment=W)
min(predict(rct))
max(predict(rct))
class(rct) == "causalTree"
#
# Test refitting with small, potentially empty, leaves
#
n2 = 25
X2 = matrix(rnorm(n2 * p), n2, p)
colnames(X) = sample.int(p)
Y2 = rnorm(n2)
DF2 = data.frame(X=X2, Y=Y2)
W2 = rbinom(n2, 1, 0.5)
ct <- causalTree(Y ~ ., data = DF, treatment = W, method = "anova", cp = 0, minbucket = 1, cv.option = "matching", split.option = "CT", xval = 0)
rct = refit.causalTree(ct, newx=DF2, newy=Y2, treatment=W2)
sum(is.na(predict(rct))) == 0
rct.tot = refit.causalTree(ct, newx=DF2, newy=Y2, treatment=W2)
sum(is.na(predict(rct.tot))) == 0
#
# Test forests
#
cf = causalForest(DF[,1:p], DF$Y, W, num.trees = 10)
min(predict(cf, data.frame(X=X)))
max(predict(cf, data.frame(X=X)))
class(cf) == "causalForest"
pf = propensityForest(DF[,1:p], DF$Y, W, num.trees = 10)
min(predict(pf, data.frame(X=X)))
max(predict(pf, data.frame(X=X)))
class(pf) == "causalForest"