-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpDoseResponseDR.py
More file actions
454 lines (369 loc) · 18.6 KB
/
Copy pathnpDoseResponseDR.py
File metadata and controls
454 lines (369 loc) · 18.6 KB
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
# -*- coding: utf-8 -*-
# Author: Yikun Zhang
# Last Editing: Dec 22, 2024
# Description: This script contains the implementations of the regression
# adjustment (RA), inverse probability weighting (IPW), and doubly robust (DR)
# estimators of the dose-response curve under the positivity condition.
import numpy as np
from rbf import KernelRetrieval
from utils1 import CondDenEst, CondDenEstKDE
from sklearn.model_selection import KFold
#=======================================================================================#
def RegAdjustDR(Y, X, t_eval, mu, L=1, multi_boot=False, B=1000):
'''
Estimating the dose-response curve through the regression adjustment
(or G-computation) form.
Parameters
----------
Y: (n,)-array
The outcome variables of n observations.
X: (n,d+1)-array
The first column of X is the treatment/exposure variable, while
the other d columns are the confounding variables of n observations.
t_eval: (m,)-array
The coordinates of the m evaluation points.
mu: scikit-learn model or any python model that can use ".fit()" and ".predict()"
The conditional mean outcome (or regression) model of Y given X.
L: int
The number of data folds for cross-fitting. When L<= 1, no cross-fittings
are applied and the regression model is fitted on the entire dataset.
(Default: L=1.)
multi_boot: boolean
An indicator of whether the multiplier bootstrap will be run.
(Default: multi_boot=False.)
B: int
The number of bootstrapping times. (Default: B=1000.)
Return
----------
m_est: (m,)-array
The estimated dose-response curve evaluated at points "t_eval".
mu_boot: (B,m)-array
The estimated dose-response curves on bootstrapping data evaluated
at points "t_eval". (Only return this quantity when "multi_boot=True".)
'''
n = X.shape[0] ## Number of data points
if L <= 1:
# No cross-fittings: fit the regression model on the entire data
mu_hat = mu.fit(X, Y)
mu_est = np.zeros((n, t_eval.shape[0]))
for i in range(t_eval.shape[0]):
# Define the data matrix for evaluating the fitted regression model
X_eval = np.column_stack([t_eval[i]*np.ones(n), X[:,1:]])
mu_est[:,i] = mu_hat.predict(X_eval)
else:
# Conduct L-fold cross-fittings: fit the regression model on the training fold data
# and evaluate it on the test fold data
kf = KFold(n_splits=L, shuffle=True, random_state=0)
mu_est = np.zeros((n, t_eval.shape[0]))
for tr_ind, te_ind in kf.split(X):
X_tr = X[tr_ind,:]
Y_tr = Y[tr_ind]
X_te = X[te_ind,:]
mu_hat = mu.fit(X_tr, Y_tr)
for i in range(t_eval.shape[0]):
X_eval_te = np.column_stack([t_eval[i]*np.ones(X_te.shape[0]), X_te[:,1:]])
mu_est[te_ind,i] = mu_hat.predict(X_eval_te)
if multi_boot:
mu_boot = np.zeros((B, t_eval.shape[0]))
for b in range(B):
Z = np.random.randn(n, t_eval.shape[0]) + 1
mu_boot[b,:] = np.mean(Z * mu_est, axis=0)
m_est = np.mean(mu_est, axis=0)
return m_est, mu_boot
else:
m_est = np.mean(mu_est, axis=0)
return m_est
def IPWDR(Y, X, t_eval, condTS_type, condTS_mod, L, h, kern="epanechnikov",
tau=0.001, b=None, self_norm=True):
'''
Estimating the dose-response curve through the inverse probability weighting
(IPW) form.
Parameters
----------
Y: (n,)-array
The outcome variables of n observations.
X: (n,d+1)-array
The first column of X is the treatment/exposure variable, while
the other d columns are the confounding variables of n observations.
t_eval: (m,)-array
The coordinates of the m evaluation points.
condTS_type: str
Specifying the model type for estimating the conditional density of
the treatment variable T given the covariate vector S.
condTS_mod: scikit-learn model or any python model that can use ".fit()" and ".predict()"
The regression model for estimating the conditional density of T given S.
L: int
The number of data folds for cross-fitting. When L<= 1, no cross-fittings
are applied and the regression model is fitted on the entire dataset.
(Default: L=1.)
h: float
The bandwidth parameter.
kern: str
The name of the kernel function. (Default: kern="epanechnikov".)
tau: float
The threshold value that lower bounds the estimated conditional density
values. (Default: tau=0.001.)
b: float
The bandwidth parameter for the kernel-smoothed conditional density
estimation methods. (Default: b=None.)
self_norm: boolean
An indicator of whether the self-normalized version is implemented.
(Default: self_norm=True.)
Return
----------
m_est: (m,)-array
The estimated dose-response curve evaluated at points "t_eval".
cond_est_full: (n,)-array
The estimated conditional density function of T given S evaluated at
the n observed data points.
'''
kern_type = kern
kern, sigmaK_sq, K_sq = KernelRetrieval(kern_type)
n = X.shape[0] ## Number of data points
if L <= 1:
# No cross-fittings: fit the conditional density model on the entire data
if condTS_type == 'true':
condTS_est = condTS_mod
elif condTS_type == 'kde':
condTS_est = CondDenEstKDE(X[:,0], X[:,1:], reg_mod=condTS_mod, y_eval=X[:,0],
x_eval=X[:,1:], kern=kern_type, b=b)
else:
condTS_est = CondDenEst(X[:,0], X[:,1:], reg_mod=condTS_mod, y_eval=X[:,0],
x_eval=X[:,1:], kern='gaussian', b=b)
condTS_est[condTS_est < tau] = tau
m_hat = np.zeros((n, t_eval.shape[0]))
norm_w = np.zeros((t_eval.shape[0],))
for i in range(t_eval.shape[0]):
# Self-normalizing weights
norm_w[i] = np.sum(kern((t_eval[i] - X[:,0])/h) / condTS_est) / h
m_hat[:,i] = kern((t_eval[i] - X[:,0])/h) * Y / (h * condTS_est)
if self_norm:
# Self-normalized IPW estimator
m_hat = m_hat / norm_w
m_est = np.sum(m_hat, axis=0, where=~np.isnan(m_hat))
else:
m_est = np.mean(m_hat, axis=0, where=~np.isnan(m_hat))
cond_est_full = condTS_est.copy()
else:
# Conduct L-fold cross-fittings: fit the conditional density model on the training fold
# data and evaluate it on the test fold data
kf = KFold(n_splits=L, shuffle=True, random_state=0)
m_hat = np.zeros((n, t_eval.shape[0]))
norm_w = np.zeros((t_eval.shape[0],))
cond_est_full = np.zeros((n,))
for tr_ind, te_ind in kf.split(X):
X_tr = X[tr_ind,:]
X_te = X[te_ind,:]
Y_te = Y[te_ind]
if condTS_type == 'true':
condTS_est = condTS_mod[te_ind]
elif condTS_type == 'kde':
condTS_est = CondDenEstKDE(X_tr[:,0], X_tr[:,1:], reg_mod=condTS_mod,
y_eval=X_te[:,0], x_eval=X_te[:,1:], kern=kern_type, b=b)
else:
condTS_est = CondDenEst(X_tr[:,0], X_tr[:,1:], reg_mod=condTS_mod,
y_eval=X_te[:,0], x_eval=X_te[:,1:], kern='gaussian', b=b)
condTS_est[condTS_est < tau] = tau
cond_est_full[te_ind] = condTS_est
for i in range(t_eval.shape[0]):
# Self-normalizing weights
w = np.sum(kern((t_eval[i] - X[te_ind,0])/h) / condTS_est) / h
norm_w[i] = norm_w[i] + w
m_hat[te_ind,i] = kern((t_eval[i] - X[te_ind,0])/h) * Y_te / (h * condTS_est)
if self_norm:
norm_w[norm_w == 0] = 1
m_est = np.sum(m_hat, axis=0, where=~np.isnan(m_hat)) / norm_w
else:
m_est = np.mean(m_hat, axis=0, where=~np.isnan(m_hat))
return m_est, cond_est_full
def DRDR(Y, X, t_eval, mu, condTS_type, condTS_mod, L, h, kern="epanechnikov",
tau=0.001, b=None, self_norm=True):
'''
Estimating the dose-response curve through the doubly robust (DR) form.
Parameters
----------
Y: (n,)-array
The outcome variables of n observations.
X: (n,d+1)-array
The first column of X is the treatment/exposure variable, while
the other d columns are the confounding variables of n observations.
t_eval: (m,)-array
The coordinates of the m evaluation points.
mu: scikit-learn model or any python model that can use ".fit()" and ".predict()"
The conditional mean outcome (or regression) model of Y given X.
condTS_type: str
Specifying the model type for estimating the conditional density of
the treatment variable T given the covariate vector S.
condTS_mod: scikit-learn model or any python model that can use ".fit()" and ".predict()"
The regression model for estimating the conditional density of T given S.
L: int
The number of data folds for cross-fitting. When L<= 1, no cross-fittings
are applied and the regression model is fitted on the entire dataset.
h: float
The bandwidth parameter.
kern: str
The name of the kernel function. (Default: kern="epanechnikov".)
tau: float
The threshold value that lower bounds the estimated conditional density
values. (Default: tau=0.001.)
b: float
The bandwidth parameter for the kernel-smoothed conditional density
estimation methods. (Default: b=None.)
self_norm: boolean
An indicator of whether the self-normalized version is implemented.
(Default: self_norm=True.)
Return
----------
m_est: (m,)-array
The estimated dose-response curve evaluated at points "t_eval".
sd_est: (m,)-array
The estimated asymptotic stdndard deviation of the DR estimator
evaluated at points "t_eval".
'''
kern_type = kern
kern, sigmaK_sq, K_sq = KernelRetrieval(kern)
n = X.shape[0] ## Number of data points
if L <= 1:
# No cross-fittings: fit the conditional density model and the regression model on the entire data
if condTS_type == 'true':
condTS_est = condTS_mod
elif condTS_type == 'kde':
condTS_est = CondDenEstKDE(X[:,0], X[:,1:], reg_mod=condTS_mod,
y_eval=X[:,0], x_eval=X[:,1:], kern=kern_type, b=b)
else:
condTS_est = CondDenEst(X[:,0], X[:,1:], reg_mod=condTS_mod, y_eval=X[:,0],
x_eval=X[:,1:], kern='gaussian', b=b)
condTS_est[condTS_est < tau] = tau
mu_fit = mu.fit(X, Y)
mu_hat = np.zeros((n, t_eval.shape[0]))
IPW_hat = np.zeros((n, t_eval.shape[0]))
norm_w = np.zeros((t_eval.shape[0],))
for i in range(t_eval.shape[0]):
# Define the data matrix for evaluating the fitted regression model
X_eval = np.column_stack([t_eval[i]*np.ones(n), X[:,1:]])
mu_hat[:,i] = mu_fit.predict(X_eval)
IPW_hat[:,i] = kern((t_eval[i] - X[:,0])/h) * (Y - mu_hat[:,i]) / (h * condTS_est)
# Self-normalizing weights
norm_w[i] = np.sum(kern((t_eval[i] - X[:,0])/h) / condTS_est) / (n * h)
if self_norm:
IPW_hat = IPW_hat / norm_w
# Add up the IPW and RA components
m_hat = IPW_hat + mu_hat
m_est = np.mean(m_hat, axis=0, where=~np.isnan(m_hat))
# Estimate the variance of m(t) using the square of the influence function
var_est = np.zeros((n, t_eval.shape[0]))
for i in range(t_eval.shape[0]):
var_est[:,i] = (IPW_hat[:,i] + (mu_hat[:,i] - m_est[i]))**2 * h
sd_est = np.sqrt(np.mean(var_est, axis=0)/(n*h))
else:
# Conduct L-fold cross-fittings: fit the reciprocal of the conditional model
# and the regression model on the training fold data and evaluate it on the test fold data
kf = KFold(n_splits=L, shuffle=True, random_state=0)
mu_hat = np.zeros((n, t_eval.shape[0]))
IPW_hat = np.zeros((n, t_eval.shape[0]))
norm_w = np.zeros((t_eval.shape[0],))
cond_est_full = np.zeros((n,))
for tr_ind, te_ind in kf.split(X):
X_tr = X[tr_ind,:]
Y_tr = Y[tr_ind]
X_te = X[te_ind,:]
Y_te = Y[te_ind]
if condTS_type == 'true':
condTS_est = condTS_mod[te_ind]
elif condTS_type == 'kde':
condTS_est = CondDenEstKDE(X_tr[:,0], X_tr[:,1:], reg_mod=condTS_mod,
y_eval=X_te[:,0], x_eval=X_te[:,1:], kern=kern_type, b=b)
else:
condTS_est = CondDenEst(X_tr[:,0], X_tr[:,1:], reg_mod=condTS_mod,
y_eval=X_te[:,0], x_eval=X_te[:,1:], kern='gaussian', b=b)
condTS_est[condTS_est < tau] = tau
cond_est_full[te_ind] = condTS_est
mu_fit = mu.fit(X_tr, Y_tr)
for i in range(t_eval.shape[0]):
X_eval_te = np.column_stack([t_eval[i]*np.ones(X_te.shape[0]), X_te[:,1:]])
mu_hat[te_ind,i] = mu_fit.predict(X_eval_te)
IPW_hat[te_ind,i] = kern((t_eval[i] - X[te_ind,0])/h) * (Y_te - mu_hat[te_ind,i]) / (h * condTS_est)
# Self-normalizing weights
w = np.sum(kern((t_eval[i] - X[te_ind,0])/h) / condTS_est) / (n * h)
norm_w[i] = norm_w[i] + w
if self_norm:
IPW_hat = IPW_hat / norm_w
# Add up the IPW and RA components
m_hat = IPW_hat + mu_hat
m_est = np.mean(m_hat, axis=0, where=~np.isnan(m_hat))
# Estimate the variance of m(t) using the square of the influence function
var_est = np.zeros((n, t_eval.shape[0]))
for i in range(t_eval.shape[0]):
var_est[:,i] = (IPW_hat[:,i] + (mu_hat[:,i] - m_est[i]))**2 * h
sd_est = np.sqrt(np.mean(var_est, axis=0)/(n*h))
return m_est, sd_est
def DRCurve(Y, X, t_eval=None, est="RA", mu=None, condTS_type=None, condTS_mod=None,
L=1, h=None, kern="epanechnikov", tau=0.001, h_cond=None, self_norm=True,
print_bw=True):
'''
Dose-response curve estimation under the positivity condition.
Parameters
----------
Y: (n,)-array
The outcome variables of n observations.
X: (n,d+1)-array
The first column of X is the treatment/exposure variable, while
the other d columns are confounding variables of n observations.
t_eval: (m,)-array
The coordinates of the m evaluation points. (Default: t_eval=None.
Then, t_eval=X[:,0], which consists of the observed treatment variables.)
est: str
The type of the dose-response curve estimator. (Default: est="RA".
Other choices include "IPW" and "DR".)
mu: scikit-learn model or any python model that can use ".fit()" and ".predict()"
The conditional mean outcome (or regression) model of Y given X.
condTS_type: str
Specifying the model type for estimating the conditional density of
the treatment variable T given the covariate vector S.
condTS_mod: scikit-learn model or any python model that can use ".fit()" and ".predict()"
The regression model for estimating the conditional density of T given S.
L: int
The number of data folds for cross-fitting. When L<= 1, no cross-fittings
are applied and the regression model is fitted on the entire dataset.
h: float
The bandwidth parameter for the IPW/DR estimator. (Default: h=None.
Then the Silverman's rule of thumb is applied; see Chen et al.(2016)
for details.)
kern: str
The name of the kernel function. (Default: kern="epanechnikov".)
tau: float
The threshold value that lower bounds the estimated conditional density
values. (Default: tau=0.001.)
h_cond: float
The bandwidth parameter for the kernel-smoothed conditional density
estimation methods. (Default: b=None.)
self_norm: boolean
An indicator of whether the self-normalized version is implemented.
(Default: self_norm=True.)
print_bw: boolean
The indicator of whether the current bandwidth parameters should be
printed to the console. (Default: print_bw=True.)
Return
----------
m_est: (m,)-array
The estimated dose-response curve evaluated at points "t_eval".
sd_est: (m,)-array (if est="DR")
The estimated asymptotic standard deviation of the DR estimator
evaluated at points "t_eval".
'''
if t_eval is None:
t_eval = X[:,0].copy()
n = X.shape[0] ## Number of data points
if h is None:
# Apply Silverman's rule of thumb to select the bandwidth parameter
h = (4/3)**(1/5)*(n**(-1/5))*np.std(X[:,0])
if print_bw:
print("The current bandwidth for the "+str(est)+" estimator is "+ str(h) + ".\n")
if est == "RA":
m_est = RegAdjustDR(Y, X, t_eval, mu, L)
elif est == "IPW":
m_est, cond_est = IPWDR(Y, X, t_eval, condTS_type, condTS_mod, L, h, kern, tau, h_cond,
self_norm)
else:
m_est = DRDR(Y, X, t_eval, mu, condTS_type, condTS_mod, L, h, kern, tau, h_cond, self_norm)
return m_est