-
Notifications
You must be signed in to change notification settings - Fork 1
/
fit_mechanisms_rule_ate.R
378 lines (328 loc) · 11.8 KB
/
fit_mechanisms_rule_ate.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
utils::globalVariables(c("..w_names", "A"))
#' Fit propensity scores for treatment contrasts
#'
#' @param train_data A \code{data.table} containing the observed data; columns
#' are in the order specified by the NPSEM (Y, M, Z, A, W), with column names
#' set appropriately based on the input data. Such a structure is merely a
#' convenience utility to passing data around to the various core estimation
#' routines and is automatically generated \code{\link{medoutcon}}.
#' @param valid_data A holdout data set, with columns exactly matching those
#' appearing in the preceding argument \code{train_data}, to be used for
#' estimation via cross-fitting. Optional, defaulting to \code{NULL}.
#' @param contrast A \code{numeric} double indicating the two values of the
#' intervention \code{A} to be compared. The default value of \code{c(0, 1)}
#' assumes a binary intervention node \code{A}.
#' @param learners \code{\link[sl3]{Stack}}, or other learner class (inheriting
#' from \code{\link[sl3]{Lrnr_base}}), containing a set of learners from
#' \pkg{sl3}, to be used in fitting a propensity score models, i.e., g :=
#' P(A = 1 | W) and h := P(A = 1 | M, W).
#' @param w_names A \code{character} vector of the names of the columns that
#' correspond to baseline covariates (W). The input for this argument is
#' automatically generated by \code{\link{medoutcon}}.
#'
#' @importFrom data.table as.data.table copy setnames ":="
#' @importFrom sl3 sl3_Task
#'
fit_ruleclass_mech <- function(train_data,
valid_data = NULL,
learners,
w_names) {
cov_names <- w_names
## construct task for treatment mechanism fit
treat_task <- sl3::sl3_Task$new(
data = train_data,
weights = "obs_weights",
covariates = cov_names,
outcome = "Dclass",
outcome_type = "binomial"
)
## fit and predict treatment mechanism
treat_fit <- learners$train(treat_task)
treat_pred <- treat_fit$predict()
## use full data for prediction if no validation data provided
if (is.null(valid_data)) {
out_treat_est <- data.table::as.data.table(treat_pred)
data.table::setnames(out_treat_est, c(
"treat_pred"
))
## output
out <- list(
treat_est = out_treat_est,
treat_fit = treat_fit
)
} else {
out_treat_est <- lapply(
list(train_data, valid_data),
function(data) {
## create task to generate contrast-specific predictions
treat_task <- sl3::sl3_Task$new(
data = data,
weights = "obs_weights",
covariates = cov_names,
outcome = "Dclass",
outcome_type = "binomial"
)
## predictions for training data
treat_pred <- treat_fit$predict(treat_task)
## bounding to numerical precision and for positivity considerations
out_treat_est <- treat_pred
out_treat_est <- data.table::as.data.table(out_treat_est)
data.table::setnames(out_treat_est, c(
"treat_pred"
))
})
## output
out <- list(
treat_est_train = out_treat_est[[1]],
treat_est_valid = out_treat_est[[2]],
treat_fit = treat_fit
)
}
return(out)
}
fit_rule_mech <- function(train_data,
valid_data = NULL,
learners,
w_names) {
cov_names <- w_names
## construct task for treatment mechanism fit
treat_task <- sl3::sl3_Task$new(
data = train_data,
weights = "obs_weights",
covariates = cov_names,
outcome = "D1",
outcome_type = "continuous"
)
## fit and predict treatment mechanism
treat_fit <- learners$train(treat_task)
treat_pred <- treat_fit$predict()
## use full data for prediction if no validation data provided
if (is.null(valid_data)) {
out_treat_est <- data.table::as.data.table(treat_pred)
data.table::setnames(out_treat_est, c(
"treat_pred"
))
## output
out <- list(
treat_est = out_treat_est,
treat_fit = treat_fit
)
} else {
out_treat_est <- lapply(
list(train_data, valid_data),
function(data) {
## create task to generate contrast-specific predictions
treat_task <- sl3::sl3_Task$new(
data = data,
weights = "obs_weights",
covariates = cov_names,
outcome = "D1",
outcome_type = "continuous"
)
## predictions for training data
treat_pred <- treat_fit$predict(treat_task)
## bounding to numerical precision and for positivity considerations
out_treat_est <- treat_pred
out_treat_est <- data.table::as.data.table(out_treat_est)
data.table::setnames(out_treat_est, c(
"treat_pred"
))
})
## output
out <- list(
treat_est_train = out_treat_est[[1]],
treat_est_valid = out_treat_est[[2]],
treat_fit = treat_fit
)
}
return(out)
}
fit_treat_mech <- function(train_data,
valid_data = NULL,
contrast,
learners,
w_names) {
cov_names <- w_names
## construct task for treatment mechanism fit
treat_task <- sl3::sl3_Task$new(
data = train_data,
weights = "obs_weights",
covariates = cov_names,
outcome = "A",
outcome_type = "binomial"
)
## fit and predict treatment mechanism
treat_fit <- learners$train(treat_task)
treat_pred <- treat_fit$predict()
## use full data for prediction if no validation data provided
if (is.null(valid_data)) {
treat_pred_A_prime <- train_data$C1 * treat_pred +
(1 - train_data$C1) * (1 - treat_pred)
## bounding to numerical precision and for positivity considerations
x_precise <- bound_precision(treat_pred_A_prime)
out_treat_est <- bound_propensity(x_precise)
out_treat_est <- data.table::as.data.table(out_treat_est)
data.table::setnames(out_treat_est, c(
"treat_pred_A_prime"
))
## output
out <- list(
treat_est = out_treat_est,
treat_fit = treat_fit
)
} else {
out_treat_est <- lapply(
list(train_data, valid_data),
function(data) {
## create task to generate contrast-specific predictions
treat_task <- sl3::sl3_Task$new(
data = data,
weights = "obs_weights",
covariates = cov_names,
outcome = "A",
outcome_type = "binomial"
)
## predictions for training data
treat_pred <- treat_fit$predict(treat_task)
treat_pred_A_prime <- data[,"C1"] * treat_pred +
(1 - data[,"C1"]) * (1 - treat_pred)
x_precise <- bound_precision(treat_pred_A_prime)
out_treat_est <- bound_propensity(x_precise)
out_treat_est <- data.table::as.data.table(out_treat_est)
data.table::setnames(out_treat_est, c(
"treat_pred_A_prime"
))
}
)
## output
out <- list(
treat_est_train = out_treat_est[[1]],
treat_est_valid = out_treat_est[[2]],
treat_fit = treat_fit
)
}
return(out)
}
###############################################################################
#' Fit outcome regression
#'
#' @param train_data A \code{data.table} containing the observed data, with
#' columns in the order specified by the NPSEM (Y, M, Z, A, W), with column
#' names set based on the original input data. Such a structure is merely a
#' convenience utility to passing data around to the various core estimation
#' routines and is automatically generated \code{\link{medoutcon}}.
#' @param valid_data A holdout data set, with columns exactly matching those
#' appearing in the preceding argument \code{data}, to be used for estimation
#' via cross-fitting. Optional, defaulting to \code{NULL}.
#' @param contrast A \code{numeric} double indicating the two values of the
#' intervention \code{A} to be compared. The default of \code{c(0, 1)} assumes
#' a binary intervention node \code{A}.
#' @param learners \code{\link[sl3]{Stack}}, or other learner class (inheriting
#' from \code{\link[sl3]{Lrnr_base}}), containing a set of learners from
#' \pkg{sl3}, to be used in fitting the outcome regression, i.e., b(A,Z,M,W).
#' @param w_names A \code{character} vector of the names of the columns that
#' correspond to baseline covariates (W). The input for this argument is
#' automatically generated by \code{\link{medoutcon}}.
#'
#' @importFrom data.table as.data.table copy setnames ":="
#' @importFrom sl3 sl3_Task
fit_out_mech <- function(train_data,
valid_data = NULL,
contrast,
learners,
w_names) {
## construct task for propensity score fit
b_natural_task <- sl3::sl3_Task$new(
data = train_data,
weights = "obs_weights",
covariates = c("A", w_names),
outcome = "Y"
)
## fit and predict
b_natural_fit <- learners$train(b_natural_task)
b_natural_pred <- b_natural_fit$predict()
## use full data for counterfactual prediction if no validation data given
if (is.null(valid_data)) {
## set intervention to first contrast a_prime := contrast[1]
C1<-train_data$C1
train_data_intervene <- data.table::copy(train_data)
train_data_intervene[, A := C1]
## predictions on observed data (i.e., under observed treatment status)
b_natural_pred <- b_natural_fit$predict()
## create task for post-intervention outcome regression
b_intervened_prime_task <- sl3::sl3_Task$new(
data = train_data_intervene,
weights = "obs_weights",
covariates = c("A", w_names),
outcome = "Y"
)
## predict from trained model on counterfactual data
b_intervened_pred_A_prime <- b_natural_fit$predict(b_intervened_prime_task)
## output
out_b_est <- data.table::as.data.table(cbind(
b_natural_pred,
b_intervened_pred_A_prime
))
data.table::setnames(out_b_est, c(
"b_pred_A_natural",
"b_pred_A_prime"
))
## output
out <- list(
b_est = out_b_est,
b_fit = b_natural_fit
)
} else {
## copy both training and validation data, once for each contrast
train_data_intervene <- data.table::copy(train_data)
valid_data_intervene <- data.table::copy(valid_data)
## predictions on observed data (i.e., under observed treatment status)
b_natural_pred_train <- b_natural_fit$predict()
b_natural_task_valid <- sl3::sl3_Task$new(
data = valid_data,
weights = "obs_weights",
covariates = c("A", w_names),
outcome = "Y"
)
b_natural_pred_valid <- b_natural_fit$predict(b_natural_task_valid)
## set intervention to first contrast a' := contrast[1]
out_b_est <- lapply(
list(train_data_intervene, valid_data_intervene),
function(data_intervene) {
## set intervention to first contrast a' := contrast[1]
C1<-data_intervene$C1
data_intervene[, A := C1]
b_intervened_prime_task <- sl3::sl3_Task$new(
data = data_intervene,
weights = "obs_weights",
covariates = c("A", w_names),
outcome = "Y"
)
## predict from trained model on counterfactual data
b_intervened_pred_A_prime <-
b_natural_fit$predict(b_intervened_prime_task)
## output
out_b_est <- data.table::as.data.table(cbind(
b_intervened_pred_A_prime
))
return(out_b_est)
}
)
## add natural treatment estimates to post-intervention predictions
out_b_est[[1]] <- cbind(b_natural_pred_train, out_b_est[[1]])
out_b_est[[2]] <- cbind(b_natural_pred_valid, out_b_est[[2]])
lapply(out_b_est, function(x) {
data.table::setnames(x, c(
"b_pred_A_natural",
"b_pred_A_prime"
))
})
## output
out <- list(
b_est_train = out_b_est[[1]],
b_est_valid = out_b_est[[2]],
b_fit = b_natural_fit
)
}
return(out)
}