-
Notifications
You must be signed in to change notification settings - Fork 1
/
treatment_modification_CF.R
349 lines (297 loc) · 11.1 KB
/
treatment_modification_CF.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
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
library(tidyverse)
library(RODBC)
library(scales)
library(usmap)
report_directory = paste(getwd(), "/", 'reports/treatment_modification/', sep = "")
cbp <- c('#e69f00', '#0072b2','#d55e00', '#009e73',
'#f0e442', '#56b4e9')
patient_list = read.csv('reports/prevalence/overall/patient_list.csv')
ch = odbcConnect('vertica',
uid = 'XXXXX',
pwd = 'XXX')
## Cystic Fibrosis
patients_raw = patient_list %>%
filter(disease =='Cystic_Fibrosis') %>%
select(z_patid) %>%
unlist()
patients_raw_query = paste("'",patients_raw,"'", sep="")
first_dates = sqlQuery(ch, paste("SELECT rs.z_patid, min(rs.fill_dt) as first_date
from HCCI_2.RX_SDDV2 rs
where rs.yr>2015 AND rs.ndc like '511670%'
and rs.ndc not in ('51167010001' ,'51167010003') AND rs.z_patid in (",
paste(patients_raw_query, collapse = ','), ")
GROUP by 1"))
patients = paste("'",first_dates %>% select(z_patid) %>% unlist(),"'", sep="")
inp = sqlQuery(ch, paste("SELECT inp.z_patid, inp.fst_dt,
SUM(inp.calc_allwd) as cost, 'Inpatient' as claim_type
from HCCI_2.INP_SDDV2 inp
where inp.yr > 2015 AND inp.z_patid in (",
paste(patients, collapse = ','), ")
group by 1,2"))
op = sqlQuery(ch, paste("SELECT op.z_patid, op.fst_dt,
SUM(op.calc_allwd) as cost, 'Outpatient' as claim_type
from HCCI_2.OP_SDDV2 op
where op.yr>2015 AND op.z_patid in (",
paste(patients, collapse = ','), ")
group by 1,2"))
ph = sqlQuery(ch, paste("SELECT ph.z_patid, ph.fst_dt,
SUM(ph.calc_allwd) as cost, 'Physician' as claim_type
from HCCI_2.PHYS_SDDV2 ph
where ph.yr > 2015 AND ph.z_patid in (",
paste(patients, collapse = ','), ")
group by 1,2"))
pharmacy = sqlQuery(ch, paste("SELECT rs.z_patid, rs.fill_dt as fst_dt,
SUM(rs.calc_allwd) as cost, 'Pharmacy' as claim_type
from HCCI_2.RX_SDDV2 rs
where rs.yr > 2015 AND rs.z_patid in (",
paste(patients, collapse = ','), ")",
"group by 1,2
"))
pt0 = inp %>%
bind_rows(op) %>%
bind_rows(ph) %>%
bind_rows(pharmacy) %>%
left_join(first_dates) %>%
mutate('treatment' = ifelse(fst_dt<first_date, 'Before', 'After'),
day_diff = as.numeric(fst_dt-first_date))
vc = c()
for (val in seq(-5, 5, 1)){
if(val!=0){
if(val<0){
vc = append(vc, paste(abs(val), 'years before'))
if(val == -1){
vc[length(vc)] = 'last year'
}
}else{
vc = append(vc, paste(abs(val), 'years after'))
if(val == 1){
vc[length(vc)] = '1st year'
}
}
}
}
pt0$time_cat = cut(pt0$day_diff,
breaks = seq(-365*5, 365*5, by = 365),
right = FALSE,
labels = vc, ordered_result = TRUE)
df_main = pt0 %>%
filter(claim_type!='Pharmacy') %>%
group_by(z_patid, time_cat,treatment) %>%
summarise(cost = sum(cost))
df_info = df_main %>%
group_by(time_cat) %>%
summarise(average = log(median(cost+1),2),
patients = n_distinct(z_patid),
qu = max(log((cost+1),2))*1.1) %>%
mutate(qu = max(qu))
df_main %>% ggplot(mapping = aes(x = time_cat,
y = log(cost+1,2)))+
geom_boxplot(aes(fill = treatment))+
scale_fill_manual(values = cbp)+
geom_point(data = df_info,
mapping = aes(x = time_cat, y = average, size = patients),
color = 'black')+
geom_line(data = df_info,
mapping = aes(x = time_cat, y = average, group = 1),
color = 'black', size = 1)+
geom_label(data = df_info, mapping = aes(x = time_cat,
y = qu, label = patients),
fill = 'green')+
ylab('Cost log()')+
xlab('')+
ggtitle("Costs (without pharmacy) in different time intervals",
subtitle = '2016-2020')+
theme_classic()+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1, size = 12))
ggsave(paste(report_directory, 'time_intervals_cost_Cystic_Fibrosis.png', sep = ""),
dpi = 600, width = 7.2, height = 5, units = 'in')
pt = pt0%>%
group_by(z_patid, treatment, claim_type) %>%
summarise(cost = sum(cost),
claim_count = n())
pt$treatment = factor(pt$treatment,
levels = c('Before', 'After'))
p1 = ggplot(pt,
aes(x=treatment, y = log(cost+1), fill = treatment))+
geom_boxplot(outlier.shape = NA)+
geom_point(shape = 1, aes(color = treatment),
alpha = .15, position = position_jitter())+
facet_wrap(~claim_type, nrow = 1)+
ylab(label = 'Cost log()')+
xlab(label = "")+
theme_bw()+
theme(legend.position = 'none',
strip.background = element_blank(),
strip.text = element_text(size = 8, face = 'bold'),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
plot.margin = margin(b=-5, unit = 'in'))+
scale_fill_manual(values = cbp[1:2])+
scale_color_manual(values = cbp[1:2])
p2 = ggplot(pt,
aes(x=treatment, y = log(claim_count), fill = treatment))+
geom_boxplot(outlier.shape = NA)+
geom_point(shape = 1, aes(color = treatment),
alpha = .15, position = position_jitter())+
facet_wrap(~claim_type, nrow = 1)+
ylab(label = 'Claim counts')+
xlab(label = "")+
theme_bw()+
theme(legend.position = 'none',
strip.background = element_blank(),
strip.text = element_blank(),
plot.margin = margin(t = -5, unit = 'in'))+
scale_fill_manual(values = cbp[1:2])+
scale_color_manual(values = cbp[1:2])
p3 = cowplot::plot_grid(p1,
p2,nrow = 2,
align = 'hv',
labels = c('a', 'b')
)
write.csv(pt0, paste(report_directory,'Cystic_Fibrosis.csv', sep = ''), row.names = FALSE)
ggsave(plot = p3, filename = paste(report_directory, 'Cystic_Fibrosis.png', sep = ""),
dpi = 600, width = 7.2, height = 5.5, units = 'in')
pvals = c()
mean_vec = c()
median_vec = c()
for (i in c('all','Pharmacy','Without Pharmacy')){
df = pt0 %>%
group_by(z_patid) %>%
summarise(start_day = min(fst_dt),
last_day = max(fst_dt),
first_date = max(first_date)) %>%
mutate(days_before = as.numeric(first_date - start_day),
days_after = as.numeric(last_day -first_date))
if (i == 'all'){
df2 = pt0 %>%
group_by(z_patid, treatment) %>%
summarise(cost = n()) %>%
spread(treatment, cost)
}else{
if(i == 'Pharmacy'){
df2 = pt0 %>%
filter(claim_type=='Pharmacy') %>%
group_by(z_patid, treatment) %>%
summarise(cost = n()) %>%
spread(treatment, cost)
}else{
df2 = pt0 %>%
filter(claim_type!='Pharmacy') %>%
group_by(z_patid, treatment) %>%
summarise(cost = n()) %>%
spread(treatment, cost)
}
}
df3 = df2 %>%
drop_na() %>%
left_join(df) %>%
filter(days_before>90, days_after >90)
df3 = df3 %>%
mutate(daily_before = Before/days_before,
daily_after = After/days_after,
cost_diff = daily_after-daily_before)
df3$claim = i
wl = wilcox.test(df3$cost_diff)
pvals = append(pvals,wl$p.value)
mean_vec =append(mean_vec,mean(df3$cost_diff))
median_vec = append(median_vec,median(df3$cost_diff))
if(i =='all'){
tmp = df3
}else{
tmp = tmp %>% bind_rows(df3)
}
}
tmp$cost_diff = tmp$cost_diff*365
mu = tmp %>% group_by(claim) %>% summarise(mn = mean(cost_diff))
p1 = ggplot(tmp %>% filter(between(cost_diff, -150,150)) ,
aes(x = cost_diff, color = claim, fill = claim))+
geom_histogram(aes(y=..density..), position = 'identity',
alpha = 0.2)+
geom_density(alpha = .3)+
geom_vline(data = mu, aes(xintercept = mn, color = claim), linetype = 'dashed')+
scale_color_manual(values = cbp[1:3])+
scale_fill_manual(values = cbp[1:3])+
ggtitle('Difference in the number of claims')+
ylab(label = 'Density')+
xlab(label = 'Claims frequency difference (per year)')+
theme_classic()+
theme(legend.position = c(.2,.6))
ggsave(p1, filename = paste(report_directory, 'Cystic_Fibrosis_claims_plot_year.png', sep = ""),
dpi = 600, width = 7.2, height = 5.5, units = 'in')
## costs
for (i in c('all','Pharmacy','Without Pharmacy')){
df = pt0 %>%
group_by(z_patid) %>%
summarise(start_day = min(fst_dt),
last_day = max(fst_dt),
first_date = max(first_date)) %>%
mutate(days_before = as.numeric(first_date - start_day),
days_after = as.numeric(last_day -first_date))
if (i == 'all'){
df2 = pt0 %>%
group_by(z_patid, treatment) %>%
summarise(cost = sum(cost)) %>%
spread(treatment, cost)
}else{
if(i == 'Pharmacy'){
df2 = pt0 %>%
filter(claim_type=='Pharmacy') %>%
group_by(z_patid, treatment) %>%
summarise(cost = sum(cost)) %>%
spread(treatment, cost)
}else{
df2 = pt0 %>%
filter(claim_type!='Pharmacy') %>%
group_by(z_patid, treatment) %>%
summarise(cost = sum(cost)) %>%
spread(treatment, cost)
}
}
df3 = df2 %>%
drop_na() %>%
left_join(df) %>%
filter(days_before>90, days_after >90)
df3 = df3 %>%
mutate(daily_before = Before/days_before,
daily_after = After/days_after,
cost_diff = daily_after-daily_before)
df3$claim = i
wl = wilcox.test(df3$cost_diff)
pvals = append(pvals,wl$p.value)
mean_vec =append(mean_vec,mean(df3$cost_diff))
median_vec = append(median_vec,median(df3$cost_diff))
if(i =='all'){
tmp = df3
}else{
tmp = tmp %>% bind_rows(df3)
}
}
tmp$cost_diff = tmp$cost_diff*365/1000
mu = tmp %>% group_by(claim) %>% summarise(mn = mean(cost_diff))
p2 = ggplot(tmp %>% filter(between(cost_diff, -500, 800)),
aes(x = cost_diff, color = claim, fill = claim))+
geom_histogram(aes(y=..density..), position = 'identity',
alpha = 0.2)+
geom_density(alpha = .3)+
geom_vline(data = mu, aes(xintercept = mn, color = claim), linetype = 'dashed')+
scale_color_manual(values = cbp[1:3])+
scale_fill_manual(values = cbp[1:3])+
ggtitle('Difference in the costs')+
ylab(label = 'Density')+
xlab(label = 'Costs difference (thousands of dollars per year)')+
theme_classic()
ggsave(p2, filename = paste(report_directory, 'Cystic_Fibrosis_costs_plot_year.png', sep = ""),
dpi = 600, width = 7.2, height = 5.5, units = 'in')
p3 = cowplot::plot_grid(p1,byrow = F,
p2+theme(legend.position = 'none' ),nrow = 1,
labels = c('a', 'b')
)
ggsave(p3, filename = paste(report_directory, 'Cystic_Fibrosis_comb_plot_year.png', sep = ""),
dpi = 600, width = 10, height = 3, units = 'in')
p3_cf_dist = cowplot::plot_grid(p1+theme(axis.title.x = element_blank()),
p2+theme(legend.position = 'none',
axis.title.x = element_blank(),
axis.title.y = element_blank()),
byrow = F,nrow = 1,
labels = c('a', 'b')
)