|
a |
|
b/function/plot_evaluation.R |
|
|
1 |
library(dplyr) |
|
|
2 |
library(ggplot2) |
|
|
3 |
library(ggpubr) |
|
|
4 |
library(reshape2) |
|
|
5 |
library(rstatix) |
|
|
6 |
######### test ############ |
|
|
7 |
# out = out_all |
|
|
8 |
# folder = 'results/CV_all_patients' |
|
|
9 |
############# res ############### |
|
|
10 |
plot_evaluation = function(folder,out){ |
|
|
11 |
|
|
|
12 |
if (!file.exists(folder)) dir.create(folder) |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
# out |
|
|
17 |
out2 = out %>% rowwise() %>% mutate(repeats = unlist(strsplit(column_label, split="[.]"))[2]) %>% as.data.frame() |
|
|
18 |
get_out = function(rep,model,time,v,tp,k){ |
|
|
19 |
dt = out2 %>% subset(.,repeats==rep&model2==model&variable==v&type==tp) |
|
|
20 |
n= length(na.omit(dt[,as.character(time)])) |
|
|
21 |
mean = mean(dt[,as.character(time)],na.rm = TRUE) |
|
|
22 |
return(dynGet(k)) |
|
|
23 |
} |
|
|
24 |
new_out = expand.grid( |
|
|
25 |
'rep' = unique(out2$rep), |
|
|
26 |
"model2" = unique(out2$model2) , |
|
|
27 |
"time" = as.character(predict_times), |
|
|
28 |
"variable"= c("AUC","PE"), |
|
|
29 |
"type"= c('training','testing') |
|
|
30 |
) |
|
|
31 |
|
|
|
32 |
new_out$value = mapply(get_out,rep=new_out$rep, model=new_out$model2,time=new_out$time, |
|
|
33 |
v=new_out$variable,tp=new_out$type,MoreArgs = list(k="mean")) |
|
|
34 |
NA_rep = new_out[is.na(new_out$value),c('rep','model2','type','time')] |
|
|
35 |
for ( i in 1:nrow(NA_rep)){ |
|
|
36 |
new_out[new_out$rep==NA_rep$rep[i]& |
|
|
37 |
new_out$model2==NA_rep$model2[i]& |
|
|
38 |
new_out$type==NA_rep$type[i]& |
|
|
39 |
new_out$time==NA_rep$time[i],'value'] = NA |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
write.csv(new_out %>% mutate(model2 = recode(model2,"LM_bi" = "cox(Landmark)","LM_P2"="cox(Postsurgical)")), |
|
|
43 |
file = paste0(folder,"/outdata.csv"),quote = F) |
|
|
44 |
|
|
|
45 |
# res |
|
|
46 |
get_res = function(model,t,v,tp,k){ |
|
|
47 |
dt = new_out %>% subset(.,model2==model&variable==v&type==tp) |
|
|
48 |
n= length(na.omit(dt[dt$time==t,])) |
|
|
49 |
mean = mean(dt[dt$time==t,'value'],na.rm = TRUE) |
|
|
50 |
return(dynGet(k)) |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
res_table = expand.grid( |
|
|
54 |
"model2" = unique(new_out$model2), |
|
|
55 |
"variable"= c("AUC","PE"), |
|
|
56 |
"time" = as.character(predict_times), |
|
|
57 |
"type"= c('training','testing') |
|
|
58 |
) |
|
|
59 |
|
|
|
60 |
res_table$value = mapply(get_res,model=res_table$model2,t=res_table$time, |
|
|
61 |
v=res_table$variable,tp=res_table$type,MoreArgs = list(k="mean")) |
|
|
62 |
res_table = dcast(res_table,...~model2) %>% rename( "cox-Landmark" = LM_bi, 'cox-Postsurgical' = LM_P2) %>% arrange(variable,time,type) |
|
|
63 |
write.csv(res_table,file = paste0(folder,"/res_table.csv"),quote = F) |
|
|
64 |
|
|
|
65 |
|
|
|
66 |
# label |
|
|
67 |
rs_label = c("AUC"="AUROC","PE"="Prediction Error") |
|
|
68 |
rs_label2 = c('12' = "12 months",'15' = "15 months") |
|
|
69 |
|
|
|
70 |
## between JMs using different association structures |
|
|
71 |
df2 = subset(new_out,model2 %in% c('JM_cumulative','JM_value','JM_value-slope')) %>% |
|
|
72 |
rename( evaluation = variable ) |
|
|
73 |
|
|
|
74 |
ggplot(df2,aes(x=model2,y=value,fill=type))+ |
|
|
75 |
geom_boxplot(outlier.shape = NA)+ |
|
|
76 |
geom_point(pch = 21, position = position_jitterdodge(jitter.width=0.1))+ |
|
|
77 |
scale_fill_brewer(palette ='Pastel1',)+ |
|
|
78 |
scale_color_brewer(palette ='Pastel1')+ |
|
|
79 |
scale_y_continuous(name = "")+ |
|
|
80 |
scale_x_discrete(name="Joint Models", |
|
|
81 |
limits=c('JM_value','JM_value-slope','JM_cumulative'), |
|
|
82 |
breaks=c('JM_value','JM_value-slope','JM_cumulative'), |
|
|
83 |
labels=c('value','value\n+slope','value\n+cumulative'))+ |
|
|
84 |
facet_grid(rows = vars(evaluation) ,cols = vars(time),scales="free",space="free",switch = 'y', |
|
|
85 |
labeller = labeller(evaluation=rs_label,time = rs_label2))+ |
|
|
86 |
theme_bw()+ |
|
|
87 |
theme( |
|
|
88 |
panel.grid.major = element_blank(), panel.grid.minor = element_blank(), |
|
|
89 |
plot.background = element_blank(), |
|
|
90 |
# legend.position = 'none', |
|
|
91 |
axis.text = element_text(size=12), |
|
|
92 |
axis.title = element_text(size=15), |
|
|
93 |
strip.background=element_blank(), |
|
|
94 |
strip.text=element_text(size = 14), |
|
|
95 |
strip.placement = 'outside' |
|
|
96 |
) |
|
|
97 |
|
|
|
98 |
ggsave(paste0(folder,"/betweenJMs.pdf"),device='pdf',width=8,height=8) |
|
|
99 |
|
|
|
100 |
## comparison between jm and cox |
|
|
101 |
|
|
|
102 |
df = subset(new_out,model2 %in% c('JM_cumulative','LM_P2','LM_bi')) %>% rename( evaluation = variable ) |
|
|
103 |
|
|
|
104 |
stat.test <- df %>% |
|
|
105 |
group_by(time,evaluation,type) %>% |
|
|
106 |
wilcox_test(value ~ model2,ref.group = 'JM_cumulative') %>% |
|
|
107 |
add_significance("p",cutpoints = c( 0,0.001, 0.01, 0.05, 1), |
|
|
108 |
symbols = c("***", "**", "*", "ns")) %>% |
|
|
109 |
add_xy_position(x='model2',step.increase=0.03) |
|
|
110 |
|
|
|
111 |
# df %>% group_by(time,evaluation,type) %>% wilcox_effsize(value ~ model2,ref.group = 'JM_cumulative') |
|
|
112 |
|
|
|
113 |
ggplot(subset(df,type == "testing"),aes(x=model2,y=value,fill=time))+ |
|
|
114 |
geom_boxplot(outlier.shape = NA)+ |
|
|
115 |
geom_point(position = position_jitterdodge(jitter.width=0.3),shape = 21)+ |
|
|
116 |
scale_fill_brewer(palette ='Pastel1')+ |
|
|
117 |
scale_color_brewer(palette ='Pastel1')+ |
|
|
118 |
scale_y_continuous(name = "")+ |
|
|
119 |
scale_x_discrete(name="Models", |
|
|
120 |
breaks=c('JM_cumulative','LM_P2','LM_bi'), |
|
|
121 |
labels=c('Joint model','Postsurgical\nCox','Landmark\nCox'))+ |
|
|
122 |
# labs(title = paste0('Testing Sets'))+ |
|
|
123 |
facet_grid(rows = vars(evaluation) ,cols = vars(time),scales="free",space="free",switch = 'y', |
|
|
124 |
labeller = labeller(evaluation=rs_label,time = rs_label2))+ |
|
|
125 |
stat_pvalue_manual(subset(stat.test,type == "testing"), label = "p.signif",hide.ns = F,tip.length = 0)+ |
|
|
126 |
theme_bw()+ |
|
|
127 |
theme( |
|
|
128 |
panel.grid.major = element_blank(), panel.grid.minor = element_blank(), |
|
|
129 |
plot.background = element_blank(), |
|
|
130 |
legend.position = 'none', |
|
|
131 |
axis.text = element_text(size=10), |
|
|
132 |
axis.title = element_text(size=12), |
|
|
133 |
strip.background=element_blank(), |
|
|
134 |
strip.text=element_text(size = 12), |
|
|
135 |
strip.placement = 'outside' |
|
|
136 |
) |
|
|
137 |
|
|
|
138 |
ggsave(paste0(folder,"/JMvsCox_testing.pdf"),device='pdf',width=6,height=6) |
|
|
139 |
|
|
|
140 |
ggplot(subset(df,type == "training"),aes(x=model2,y=value,fill=time))+ |
|
|
141 |
geom_boxplot(outlier.shape = NA)+ |
|
|
142 |
geom_point(pch = 21, position = position_jitterdodge(jitter.width=0.3))+ |
|
|
143 |
scale_fill_brewer(palette ='Pastel1')+ |
|
|
144 |
scale_color_brewer(palette ='Pastel1')+ |
|
|
145 |
scale_y_continuous(name = "")+ |
|
|
146 |
scale_x_discrete(name="Models", |
|
|
147 |
breaks=c('JM_cumulative','LM_P2','LM_bi'), |
|
|
148 |
labels=c('Joint model','Postsurgical\nCox','Landmark\nCox'))+ |
|
|
149 |
# labs(title = paste0('Training Sets'))+ |
|
|
150 |
facet_grid(rows = vars(evaluation) ,cols = vars(time),scales="free",space="free",switch = 'y', |
|
|
151 |
labeller = labeller(evaluation=rs_label,time = rs_label2))+ |
|
|
152 |
stat_pvalue_manual(subset(stat.test,type == "testing"), label = "p.signif",hide.ns = F,tip.length = 0)+ |
|
|
153 |
theme_bw()+ |
|
|
154 |
theme( |
|
|
155 |
panel.grid.major = element_blank(), panel.grid.minor = element_blank(), |
|
|
156 |
plot.background = element_blank(), |
|
|
157 |
legend.position = 'none', |
|
|
158 |
axis.text = element_text(size=12), |
|
|
159 |
axis.title = element_text(size=15), |
|
|
160 |
strip.background=element_blank(), |
|
|
161 |
strip.text=element_text(size = 14), |
|
|
162 |
strip.placement = 'new_outside' |
|
|
163 |
) |
|
|
164 |
ggsave(paste0(folder,"/JMvsCox_training.pdf"),device='pdf',width=6,height=6) |
|
|
165 |
} |
|
|
166 |
|