-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathestim.R
321 lines (282 loc) · 9.34 KB
/
estim.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
#' Stochastic Mediation Effect of Contrasts Under Intermediate Confounding
#'
#' @param data ...
#' @param contrast ...
#' @param weights ...
#' @param candidatesg ...
#' @param candidatese ...
#' @param candidatesm ...
#' @param candidatesq ...
#' @param candidatesr ...
#' @param candidatesu ...
#' @param candidatesv ...
#' @param nfolds ...
#' @param family.outcome ...
#'
#' @importFrom stats glm coef offset predict var sd
#' @importFrom stats gaussian binomial plogis qlogis
#'
#' @export
estimatheta <- function(data, contrast, weights,
candidatesg,
candidatese,
candidatesm,
candidatesq,
candidatesr,
candidatesu,
candidatesv,
nfolds,
family.outcome) {
# contrasts
aprime <- contrast[1]
astar <- contrast[2]
# extract data
A <- data[, "A"]
M <- data[, substr(names(data), 1, 1) == "M"]
Z <- data[, substr(names(data), 1, 1) == "Z"]
Y <- data[, "Y"]
W <- data[, substr(names(data), 1, 1) == "W"]
# observations and cross-validation
n <- length(A)
valSets <- split(sample(seq_len(n)), rep(seq_len(nfolds), length = n))
# fit nuisance functions
fitg <- mySL(
Y = A,
X = W,
family = stats::binomial(),
SL.library = candidatesg,
validRows = valSets
)
fite <- mySL(
Y = A,
X = data.frame(W, M),
family = stats::binomial(),
SL.library = candidatese,
validRows = valSets
)
fitm <- mySL(
Y = Y,
X = data.frame(W, M, Z, A),
family = family.outcome,
SL.library = candidatesm,
validRows = valSets
)
fitq <- mySL(
Y = Z,
X = data.frame(W, A),
family = stats::binomial(),
SL.library = candidatesq,
validRows = valSets
)
fitr <- mySL(
Y = Z,
X = data.frame(W, M, A),
family = stats::binomial(),
SL.library = candidatesr,
validRows = valSets
)
# compute u pseudo outcome and fit u function
gone <- fitg$SL.predict[, 1]
eone <- fite$SL.predict[, 1]
gprime <- gone * aprime + (1 - gone) * (1 - aprime)
gstar <- gone * astar + (1 - gone) * (1 - astar)
eprime <- eone * aprime + (1 - eone) * (1 - aprime)
estar <- eone * astar + (1 - eone) * (1 - astar)
qoneprime <- stats::predict(fitq,
newdata = data.frame(W, A = aprime))$pred[, 1]
roneprime <- stats::predict(fitr,
newdata = data.frame(W, M, A = aprime))$pred[, 1]
qprime <- Z * qoneprime + (1 - Z) * (1 - qoneprime)
rprime <- Z * roneprime + (1 - Z) * (1 - roneprime)
mprime <- stats::predict(fitm,
newdata = data.frame(W, M, Z, A = aprime))$pred[, 1]
hstar <- gprime / gstar * qprime / rprime * estar / eprime
upseudo <- mprime * hstar
# in case values are all very close to each other
if (sd(upseudo) < .Machine$double.eps) {
candidatesu <- c("SL.mean")
}
# predict on estimated u
fitu <- mySL(
Y = upseudo,
X = data.frame(W, Z, A),
family = stats::gaussian(),
SL.library = candidatesu,
validRows = valSets
)
uprimeone <-
stats::predict(fitu, newdata = data.frame(W, Z = 1, A = aprime))$pred[, 1]
uprimezero <-
stats::predict(fitu, newdata = data.frame(W, Z = 0, A = aprime))$pred[, 1]
uprime <- Z * uprimeone + (1 - Z) * uprimezero
# compute v pseudo outcome and fit v function
mprimeone <-
stats::predict(fitm,
newdata = data.frame(W, M, Z = 1, A = aprime))$pred[, 1]
mprimezero <-
stats::predict(fitm,
newdata = data.frame(W, M, Z = 0, A = aprime))$pred[, 1]
# build v nuisance function
vpseudo <- mprimeone * qoneprime + mprimezero * (1 - qoneprime)
if (stats::sd(vpseudo) < .Machine$double.eps) {
candidatesv <- c("SL.mean")
}
# fit nuisance function v
fitv <- mySL(
Y = vpseudo,
X = data.frame(W, A),
family = stats::gaussian(),
SL.library = candidatesv,
validRows = valSets
)
vstar <- stats::predict(fitv, newdata = data.frame(W, A = astar))$pred[, 1]
# compute one step
ipwprime <- as.numeric(A == aprime) / gprime
ipwstar <- as.numeric(A == astar) / gstar
# compute EIF components
eify <- ipwprime * hstar / mean(ipwprime * hstar) * (Y - mprime)
eifu <- ipwprime / mean(ipwprime) * (uprimeone - uprimezero) *
(Z - qoneprime)
eifv <- ipwstar / mean(ipwstar) * (vpseudo - vstar)
eif <- weights * (eify + eifu + eifv + vstar)
os <- mean(eif)
eifos <- eif
# now, compute the TMLE
stopcrit <- FALSE
iter <- 1
# iterative TMLE
while (!stopcrit) {
# compute contrast difference for u
uprimediff <- (uprimeone - uprimezero)
# evaluate h' under various contrasts
hstar <- gprime / gstar * qprime / rprime * estar / eprime
hstarone <- gprime / gstar * qoneprime / roneprime * estar / eprime
hstarzero <- gprime / gstar * (1 - qoneprime) / (1 - roneprime) *
estar / eprime
# first fluctuation/tilting
suppressWarnings(
tiltm <- stats::glm(
as.formula("Y ~ -1 + offset(mprime_logit) + hstar"),
data = data.frame(list(Y = Y, A = A,
mprime_logit = stats::qlogis(mprime),
hstar = hstar)),
subset = A == aprime,
weights = weights / gprime,
family = stats::binomial()
)
)
# second fluctuation/tilting
suppressWarnings(
tiltq <- stats::glm(
stats::as.formula("Z ~ -1 + offset(qoneprime_logit) + uprimediff"),
data = data.frame(list(Z = Z, A = A,
qoneprime_logit = stats::qlogis(qoneprime),
uprimediff = uprimediff)),
subset = A == aprime,
weights = weights / gprime,
family = stats::binomial()
)
)
# extract epsilon and set to zero if failed fluctuation/tilting
coefq <- stats::coef(tiltq)
coefm <- stats::coef(tiltm)
if (is.na(coefq)) coefq <- 0
if (is.na(coefm)) coefm <- 0
mprime <- stats::plogis(stats::qlogis(mprime) + coefm * hstar)
mprimeone <- stats::plogis(stats::qlogis(mprimeone) + coefm * hstarone)
mprimezero <- stats::plogis(stats::qlogis(mprimezero) + coefm *
hstarzero)
qoneprime <- stats::plogis(stats::qlogis(qoneprime) + coefq *
uprimediff)
qprime <- Z * qoneprime + (1 - Z) * (1 - qoneprime)
# iterate iterator
iter <- iter + 1
# note: interesting stopping criterion
stopcrit <- max(abs(c(coefm, coefq))) < 0.001 / n^(0.6) | iter > 6
}
vpseudo <- mprimeone * qoneprime + mprimezero * (1 - qoneprime)
vstar[vstar < 1e-3] <- 1e-3
vstar[vstar > 1 - 1e-3] <- 1 - 1e-3
suppressWarnings(
tiltv <- stats::glm(
as.formula("vpseudo ~ offset(vstar_logit)"),
data = data.frame(list(vpseudo = vpseudo, A = A,
vstar_logit = stats::qlogis(vstar))),
subset = A == astar,
weights = weights / gstar,
family = stats::binomial()
)
)
vstar <- stats::plogis(stats::qlogis(vstar) + stats::coef(tiltv))
qprime <- Z * qoneprime + (1 - Z) * (1 - qoneprime)
hstar <- gprime / gstar * qprime / rprime * estar / eprime
upseudo <- mprime * hstar
vpseudo <- mprimeone * qoneprime + mprimezero * (1 - qoneprime)
ipwprime <- as.numeric(A == aprime) / gprime
ipwstar <- as.numeric(A == astar) / gstar
eify <- ipwprime * hstar / mean(ipwprime * hstar) * (Y - mprime)
eifu <- ipwprime / mean(ipwprime) * (uprimeone - uprimezero) *
(Z - qoneprime)
eifv <- ipwstar / mean(ipwstar) * (vpseudo - vstar)
eif <- weights * (eify + eifu + eifv + vstar)
tmle <- mean(eif)
eiftmle <- weights * (eify + eifu + eifv + vstar)
return(list(
estimates = c(os = os, tmle = tmle),
eifs = cbind(os = eifos, tmle = eiftmle)
))
}
#' Stochastic Direct/Indirect Effects Under Intermediate Confounding
#'
#' @param data ...
#' @param weights ...
#' @param candidatesg ...
#' @param candidatese ...
#' @param candidatesm ...
#' @param candidatesq ...
#' @param candidatesr ...
#' @param candidatesu ...
#' @param candidatesv ...
#' @param nfolds ...
#' @param family.outcome ...
#'
#' @importFrom stats var
#'
#' @export
mediation <- function(data, weights, candidatesg, candidatese,
candidatesm, candidatesq, candidatesr,
candidatesu, candidatesv, nfolds,
family.outcome) {
# preliminaries
n <- nrow(data)
# compute under different contrasts
theta11 <- estimatheta(data,
contrast = c(1, 1), weights,
candidatesg, candidatese, candidatesm,
candidatesq, candidatesr, candidatesu,
candidatesv, nfolds, family.outcome
)
theta10 <- estimatheta(data,
contrast = c(1, 0), weights,
candidatesg, candidatese, candidatesm,
candidatesq, candidatesr, candidatesu,
candidatesv, nfolds, family.outcome
)
theta00 <- estimatheta(data,
contrast = c(0, 0), weights,
candidatesg, candidatese, candidatesm,
candidatesq, candidatesr, candidatesu,
candidatesv, nfolds, family.outcome
)
# point estimates
indirect <- theta11$estimates - theta10$estimates
direct <- theta10$estimates - theta00$estimates
# standard error estimates
seindirect <- sqrt(diag(stats::var(theta11$eifs - theta10$eifs)) / n)
sedirect <- sqrt(diag(stats::var(theta10$eifs - theta00$eifs)) / n)
# output
return(list(
effects = rbind(indirect, direct),
ses = rbind(seindirect, sedirect)
))
}