pygformula 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pygformula might be problematic. Click here for more details.
- pygformula-0.0.1/LICENSE +21 -0
- pygformula-0.0.1/PKG-INFO +47 -0
- pygformula-0.0.1/README.md +38 -0
- pygformula-0.0.1/pygformula/__init__.py +1 -0
- pygformula-0.0.1/pygformula/comparisons.py +264 -0
- pygformula-0.0.1/pygformula/data.py +247 -0
- pygformula-0.0.1/pygformula/parametric_gformula/__init__.py +2 -0
- pygformula-0.0.1/pygformula/parametric_gformula/bootstrap.py +312 -0
- pygformula-0.0.1/pygformula/parametric_gformula/fit.py +471 -0
- pygformula-0.0.1/pygformula/parametric_gformula/histories.py +180 -0
- pygformula-0.0.1/pygformula/parametric_gformula/interventions.py +318 -0
- pygformula-0.0.1/pygformula/parametric_gformula/parametric_gformula.py +781 -0
- pygformula-0.0.1/pygformula/parametric_gformula/simulate.py +462 -0
- pygformula-0.0.1/pygformula/plot.py +429 -0
- pygformula-0.0.1/pygformula/utils/__init__.py +0 -0
- pygformula-0.0.1/pygformula/utils/helper.py +177 -0
- pygformula-0.0.1/pygformula/utils/util.py +714 -0
- pygformula-0.0.1/pygformula.egg-info/PKG-INFO +47 -0
- pygformula-0.0.1/pygformula.egg-info/SOURCES.txt +21 -0
- pygformula-0.0.1/pygformula.egg-info/dependency_links.txt +1 -0
- pygformula-0.0.1/pygformula.egg-info/top_level.txt +1 -0
- pygformula-0.0.1/setup.cfg +4 -0
- pygformula-0.0.1/setup.py +15 -0
pygformula-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 The President and Fellows of Harvard College
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pygformula
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A python implementation of the parametric g-formula
|
|
5
|
+
Maintainer: Jing Li
|
|
6
|
+
Maintainer-email: jing_li@hsph.harvard.edu
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
|
|
10
|
+
# pygformula: a python implementation of the parametric g-formula
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## Overview
|
|
15
|
+
The pygformula package implements the non-iterative conditional expectation (NICE) algorithm of the g-formula algorithm
|
|
16
|
+
(Robins, 1986). The g-formula can estimate the counterfactual mean or risk of an outcome under hypothetical treatment strategies
|
|
17
|
+
(interventions) when there is sufficient information on time-varying treatments and confounders.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* Treatments: discrete or continuous time-varying treatments.
|
|
23
|
+
* Outcomes: failure time outcomes or continuous/binary end of follow-up outcomes.
|
|
24
|
+
* Interventions: interventions on a single treatment or joint interventions on multiple treatments.
|
|
25
|
+
* Random measurement/visit process.
|
|
26
|
+
* Incorporation of a priori knowledge of the data structure.
|
|
27
|
+
* Censoring events.
|
|
28
|
+
* Competing events.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Requirements
|
|
32
|
+
|
|
33
|
+
The package requires python 3.8+ and these necessary dependencies:
|
|
34
|
+
|
|
35
|
+
- joblib
|
|
36
|
+
- lifelines
|
|
37
|
+
- matplotlib
|
|
38
|
+
- numpy
|
|
39
|
+
- pandas
|
|
40
|
+
- prettytable
|
|
41
|
+
- scipy
|
|
42
|
+
- statsmodels
|
|
43
|
+
- tqdm
|
|
44
|
+
|
|
45
|
+
and an optional dependency:
|
|
46
|
+
|
|
47
|
+
- cmprsk
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# pygformula: a python implementation of the parametric g-formula
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
The pygformula package implements the non-iterative conditional expectation (NICE) algorithm of the g-formula algorithm
|
|
7
|
+
(Robins, 1986). The g-formula can estimate the counterfactual mean or risk of an outcome under hypothetical treatment strategies
|
|
8
|
+
(interventions) when there is sufficient information on time-varying treatments and confounders.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* Treatments: discrete or continuous time-varying treatments.
|
|
14
|
+
* Outcomes: failure time outcomes or continuous/binary end of follow-up outcomes.
|
|
15
|
+
* Interventions: interventions on a single treatment or joint interventions on multiple treatments.
|
|
16
|
+
* Random measurement/visit process.
|
|
17
|
+
* Incorporation of a priori knowledge of the data structure.
|
|
18
|
+
* Censoring events.
|
|
19
|
+
* Competing events.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Requirements
|
|
23
|
+
|
|
24
|
+
The package requires python 3.8+ and these necessary dependencies:
|
|
25
|
+
|
|
26
|
+
- joblib
|
|
27
|
+
- lifelines
|
|
28
|
+
- matplotlib
|
|
29
|
+
- numpy
|
|
30
|
+
- pandas
|
|
31
|
+
- prettytable
|
|
32
|
+
- scipy
|
|
33
|
+
- statsmodels
|
|
34
|
+
- tqdm
|
|
35
|
+
|
|
36
|
+
and an optional dependency:
|
|
37
|
+
|
|
38
|
+
- cmprsk
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .parametric_gformula import ParametricGformula
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def comparison_calculate(obs_data, time_name, time_points, id, covnames, covtypes, outcome_name, outcome_type,
|
|
5
|
+
nc_pool, nc_risk, censor, censor_name, censor_fit, ipw_cutoff_quantile, ipw_cutoff_value,
|
|
6
|
+
competing=None, compevent_name=None, compevent_cens=False, compevent_fit=None):
|
|
7
|
+
"""
|
|
8
|
+
This is an internal function to calculate the mean observed values of covariates at each time point, as well as mean
|
|
9
|
+
observed risk.
|
|
10
|
+
|
|
11
|
+
Parameters
|
|
12
|
+
----------
|
|
13
|
+
obs_data: DataFrame
|
|
14
|
+
A data frame containing the observed data.
|
|
15
|
+
|
|
16
|
+
time_name: Str
|
|
17
|
+
A string specifying the name of the time variable in obs_data.
|
|
18
|
+
|
|
19
|
+
time_points: Int
|
|
20
|
+
An integer indicating the number of time points to simulate. It is set equal to the maximum number of records
|
|
21
|
+
that obs_data contains for any individual plus 1, if not specified by users.
|
|
22
|
+
|
|
23
|
+
id: Str
|
|
24
|
+
A string specifying the name of the id variable in obs_data.
|
|
25
|
+
|
|
26
|
+
covnames: List
|
|
27
|
+
A list of strings specifying the names of the time-varying covariates in obs_data.
|
|
28
|
+
|
|
29
|
+
covtypes: List
|
|
30
|
+
A list of strings specifying the “type” of each time-varying covariate included in covnames.
|
|
31
|
+
The supported types: "binary", "normal", "categorical", "bounded normal", "zero-inflated normal",
|
|
32
|
+
"truncated normal", "absorbing", "categorical time" "square time" and "custom". The list must be the same length
|
|
33
|
+
as covnames and in the same order.
|
|
34
|
+
|
|
35
|
+
outcome_name: Str
|
|
36
|
+
A string specifying the name of the outcome variable in obs_data.
|
|
37
|
+
|
|
38
|
+
outcome_type: Str
|
|
39
|
+
A string specifying the "type" of outcome. The possible "types" are: "survival", "continuous_eof", and "binary_eof".\
|
|
40
|
+
|
|
41
|
+
nc_pool: DataFrame
|
|
42
|
+
A dataframe of the simulated data under natural course.
|
|
43
|
+
|
|
44
|
+
nc_risk: List
|
|
45
|
+
A list contains the parametric risk of all the time points for natural course.
|
|
46
|
+
|
|
47
|
+
censor: Bool
|
|
48
|
+
A boolean value indicating the if there is a censoring event.
|
|
49
|
+
|
|
50
|
+
censor_name: Str
|
|
51
|
+
A string specifying the name of the censoring variable in obs_data. Only applicable when using inverse
|
|
52
|
+
probability weights to estimate the natural course means / risk from the observed data.
|
|
53
|
+
|
|
54
|
+
censor_fit: Class
|
|
55
|
+
A class object of the fitted model for the censoring event.
|
|
56
|
+
|
|
57
|
+
ipw_cutoff_quantile: Float
|
|
58
|
+
Percentile value for truncation of the inverse probability weights
|
|
59
|
+
|
|
60
|
+
ipw_cutoff_value: Float
|
|
61
|
+
Absolute value for truncation of the inverse probability weights.
|
|
62
|
+
|
|
63
|
+
competing: Bool
|
|
64
|
+
A boolean value indicating the if there is a competing event.
|
|
65
|
+
|
|
66
|
+
compevent_name: Str
|
|
67
|
+
A string specifying the name of the competing event variable in obs_data. Only applicable for survival outcomes.
|
|
68
|
+
|
|
69
|
+
compevent_cens: Bool
|
|
70
|
+
A boolean value indicating whether to treat competing events as censoring events.
|
|
71
|
+
|
|
72
|
+
compevent_fit: Class
|
|
73
|
+
A class object of the fitted model for the competing event.
|
|
74
|
+
|
|
75
|
+
Returns
|
|
76
|
+
-------
|
|
77
|
+
obs_means: Dict
|
|
78
|
+
A dictionary, where the key is the covariate / risk name and the value is its observational mean at all the time points.
|
|
79
|
+
|
|
80
|
+
est_means: Dict
|
|
81
|
+
A dictionary, where the key is the covariate / risk name and the value is its parametric mean at all the time points.
|
|
82
|
+
|
|
83
|
+
obs_res: Float
|
|
84
|
+
A value of the observational risk / mean at final time point.
|
|
85
|
+
|
|
86
|
+
IP_weights: List
|
|
87
|
+
A list contains the inverse probability weights from the censor model.
|
|
88
|
+
|
|
89
|
+
"""
|
|
90
|
+
if censor:
|
|
91
|
+
# for non-parametric cov means and risks
|
|
92
|
+
censor_pre = censor_fit.predict(obs_data)
|
|
93
|
+
censor_p0_inv = 1 / (1 - censor_pre)
|
|
94
|
+
obs_data['censor_p0_inv'] = censor_p0_inv
|
|
95
|
+
censor_inv_cum = obs_data.groupby([id])['censor_p0_inv'].cumprod()
|
|
96
|
+
obs_data['censor_inv_cum'] = censor_inv_cum
|
|
97
|
+
w_censor = censor_inv_cum * (1 - obs_data[censor_name])
|
|
98
|
+
if outcome_type == 'survival' and compevent_cens:
|
|
99
|
+
comprisk_p0_inv = 1 / (1 - compevent_fit.predict(obs_data))
|
|
100
|
+
obs_data['comprisk_p0_inv'] = comprisk_p0_inv
|
|
101
|
+
comprisk_inv_cum = obs_data.groupby([id])['comprisk_p0_inv'].cumprod()
|
|
102
|
+
w_comp = np.where((obs_data[compevent_name].isna()) | (obs_data[compevent_name] == 1), 0, comprisk_inv_cum)
|
|
103
|
+
w = w_comp * w_censor
|
|
104
|
+
else:
|
|
105
|
+
w = w_censor
|
|
106
|
+
obs_data['IP_weight'] = w
|
|
107
|
+
|
|
108
|
+
if ipw_cutoff_quantile:
|
|
109
|
+
quantile_w = np.percentile(list(w_censor), ipw_cutoff_quantile * 100)
|
|
110
|
+
obs_data.loc[obs_data['IP_weight'] > quantile_w, 'IP_weight'] = quantile_w
|
|
111
|
+
if ipw_cutoff_value:
|
|
112
|
+
obs_data.loc[obs_data['IP_weight'] > ipw_cutoff_value, 'IP_weight'] = ipw_cutoff_value
|
|
113
|
+
|
|
114
|
+
obs_data['IP_weight_cov'] = np.where(obs_data[time_name] > 0, obs_data['IP_weight'].shift(1), 1)
|
|
115
|
+
|
|
116
|
+
obs_means = {}
|
|
117
|
+
if covnames is not None:
|
|
118
|
+
for k, covname in enumerate(covnames):
|
|
119
|
+
if covtypes[k] == 'categorical':
|
|
120
|
+
all_levels = np.unique(obs_data[covname])
|
|
121
|
+
all_levels_obs_prob = []
|
|
122
|
+
for level in all_levels:
|
|
123
|
+
obs_level_prob = obs_data[obs_data[covname].notna()].groupby([time_name]).apply(lambda g:
|
|
124
|
+
(((g[covname] == level) * g['IP_weight_cov']).mean()) /g['IP_weight_cov'].mean()).tolist()[:time_points]
|
|
125
|
+
all_levels_obs_prob.append(obs_level_prob)
|
|
126
|
+
else:
|
|
127
|
+
cov_mean = obs_data[obs_data[covname].notna()].groupby(time_name).apply(lambda g: (g['IP_weight_cov'] * g[covname]).mean() / g['IP_weight_cov'].mean()).tolist()[:time_points]
|
|
128
|
+
obs_means[covname] = cov_mean
|
|
129
|
+
|
|
130
|
+
if outcome_type == 'binary_eof' or outcome_type == 'continuous_eof':
|
|
131
|
+
obs_data_last_record = obs_data.loc[obs_data[outcome_name].notna()]
|
|
132
|
+
obs_mean_Ey = (obs_data_last_record[outcome_name] * obs_data_last_record['IP_weight']).mean() / obs_data_last_record['IP_weight'].mean()
|
|
133
|
+
|
|
134
|
+
if outcome_type == 'survival':
|
|
135
|
+
if competing and not compevent_cens:
|
|
136
|
+
w_elimD = obs_data['IP_weight'] * (1 - obs_data[compevent_name])
|
|
137
|
+
obs_data['w_elimD'] = w_elimD
|
|
138
|
+
h_k = obs_data[obs_data[outcome_name].notna()].groupby(time_name).apply(
|
|
139
|
+
lambda g: (g['w_elimD'] * g[outcome_name]).mean() / g['w_elimD'].mean())
|
|
140
|
+
h_k2 = obs_data[obs_data[compevent_name].notna()].groupby(time_name).apply(
|
|
141
|
+
lambda g: (g['IP_weight'] * g[compevent_name]).mean() / g['IP_weight'].mean())
|
|
142
|
+
risks = np.array([list(h_k)[k] * (1 - list(h_k2)[k]) if k == 0 else list(h_k)[k]
|
|
143
|
+
* (1 - list(h_k2)[k]) * list((1 - h_k).cumprod())[k - 1] * list((1 - h_k2).cumprod())[k - 1]
|
|
144
|
+
for k in range(time_points)]).cumsum().tolist()[:time_points]
|
|
145
|
+
obs_means['risk'] = risks
|
|
146
|
+
obs_risk = risks[-1]
|
|
147
|
+
else:
|
|
148
|
+
weight_outcome_mean = obs_data[obs_data[outcome_name].notna()].groupby(time_name).apply(
|
|
149
|
+
lambda g: (g['IP_weight'] * g[outcome_name]).mean() / g['IP_weight'].mean())
|
|
150
|
+
weight_p0_mean = 1 - weight_outcome_mean
|
|
151
|
+
risks = np.array([weight_outcome_mean.tolist()[k] if k == 0 else weight_outcome_mean.tolist()[k] *
|
|
152
|
+
weight_p0_mean.cumprod().tolist()[k - 1] for k in
|
|
153
|
+
range(time_points)]).cumsum().tolist()[:time_points]
|
|
154
|
+
obs_means['risk'] = risks
|
|
155
|
+
obs_risk = risks[-1]
|
|
156
|
+
|
|
157
|
+
if outcome_type == 'survival':
|
|
158
|
+
# for parametric cov means and risks
|
|
159
|
+
if competing and not compevent_cens:
|
|
160
|
+
nc_pool['p0_cum'] = nc_pool.groupby(id)['prob0'].cumprod()
|
|
161
|
+
nc_pool['pd_0'] = 1 - nc_pool['prob_D']
|
|
162
|
+
nc_pool['pd_0_cum'] = nc_pool.groupby(id)['pd_0'].cumprod()
|
|
163
|
+
nc_pool['w_cov'] = np.where(nc_pool[time_name] > 0,
|
|
164
|
+
nc_pool['p0_cum'].shift(1) * nc_pool['pd_0_cum'].shift(1), 1)
|
|
165
|
+
else:
|
|
166
|
+
nc_pool['p0_cum'] = nc_pool.groupby([id])['prob0'].cumprod()
|
|
167
|
+
nc_pool['w_cov'] = np.where(nc_pool[time_name] > 0, nc_pool['p0_cum'].shift(1), 1)
|
|
168
|
+
else:
|
|
169
|
+
nc_pool['w_cov'] = 1
|
|
170
|
+
|
|
171
|
+
est_means = {}
|
|
172
|
+
if covnames is not None:
|
|
173
|
+
for k, covname in enumerate(covnames):
|
|
174
|
+
if covtypes[k] == 'categorical':
|
|
175
|
+
all_levels = np.unique(obs_data[covname])
|
|
176
|
+
all_levels_est_prob_mean = []
|
|
177
|
+
for level in all_levels:
|
|
178
|
+
est_level_prob = nc_pool[nc_pool[covname].notna()].groupby([time_name]).apply(
|
|
179
|
+
lambda g: ((g[covname] == level) * g['w_cov']).mean() / g['w_cov'].mean()).tolist()[:time_points]
|
|
180
|
+
all_levels_est_prob_mean.append(est_level_prob)
|
|
181
|
+
est_means[covname] = all_levels_est_prob_mean
|
|
182
|
+
else:
|
|
183
|
+
cov_mean = nc_pool[nc_pool[covname].notna()].groupby(time_name).apply(
|
|
184
|
+
lambda g: (g['w_cov'] * g[covname]).mean() / g['w_cov'].mean()).tolist()[:time_points]
|
|
185
|
+
est_means[covname] = cov_mean
|
|
186
|
+
if outcome_type == 'survival':
|
|
187
|
+
est_means['risk'] = nc_risk
|
|
188
|
+
|
|
189
|
+
else:
|
|
190
|
+
# for non-parametric cov means and risks
|
|
191
|
+
obs_means = {}
|
|
192
|
+
if covnames is not None:
|
|
193
|
+
for k, covname in enumerate(covnames):
|
|
194
|
+
if covtypes[k] == 'categorical':
|
|
195
|
+
all_levels = np.unique(obs_data[covname])
|
|
196
|
+
all_levels_obs_prob_mean = []
|
|
197
|
+
for level in all_levels:
|
|
198
|
+
obs_level_prob = obs_data[obs_data[covname].notna()].groupby([time_name]).apply(lambda g: ((g[covname] == level)).mean()).tolist()[:time_points]
|
|
199
|
+
all_levels_obs_prob_mean.append(obs_level_prob)
|
|
200
|
+
obs_means[covname] = all_levels_obs_prob_mean
|
|
201
|
+
else:
|
|
202
|
+
obs_mean = obs_data[obs_data[covname].notna()].groupby([time_name])[covname].mean().tolist()[:time_points]
|
|
203
|
+
obs_means[covname] = obs_mean
|
|
204
|
+
|
|
205
|
+
if outcome_type == 'binary_eof' or outcome_type == 'continuous_eof':
|
|
206
|
+
obs_mean_Ey = obs_data.loc[obs_data[time_name] == time_points - 1][outcome_name].mean()
|
|
207
|
+
|
|
208
|
+
if outcome_type == 'survival':
|
|
209
|
+
if competing and not compevent_cens:
|
|
210
|
+
p1_mean = obs_data[obs_data[outcome_name].notna()].groupby(time_name)[outcome_name].mean()
|
|
211
|
+
pd_mean = obs_data[obs_data[compevent_name].notna()].groupby(time_name)[compevent_name].mean()
|
|
212
|
+
comrisks = np.array(
|
|
213
|
+
[list(p1_mean)[k] * (1 - list(pd_mean)[k]) if k == 0 else list(p1_mean)[k] * (1 - list(pd_mean)[k]) *
|
|
214
|
+
list((1 - p1_mean).cumprod())[k - 1] * list((1 - pd_mean).cumprod())[k - 1]
|
|
215
|
+
for k in range(time_points)]).cumsum().tolist()[:time_points]
|
|
216
|
+
obs_means['risk'] = comrisks
|
|
217
|
+
obs_risk = comrisks[-1]
|
|
218
|
+
else:
|
|
219
|
+
p1_mean_obs = obs_data[obs_data[outcome_name].notna()].groupby(time_name)[outcome_name].mean()
|
|
220
|
+
p0_mean_obs = 1 - p1_mean_obs
|
|
221
|
+
risks = np.array(
|
|
222
|
+
[p1_mean_obs.tolist()[k] if k == 0 else p1_mean_obs.tolist()[k] * p0_mean_obs.cumprod().tolist()[k - 1]
|
|
223
|
+
for k in range(time_points)]).cumsum().tolist()[:time_points]
|
|
224
|
+
obs_means['risk'] = risks
|
|
225
|
+
obs_risk = risks[-1]
|
|
226
|
+
|
|
227
|
+
if outcome_type == 'survival':
|
|
228
|
+
# for parametric cov means and risks
|
|
229
|
+
if competing and not compevent_cens:
|
|
230
|
+
nc_pool['p0_cum'] = nc_pool.groupby(id)['prob0'].cumprod()
|
|
231
|
+
nc_pool['pd_0'] = 1 - nc_pool['prob_D']
|
|
232
|
+
nc_pool['pd_0_cum'] = nc_pool.groupby(id)['pd_0'].cumprod()
|
|
233
|
+
nc_pool['w_cov'] = np.where(nc_pool[time_name] > 0,
|
|
234
|
+
nc_pool['p0_cum'].shift(1) * nc_pool['pd_0_cum'].shift(1), 1)
|
|
235
|
+
else:
|
|
236
|
+
nc_pool['p0_cum'] = nc_pool.groupby([id])['prob0'].cumprod()
|
|
237
|
+
nc_pool['w_cov'] = np.where(nc_pool[time_name] > 0, nc_pool['p0_cum'].shift(1), 1)
|
|
238
|
+
else:
|
|
239
|
+
nc_pool['w_cov'] = 1
|
|
240
|
+
|
|
241
|
+
est_means = {}
|
|
242
|
+
if covnames is not None:
|
|
243
|
+
for k, covname in enumerate(covnames):
|
|
244
|
+
if covtypes[k] == 'categorical':
|
|
245
|
+
all_levels = np.unique(obs_data[covname])
|
|
246
|
+
all_levels_est_prob_mean = []
|
|
247
|
+
for level in all_levels:
|
|
248
|
+
est_level_prob = nc_pool[nc_pool[covname].notna()].groupby([time_name]).apply(
|
|
249
|
+
lambda g: ((g[covname] == level) * g['w_cov']).mean() / g['w_cov'].mean()).tolist()[:time_points]
|
|
250
|
+
all_levels_est_prob_mean.append(est_level_prob)
|
|
251
|
+
est_means[covname] = all_levels_est_prob_mean
|
|
252
|
+
else:
|
|
253
|
+
est_mean = nc_pool[nc_pool[covname].notna()].groupby(time_name).apply(lambda g:
|
|
254
|
+
(g['w_cov'] * g[covname]).mean()
|
|
255
|
+
/ g['w_cov'].mean()).tolist()[:time_points]
|
|
256
|
+
est_means[covname] = est_mean
|
|
257
|
+
if outcome_type == 'survival':
|
|
258
|
+
est_means['risk'] = nc_risk
|
|
259
|
+
|
|
260
|
+
obs_res = obs_risk if outcome_type == 'survival' else obs_mean_Ey
|
|
261
|
+
IP_weights = obs_data['IP_weight'].tolist() if censor else None
|
|
262
|
+
|
|
263
|
+
return obs_means, est_means, obs_res, IP_weights
|
|
264
|
+
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def load_basicdata():
|
|
6
|
+
"""
|
|
7
|
+
Data description: a survival dataset that contains 11,332 observations on 2,500 individuals over 7 time points.
|
|
8
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
9
|
+
|
|
10
|
+
id: Unique identifier for each individual.
|
|
11
|
+
t0: Time index.
|
|
12
|
+
L1: Binary time-varying covariate.
|
|
13
|
+
L2: Continuous time-varying covariate.
|
|
14
|
+
L3: Categorical baseline covariate. For each individual, the baseline values are repeated at each time point.
|
|
15
|
+
A: Binary treatment variable.
|
|
16
|
+
D: Competing event; time-varying indicator of failure.
|
|
17
|
+
Y: Outcome of interest; time-varying indicator of failure.
|
|
18
|
+
|
|
19
|
+
Returns
|
|
20
|
+
-------
|
|
21
|
+
A pandas dataframe
|
|
22
|
+
"""
|
|
23
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_basicdata.csv'))
|
|
24
|
+
return data
|
|
25
|
+
|
|
26
|
+
def load_basicdata_nocomp():
|
|
27
|
+
"""
|
|
28
|
+
Data description: a survival dataset that contains 13,170 observations on 2,500 individuals over 7 time points.
|
|
29
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
30
|
+
|
|
31
|
+
id: Unique identifier for each individual.
|
|
32
|
+
t0: Time index.
|
|
33
|
+
L1: Binary time-varying covariate.
|
|
34
|
+
L2: Continuous time-varying covariate.
|
|
35
|
+
L3: Categorical baseline covariate. For each individual, the baseline values are repeated at each time point.
|
|
36
|
+
A: Binary treatment variable.
|
|
37
|
+
Y: Outcome of interest; time-varying indicator of failure.
|
|
38
|
+
|
|
39
|
+
This is a survival dataset that contains 2500 individuals with maximum 7 time points. There are
|
|
40
|
+
one binary covariate L1, one normal covariate L2, one baseline covariate L3, one binary treatment variable A, and a
|
|
41
|
+
binary outcome Y.
|
|
42
|
+
|
|
43
|
+
Returns
|
|
44
|
+
-------
|
|
45
|
+
A pandas dataframe
|
|
46
|
+
"""
|
|
47
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_basicdata_nocomp.csv'))
|
|
48
|
+
return data
|
|
49
|
+
|
|
50
|
+
def load_absorbing_data():
|
|
51
|
+
"""
|
|
52
|
+
Data description: a survival dataset that contains 6,033 observations, 1,000 individuals over 10 time points.
|
|
53
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
54
|
+
|
|
55
|
+
id: Unique identifier for each individual.
|
|
56
|
+
t0: Time index.
|
|
57
|
+
L: Binary time-varying covariate with absorbing type, once it takes value 1, it keeps 1 at subsequent time points.
|
|
58
|
+
A: Binary treatment variable.
|
|
59
|
+
Y: Outcome of interest; time-varying indicator of failure.
|
|
60
|
+
|
|
61
|
+
Returns
|
|
62
|
+
-------
|
|
63
|
+
A pandas dataframe
|
|
64
|
+
"""
|
|
65
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_absorbing.csv'))
|
|
66
|
+
return data
|
|
67
|
+
|
|
68
|
+
def load_binary_eof():
|
|
69
|
+
"""
|
|
70
|
+
Data description: a dataset that contains 17,500 observations on 2,500 individuals over 7 time points.
|
|
71
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
72
|
+
|
|
73
|
+
id: Unique identifier for each individual.
|
|
74
|
+
t0: Time index.
|
|
75
|
+
L1: Binary time-varying covariate.
|
|
76
|
+
L2: Continuous time-varying covariate.
|
|
77
|
+
L3: Categorical baseline covariate. For each individual, the baseline values are repeated at each time point.
|
|
78
|
+
A: Continuous treatment variable.
|
|
79
|
+
Y: Binary outcome of interest. Because this outcome is only defined at the end of follow-up, values of NA are given
|
|
80
|
+
in all other time points.
|
|
81
|
+
|
|
82
|
+
Returns
|
|
83
|
+
-------
|
|
84
|
+
A pandas dataframe
|
|
85
|
+
"""
|
|
86
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_binary_eof.csv'))
|
|
87
|
+
return data
|
|
88
|
+
|
|
89
|
+
def load_categorical():
|
|
90
|
+
"""
|
|
91
|
+
Data description: a survival dataset that contains 7,822 observations, 1,000 individuals over 10 time points.
|
|
92
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
93
|
+
|
|
94
|
+
id: Unique identifier for each individual.
|
|
95
|
+
t0: Time index.
|
|
96
|
+
L: Categorical covariate with 5 categories.
|
|
97
|
+
A: Binary treatment variable.
|
|
98
|
+
Y: Outcome of interest; time-varying indicator of failure.
|
|
99
|
+
|
|
100
|
+
Returns
|
|
101
|
+
-------
|
|
102
|
+
A pandas dataframe
|
|
103
|
+
"""
|
|
104
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_categorical.csv'))
|
|
105
|
+
return data
|
|
106
|
+
|
|
107
|
+
def load_censor_data():
|
|
108
|
+
"""
|
|
109
|
+
Data description: a survival dataset with censoring event that contains 118,725 observations, 10,000 individuals
|
|
110
|
+
over 10 time points. Each row in the dataset corresponds to the record of one individual at one time point.
|
|
111
|
+
Individuals who are censored at time k+1 only have a total of k+1 records, which correspond to time indices 0,..., k.
|
|
112
|
+
|
|
113
|
+
id: Unique identifier for each individual.
|
|
114
|
+
t0: Time index.
|
|
115
|
+
L: Binary time-varying covariate.
|
|
116
|
+
A: Continuous treatment variable.
|
|
117
|
+
C: Censoring indicator.
|
|
118
|
+
Y: Outcome of interest; time-varying indicator of failure.
|
|
119
|
+
|
|
120
|
+
Returns
|
|
121
|
+
-------
|
|
122
|
+
A pandas dataframe
|
|
123
|
+
"""
|
|
124
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_censor.csv'))
|
|
125
|
+
return data
|
|
126
|
+
|
|
127
|
+
def load_continuous_eof():
|
|
128
|
+
"""
|
|
129
|
+
Data description: a dataset that contains 17,500 observations on 2,500 individuals over 7 time points.
|
|
130
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
131
|
+
|
|
132
|
+
id: Unique identifier for each individual.
|
|
133
|
+
t0: Time index.
|
|
134
|
+
L1: Categorical time-varying covariate with 3 categories.
|
|
135
|
+
L2: Continuous time-varying covariate.
|
|
136
|
+
L3: Categorical baseline covariate. For each individual, the baseline values are repeated at each time point.
|
|
137
|
+
A: Binary treatment variable.
|
|
138
|
+
Y: Continuous outcome of interest. Because this outcome is only defined at the end of follow-up, values of NA are
|
|
139
|
+
given in all other time points.
|
|
140
|
+
|
|
141
|
+
Returns
|
|
142
|
+
-------
|
|
143
|
+
A pandas dataframe
|
|
144
|
+
"""
|
|
145
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_continuous_eof.csv'))
|
|
146
|
+
return data
|
|
147
|
+
|
|
148
|
+
def load_visit_process():
|
|
149
|
+
"""
|
|
150
|
+
Data description: a survival dataset with visit process that contains 1,739 observations on 200 individuals over 37 time points.
|
|
151
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
152
|
+
|
|
153
|
+
id: Unique identifier for each individual.
|
|
154
|
+
month: Time index.
|
|
155
|
+
sex: Binary baseline covariate. For each individual, the baseline values are repeated at each time point.
|
|
156
|
+
age: Continuous baseline covariate.
|
|
157
|
+
race: Categorical baseline covariate.
|
|
158
|
+
cd4_v: Continuous time-varying covariate.
|
|
159
|
+
visit_cd4: Indicator of whether there is a cd4 visit/measurement.
|
|
160
|
+
rna_v: Continuous time-varying covariate.
|
|
161
|
+
visit_rna: Indicator of whether there is a rna visit/measurement.
|
|
162
|
+
everhaart: Binary treatment variable.
|
|
163
|
+
event: Outcome of interest; time-varying indicator of failure.
|
|
164
|
+
|
|
165
|
+
Returns
|
|
166
|
+
-------
|
|
167
|
+
A pandas dataframe
|
|
168
|
+
"""
|
|
169
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_visit_process.csv'))
|
|
170
|
+
return data
|
|
171
|
+
|
|
172
|
+
def load_truncated_normal():
|
|
173
|
+
"""
|
|
174
|
+
Data description: a survival dataset with visit process that contains 7,855 observations on 1,000 individuals over 10 time points.
|
|
175
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
176
|
+
|
|
177
|
+
id: Unique identifier for each individual.
|
|
178
|
+
t0: Time index.
|
|
179
|
+
L: Continuous time-varying covariate with truncated normal distribution.
|
|
180
|
+
A: Binary treatment variable.
|
|
181
|
+
Y: Outcome of interest; time-varying indicator of failure.
|
|
182
|
+
|
|
183
|
+
Returns
|
|
184
|
+
-------
|
|
185
|
+
A pandas dataframe
|
|
186
|
+
"""
|
|
187
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_truncated_normal.csv'))
|
|
188
|
+
return data
|
|
189
|
+
|
|
190
|
+
def load_zero_inflated_normal():
|
|
191
|
+
"""
|
|
192
|
+
Data description: a survival dataset with visit process that contains 7,678 observations on 1,000 individuals over 10 time points.
|
|
193
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
194
|
+
|
|
195
|
+
id: Unique identifier for each individual.
|
|
196
|
+
t0: Time index.
|
|
197
|
+
L: Continuous time-varying covariate with zero-inflated normal distribution.
|
|
198
|
+
A: Binary treatment variable.
|
|
199
|
+
Y: Outcome of interest; time-varying indicator of failure.
|
|
200
|
+
|
|
201
|
+
Returns
|
|
202
|
+
-------
|
|
203
|
+
A pandas dataframe
|
|
204
|
+
"""
|
|
205
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_zero_inflated_normal.csv'))
|
|
206
|
+
return data
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def load_multiple_treatments_data():
|
|
210
|
+
"""
|
|
211
|
+
Data description: a survival dataset that contains 3,416 observations on 1,000 individuals over 5 time points.
|
|
212
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
213
|
+
|
|
214
|
+
id: Unique identifier for each individual.
|
|
215
|
+
t0: Time index.
|
|
216
|
+
L1: Binary time-varying covariate.
|
|
217
|
+
L2: Continuous time-varying covariate.
|
|
218
|
+
A1: Binary treatment variable.
|
|
219
|
+
A2: Binary treatment variable.
|
|
220
|
+
Y: Outcome of interest; time-varying indicator of failure.
|
|
221
|
+
|
|
222
|
+
Returns
|
|
223
|
+
-------
|
|
224
|
+
A pandas dataframe
|
|
225
|
+
"""
|
|
226
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_data_multiple_treatments.csv'))
|
|
227
|
+
return data
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def load_threshold_data():
|
|
231
|
+
"""
|
|
232
|
+
Data description: a survival dataset that contains 1,853 observations on 1,000 individuals over 5 time points.
|
|
233
|
+
Each row in the dataset corresponds to the record of one individual at one time point.
|
|
234
|
+
|
|
235
|
+
id: Unique identifier for each individual.
|
|
236
|
+
t0: Time index.
|
|
237
|
+
L1: Binary time-varying covariate.
|
|
238
|
+
L2: Continuous time-varying covariate.
|
|
239
|
+
A: Continuous treatment variable.
|
|
240
|
+
Y: Outcome of interest; time-varying indicator of failure.
|
|
241
|
+
|
|
242
|
+
Returns
|
|
243
|
+
-------
|
|
244
|
+
A pandas dataframe
|
|
245
|
+
"""
|
|
246
|
+
data = pd.read_csv(os.path.join('..', 'datasets', 'example_threshold_data.csv'))
|
|
247
|
+
return data
|