This repository has been archived by the owner on Apr 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmle3_lecture.html
682 lines (481 loc) · 18.5 KB
/
tmle3_lecture.html
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
<!DOCTYPE html>
<html>
<head>
<title>tlverse: Implement frameworks, not algorithms</title>
<meta charset="utf-8">
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link rel="stylesheet" href="custom.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# <code>tlverse</code>: Implement frameworks, not algorithms
### 2018 Apr 24 (Tue), 09:39
---
class: center, middle
# `tlverse`: The Targeted Learning Analytics Ecosystem
---
# The `tlverse` Ecosystem
What is the `tlverse`?
By analogy to the [`tidyverse`](https://tidyverse.org/):
> The tidyverse is an opinionated collection of R packages designed for data
> science. All packages share an underlying design philosophy, grammar, and data
> structures.
So, the [`tlverse`](https://tlverse.org) is
* an opinionated collection of R packages for Targeted Learning
* sharing an underlying philosophy, grammar, and set of data structures
---
# The `tlverse` Ecosystem
These are the main packages that represent the **core** of the `tlverse`:
--
* [`sl3`](https://github.com/tlverse/sl3): Modern Super Learning with Pipelines
* _What?_ A modern object-oriented re-implementation of the Super Learner
algorithm, employing recently developed paradigms for `R` programming.
* _Why?_ A design that leverages modern tools for fast computation, is
forward-looking, and can form one of the cornerstones of the `tlverse`.
--
* [`tmle3`](https://github.com/tlverse/tmle3): An Engine for Targeted Learning
* _What?_ A generalized framework that simplifies Targeted Learning by
identifying and implementing a series of common statistical estimation
procedures.
* _Why?_ A common interface and engine that accommodates current algorithmic
approaches to Targeted Learning and is still flexible enough to remain the
engine even as new techniques are developed.
---
# The `tlverse` Ecosystem
In addition to the engines that drive development in the `tlverse`, there are
some supporting packages -- in particular, we have two...
--
* [`origami`](https://github.com/tlverse/origami): A Generalized Framework for
Cross-Validation
* _What?_ A generalized framework for flexible cross-validation
* _Why?_ Cross-validation is a key part of ensuring error estimates are honest
and preventing overfitting. It is an essential part of the both the Super
Learner algorithm and Targeted Learning.
--
* [`delayed`](https://github.com/tlverse/delayed): Parallelization Framework for
Dependent Tasks
* _What?_ A framework for delayed computations (futures) based on task
dependencies.
* _Why?_ Efficient allocation of compute resources is essential when deploying
large-scale, computationally intensive algorithms.
---
# The Problem
--
Mark is too productive. Even for point treatment data, we have a huge range of
TMLE methodologies:
* **TMLE Variants:** TMLE, IPCW-TMLE, CV-TMLE, C-TMLE, One-step TMLE, HAL-TMLE,
Targeted HAL, etc.
* **Parameters:** Means under static interventions, dynamic rules, stochastic
interventions, ATE, ATT, CDE, NDE, Blip Variance, etc.
--
For each TMLE-based method, someone has probably implemented the estimator at
least well enough to run simulations for a paper. However, those implementations
are not often maintained after the paper is published, and they might not have
been terribly robust in the first place (e.g., `opttx`). Thus, these methods are
not easy for the average user to apply.
---
# The Solution: `tlverse` + `tmle3`
* Identify the parts of the framework common across a range of methods, and
implement those.
* Build tools to let others build on the framework.
* Build a unified and accessible user interface to these methods.
* Establish a good software development community so that the project can
persist past the interest of any one developer.
---
# It's Roadmap Time!
<u>**Steps 1-3: Define the research question**</u>
**Data:** `\(O = (W, A, Y) \sim P_0 \in \mathcal{M}\)`, where, for simplicity,
consider `\(W \in \mathbb{R}^d\)`, `\(A \in \{0,1\}\)`, `\(Y\in \{0,1\}\)`.
**Model:** `\(\mathcal{M}\)`, a nonparametric model that makes no assumptions on the
distribution governing the data `\(P_0\)`.
We consider the following _nonparametric structural equation model_ (NPSEM):
`$$W = f_W(U_W)$$`
`$$A = f_A(W, U_A)$$`
`$$Y = f_Y(W, U_Y)$$`
Which corresponds to the likelihood factorization
`\(P_0 = P_0(Y \mid A,W)P_0(A \mid W)P_0(W)\)`
**Parameter:** Treatment Specific Mean `\(\Psi(P) = E_W[E[Y \mid A = 1, W]]\)`
---
# It's Roadmap Time!
<u>**Steps 4-6: Estimation**</u>
**Super Learner (Likelihood Estimation):** Identify and estimate the relevant
likelihood factors with Super Learner
**TMLE:** Update our initial likelihood estimates to solve the efficient
influence function (EIF), apply the parameter mapping to the updated likelihood.
In addition to the elements defined in steps 1-3, this requires the following:
* **EIF** -- for TSM `\(\frac{I(A = 1)}{P(A = 1 \mid W)}(Y - E[Y \mid A, W]) + E[Y \mid A = 1 ,W] - \psi\)`
* **submodel** -- we'll use a logistic submodel
* **loss function** -- we'll use log-likelihood loss
* **solver** (iterative vs. one-step) -- not relevant for the TSM parameter
**Inference:** Use the variance of the EIF to construct confidence intervals:
`$$\sigma^2 = \frac{1}{n}\sum_{i = 1}^n D^2(O_i) + o_p\left(\frac{1}{\sqrt{n}}\right)$$`
---
# Example: Define the Research Question
We use data from the Collaborative Perinatal Project (CPP), available in the
`sl3` package. To simplify this example, we define a binary intervention
variable, `parity01` -- an indicator of having one or more children before the
current child and a binary outcome, `haz01` -- an indicator of having an above
average height for age.
```r
data(cpp_imputed)
data <- as.data.table(cpp_imputed)
# generate binary treatment and outcome
data$parity01 <- as.numeric(data$parity > 0)
data$haz01 <- as.numeric(data$haz > 0)
data[is.na(data)] <- 0
# define variable roles
node_list <- list(
W = c("apgar1", "apgar5", "gagebrth", "mage",
"meducyrs", "sexn"),
A = "parity01",
Y = "haz01"
)
```
---
# Example: Super Learning with `sl3`
```r
# define regression tasks
Q_task <- make_sl3_Task(data,
covariates=c(node_list$W, node_list$A),
outcome=node_list$Y)
g_task <- make_sl3_Task(data,
covariates=node_list$W,
outcome=node_list$A)
# simple sl
stack <- make_learner_stack(Lrnr_glm, Lrnr_mean)
lrnr_nnls <- make_learner(Lrnr_nnls)
lrnr_sl <- make_learner(Lrnr_sl, stack, lrnr_nnls)
Q_fit <- lrnr_sl$train(Q_task)
g_fit <- lrnr_sl$train(g_task)
```
---
# A Simple TMLE Implementation
```r
# get relevant quantities
A <- data$parity01
Y <- data$haz01
#P(A=1|W)
g1W <- g_fit$predict(g_task)
#E(Y|A=a,W)
QAW <- Q_fit$predict(Q_task)
#create counterfactual data and make task
cf_data <- copy(data)
set(cf_data, , node_list$A, 1)
cf_Q_task <- make_sl3_Task(cf_data,
covariates=c(node_list$W, node_list$A),
outcome=node_list$Y)
#E(Y|A=1,W)
Q1W <- Q_fit$predict(cf_Q_task)
```
---
# A Simple TMLE Implementation
```r
####
# construct clever covariate
# I(A=1|W)
HA = A/g1W
# 1/P(A=1|W)
H1 = 1/g1W
####
# fit logistic submodel
submodel_fit <- glm(Y ~ HA - 1 + offset(qlogis(QAW)), family=binomial())
epsilon <- coef(submodel_fit)
####
# update likelihood
Q1W_star <- plogis(qlogis(Q1W) + H1*epsilon)
QAW_star <- plogis(qlogis(QAW) + HA*epsilon)
```
---
# A Simple TMLE Implementation
```r
####
# calculate IC
IC <- HA * (Y - QAW_star) + Q1W_star - mean(Q1W_star)
####
# verify convergence
mean(IC)
```
```
## [1] 1.748805e-12
```
```r
####
# get estimate
psi_hat <- mean(Q1W_star)
print(psi_hat)
```
```
## [1] 0.5280257
```
---
# Summary
* We implemented a simple TMLE in ~ 50 lines of code.
* But we haven't done much to implement TMLE _in general_
* Lots of things are hardcoded, or not explictly coded at all, including the
NPSEM, the submodel, the parameter, and the loss function.
--
* `tmle3` takes a different approach to this problem
* Defining a TMLE method in `tmle3` loosely follows the roadmap (order is a bit
different)
* Most parts of it are _modular_ in that they can be easily replaced to
implement slightly different TMLEs
---
# Model: `tmle3_Node`s
In `tmle3`, we define the NPSEM using the `define_node` function for each node.
`define_node` allows a user to specify the node_name, which columns in the data
comprise the node, and a list of parent nodes.
```r
npsem <- list(
define_node("W", c(
"apgar1", "apgar5", "gagebrth", "mage",
"meducyrs", "sexn"
)),
define_node("A", c("parity01"), c("W")),
define_node("Y", c("haz01"), c("A", "W"))
)
```
Nodes also track information about the data types of the variables (continuous,
categorical, binomial, etc). Here, that information is being estimated
automatically from the data. In the future, each node will also contain
information about censoring indicators, where applicable, but this is not yet
implemented.
---
# Data: `tmle3_task`
A `tmle3_Task` is an object comprised of observed data, and the NPSEM defined
above:
```r
tmle_task <- tmle3_Task$new(data, npsem = npsem)
```
This task object contains methods to help subset the data as needed for various
steps in the tmle process:
```r
#get the outcome node data
head(tmle_task$get_tmle_node("Y"))
```
```
## [1] 1 1 1 0 0 1
```
A `tmle3_Task` is a special kind of `sl3` task.
---
# Likelihood Fits: `Likelihood`
`tmle3` models likelihoods as a list of likelihood factor objects, where each
likelihood factor object describes either _a priori_ knowledge or an estimation
strategy for the corresponding likelihood factor. These objects all inherit from
the `LF_base` base class, and there are different types depending on which of a
range of estimation strategies or _a priori_ knowledge is appropriate.
In some cases, a full conditional density for a particular factor is not
necessary. Instead, a conditional mean (a much easier quantity to estimate), is
all that's required. Although conditional means are not truly likelihood
factors, conditional means are also modeled using using likelihood factor
objects.
Examples:
* `LF_fit` - estimate a likelihood factor using `sl3` learners
* `LF_emp` - estimate a likelihood factor using NP-MLE (marginal likelihood
only)
---
# Likelihood Fits: `Likelihood`
Going back to our CPP example, we will estimate the marginal likelihood of `\(W\)`,
using NP-MLE, the conditional density of `\(A\)` given `\(W\)` using a GLM fit via `sl3`
and the conditional mean of `\(Y\)` given `\(A\)` and `\(W\)` using another GLM fit via
`sl3`:
```r
# define and fit likelihood
factor_list <- list(
define_lf(LF_emp, "W"),
define_lf(LF_fit, "A", lrnr_sl),
define_lf(LF_fit, "Y", lrnr_sl, type="mean")
)
likelihood_def <- Likelihood$new(factor_list)
likelihood <- likelihood_def$train(tmle_task)
print(likelihood)
```
```
## W: Lf_np
## A: LF_fit
## Y: LF_fit
```
A `tmle3` `Likelihood` is actually a special type of `sl3` learner, so the
syntax to train it on data is analogous.
---
# Likelihood Fits: `Likelihood`
Having fit the likelihood, we can now get likelihood values for any
`tmle3_task`:
```r
likelihood_values <- likelihood$get_likelihoods(tmle_task)
head(likelihood_values)
```
```
## W A Y
## 1: 0.0006939625 0.6328863 0.5761324
## 2: 0.0006939625 0.6328863 0.5761324
## 3: 0.0006939625 0.8372018 0.6753845
## 4: 0.0006939625 0.8372018 0.6753845
## 5: 0.0006939625 0.8372018 0.6753845
## 6: 0.0006939625 0.5910001 0.4632644
```
---
# Counterfactuals: `CF_Likelihood`
In `tmle3`, interventions are modeled by likelihoods where one or more
likelihood factors is replaced with a counterfactual version representing some
intervention.
`tmle3` defines the `CF_Likelihood` class, which inherits from `Likelihood`, and
takes an `observed_likelihood` and an `intervention_list`.
For our CPP example, we'll define a simple intervention where we set all
treatment `\(A=1\)`. We do this by defining a static likelihood factor, as a simple
indicator function `\(P(A \mid W) = I(A = 1)\)`
```r
intervention <- define_lf(LF_static, "A", value = 1)
```
We can then use this to construct a counterfactual likelihood:
```r
cf_likelihood <- make_CF_Likelihood(likelihood, intervention)
```
A `cf_likelihood` is a likelihood object, and so has the same behavior as the
observed likelihood object defined above, but with the observed likelihood
factors being replaced by the defined intervention likelihood factors.
---
# Counterfactuals: `CF_Likelihood`
In particular, we can get likelihood values under the counterfactual likelihood:
```r
cf_likelihood_values <- cf_likelihood$get_likelihoods(tmle_task)
head(cf_likelihood_values)
```
```
## W A Y
## 1: 0.0006939625 1 0.5761324
## 2: 0.0006939625 1 0.5761324
## 3: 0.0006939625 0 0.6753845
## 4: 0.0006939625 0 0.6753845
## 5: 0.0006939625 0 0.6753845
## 6: 0.0006939625 1 0.4632644
```
We see that the likelihood values for the `\(A\)` node are all either 0 or 1, as
would be expected from an indicator likelihood function. In addition, the
likelihood values for the non-intervention nodes have not changed.
---
# Parameter: `Param_base`
In the TMLE framework, we define a target parameter `\(\Psi(P)\)` as a mapping from
a probability distribution `\(P \in \mathcal{M}\)` to a set of real numbers
`\(\mathbb{R}^d\)`. Here `\(\mathcal{M}\)` is implied by the NPSEM we defined above.
In `tmle3`, we define parameter objects as objects inheriting from the
`Param_base` class, which keep track of not only the mapping from a probability
distribution to a parameter value, but also the corresponding EIF of the
parameter, and the "clever covariates" needed to calculate a TMLE update to the
likelihood. These values are calculated using the `tmle3_task`, `Likelihood`,
and `CF_likelihood` objects defined above.
Here, we define a treatment specific mean (TSM) parameter based on the
intervention we defined previously:
```r
tsm <- define_param(Param_TSM, likelihood, intervention)
```
This structure will be documented so that new parameters can be implemented
easily, analogous to learners in `sl3`.
---
# Update
The update procedure component of `tmle3` is currently in flux. The current
structure is as follows: We have an object, `tmle3_Update`, which calculates the
individual update steps using `tmle3_Update$update_step`. This contains the
submodel and loss function needed to solve the EIF.
```r
updater <- tmle3_Update$new(tsm)
likelihood$update_list <- updater
```
This is currently the weak link in `tmle3`, and it's under active development to
make it faster and more elegant.
---
# Fit
Now that we have specified all the components required for the TMLE procedure,
we can generate an object that manages all the components and updates the
likelihood and solves the EIF.
```r
tmle_fit <- fit_tmle3(tmle_task, likelihood, tsm, updater)
print(tmle_fit)
```
```
## A tmle3_Fit that took 1 step(s)
## param init_est tmle_est se lower upper
## 1: E[Y_{A=1}] 0.5305745 0.5280561 0.01462578 0.4993901 0.5567221
## psi_transformed lower_transformed upper_transformed
## 1: 0.5280561 0.4993901 0.5567221
```
Currently, the solver is iterative, but we plan to extend it to accomodate the
one-step approach.
---
# User-Friendly Interface
* The `tmle3` framework described above is general, and allows most components of
the TMLE procedure to be specified in a modular way.
* However, most end-users will not be interested in manually specifying all of
these components.
* Therefore, `tmle3` implements a `tmle3_Spec` object that bundles a set of
components into a _specification_ that, with minimal additional detail, can
be run by an end-user:
---
# User-Friendly Interface
```r
nodes <- list(W = c("apgar1", "apgar5", "gagebrth", "mage",
"meducyrs", "sexn"),
A = "parity01",
Y = "haz01")
learner_list <- list(Y = lrnr_sl, A = lrnr_sl)
data2 <- data.table::copy(data) # make a new copy to deal with data.table
tmle_fit_from_spec <- tmle3(tmle_TSM_all(), data2, nodes, learner_list)
print(tmle_fit_from_spec)
```
```
## A tmle3_Fit that took 1 step(s)
## param init_est tmle_est se lower upper
## 1: E[Y_{A=0}] 0.6647247 0.6529347 0.43087967 -0.1915739 1.4974434
## 2: E[Y_{A=1}] 0.5310694 0.5280384 0.01462398 0.4993759 0.5567009
## psi_transformed lower_transformed upper_transformed
## 1: 0.6529347 -0.1915739 1.4974434
## 2: 0.5280384 0.4993759 0.5567009
```
---
# Summary
* We have tried to explicitly model all the steps of the roadmap, including all
the different components necessary to specify and implement TMLE methods.
* We believe this results in a general framework that can implement most, if not
all, of the TMLE methods being developed.
* Much more work needs to be done on both the framework and building out the
various TMLE methods on top of the framework.
* Success will depend on engagement with the broader Targeted Learning community
-- We'd appreciate any feedback or contributions you can offer!
</textarea>
<script src="libs/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "zenburn",
"highlightLines": true,
"navigation": {
"scroll": false
}
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function() {
var d = document, s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})();</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
}
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cdn.bootcss.com/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>