-
Notifications
You must be signed in to change notification settings - Fork 0
/
683-final.Rmd
380 lines (279 loc) · 9.81 KB
/
683-final.Rmd
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
---
title: "683 Final project"
author: "Ariane Stark, Minsu Kim, Diezhang Wu, Alona Muzikansky"
date: "11/6/2021"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
library(tidyverse)
library(SuperLearner)
library(ltmle)
library(MASS)
library(dplyr)
library(kableExtra)
library(randomForest)
knitr::opts_chunk$set(echo = TRUE)
set.seed(123)
```
```{r include=FALSE}
data <-read.csv("fertility_sperm.csv")
colnames(data)
data <- data %>%
mutate(W11 = age,
W12 = sitting_hour,
W13 = trauma,
W14 = case_when(alcohol<0.8 ~ '2',
alcohol==0.8 ~ '1',
alcohol==1 ~ '0'),
W2 = case_when(season==-1 ~ 'Fall/Winter',
season==-0.33 ~'Spring/Summer',
season==0.33 ~ 'Spring/Summer',
season==1 ~ 'Fall/Winter'),
A = case_when(smoking==-1 ~ 0,
smoking==0 ~ 1,
smoking==1 ~ 1),
Y = ifelse(diagnosis=='N',0,1))
ObsData <- data %>% dplyr::select(W11, W12, W13, W14, W2, A, Y)
```
```{r warning=FALSE}
set.seed(123)
# simple substitution estimator (a.k.a. parameteric G-computation)
txt <- ObsData
control <- ObsData
txt$A <- 1
control$A <- 0
g.comp.reg <- glm(Y ~ W11 + W12 + W13 + W14 + W2 + A, family="binomial", data=ObsData)
pred.txt <- predict(g.comp.reg, newdata = txt, type = "response")
pred.control <- predict(g.comp.reg, newdata = control, type = "response")
psi.hat <- mean(pred.txt - pred.control)
psi.hat
# IPTW estimator
prob.AW.reg <- glm(A ~ W11 + W12 + W13 + W14, family="binomial", data=ObsData)
prob.1W <- predict(prob.AW.reg, type= "response")
prob.0W <- 1 - prob.1W
hist(prob.1W)
summary(prob.1W)
hist(prob.0W)
summary(prob.0W)
wt1 <- as.numeric(ObsData$A==1)/prob.1W
wt0 <- as.numeric(ObsData$A==0)/prob.0W
summary(wt1)
hist(wt1)
summary(wt0)
hist(wt0)
psi.iptw <- mean(wt1*ObsData$Y) - mean(wt0*ObsData$Y)
psi.iptw
# Modified HT
psi.ht <- mean(wt1*ObsData$Y)/mean(wt1) - mean(wt0*ObsData$Y)/mean(wt0)
psi.ht
# Unadjusted estimator
wt1.ua <- as.numeric(ObsData$A==1)/mean(ObsData$A == 1)
wt0.ua <- as.numeric(ObsData$A==0)/mean(ObsData$A == 0)
psi.unadj <- mean(wt1.ua*ObsData$Y) - mean(wt0.ua*ObsData$Y)
psi.unadj
# TMLE estimator
```
## SS, IPTW and TMLE estimator with super learner
```{r warning=FALSE}
library("SuperLearner")
SL.library<- c('SL.glm', 'SL.glm.interaction', "SL.step",
"SL.randomForest","SL.step.forward","SL.stepAIC","SL.mean")
```
```{r}
run.tmle <- function(ObsData, SL.library){
#------------------------------------------
# Simple substitution estimator
#------------------------------------------
# dataframe X with baseline covariates and exposure
X <- subset(ObsData, select=c(A, W11, W12, W13, W14,W2))
# set the exposure=1 in X1 and the exposure=0 in X0
X1 <- X0 <- X
X1$A <- 1
X0$A <- 0
# Estimate E_0(Y|A,W) with Super Learner
SL.outcome <- SuperLearner(Y=ObsData$Y, X=X, SL.library=SL.library,
family="binomial", cvControl=list(V=10))
# get the expected outcome, given the observed exposure and covariates
expY.givenAW <- predict(SL.outcome, newdata=ObsData)$pred
# expected outcome, given A=1 and covariates
expY.given1W <- predict(SL.outcome, newdata=X1)$pred
# expected outcome, given A=0 and covariates
expY.given0W <- predict(SL.outcome, newdata=X0)$pred
# simple substitution estimator would be
PsiHat.SS <- mean(expY.given1W - expY.given0W)
#------------------------------------------
# Inverse probability of txt weighting
#------------------------------------------
# Super Learner for the exposure mechanism P_0(A=1|W)
SL.exposure <- SuperLearner(Y=ObsData$A,
X=subset(ObsData, select= -c(A,Y,W2)),
SL.library=SL.library, family="binomial",
cvControl=list(V=10, stratifyCV = TRUE))
# generate the predicted prob of being exposed, given baseline cov
probA1.givenW <- SL.exposure$SL.predict
# generate the predicted prob of not being exposed, given baseline cov
probA0.givenW <- 1- probA1.givenW
# clever covariate
H.AW <- as.numeric(ObsData$A==1)/probA1.givenW - as.numeric(ObsData$A==0)/probA0.givenW
# also want to evaluate the clever covariate at A=1 and A=0 for all participants
H.1W <- 1/probA1.givenW
H.0W <- -1/probA0.givenW
# IPTW estimate
PsiHat.IPTW <- mean(H.AW*ObsData$Y, na.rm = TRUE)
#------------------------------------------
# Targeting & TMLE
#------------------------------------------
# Update the initial estimator of E_0(Y|A,W)
# run logistic regression of Y on H.AW using the logit of the esimates as offset
expY.givenAW <- expY.givenAW - 0.000001
logitUpdate<- glm( ObsData$Y ~ -1 +offset(qlogis(expY.givenAW)) +
H.AW, family='binomial')
epsilon <- logitUpdate$coef
# obtain the targeted estimates
expY.givenAW.star<- plogis( qlogis(expY.givenAW)+ epsilon*H.AW )
expY.given1W.star<- plogis( qlogis(expY.given1W)+ epsilon*H.1W )
expY.given0W.star<- plogis( qlogis(expY.given0W)+ epsilon*H.0W )
# TMLE point estimate
PsiHat.TMLE<- mean(expY.given1W.star - expY.given0W.star)
#------------------------------------------
# Return a list withthe point estimates, targeted estimates of E_0(Y|A,W),
# and the vector of clever covariates
#------------------------------------------
estimates <- data.frame(cbind(PsiHat.SS=PsiHat.SS, PsiHat.IPTW, PsiHat.TMLE))
predictions <- data.frame(cbind(expY.givenAW.star, expY.given1W.star, expY.given0W.star))
colnames(predictions) <- c('givenAW', 'given1W', 'given0W')
list(estimates=estimates, predictions=predictions, H.AW=H.AW, probA1.givenW=probA1.givenW, probA0.givenW=probA1.givenW)
}
```
```{r}
set.seed(123)
out <- run.tmle(ObsData = ObsData, SL.library = SL.library)
est <- out$estimates
est
```
# CV Superlearner
```{r message=FALSE, warning=FALSE}
X<- subset(ObsData, select= -Y )
CV.SL.out<- CV.SuperLearner(Y=ObsData$Y, X=X,
SL.library=SL.library, family='binomial',
cvControl = list(V = 5),
innerCvControl = list(list(V =20)))
summary(CV.SL.out)
```
# Influence Curve
```{r}
n <- nrow(ObsData)
# clever covariate
H.AW <- out$H.AW
# targeted predictions
expY.AW.star <- out$predictions[,'givenAW']
expY.1W.star <- out$predictions[,'given1W']
expY.0W.star <- out$predictions[,'given0W']
# point estimate
PsiHat.TMLE <- est$PsiHat.TMLE
# plug-in
IC <- H.AW*(ObsData$Y - expY.AW.star) + expY.1W.star - expY.0W.star - PsiHat.TMLE
summary(IC)
hist(IC)
# estimate sigma^2 with the variance of the IC divided by n
varHat.IC <- var(IC)/n
varHat.IC
# standard error estimate
se <- sqrt(varHat.IC)
se
##### TMLE
# obtain 95% two-sided confidence intervals TMLE:
alpha <- 0.05
round(c(PsiHat.TMLE+qnorm(alpha/2, lower.tail=T)*se,
PsiHat.TMLE+qnorm(alpha/2, lower.tail=F)*se),4)
# calculate the pvalue tmle
2* pnorm( abs(PsiHat.TMLE /se), lower.tail=F )
####### IPTW
PsiHat.IPTW <- est$PsiHat.IPTW
# obtain 95% two-sided confidence intervals:
round(c(PsiHat.IPTW+qnorm(alpha/2, lower.tail=T)*se,
PsiHat.IPTW+qnorm(alpha/2, lower.tail=F)*se),4)
# calculate the pvalue
2* pnorm( abs(PsiHat.IPTW /se), lower.tail=F )
####### SS
PsiHat.SS <- est$PsiHat.SS
# obtain 95% two-sided confidence intervals:
round(c(PsiHat.SS+qnorm(alpha/2, lower.tail=T)*se,
PsiHat.SS+qnorm(alpha/2, lower.tail=F)*se),4)
# calculate the pvalue
2* pnorm( abs(PsiHat.SS /se), lower.tail=F )
```
# Non-parametric bootstrap
```{r}
load('boot_par.Rdata')
colnames(estimates)<-c("SimpSubs", "IPTW", "TMLE")
summary(estimates)
```
```{r}
ggplot(mapping = aes(estimates[,1]))+
geom_histogram(fill="dark green",bins = 25)+
xlab("Point Estimates")+
ylab("Freq")+
labs(title="Simple Substitution Estimator",
subtitle = "500 Bootstrap Samples")+
theme(plot.title = element_text(colour = "red"))
ggplot(mapping = aes(estimates[,2]))+
geom_histogram(fill="dark green",bins = 25)+
xlab("Point Estimates")+
ylab("Freq")+
labs(title="IPTW Estimator",
subtitle = "500 Bootstrap Samples")+
theme(plot.title = element_text(colour = "red"))
ggplot(mapping = aes(estimates[,3]))+
geom_histogram(fill="dark green",bins = 25)+
xlab("Point Estimates")+
ylab("Freq")+
labs(title="TMLE Estimator",
subtitle = "500 Bootstrap Samples")+
theme(plot.title = element_text(colour = "red"))
```
```{r}
ggplot(mapping = aes(out$probA1.givenW))+
geom_histogram(fill="red3",bins = 25)+
xlab("Probability")+
ylab("Freq")+
labs(title="Propensity Score A=1")+
theme(plot.title = element_text(colour = "dark green"))+
xlim(0,1)
weights1 <- as.numeric(ObsData$A==1)/out$probA1.givenW
summary(weights1)
```
```{r}
#---------------------------------
# 95% Confidence intervals assuming a normal dist & via quantiles
#---------------------------------
create.CI <- function(pt, boot, alpha=0.05){
Zquant <- qnorm(alpha/2, lower.tail=F)
CI.normal <- c(pt - Zquant*sd(boot,na.rm = TRUE),
pt + Zquant*sd(boot,na.rm = TRUE) )
CI.quant <- quantile(boot, prob=c(0.025,0.975) ,na.rm=TRUE)
out <- data.frame(rbind(CI.normal, CI.quant))
colnames(out) <- c('CI.lo', 'CI.hi')
out
}
```
```{r}
# IMPORTANT - POINT OF CONFUSION FOR PAST STUDENTS
# The point estimate 'pt' is from the original dataset
# Simple Subs - note the bias because of misspecified regression? Will it converge fast enough?
est$PsiHat.SS
create.CI(pt=est$PsiHat.SS, boot=estimates[,"SimpSubs"])
# IPTW
est$PsiHat.IPTW
create.CI(pt=est$PsiHat.IPTW, boot=estimates[,"IPTW"])
# TMLE
est$PsiHat.TMLE
create.CI(pt=est$PsiHat.TMLE, boot=estimates[,"TMLE"])
```
```{r}
# Compare to IC estimate
c(PsiHat.TMLE+qnorm(alpha/2, lower.tail=T)*se,
PsiHat.TMLE+qnorm(alpha/2, lower.tail=F)*se)
```