-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforest_plot.py
251 lines (240 loc) · 9.03 KB
/
forest_plot.py
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
import string
import numpy as np
import pandas as pd
import os
import pickle
import forestplot as fp
import matplotlib.pyplot as plt
import matplotlib
import statsmodels.api as sm
#load in the model fits
model_fits = {}
for pkl_file in os.listdir('outcomes_vs_demographics_regression_outputs'):
if ".pkl" not in pkl_file:
continue
with open(f'outcomes_vs_demographics_regression_outputs/{pkl_file}', 'rb') as f:
model_fits[pkl_file] = pickle.load(f)
#format the data into tables
forest_data = []
#reference male == 0
forest_data.append({
'group':'Gender',
'OR':1,
'OR_2.5_ci':1,
'OR_97.5_ci':1,
'P-value':0,
'label': 'Male (Ref.)'
})
#female == 1
forest_data.append({
'group':'Gender',
'OR':model_fits['uni_gender_mdl_py38.pkl'].coefs['OR']['gender'],
'OR_2.5_ci':model_fits['uni_gender_mdl_py38.pkl'].coefs['OR_2.5_ci']['gender'],
'OR_97.5_ci':model_fits['uni_gender_mdl_py38.pkl'].coefs['OR_97.5_ci']['gender'],
'P-value':model_fits['uni_gender_mdl_py38.pkl'].coefs['P-val']['gender'],
'label': 'Female'
})
#reference white == 0
forest_data.append({
'group':'Race',
'OR':1,
'OR_2.5_ci':1,
'OR_97.5_ci':1,
'P-value':0,
'label': 'White (Ref.)'
})
#black == 1
forest_data.append({
'group':'Race',
'OR':model_fits['uni_race_mdl_py38.pkl'].coefs['OR']['race1'],
'OR_2.5_ci':model_fits['uni_race_mdl_py38.pkl'].coefs['OR_2.5_ci']['race1'],
'OR_97.5_ci':model_fits['uni_race_mdl_py38.pkl'].coefs['OR_97.5_ci']['race1'],
'P-value':model_fits['uni_race_mdl_py38.pkl'].coefs['P-val']['race1'],
'label': 'Black'
})
#asian == 2
forest_data.append({
'group':'Race',
'OR':model_fits['uni_race_mdl_py38.pkl'].coefs['OR']['race2'],
'OR_2.5_ci':model_fits['uni_race_mdl_py38.pkl'].coefs['OR_2.5_ci']['race2'],
'OR_97.5_ci':model_fits['uni_race_mdl_py38.pkl'].coefs['OR_97.5_ci']['race2'],
'P-value':model_fits['uni_race_mdl_py38.pkl'].coefs['P-val']['race2'],
'label': 'Asian'
})
#other == 3
forest_data.append({
'group':'Race',
'OR':model_fits['uni_race_mdl_py38.pkl'].coefs['OR']['race3'],
'OR_2.5_ci':model_fits['uni_race_mdl_py38.pkl'].coefs['OR_2.5_ci']['race3'],
'OR_97.5_ci':model_fits['uni_race_mdl_py38.pkl'].coefs['OR_97.5_ci']['race3'],
'P-value':model_fits['uni_race_mdl_py38.pkl'].coefs['P-val']['race3'],
'label': 'Other'
})
#reference not hispanic/latino == 0
forest_data.append({
'group':'Ethnicity',
'OR':1,
'OR_2.5_ci':1,
'OR_97.5_ci':1,
'P-value':0,
'label': 'Not Hispanic or Latino (Ref.)'
})
#hispanic or latino == 1
forest_data.append({
'group':'Ethnicity',
'OR':model_fits['uni_ethnicity_mdl_py38.pkl'].coefs['OR']['ethnicity'],
'OR_2.5_ci':model_fits['uni_ethnicity_mdl_py38.pkl'].coefs['OR_2.5_ci']['ethnicity'],
'OR_97.5_ci':model_fits['uni_ethnicity_mdl_py38.pkl'].coefs['OR_97.5_ci']['ethnicity'],
'P-value':model_fits['uni_ethnicity_mdl_py38.pkl'].coefs['P-val']['ethnicity'],
'label': 'Hispanic or Latino'
})
#reference private == 0
forest_data.append({
'group':'Insurance',
'OR':1,
'OR_2.5_ci':1,
'OR_97.5_ci':1,
'P-value':0,
'label': 'Private (Ref.)'
})
#public == 1
forest_data.append({
'group':'Insurance',
'OR':model_fits['uni_insurance_mdl_py38.pkl'].coefs['OR']['insurance_type'],
'OR_2.5_ci':model_fits['uni_insurance_mdl_py38.pkl'].coefs['OR_2.5_ci']['insurance_type'],
'OR_97.5_ci':model_fits['uni_insurance_mdl_py38.pkl'].coefs['OR_97.5_ci']['insurance_type'],
'P-value':model_fits['uni_insurance_mdl_py38.pkl'].coefs['P-val']['insurance_type'],
'label': 'Public'
})
#reference income >100k
forest_data.append({
'group':'Income',
'OR':1,
'OR_2.5_ci':1,
'OR_97.5_ci':1,
'P-value':0,
'label': r'\$100k+ (Ref.)'
})
#income 75-100k
forest_data.append({
'group':'Income',
'OR':model_fits['uni_income_mdl_py38.pkl'].coefs['OR']['income1'],
'OR_2.5_ci':model_fits['uni_income_mdl_py38.pkl'].coefs['OR_2.5_ci']['income1'],
'OR_97.5_ci':model_fits['uni_income_mdl_py38.pkl'].coefs['OR_97.5_ci']['income1'],
'P-value':model_fits['uni_income_mdl_py38.pkl'].coefs['P-val']['income1'],
'label': r'\$75k-\$100k'
})
#income 50-75k
forest_data.append({
'group':'Income',
'OR':model_fits['uni_income_mdl_py38.pkl'].coefs['OR']['income2'],
'OR_2.5_ci':model_fits['uni_income_mdl_py38.pkl'].coefs['OR_2.5_ci']['income2'],
'OR_97.5_ci':model_fits['uni_income_mdl_py38.pkl'].coefs['OR_97.5_ci']['income2'],
'P-value':model_fits['uni_income_mdl_py38.pkl'].coefs['P-val']['income2'],
'label': r'\$50k-\$75k'
})
#income <50k
forest_data.append({
'group':'Income',
'OR':model_fits['uni_income_mdl_py38.pkl'].coefs['OR']['income3'],
'OR_2.5_ci':model_fits['uni_income_mdl_py38.pkl'].coefs['OR_2.5_ci']['income3'],
'OR_97.5_ci':model_fits['uni_income_mdl_py38.pkl'].coefs['OR_97.5_ci']['income3'],
'P-value':model_fits['uni_income_mdl_py38.pkl'].coefs['P-val']['income3'],
'label': r'<\$50k'
})
#reference age 18-40
forest_data.append({
'group':'Age',
'OR':1,
'OR_2.5_ci':1,
'OR_97.5_ci':1,
'P-value':0,
'label': '18-40 yrs (Ref.)'
})
#age 40-65
forest_data.append({
'group':'Age',
'OR':model_fits['uni_age_mdl_py38.pkl'].coefs['OR']['age1'],
'OR_2.5_ci':model_fits['uni_age_mdl_py38.pkl'].coefs['OR_2.5_ci']['age1'],
'OR_97.5_ci':model_fits['uni_age_mdl_py38.pkl'].coefs['OR_97.5_ci']['age1'],
'P-value':model_fits['uni_age_mdl_py38.pkl'].coefs['P-val']['age1'],
'label': '40-65 yrs'
})
#age 65+
forest_data.append({
'group':'Age',
'OR':model_fits['uni_age_mdl_py38.pkl'].coefs['OR']['age2'],
'OR_2.5_ci':model_fits['uni_age_mdl_py38.pkl'].coefs['OR_2.5_ci']['age2'],
'OR_97.5_ci':model_fits['uni_age_mdl_py38.pkl'].coefs['OR_97.5_ci']['age2'],
'P-value':model_fits['uni_age_mdl_py38.pkl'].coefs['P-val']['age2'],
'label': '65+ yrs'
})
#reference asm == 0
forest_data.append({
'group':'Number of ASMs',
'OR':1,
'OR_2.5_ci':1,
'OR_97.5_ci':1,
'P-value':0,
'label': '0 ASMs (Ref.)'
})
#asm == 1
forest_data.append({
'group':'Number of ASMs',
'OR':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR']['num_asms1'],
'OR_2.5_ci':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR_2.5_ci']['num_asms1'],
'OR_97.5_ci':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR_97.5_ci']['num_asms1'],
'P-value':model_fits['uni_asms_mdl_py38.pkl'].coefs['P-val']['num_asms1'],
'label': '1 ASM'
})
#asm == 2
forest_data.append({
'group':'Number of ASMs',
'OR':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR']['num_asms2'],
'OR_2.5_ci':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR_2.5_ci']['num_asms2'],
'OR_97.5_ci':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR_97.5_ci']['num_asms2'],
'P-value':model_fits['uni_asms_mdl_py38.pkl'].coefs['P-val']['num_asms2'],
'label': '2 ASMs'
})
#asm == 3
forest_data.append({
'group':'Number of ASMs',
'OR':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR']['num_asms3'],
'OR_2.5_ci':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR_2.5_ci']['num_asms3'],
'OR_97.5_ci':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR_97.5_ci']['num_asms3'],
'P-value':model_fits['uni_asms_mdl_py38.pkl'].coefs['P-val']['num_asms3'],
'label': '3 ASMs'
})
#asm == 4+
forest_data.append({
'group':'Number of ASMs',
'OR':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR']['num_asms4'],
'OR_2.5_ci':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR_2.5_ci']['num_asms4'],
'OR_97.5_ci':model_fits['uni_asms_mdl_py38.pkl'].coefs['OR_97.5_ci']['num_asms4'],
'P-value':model_fits['uni_asms_mdl_py38.pkl'].coefs['P-val']['num_asms4'],
'label': '4+ ASMs'
})
#plot the forest plot
forest_data = pd.DataFrame(forest_data)
ax = fp.forestplot(forest_data,
estimate="OR",
ll="OR_2.5_ci",
hl="OR_97.5_ci",
varlabel="label",
groupvar="group",
pval="P-value",
ylabel="OR (95% CI)",
xlabel="$\leftarrow$" + " Less Sz. | More Sz. " + r"$\rightarrow$",
starpval=False,
)
plt.vlines(1.0, -1, len(forest_data)+forest_data.group.nunique()-1, colors='k', linestyles=':', linewidth=1.0)
ax.set_xscale('log')
ax.set_xticks([0.25, 0.5, 1.0, 2.0, 4.0])
ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
plt.savefig(f"outcomes_vs_demographics_regression_outputs/univariate_forest_plot.png", dpi=600, bbox_inches='tight')
plt.savefig(f"outcomes_vs_demographics_regression_outputs/univariate_forest_plot.pdf", dpi=600, bbox_inches='tight')
plt.show()
#P-value corrections
forest_data_no_ref = forest_data.loc[forest_data['P-value'] > 0]
adjusted_p = sm.stats.fdrcorrection(forest_data_no_ref['P-value'].to_numpy())
forest_data_no_ref['adjusted_p-value'] = adjusted_p[1]