-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmle_itt_analysis_eda.R
263 lines (216 loc) · 14.2 KB
/
tmle_itt_analysis_eda.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
#################################################################
# Descriptive tables and plots for ITT analysis #
#################################################################
print(paste0("Summarizing results from output directory: ", output_dir))
blind.drugs <- TRUE
slides <- TRUE
plot.legend <- FALSE
print(paste0("Generating tables and plots for outcome:", outcome, "; Condition: ", condition))
outcome.labels <-list("combined"=c("diabetes diagnosis or death within 36 months"),
"diabetes"=c("diabetes diagnosis within 36 months"),
"death"=c("death within 36 months"))
condition.labels <- list("none"=c("all patients"),
"schizophrenia"=c("patients diagnosed with Schizophrenia"),
"mdd"=c("patients diagnosed with MDD"),
"black"=c("black patients"),
"white"=c("white patients"),
"latino"=c("latino patients"))
blinded.labels <- c("Reference","A","B","C","D","E")
if(use.SL){
estimator.labels <- c("TMLE-multi. (SL)", "TMLE-bin. (SL)", "G-Comp. (SL)", "IPTW-multi. (SL)", "IPTW-bin. (SL)")
}else{
estimator.labels <- c("TMLE-multi. (GLM)", "TMLE-bin. (GLM)", "G-Comp. (GLM)", "IPTW-multi. (GLM)", "IPTW-bin. (GLM)")
}
## Summary statistics table
if(condition=="none" & outcome=="combined" & use.SL & weights.loc=='none'){
print(tableNominal(data.frame(L.unscaled,A,Y.combined,Y.death, Y.diabetes)[c("California","Georgia","Iowa",
"Mississippi","Oklahoma","West_Virginia","black","latino","white","mdd","schiz","year","female",
"payer_index_mdcr","preperiod_ever_psych","preperiod_ever_metabolic","preperiod_ever_other","preperiod_ever_mt_gluc_or_lip",
"preperiod_ever_rx_antidiab", "preperiod_ever_rx_cm_nondiab", "preperiod_ever_rx_other","Y.combined","Y.death", "Y.diabetes")], group=A, prec = 3, cumsum=FALSE, longtable = FALSE))
print(tableContinuous(data.frame(L.unscaled,A)[c("calculated_age","preperiod_drug_use_days","preperiod_er_mhsa","preperiod_er_nonmhsa","preperiod_er_injury","preperiod_cond_mhsa",
"preperiod_cond_nonmhsa","preperiod_cond_injury","preperiod_los_mhsa","preperiod_los_nonmhsa","preperiod_los_injury")], group=A, prec = 3, cumsum=FALSE, stats= c("n", "min", "mean", "max", "s"), longtable = FALSE))
}
if(outcome=="combined" & condition=="none" & use.SL){
## Print share bounded
print(paste0("% truncated, multinomial: ", colSums((g_preds > gbound[2]) | g_preds < gbound[1])/n))
## Print share bounded
print(paste0("% truncated, multiple binary: ", colSums((g_preds_bin > gbound[2]) | g_preds_bin < gbound[1])/n))
## Print ESS
print(paste0("ESS (multinomial, ATE): ", ess_ate_tmle))
print(paste0("ESS (multiple binary, ATE): ", ess_ate_tmle_bin))
## Print multinomial preds
print("Multinomial predictions")
print(tableContinuous(data.frame(g_preds), cumsum=FALSE, stats= c("n", "min", "mean", "max", "s"), longtable = FALSE,prec =3))
print("Multiple binary predictions")
print(tableContinuous(data.frame(g_preds_bin), cumsum=FALSE, stats= c("n", "min", "mean", "max", "s"), longtable = FALSE,prec =3))
}
## Forest plot for ATEs
# Create data for plot
if(blind.drugs){
comparisons <- blinded.labels[-1]
}else{
comparisons <- proper(colnames(g_preds))[2:6]
}
ates.dat <- data.frame(x =rep(comparisons, 2),
y = c(ATE_tmle,ATE_tmle_bin),
y.lo = c(ATE_CI_tmle_lower,ATE_CI_tmle_bin_lower),
y.hi = c(ATE_CI_tmle_upper,ATE_CI_tmle_bin_upper))
ates.dat$x <- as.factor(ates.dat$x)
ates.dat$Analysis <- c(rep("TMLE-Multinomial (super learner)",each=length(comparisons)), rep("TMLE-Binomial (super learner)",each=length(comparisons)))
saveRDS(ates.dat,paste0(output_dir,"tmle_",outcome,"_",outcome.type, "_use_SL_",use.SL, "_", condition, "_", use.SL, "_","ates_dat.rds"))
if(use.SL & condition!="none"){
estimand <- TeX('$CATE_{j,j^*}$')
cates.dat <- data.frame(x =rep(comparisons,2),
y = c(CATE_tmle,CATE_tmle_bin),
y.lo = c(CATE_CI_tmle_lower,CATE_CI_tmle_bin_lower),
y.hi = c(CATE_CI_tmle_upper,CATE_CI_tmle_bin_upper))
cates.dat$x <- as.factor(cates.dat$x)
cates.dat$Analysis <- c(rep("TMLE-Multinomial (super learner)",each=length(comparisons)),rep("TMLE-Binomial (super learner)",each=length(comparisons)))
cate.plot <- ForestPlot(cates.dat,
xlab=estimand,ylab="Treatment drug") +
labs(color="Estimator:") +
scale_x_discrete(limits = rev) +
theme(legend.position = "bottom",legend.margin=margin(0,0,0,0), legend.justification="left",
legend.box.margin=margin(-5,-5,-5,-5),legend.text=element_text(size=10)) +
theme(plot.title = element_text(hjust = 0.5, family="serif", size=16), plot.subtitle = element_text(hjust = 0.5, family="serif", size=12)) +
theme(axis.title=element_text(family="serif", size=16)) +
theme(axis.text.y=element_text(family="serif", size=14)) +
theme(axis.text.x=element_text(family="serif", size=14,angle = 45, vjust = 0.5, hjust=1)) +
theme(strip.text.x = element_text(family="serif", size = 14)) +
theme(strip.text.y = element_text(family="serif", size = 14)) +
theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l =0))) +
theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l =0))) +
theme(panel.spacing = unit(1, "lines"))
cate.plot <- cate.plot +
annotate("segment", x = 0.5, xend = 0.5, y = 0.002, yend = 0.025,
colour = "blue", size = 1, arrow = arrow(length = unit(0.1, "inches"), ends="last", type="closed")) +
annotate("segment", x = 0.5, xend = 0.5, y = -0.002, yend = -0.025,
colour = "blue", size = 1, arrow = arrow(length = unit(0.1, "inches"), ends="last", type="closed")) +
annotate("text", x = 0.65, y = -0.012, label = "Favors treatment")
cate.plot <- cate.plot + annotate("text", x = 0.65, y = 0.012, label = "Favors Reference")
if(slides){
if(condition=="none"){
cate.plot <- cate.plot + ggtitle(paste0("Outcome: ", outcome.labels[[outcome]]))
}else{
cate.plot <- cate.plot + ggtitle(paste0("Outcome: ", outcome.labels[[outcome]]), subtitle=paste0("Condition: ", condition.labels[[condition]]))
}
}
if(blind.drugs){
if(slides){
ggsave(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_itt_CATE_blinded_slides.png"), plot=cate.plot, scale=1.3)
}else{
ggsave(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_itt_CATE_blinded.png"), plot=cate.plot, scale=1.25)
}
}else{
if(slides){
ggsave(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_itt_CATE_slides.png"), plot=cate.plot, scale=1.25)
}else{
ggsave(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_itt_CATE.png"), plot=cate.plot, scale=1.25)
}
}
}else{
estimand <- TeX('$ATE_{j,j^*}$')
ate.plot <- ForestPlot(ates.dat,
xlab=estimand,ylab="Treatment") +
# scale_colour_manual(values= c(gg_color_hue(3)[1],gg_color_hue(3)[3])) +
labs(color="Estimator: ") +
scale_x_discrete(limits = rev) +
# theme(legend.position = "bottom",legend.margin=margin(1,5,5,5), legend.justification="center",
# legend.box.margin=margin(0,0,0,0),legend.text=element_text(size=14), legend.key.width = unit(0.75, "cm"), legend.spacing.x = unit(0.75, 'cm'), legend.spacing.y = unit(0.75, 'cm')) +
theme(plot.title = element_text(hjust = 0.5, family="serif", size=16), plot.subtitle = element_text(hjust = 0.5, family="serif", size=14)) +
theme(axis.title=element_text(family="serif", size=16)) +
theme(axis.text.y=element_text(family="serif", size=16)) +
theme(axis.text.x=element_text(family="serif", size=14, angle = 0, vjust = 0.5, hjust=0.25)) +#angle = 45, , hjust=1
theme(legend.text=element_text(family="serif", size=16)) +
theme(legend.title=element_text(family="serif", size=16)) +
theme(strip.text.x = element_text(family="serif", size=16)) +
theme(strip.text.y = element_text(family="serif", size=16)) +
theme(axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l =0))) +
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l =0))) +
theme(panel.spacing = unit(1, "lines"))
ate.plot <- ate.plot +
annotate("segment", x = 0.5, xend = 0.5, y = 0.01, yend = 0.03,
colour = "blue", size = 1, arrow = arrow(length = unit(0.1, "inches"), ends="last", type="closed")) +
annotate("segment", x = 0.5, xend = 0.5, y = -0.01, yend = -0.03,
colour = "blue", size = 1, arrow = arrow(length = unit(0.1, "inches"), ends="last", type="closed")) +
annotate("text", x = 0.65, y = -0.02, label = "Favors treatment")
ate.plot <- ate.plot + annotate("text", x = 0.65, y = 0.02, label = "Favors Reference")
if(slides){
if(condition=="none"){
ate.plot <- ate.plot + ggtitle(paste0("Outcome: ", outcome.labels[[outcome]]))
}else{
ate.plot <- ate.plot + ggtitle(paste0("Outcome: ", outcome.labels[[outcome]]), subtitle=paste0("Condition: ", condition.labels[[condition]]))
}
}
if(blind.drugs){
if(slides){
ggsave(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_itt_ATE_blinded_slides.png"), plot=ate.plot, scale=1.3)
}else{
ggsave(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_itt_ATE_blinded.png"), plot=ate.plot+theme(legend.position = "none"), scale=1.25)
}
}else{
if(slides){
ggsave(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_itt_ATE_slides.png"), plot=ate.plot, scale=1.25)
}else{
ggsave(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_itt_ATE.png"), plot=ate.plot+theme(legend.position = "none"))
}
}
if(plot.legend){
# get legend plot
legend <- cowplot::get_legend(ate.plot)
png(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_legend.png"),width = 380, height = 380)
grid.newpage()
grid.draw(legend)
dev.off() # Close the file
}
}
# Love plot to report covariate balance
if(use.SL & outcome=="combined" & condition=="none"){
# using WeightIt to generate weights with multinomial
W.out <- weightit(A ~ L.unscaled,
method = NULL,
estimand = "ATE",
ps = as.vector(rowSums(obs.treatment*g_preds))) # value corresponds to the probability of being in the treatment actually received
W.out.bin <- weightit(A ~ L.unscaled,
method = NULL,
estimand = "ATE",
ps = as.vector(rowSums(obs.treatment*g_preds_bin)))
# asssessing balance numerically
print(bal.tab(x= W.out, weights = list("Binomial (SL)"= W.out.bin), s.d.denom = "pooled", un = TRUE))
cov.names <- c("California","Georgia","Iowa","Mississippi","Oklahoma","West Virginia","Black","Latino","White","MDD","Schiz.","Medicare","Year",
"Female","Psychiatric comorbidity","Metabolic risk","Other chronic conditions","Antipsychotic drug use (days)","Lipid or glucose lab tests",
"Antidiabetic","Cardiometabolic disorders","Cardiometabolic effects","Age","Psychiatric ER visits","Non-psychiatric ER visits",
"Injury-related ER visits","Psychiatric outpatient visits","Non-psychiatric outpatient visits","Injury-related outpatient visits","Psychiatric inpatient days","Non-psychiatric inpatient (days)","Injury-related inpatient (days)")
names(cov.names) <- c("California","Georgia","Iowa","Mississippi","Oklahoma","West_Virginia","black","latino","white","mdd","schiz","payer_index_mdcr","year",
"female","preperiod_ever_psych","preperiod_ever_metabolic","preperiod_ever_other","preperiod_drug_use_days","preperiod_ever_mt_gluc_or_lip",
"preperiod_ever_rx_antidiab","preperiod_ever_rx_cm_nondiab","preperiod_ever_rx_other","calculated_age","preperiod_er_mhsa","preperiod_er_nonmhsa",
"preperiod_er_injury","preperiod_cond_mhsa","preperiod_cond_nonmhsa","preperiod_cond_injury","preperiod_los_mhsa","preperiod_los_nonmhsa","preperiod_los_injury")
png(paste0(output_dir,"tmle_", outcome, "_",condition, "_use_SL_",use.SL, "_love_plot.png"))
#Summarizing balance in a Love plot
love.plot(A ~ L.unscaled,
weights=list("Multinomial (SL)"=W.out, "Binomial (SL)"=W.out.bin),
thresholds = c(m = .2, m = .1),
var.order = c("Female", "Medicare", "Year", "California", "Georgia", "Iowa", "Mississippi", "Oklahoma", "West Virginia","Black", "Latino", "White", "MDD", "Schiz.", "Psychiatric comorbidity", "Metabolic risk", "Other chronic conditions", "Lipid or glucose lab tests", "Antidiabetic", "Cardiometabolic disorders", "Cardiometabolic effects"),
s.d.denom = "pooled",
binary = "std",
continuous = "std",
colors = c("black", "#F8766D", "#A3A500"),
shapes = c("circle", "square", "triangle"),
line = FALSE,
limits = c(0.0,1),
alpha = 0.7,
size = 3,
wrap = 30,
var.names = cov.names,
title = "", # covariate balance: max. across treatment pairs
sample.names=c("Raw", "Multinomial (SL)","Binomial (SL)"),
labels = FALSE,
position = "none",
themes =list(theme(axis.title=element_text(family="serif", size=14)),
theme(axis.text.x=element_text(family="serif", size=14)),
theme(axis.text.y=element_text(family="serif", size=14)),
theme(axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l =0))),
theme(axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l =0))),
theme(panel.spacing = unit(1, "lines"))))
dev.off() # Close the file
}