-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathXRF.R
447 lines (409 loc) · 16.5 KB
/
XRF.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# This file implements the X-Learner (https://arxiv.org/pdf/1706.03461.pdf)
# with the forestry implementation (https://github.com/soerenkuenzel/forestry)
# as base learner.
#' @include CATE_estimators.R
#' @include helper_functions.R
NULL
# X-RF class -------------------------------------------------------------------
setClass(
"X_RF",
contains = "MetaLearner",
slots = list(
m_0 = "forestry",
m_1 = "forestry",
m_tau_0 = "forestry",
m_tau_1 = "forestry",
m_prop = "forestry",
hyperparameter_list = "list"
)
)
# X_RF generator ---------------------------------------------------------------
#' @title X-Learners
#' @rdname Xleaners
#' @name X-Learner
#' @description X_RF is an implementation of the X-learner with Random Forests
#' (Breiman 2001) in the first and second stage.
#' @details
#' The X-Learner estimates the CATE in three steps:
#' \enumerate{
#' \item
#' Estimate the response functions
#' \deqn{\mu_0(x) = E[Y(0) | X = x]}
#' \deqn{\mu_1(x) = E[Y(1) | X = x]}
#' using the base learner and denote the estimates as \eqn{\hat \mu_0} and
#' \eqn{\hat \mu_1}.
#' \item
#' Impute the treatment effects for the individuals in the treated group,
#' based on the control outcome estimator, and the treatment effects for the
#' individuals in the control group, based on the treatment outcome
#' estimator, that is,
#' \deqn{D^1_i = Y_i(1) - \hat \mu_0(X_i)}
#' \deqn{D^0_i = \hat \mu_1(X_i) - Y_i(0).}
#' Now employ the base learner in two ways: using \eqn{D^1_i} as the
#' dependent variable to obtain \eqn{\hat \tau_1(x)}, and using \eqn{D^0_i}
#' as the dependent variable to obtain \eqn{\hat \tau_0(x)}.
#' \item
#' Define the CATE estimate by a weighted average of the two estimates at
#' Stage 2:
#' \deqn{\tau(x) = g(x) \hat \tau_0(x) + (1 - g(x)) \hat \tau_1(x).}
#' If \code{predmode = "propmean"}, then \eqn{g(x) = e(x)}, where
#' \eqn{e(x)} is an estimate of the propensity score using the
#' \href{https://github.com/soerenkuenzel/forestry}{\code{forestry}} Random Forests
#' version with the hyperparameters specified in \code{e.forestry}.
#' If \code{predmode = "control"}, then \eqn{g(x) = 1}, and if
#' \code{predmode = "treated"}, then \eqn{g(x) = 0}.
#' }
#' @param feat A data frame containing the features.
#' @param tr A numeric vector with 0 for control and 1 for treated variables.
#' @param yobs A numeric vector containing the observed outcomes.
#' @param predmode Specifies how the two estimators of the second stage should
#' be aggregated. Possible types are "propmean," "control," and "treated." The
#' default is "propmean," which refers to propensity score weighting.
#' @param nthread Number of threads which should be used to work in parallel.
#' @param verbose TRUE for detailed output, FALSE for no output.
#' @param mu.forestry,tau.forestry,e.forestry A list containing the
#' hyperparameters for the \code{forestry} package that are used for
#' estimating the response functions, the CATE, and the propensity score.
#' These hyperparameters are passed to the \code{forestry} package. (Please
#' refer to the \href{https://github.com/soerenkuenzel/forestry}{forestry}
#' package for a more detailed documentation of the hyperparamters.)
#' \itemize{
#' \item \code{relevant.Variable} Variables that are only used in the first
#' stage.
#' \item \code{ntree} Numbers of trees used in the first stage.
#' \item \code{replace} Sample with or without replacement in the first
#' stage.
#' \item \code{sample.fraction} The size of total samples to draw for the
#' training data in the first stage.
#' \item \code{mtry} The number of variables randomly selected in each
#' splitting point.
#' \item \code{nodesizeSpl} Minimum nodesize in the first stage for
#' the observations in the splitting set. (See the details of the
#' \code{forestry} package)
#' \item \code{nodesizeAvg} Minimum nodesize in the first stage for
#' the observations in the averaging set.
#' \item \code{splitratio} Proportion of the training data used as the
#' splitting dataset in the first stage.
#' \item \code{middleSplit} If true, the split value will be exactly in the
#' middle of two observations. Otherwise, it will take a point
#' based on a uniform distribution between the two observations.
#' }
#' @return An object from a class that contains the \code{CATEestimator}
#' class. It should be used with one of the following functions:
#' \code{EstimateCATE}, \code{CateCI}, and \code{CateBIAS}. The object has at least the
#' following slots:
#' \item{\code{feature_train}}{A copy of feat.}
#' \item{\code{tr_train}}{A copy of tr.}
#' \item{\code{yobs_train}}{A copy of yobs.}
#' \item{\code{creator}}{Function call that creates the CATE estimator. This
#' is used for different bootstrap procedures.}
#' @author Soeren R. Kuenzel
#' @references
#' \itemize{
#' \item Sören Künzel, Jasjeet Sekhon, Peter Bickel, and Bin Yu (2017).
#' MetaLearners for Estimating Heterogeneous Treatment Effects using
#' Machine Learning.
#' \url{https://www.pnas.org/content/116/10/4156}
#' \item
#' Sören Künzel, Simon Walter, and Jasjeet Sekhon (2018).
#' Causaltoolbox---Estimator Stability for Heterogeneous Treatment Effects.
#' \url{https://arxiv.org/pdf/1811.02833.pdf}
#' \item Sören Künzel, Bradly Stadie, Nikita Vemuri, Varsha Ramakrishnan,
#' Jasjeet Sekhon, and Pieter Abbeel (2018).
#' Transfer Learning for Estimating Causal Effects using Neural Networks.
#' \url{https://arxiv.org/pdf/1808.07804.pdf}
#' }
#' @family metalearners
#' @examples
#' require(causalToolbox)
#'
#' # create example data set
#' simulated_experiment <- simulate_causal_experiment(
#' ntrain = 1000,
#' ntest = 1000,
#' dim = 10
#' )
#' feat <- simulated_experiment$feat_tr
#' tr <- simulated_experiment$W_tr
#' yobs <- simulated_experiment$Yobs_tr
#' feature_test <- simulated_experiment$feat_te
#'
#' # create the CATE estimator using Random Forests (RF)
#' xl_rf <- X_RF(feat = feat, tr = tr, yobs = yobs)
#' tl_rf <- T_RF(feat = feat, tr = tr, yobs = yobs)
#' sl_rf <- S_RF(feat = feat, tr = tr, yobs = yobs)
#' ml_rf <- M_RF(feat = feat, tr = tr, yobs = yobs)
#' xl_bt <- X_BART(feat = feat, tr = tr, yobs = yobs)
#' tl_bt <- T_BART(feat = feat, tr = tr, yobs = yobs)
#' sl_bt <- S_BART(feat = feat, tr = tr, yobs = yobs)
#' ml_bt <- M_BART(feat = feat, tr = tr, yobs = yobs)
#'
#' cate_esti_xrf <- EstimateCate(xl_rf, feature_test)
#'
#' # evaluate the performance.
#' cate_true <- simulated_experiment$tau_te
#' mean((cate_esti_xrf - cate_true) ^ 2)
#' \dontrun{
#' # create confidence intervals via bootstrapping.
#' xl_ci_rf <- CateCI(xl_rf, feature_test, B = 500)
#' }
#' @export
X_RF <-
function(feat,
tr,
yobs,
predmode = "propmean",
nthread = 0,
verbose = FALSE,
mu.forestry =
list(
relevant.Variable = 1:ncol(feat),
ntree = 1000,
replace = TRUE,
sample.fraction = 0.8,
mtry = round(ncol(feat) * 13 / 20),
nodesizeSpl = 2,
nodesizeAvg = 1,
splitratio = 1,
middleSplit = TRUE
),
tau.forestry =
list(
relevant.Variable = 1:ncol(feat),
ntree = 1000,
replace = TRUE,
sample.fraction = 0.7,
mtry = round(ncol(feat) * 17 / 20),
nodesizeSpl = 5,
nodesizeAvg = 6,
splitratio = 0.8,
middleSplit = TRUE
),
e.forestry =
list(
relevant.Variable = 1:ncol(feat),
ntree = 500,
replace = TRUE,
sample.fraction = 0.5,
mtry = ncol(feat),
nodesizeSpl = 11,
nodesizeAvg = 33,
splitratio = .5,
middleSplit = FALSE
)) {
# Cast input data to a standard format -------------------------------------
feat <- as.data.frame(feat)
# Catch misspecification erros ---------------------------------------------
if (!(nthread - round(nthread) == 0) | nthread < 0) {
stop("nthread must be a positive integer!")
}
if (!is.logical(verbose)) {
stop("verbose must be either TRUE or FALSE.")
}
if (!predmode %in% c("propmean", "extreme", "control", "treated")) {
stop("predmode should be one of propmean, extreme, control, or treated.")
}
catch_input_errors(feat, yobs, tr)
# Set relevant relevant.Variable -------------------------------------------
# User often sets the relevant variables by column names and not numerical
# values. We translate it here to the index of the columns.
if (is.null(mu.forestry$relevant.Variable)) {
mu.forestry$relevant.Variable <- 1:ncol(feat)
} else{
if (is.character(mu.forestry$relevant.Variable))
mu.forestry$relevant.Variable <-
which(colnames(feat) %in% mu.forestry$relevant.Variable)
}
if (is.null(tau.forestry$relevant.Variable)) {
tau.forestry$relevant.Variable <- 1:ncol(feat)
} else{
if (is.character(tau.forestry$relevant.Variable))
tau.forestry$relevant.Variable <-
which(colnames(feat) %in% tau.forestry$relevant.Variable)
}
if (is.null(e.forestry$relevant.Variable)) {
e.forestry$relevant.Variable <- 1:ncol(feat)
} else{
if (is.character(e.forestry$relevant.Variable))
e.forestry$relevant.Variable <-
which(colnames(feat) %in% e.forestry$relevant.Variable)
}
# Translate the settings to a feature list ---------------------------------
general_hyperpara <- list("predmode" = predmode,
"nthread" = nthread)
hyperparameter_list <- list(
"general" = general_hyperpara,
"l_first_0" = mu.forestry,
"l_first_1" = mu.forestry,
"l_second_0" = tau.forestry,
"l_second_1" = tau.forestry,
"l_prop" = e.forestry
)
return(
X_RF_fully_specified(
feat = feat,
tr = tr,
yobs = yobs,
hyperparameter_list = hyperparameter_list,
verbose = verbose
)
)
}
# X-RF basic constructor -------------------------------------------------------
X_RF_fully_specified <-
function(feat,
tr,
yobs,
hyperparameter_list,
verbose) {
# First stage --------------------------------------------------------------
yobs_0 <- yobs[tr == 0]
yobs_1 <- yobs[tr == 1]
X_0 <- feat[tr == 0, ]
X_1 <- feat[tr == 1, ]
m_0 <-
forestry::forestry(
x = X_0[, hyperparameter_list[["l_first_0"]]$relevant.Variable],
y = yobs_0,
ntree = hyperparameter_list[["l_first_0"]]$ntree,
replace = hyperparameter_list[["l_first_0"]]$replace,
sample.fraction = hyperparameter_list[["l_first_0"]]$sample.fraction,
mtry = hyperparameter_list[["l_first_0"]]$mtry,
nodesizeSpl = hyperparameter_list[["l_first_0"]]$nodesizeSpl,
nodesizeAvg = hyperparameter_list[["l_first_0"]]$nodesizeAvg,
nthread = hyperparameter_list[["general"]]$nthread,
splitrule = "variance",
splitratio = hyperparameter_list[["l_first_0"]]$splitratio
)
m_1 <-
forestry::forestry(
x = X_1[, hyperparameter_list[["l_first_1"]]$relevant.Variable],
y = yobs_1,
ntree = hyperparameter_list[["l_first_1"]]$ntree,
replace = hyperparameter_list[["l_first_1"]]$replace,
sample.fraction = hyperparameter_list[["l_first_1"]]$sample.fraction,
mtry = hyperparameter_list[["l_first_1"]]$mtry,
nodesizeSpl = hyperparameter_list[["l_first_1"]]$nodesizeSpl,
nodesizeAvg = hyperparameter_list[["l_first_1"]]$nodesizeAvg,
nthread = hyperparameter_list[["general"]]$nthread,
splitrule = "variance",
splitratio = hyperparameter_list[["l_first_1"]]$splitratio
)
if (verbose) {
print("Done with the first stage.")
}
# Second Stage -------------------------------------------------------------
r_0 <-
predict(m_1,
X_0[, hyperparameter_list[["l_first_0"]]$relevant.Variable]) -
yobs_0
r_1 <-
yobs_1 -
predict(m_0, X_1[, hyperparameter_list[["l_first_1"]]$relevant.Variable])
m_tau_0 <-
forestry::forestry(
x = X_0[, hyperparameter_list[["l_second_0"]]$relevant.Variable],
y = r_0,
ntree = hyperparameter_list[["l_second_0"]]$ntree,
replace = hyperparameter_list[["l_second_0"]]$replace,
sample.fraction = hyperparameter_list[["l_second_0"]]$sample.fraction,
mtry = hyperparameter_list[["l_second_0"]]$mtry,
nodesizeSpl = hyperparameter_list[["l_second_0"]]$nodesizeSpl,
nodesizeAvg = hyperparameter_list[["l_second_0"]]$nodesizeAvg,
nthread = hyperparameter_list[["general"]]$nthread,
splitrule = "variance",
splitratio = hyperparameter_list[["l_second_0"]]$splitratio
)
m_tau_1 <-
forestry::forestry(
x = X_1[, hyperparameter_list[["l_second_1"]]$relevant.Variable],
y = r_1,
ntree = hyperparameter_list[["l_second_1"]]$ntree,
replace = hyperparameter_list[["l_second_1"]]$replace,
sample.fraction = hyperparameter_list[["l_second_1"]]$sample.fraction,
mtry = hyperparameter_list[["l_second_1"]]$mtry,
nodesizeSpl = hyperparameter_list[["l_second_1"]]$nodesizeSpl,
nodesizeAvg = hyperparameter_list[["l_second_1"]]$nodesizeAvg,
nthread = hyperparameter_list[["general"]]$nthread,
splitrule = "variance",
splitratio = hyperparameter_list[["l_second_1"]]$splitratio
)
if (verbose) {
print("Done with the second stage.")
}
# Prop score estimation ----------------------------------------------------
m_prop <-
forestry::forestry(
x = feat[, hyperparameter_list[["l_prop"]]$relevant.Variable],
y = tr,
ntree = hyperparameter_list[["l_prop"]]$ntree,
replace = hyperparameter_list[["l_prop"]]$replace,
sample.fraction = hyperparameter_list[["l_prop"]]$sample.fraction,
mtry = hyperparameter_list[["l_prop"]]$mtry,
nodesizeSpl = hyperparameter_list[["l_prop"]]$nodesizeSpl,
nodesizeAvg = hyperparameter_list[["l_prop"]]$nodesizeAvg,
nthread = hyperparameter_list[["general"]]$nthread,
splitrule = "variance",
splitratio = hyperparameter_list[["l_prop"]]$splitratio
)
if (verbose) {
print("Done with the propensity score estimation.")
}
return(
new(
"X_RF",
feature_train = feat,
tr_train = tr,
yobs_train = yobs,
m_0 = m_0,
m_1 = m_1,
m_tau_0 = m_tau_0,
m_tau_1 = m_tau_1,
m_prop = m_prop,
hyperparameter_list = hyperparameter_list,
creator = function(feat, tr, yobs) {
X_RF_fully_specified(feat,
tr,
yobs,
hyperparameter_list,
verbose)
}
)
)
}
# Estimate CATE Method ---------------------------------------------------------
#' EstimateCate-X_hRF
#' @rdname EstimateCate
#' @inherit EstimateCate
#' @exportMethod EstimateCate
setMethod(
f = "EstimateCate",
signature = "X_RF",
definition = function(theObject, feature_new)
{
feature_new <- as.data.frame(feature_new)
catch_feat_input_errors(feature_new)
predmode <- theObject@hyperparameter_list[["general"]]$predmode
prop_scores <- predict(theObject@m_prop, feature_new)
if (predmode == "propmean") {
return(
prop_scores * predict(theObject@m_tau_0, feature_new) +
(1 - prop_scores) * predict(theObject@m_tau_1, feature_new)
)
}
if (predmode == "extreme") {
return(ifelse(
prop_scores > .5,
predict(theObject@m_tau_0, feature_new),
predict(theObject@m_tau_1, feature_new)
))
}
if (predmode == "control") {
return(predict(theObject@m_tau_0, feature_new))
}
if (predmode == "treated") {
return(predict(theObject@m_tau_1, feature_new))
}
stop("predmode should be one of propmean, extreme, control, or treated.")
}
)