-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path08-tmle3mopttx.R
309 lines (228 loc) · 9.11 KB
/
08-tmle3mopttx.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
## ---- fig.cap="Dynamic Treatment Regime in a Clinical Setting", results="asis", echo=FALSE----
knitr::include_graphics(path = "img/png/DynamicA_Illustration.png")
## ----setup-mopttx-------------------------------------------------------------
library(data.table)
library(sl3)
library(tmle3)
library(tmle3mopttx)
library(devtools)
set.seed(111)
## ----load sim_bin_data--------------------------------------------------------
data("data_bin")
## ----data_nodes2-mopttx-------------------------------------------------------
# organize data and nodes for tmle3
data <- data_bin
node_list <- list(
W = c("W1", "W2", "W3"),
A = "A",
Y = "Y"
)
## ----mopttx_sl3_lrnrs2--------------------------------------------------------
# Define sl3 library and metalearners:
lrn_xgboost_50 <- Lrnr_xgboost$new(nrounds = 50)
lrn_xgboost_100 <- Lrnr_xgboost$new(nrounds = 100)
lrn_xgboost_500 <- Lrnr_xgboost$new(nrounds = 500)
lrn_mean <- Lrnr_mean$new()
lrn_glm <- Lrnr_glm_fast$new()
lrn_lasso <- Lrnr_glmnet$new()
## Define the Q learner:
Q_learner <- Lrnr_sl$new(
learners = list(lrn_lasso, lrn_mean, lrn_glm),
metalearner = Lrnr_nnls$new()
)
## Define the g learner:
g_learner <- Lrnr_sl$new(
learners = list(lrn_lasso, lrn_glm),
metalearner = Lrnr_nnls$new()
)
## Define the B learner:
b_learner <- Lrnr_sl$new(
learners = list(lrn_lasso,lrn_mean, lrn_glm),
metalearner = Lrnr_nnls$new()
)
## ----mopttx_make_lrnr_list----------------------------------------------------
# specify outcome and treatment regressions and create learner list
learner_list <- list(Y = Q_learner, A = g_learner, B = b_learner)
## ----mopttx_spec_init_complex-------------------------------------------------
# initialize a tmle specification
tmle_spec <- tmle3_mopttx_blip_revere(
V = c("W1", "W2", "W3"), type = "blip1",
learners = learner_list,
maximize = TRUE, complex = TRUE,
realistic = FALSE, resource = 1
)
## ----mopttx_fit_tmle_auto_blip_revere_complex---------------------------------
# fit the TML estimator
fit <- tmle3(tmle_spec, data, node_list, learner_list)
fit
## ----mopttx_spec_init_complex_resource----------------------------------------
# initialize a tmle specification
tmle_spec_resource <- tmle3_mopttx_blip_revere(
V = c("W1", "W2", "W3"), type = "blip1",
learners = learner_list,
maximize = TRUE, complex = TRUE,
realistic = FALSE, resource = 0.90
)
## ----mopttx_fit_tmle_auto_blip_revere_complex_resource, eval=T----------------
# fit the TML estimator
fit_resource <- tmle3(tmle_spec_resource, data, node_list, learner_list)
fit_resource
## ----mopttx_compare_resource--------------------------------------------------
# Number of individuals getting treatment (no resource constraint):
table(tmle_spec$return_rule)
# Number of individuals getting treatment (resource constraint):
table(tmle_spec_resource$return_rule)
## ----mopttx_spec_init_complex_V_empty-----------------------------------------
# initialize a tmle specification
tmle_spec_V_empty <- tmle3_mopttx_blip_revere(
type = "blip1",
learners = learner_list,
maximize = TRUE, complex = TRUE,
realistic = FALSE, resource = 0.90
)
## ----mopttx_fit_tmle_auto_blip_revere_complex_V_empty, eval=T-----------------
# fit the TML estimator
fit_V_empty <- tmle3(tmle_spec_V_empty, data, node_list, learner_list)
fit_V_empty
## ----load sim_cat_data--------------------------------------------------------
data("data_cat_realistic")
## ----data_nodes-mopttx--------------------------------------------------------
# organize data and nodes for tmle3
data <- data_cat_realistic
node_list <- list(
W = c("W1", "W2", "W3", "W4"),
A = "A",
Y = "Y"
)
## ----data_cats-mopttx---------------------------------------------------------
# organize data and nodes for tmle3
table(data$A)
## ----sl3_lrnrs-mopttx---------------------------------------------------------
# Initialize few of the learners:
lrn_xgboost_50 <- Lrnr_xgboost$new(nrounds = 50)
lrn_xgboost_100 <- Lrnr_xgboost$new(nrounds = 100)
lrn_xgboost_500 <- Lrnr_xgboost$new(nrounds = 500)
lrn_mean <- Lrnr_mean$new()
lrn_glm <- Lrnr_glm_fast$new()
## Define the Q learner, which is just a regular learner:
Q_learner <- Lrnr_sl$new(
learners = list(lrn_xgboost_100, lrn_mean, lrn_glm),
metalearner = Lrnr_nnls$new()
)
# Define the g learner, which is a multinomial learner:
# specify the appropriate loss of the multinomial learner:
mn_metalearner <- make_learner(Lrnr_solnp,
eval_function = loss_loglik_multinomial,
learner_function = metalearner_linear_multinomial
)
g_learner <- make_learner(Lrnr_sl, list(lrn_xgboost_100, lrn_xgboost_500, lrn_mean), mn_metalearner)
# Define the Blip learner, which is a multivariate learner:
learners <- list(lrn_xgboost_50, lrn_xgboost_100, lrn_xgboost_500, lrn_mean, lrn_glm)
b_learner <- create_mv_learners(learners = learners)
## ----cat_learners-------------------------------------------------------------
# See which learners support multi-class classification:
sl3_list_learners(c("categorical"))
## ----make_lrnr_list-mopttx----------------------------------------------------
# specify outcome and treatment regressions and create learner list
learner_list <- list(Y = Q_learner, A = g_learner, B = b_learner)
## ----spec_init----------------------------------------------------------------
# initialize a tmle specification
tmle_spec_cat <- tmle3_mopttx_blip_revere(
V = c("W1", "W2", "W3", "W4"), type = "blip2",
learners = learner_list, maximize = TRUE, complex = TRUE,
realistic = FALSE
)
## ----fit_tmle_auto------------------------------------------------------------
# fit the TML estimator
fit_cat <- tmle3(tmle_spec_cat, data, node_list, learner_list)
fit_cat
# How many individuals got assigned each treatment?
table(tmle_spec_cat$return_rule)
## ----mopttx_spec_init_noncomplex----------------------------------------------
# initialize a tmle specification
tmle_spec_cat_simple <- tmle3_mopttx_blip_revere(
V = c("W4", "W3", "W2", "W1"), type = "blip2",
learners = learner_list,
maximize = TRUE, complex = FALSE, realistic = FALSE
)
## ----mopttx_fit_tmle_auto_blip_revere_noncomplex------------------------------
# fit the TML estimator
fit_cat_simple <- tmle3(tmle_spec_cat_simple, data, node_list, learner_list)
fit_cat_simple
## ----mopttx_spec_init_realistic-----------------------------------------------
# initialize a tmle specification
tmle_spec_cat_realistic <- tmle3_mopttx_blip_revere(
V = c("W4", "W3", "W2", "W1"), type = "blip2",
learners = learner_list,
maximize = TRUE, complex = TRUE, realistic = TRUE
)
## ----mopttx_fit_tmle_auto_blip_revere_realistic-------------------------------
# fit the TML estimator
fit_cat_realistic <- tmle3(tmle_spec_cat_realistic, data, node_list, learner_list)
fit_cat_realistic
# How many individuals got assigned each treatment?
table(tmle_spec_cat_realistic$return_rule)
## ----data_nodes-add-missigness-mopttx-----------------------------------------
data_missing <- data_cat_realistic
#Add some random missingless:
rr <- sample(nrow(data_missing), 100, replace = FALSE)
data_missing[rr,"Y"]<-NA
summary(data_missing$Y)
## ----sl3_lrnrs-add-mopttx-----------------------------------------------------
delta_learner <- Lrnr_sl$new(
learners = list(lrn_mean, lrn_glm),
metalearner = Lrnr_nnls$new()
)
# specify outcome and treatment regressions and create learner list
learner_list <- list(Y = Q_learner, A = g_learner, B = b_learner, delta_Y=delta_learner)
## ----spec_init_missingness, eval = FALSE--------------------------------------
## # initialize a tmle specification
## tmle_spec_cat_miss <- tmle3_mopttx_blip_revere(
## V = c("W1", "W2", "W3", "W4"), type = "blip2",
## learners = learner_list, maximize = TRUE, complex = TRUE,
## realistic = FALSE
## )
## ----fit_tmle_auto2, eval = FALSE---------------------------------------------
## # fit the TML estimator
## fit_cat_miss <- tmle3(tmle_spec_cat_miss, data_missing, node_list, learner_list)
## fit_cat_miss
## ----spec_init_Qlearning2, eval=FALSE-----------------------------------------
## # initialize a tmle specification
## tmle_spec_Q <- tmle3_mopttx_Q(maximize = TRUE)
##
## # Define data:
## tmle_task <- tmle_spec_Q$make_tmle_task(data, node_list)
##
## # Define likelihood:
## initial_likelihood <- tmle_spec_Q$make_initial_likelihood(
## tmle_task,
## learner_list
## )
##
## # Estimate the parameter:
## Q_learning(tmle_spec_Q, initial_likelihood, tmle_task)[1]
## ----data_vim-nodes-mopttx----------------------------------------------------
# bin baseline covariates to 3 categories:
data$W1<-ifelse(data$W1<quantile(data$W1)[2],1,ifelse(data$W1<quantile(data$W1)[3],2,3))
node_list <- list(
W = c("W3", "W4", "W2"),
A = c("W1", "A"),
Y = "Y"
)
## ----mopttx_spec_init_vim-----------------------------------------------------
# initialize a tmle specification
tmle_spec_vim <- tmle3_mopttx_vim(
V=c("W2"),
type = "blip2",
learners = learner_list,
maximize = FALSE,
method = "SL",
complex = TRUE,
realistic = FALSE
)
## ----mopttx_fit_tmle_auto_vim, eval=TRUE--------------------------------------
# fit the TML estimator
vim_results <- tmle3_vim(tmle_spec_vim, data, node_list, learner_list,
adjust_for_other_A = TRUE
)
print(vim_results)