-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcmcabn_result_postproc_analysis.Rmd
1070 lines (895 loc) · 38.6 KB
/
mcmcabn_result_postproc_analysis.Rmd
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "ABN MCMC Postprocessing and Result Analysis"
output:
rmarkdown::html_document:
toc: true
toc_depth: 2
toc_float: true
df_print: paged
rmarkdown::html_vignette: default
# runtime: shiny
vignette: >
%\VignetteIndexEntry{ABNAIA results analysis}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::knitr}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
cache.path="./"
)
```
```{r message=FALSE, warning=FALSE}
rm(list = ls())
library(parallel)
library(dplyr)
library(ggplot2)
library(tidyr)
library(reshape2)
library(svglite)
library(mcmcabn)
library(bnlearn)
library(bnaiaR)
library(coda)
library(abn)
```
# Document Settings
```{r}
SAVEPLOTS <- F
EXPNO <- "ABNmultinomial"
FILENAME <- paste0("exp", EXPNO)
FILENAMEbase <- "/inst/extdata/mcmcabn/results/raw/"
PLOTPATH <- "/results/"
DATPATH <- "/results/raw/"
PLOTWIDTH = 16
PLOTHEIGHT = 9
```
# Disclosure
BEWARE, THIS IS DRAFT CODE WITH PERSONAL NOTES OF MATTEO NOT INTENDED TO BE
REPRODUCIBLE IN ANY WAY!
# Load Data and take a first glimpse
```{r}
load(paste0(getwd(), "/results/raw/", FILENAME, "_final.RData"))
```
```{r}
FILENAME <- paste0(FILENAME, "_100k")
FILENAME
```
List of all mcmcabn outputs.
```{r}
length(mcmc.out.list)
```
Show first mcmcabn output.
```{r}
str(mcmc.out.list[1])
```
DAG from first mcmcabn output. [rows, columns, dags]. dags = number of returned DAGS specified in mcmcscheme.
```{r}
str(mcmc.out.list[[1]]$dags)
```
## Max. parents plot
```{r}
df <- net.scores %>%
mutate(
scoretype = as.factor(scoretype),
npar = as.factor(npar),
scorevalue = as.numeric(scorevalue)) %>%
group_by(scoretype) %>%
summarise(
scorevalue.norm = (1 - abs(1 - scorevalue / max(scorevalue))) * 100,
.groups = "keep",
npar = npar,
scorevalue = scorevalue) %>%
ungroup(
)
df
```
### Plot Network score by increasing max parents
```{r}
plt.rel <- ggplot(df, aes(x=npar, color = scoretype, y = scorevalue.norm)) +
geom_point(aes(shape = scoretype, alpha = 0.5, size = 1))+
# facet_wrap(.~dist_type)+
labs(title = "Relative network score per no. parent nodes",
x="number of parent nodes",
y="network score [%]") +
scale_x_discrete(limits = unique(df$npar))+
theme_bw()
plt.abs <- ggplot(df, aes(color = scoretype, y = scorevalue, group = scoretype)) +
geom_point(aes(x=npar, shape = scoretype, alpha = 0.5, size = 1))+
# facet_wrap(.~dist_type)+
labs(title = "Absolute network score per no. parent nodes",
x="number of parent nodes",
y="network score") +
scale_x_discrete(limits = unique(df$npar))+
theme_bw()
plt.comb <- cowplot::plot_grid(plt.rel, plt.abs, labels = "AUTO", ncol = 1)
if (SAVEPLOTS){
plotname <-paste0(getwd(), "/results/", FILENAME, "_netscore_per_no.parent_nodes")
ggsave(paste0(plotname, ".png"),
plot = plt.comb,
width =9, height = 10, dpi = 600)
} else {
plt.comb
}
```
### Plot DAGs with different max parents
```{r}
parents <- 1
plt <- list()
for (i in net.scores.dags) {
# plotAbn(i, dist, edge.direction = "undirected")
dag = bnlearn::empty.graph(names(abndata))
bnlearn::amat(dag) = t(i$dag)
temp.plt <- bnlearn::graphviz.plot(dag,
shape = "rectangle",
main = paste0("DAG with max.par=", parents),
render = F)
plt[[parents]] <- temp.plt
parents <- parents+1
}
png(filename = paste0(getwd(), "/results/", FILENAME, "_maxparentDAGs.png"),width = 1920, height = 1080)
par(mfrow=c(3,3))
parents <- 1
for(i in plt){
plot(i)
title(paste0("DAG with max.par=", parents))
parents <- parents + 1
}
dev.off()
```
# Preprocess
## Thin and Burn-in
thinning: mcmc.scheme = c(number of returned DAGS, thinned steps, length of burn-in phase)
```{r}
# THINNING <- 2 # keep every second draw
# BURNIN.LEN <- 2500 # remove the first n draws
THINNING <- 7
BURNIN.LEN <- 25000 # remove the first n draws
mcmc.out.list.burn <- postBURNin(mcmc.out.list = mcmc.out.list, burnin.length = BURNIN.LEN)
mcmc.out.list.thin <- postTHINN(mcmc.out.list = mcmc.out.list, thinningsteps = THINNING)
mcmc.out.list.burn.thin <- postTHINN(mcmc.out.list = mcmc.out.list.burn, thinningsteps = THINNING)
str(mcmc.out.list.burn[1])
str(mcmc.out.list.thin[1])
str(mcmc.out.list.burn.thin[1])
```
## Reformat
```{r}
# thinned only
mc.out.thin.1 <- mcmc.out.list.thin[[1]]
mc.out.thin.2 <- mcmc.out.list.thin[[2]]
mc.out.thin.3 <- mcmc.out.list.thin[[3]]
mc.out.thin.4 <- mcmc.out.list.thin[[4]]
mc.out.thin.score.1 <- mcmc(mc.out.thin.1$scores)
mc.out.thin.score.2 <- mcmc(mc.out.thin.2$scores)
mc.out.thin.score.3 <- mcmc(mc.out.thin.3$scores)
mc.out.thin.score.4 <- mcmc(mc.out.thin.4$scores)
list.mc.out.thin.score <- mcmc.list(mc.out.thin.score.1, mc.out.thin.score.2, mc.out.thin.score.3, mc.out.thin.score.4)
# burned and thinned
mc.out.burn.thin.1 <- mcmc.out.list.burn.thin[[1]]
mc.out.burn.thin.2 <- mcmc.out.list.burn.thin[[2]]
mc.out.burn.thin.3 <- mcmc.out.list.burn.thin[[3]]
mc.out.burn.thin.4 <- mcmc.out.list.burn.thin[[4]]
mc.out.burn.thin.score.1 <- mcmc(mc.out.burn.thin.1$scores)
mc.out.burn.thin.score.2 <- mcmc(mc.out.burn.thin.2$scores)
mc.out.burn.thin.score.3 <- mcmc(mc.out.burn.thin.3$scores)
mc.out.burn.thin.score.4 <- mcmc(mc.out.burn.thin.4$scores)
# list.mc <- mcmc.list(mc.score.1, mc.score.2, mc.score.3, mc.score.4)
list.mc.out.burn.thin.score <- mcmc.list(mc.out.burn.thin.score.1, mc.out.burn.thin.score.2, mc.out.burn.thin.score.3, mc.out.burn.thin.score.4)
mc.out.burn.thin.dag.1 <- mc.out.burn.thin.1$dags
mc.out.burn.thin.dag.2 <- mc.out.burn.thin.2$dags
mc.out.burn.thin.dag.3 <- mc.out.burn.thin.3$dags
mc.out.burn.thin.dag.4 <- mc.out.burn.thin.4$dags
list.mc.out.burn.thin.dag <- abind::abind(mc.out.burn.thin.dag.1, mc.out.burn.thin.dag.2, mc.out.burn.thin.dag.3, mc.out.burn.thin.dag.4)
```
# Best fitting DAG
number of max parents per node.
```{r}
max.par
```
total arcs.
```{r}
sum(dag.maxpar$dag)
summary(fabn.maxpar)
```
# MCMC Quality check
## Gelman
```{r}
gelman.diag(x = list.mc.out.thin.score,autoburnin = F) # if higher than 1.1 or 1.2, run chain longer to improve convergence
gelman.plot(list.mc.out.thin.score, autoburnin = F)
if (SAVEPLOTS){
PLOTNAME <- "_gelmanplot"
dev.print(svg, filename = paste0(getwd(), PLOTPATH, FILENAME, PLOTNAME, ".svg"), width = PLOTWIDTH, height = PLOTHEIGHT)
dev.off()
} else {
gelman.plot(list.mc.out.thin.score, autoburnin = T)
}
```
## Raftery
calculate no. of iterations and no. of burn-ins to satisfy specified conditions.
```{r}
raftery.diag(unlist(list.mc.out.thin.score))
```
## Heidelberg and Welch Diagnostics
test H0: The Markov Chain is from a stationary distribution. If not passed, chain must run longer.
```{r}
for (chain in 1:length(list.mc.out.thin.score)){
print("------------------------")
print(paste("Chain no: ", chain))
print(heidel.diag(list.mc.out.thin.score[[chain]]))
}
```
## Trace Plot
```{r}
mcmcabn::plot.mcmcabn(mcmc.out.list.burn.thin[[1]])
if (SAVEPLOTS){
PLOTNAME <- "_traceplot"
dev.print(svg, filename = paste0(getwd(), PLOTPATH, FILENAME, PLOTNAME, ".svg"), width = PLOTWIDTH, height = PLOTHEIGHT)
dev.off()
} else {
mcmcabn::plot.mcmcabn(mcmc.out.list.burn.thin[[1]])
}
```
```{r}
if (SAVEPLOTS){
PLOTNAME <- "_traceplot_maxscore"
mcmcabn::plot.mcmcabn(mcmc.out.list.burn.thin[[1]], max.score = TRUE)
dev.print(svg, filename = paste0(getwd(), PLOTPATH, FILENAME, PLOTNAME, ".svg"), width = PLOTWIDTH, height = PLOTHEIGHT)
dev.off()
} else {
mcmcabn::plot.mcmcabn(mcmc.out.list.burn.thin[[1]], max.score = TRUE)
}
```
```{r}
if (SAVEPLOTS){
PLOTNAME <- "_traceplot_classic"
plot(list.mc.out.burn.thin.score)
dev.print(svg, filename = paste0(getwd(), PLOTPATH, FILENAME, PLOTNAME, ".svg"), width = PLOTWIDTH, height = PLOTHEIGHT)
dev.off()
} else {
plot(list.mc.out.burn.thin.score)
}
```
```{r}
## traceplot
fabn <-fitAbn(dag = dag.maxpar$dag,
data.df = abndata,
data.dists = dist,
method = METHOD)
max.score <- -fabn$bic
dta <- data.frame(mc.out.thin.1[2:4],
mc.out.thin.2[2:4],
mc.out.thin.3[2:4],
mc.out.thin.4[2:4]) # Thinned but not burned
dta <- dta[,c(1,4,7,10)]
# dta <- dta[thin, ]
names(dta) <- c("Run1","Run2","Run3","Run4")
dta$X <- (1:length(dta$Run1))
dta <- reshape2::melt(dta, "X", value.name = "scores")
dta$cummax[1] <- dta$scores[1]
for (i in 2:length(dta$scores)) {
if (dta$scores[i] > dta$cummax[i - 1]) {
dta$cummax[i] <- dta$scores[i]
} else {
dta$cummax[i] <- dta$cummax[i - 1]
}
}
# Create a text
original_plot <- ggplot(data = dta, aes_string(x = "X", y="scores", color = "variable")) +
geom_line(alpha = 0.8,lwd=1.1) +
geom_hline(yintercept = max.score,linetype = "dashed", color = "red", alpha = 1) +
geom_text(aes(25, max.score, label = round(max.score,digits = 2), vjust = -0.5), color = "red", check_overlap = TRUE) +
labs(x = "DAG index", y = "DAG scores", colour = "MCMC:") +
ggpubr::theme_pubr()+
ylim(min(dta$scores),max(dta$scores))
# annotate("rect", xmin=0, xmax=BURNIN.LEN, ymin=min(dta$scores), ymax=max.score,alpha = .3) +
# geom_text(aes(BURNIN.LEN/THINNING*0.5, min(dta$scores), label = "Burn-in phase", vjust = -0.5, hjust=0), color = "black", check_overlap = TRUE)
# print(original_plot)
# Plot
y_density <- cowplot::axis_canvas(original_plot, axis = "y", coord_flip = TRUE) +
geom_density(data = dta, aes_string(x = "scores",fill = "factor(variable)"), alpha = 0.5) +
coord_flip()
cummax_plt <- ggplot(data = dta, aes_string(x = "X", y="cummax", color = "variable")) +
geom_line(alpha = 0.8,lwd=1.1, inherit.aes = T) +
geom_point(aes_string(color = "variable"), inherit.aes = T)+
geom_hline(yintercept = max.score,linetype = "dashed", color = "red", alpha = 1) +
geom_text(aes(25, max.score, label = round(max.score,digits = 2), vjust = -0.5), color = "red", check_overlap = TRUE) +
labs(x = "DAG index", y = "DAG scores", colour = "MCMC:") +
ggpubr::theme_pubr()+
ylim(min(dta$scores),max(dta$scores))
# annotate("rect", xmin=0, xmax=BURNIN.LEN, ymin=min(dta$scores), ymax=max.score, alpha = .3) +
# geom_text(aes(BURNIN.LEN/THINNING*0.5, min(dta$scores), label = "Burn-in phase", vjust = -0.5, hjust=0), color = "black", check_overlap = TRUE)
# cummax_plt
# create the combined plot
combined_plot <- cowplot::ggdraw(cowplot::insert_yaxis_grob(plot = original_plot, grob = y_density, position = "right"))
combined_cummax_plot <- cowplot::ggdraw(cowplot::insert_yaxis_grob(plot = original_plot, grob = cummax_plt, position = "right"))
# ggsave(paste0(FILENAMEbase, FILENAME, "traceplot_combined.png"),
# plot = cowplot::ggdraw(cowplot::insert_yaxis_grob(plot = original_plot, grob = y_density, position = "right")),
# width = 9,height = 7)
# dev.off()
# print(combined_plot)
if (SAVEPLOTS){
PLOTNAME <- paste0(getwd(), "/results/", FILENAME,"traceplot_allruns")
ggsave(paste0(PLOTNAME, ".svg"),
width = PLOTWIDTH, height = PLOTHEIGHT)
PLOTNAME <- paste0(getwd(), "/results/", FILENAME,"traceplot_allruns_combined")
ggsave(paste0(PLOTNAME, ".svg"),
width = PLOTWIDTH, height = PLOTHEIGHT)
PLOTNAME <- paste0(getwd(), "/results/", FILENAME,"traceplot_allruns_cummax")
ggsave(paste0(PLOTNAME, ".svg"),
width = PLOTWIDTH, height = PLOTHEIGHT)
} else {
original_plot
combined_plot
cummax_plt
}
```
# Consensus DAG
best dag trimmed for controlling overfitting
```{r}
# Best DAG not trimmed
dag.mcmc.boot <- apply(list.mc.out.burn.thin.dag, 1:2, mean)
colnames(dag.mcmc.boot) <- rownames(dag.mcmc.boot) <- names(dist)
# Best DAG Trimmed on THRESHOLD
dag.mcmc.boot.th <- dag.mcmc.boot
dag.mcmc.boot.th[dag.mcmc.boot.th>THRESHOLD]<-1
dag.mcmc.boot.th[dag.mcmc.boot.th<=THRESHOLD]<-0
# Plot Best DAG trimmed
cons.dag.plt <- plotAbn(dag = dag.mcmc.boot.th,data.dists = dist, plot = F)
if (SAVEPLOTS){
PLOTNAME <- "_consensus_dag"
dag = bnlearn::empty.graph(names(abndata))
bnlearn::amat(dag) = t(dag.mcmc.boot.th)
bnlearn::graphviz.plot(dag,
shape = "rectangle",
main = "MCMC ABN DAG")
dev.print(svg, filename = paste0(getwd(), PLOTPATH, FILENAME, PLOTNAME, ".svg"), width = PLOTWIDTH/2, height = PLOTHEIGHT/2)
dev.off()
abn::toGraphviz(dag.mcmc.boot.th,
data.dists = dist,
data.df = abndata,
outfile =paste0(getwd(), PLOTPATH, FILENAME, PLOTNAME, ".dot"),
directed=TRUE)
saveRDS(dag.mcmc.boot.th, file = paste0(getwd(), PLOTPATH, FILENAME, PLOTNAME, ".rds"))
} else {
cons.dag.plt
dag = bnlearn::empty.graph(names(abndata))
bnlearn::amat(dag) = t(dag.mcmc.boot.th)
bnlearn::graphviz.plot(dag,
shape = "rectangle",
main = "MCMC ABN DAG")
}
```
# Arc strength significance threshold
```{r}
dag.mcmc.boot.stren <- as.vector(round(dag.mcmc.boot, 3))
arc.stren.sign.threshold <-arc.stren.threshold(dag.mcmc.boot.stren)
# relative arc strength
plot(ecdf(dag.mcmc.boot.stren))
abline(v = arc.stren.sign.threshold, lty=2)
abline(v=0.5)
# # absolute arc strength
# plot(ecdf(apply(list.mc.out.burn.thin.dag, 1:2, sum)))
if (SAVEPLOTS){
PLOTNAME <- "_cdf_arcstrength"
dev.print(svg, filename = paste0(getwd(), PLOTPATH, FILENAME, PLOTNAME, ".svg"), width = PLOTWIDTH/2, height = PLOTHEIGHT/2)
dev.off()
}
```
Difference in DAG between two arc strength thresholds:
```{r}
# relative frequency of an arc appearing in the MCMC sample
x <- dag.mcmc.boot
x[which(x<THRESHOLD & x>arc.stren.sign.threshold)]
x[which(x<THRESHOLD & x>arc.stren.sign.threshold-0.01)] <- 100 # Assign impossible value to highlight arc
x
```
```{r}
# Fit best DAG trimmed
fabn.boot.th.mle <-fitAbn(dag = dag.mcmc.boot.th,
data.df = abndata,
data.dists = dist,
method = METHOD,
compute.fixed = T,
create.graph = T)
fabn.boot.th.mle$bic
infoDag(dag.mcmc.boot.th)
# plot with arc strength
plotdag <- dag.mcmc.boot
plotdag[plotdag>THRESHOLD]<-1
plotdag[plotdag<=THRESHOLD]<-0
fitvals <- fabn.boot.th.mle$coef
for (i in 1:length(fitvals)){
names(fitvals[[i]]) <- colnames(fitvals[[i]])
}
edgestren <- round(dag.mcmc.boot, 2)
edgestren[edgestren<=THRESHOLD]<-0
# svg(filename = paste0(FILENAMEbase, FILENAME, "consensus_dag_edgestrength.svg"))
cons.dag.plt.edgestrength <- plotAbn(dag = plotdag,
data.dists = dist,
# fitted.values = fitvals,
digits = 2,
edge.strength = edgestren,
plot = F)
# dev.off()
if (SAVEPLOTS){
PLOTNAME <- "consensus_dag_edgestrength"
plot(cons.dag.plt.edgestrength)
dev.print(svg, filename = paste0(getwd(), PLOTPATH, FILENAME, PLOTNAME, ".svg"), width = PLOTWIDTH/2, height = PLOTHEIGHT/2)
dev.off()
} else {
plot(cons.dag.plt.edgestrength)
}
```
# Inference
```{r}
# Fit best trimmed DAG with MLE framework to compute marginals
fabn.boot.th.mle <-fitAbn(dag = dag.mcmc.boot.th,
data.df = abndata,
data.dists = dist,
method = "mle",
compute.fixed = T,
create.graph = T)
# out.bayes <- unlist(fabn.boot.th.bayes$modes)
out.mle <- unlist(fabn.boot.th.mle$coef)
```
```{r}
##numeric
df.mle <- as.data.frame(out.mle) %>%
tibble::rownames_to_column()%>%
filter(!stringr::str_detect(rowname, "precision") &
!stringr::str_detect(rowname, "Intercept"))
# df.bayes
df.mle
```
## Maximum Likelihood Estimation
### Regression coefficient estimates and 95% Confidence Intervals (CI)
with their interpretation and data support (computed with structural MCMC).
Second table `spportdag` is the percentage of the individual arcs supported by the
MCMC sample.
```{r}
basicresulttable <- function(FITABN=fabn.boot.th.mle, SUPPORTDAG=dag.mcmc.boot){
fabn.boot.th.mle <- FITABN
dag.mcmc.boot <- SUPPORTDAG
# Look-up table for interpretation of results depending on variable distribution
interpretLUT <- lapply(fabn.boot.th.mle$abnDag$data.dists, function(x){
if(x=="binomial"){
"log odds ratio"
} else if(x=="gaussian"){
"correlation"
} else if(x=="poisson"){
"log rate ratio"
} else if(x=="multinomial"){
"log odds ratio"
}
})
# Extract each edge (cornames) and it's interpretation
cornames <- c()
interpret <- c()
for (i in 1:length(fabn.boot.th.mle$coef)){
for (j in colnames(fabn.boot.th.mle$coef[[i]])){
interpret <- c(interpret, interpretLUT[which(names(unlist(interpretLUT)) == names(fabn.boot.th.mle$coef[i]))][[1]])
if(stringr::str_detect(j, "intercept")){
cornames <- c(cornames, j)
} else {
cornames <- c(cornames, paste0(names(fabn.boot.th.mle$coef[i]), "|", j))
}
}
}
# each edge's support aka. arc-strength
possibleLevels <- c("\\.Former", "\\.No", "\\.Current", "\\.No", "\\.Yes", "\\.Low", "\\.Medium", "\\.High", "High", "Low", "Medium")
support <- c()
from_names <- c()
from_levels <- c()
to_names <- c()
to_levels <- c()
supportdag <- dag.mcmc.boot
supportdag[supportdag<=THRESHOLD] <- 0
for (i in 1:length(cornames)){
# iterate through all edges in cornames (i.e. "Multiple.IAs|Hypertension")
# split up cornames in from and to names
edgename <- stringr::str_split(cornames[[i]], pattern = "\\|", simplify = T)
fromname <- stringr::str_split(edgename[2], "\\.", simplify = T)[1]
toname <- stringr::str_split(edgename[1], "\\.", simplify = T)[1]
# Look up levels of from and to nodes
tolevels <- stringr::str_split(as.character(na.omit(stringr::str_match(edgename[2], possibleLevels))), "\\.", simplify = T)
if (length(tolevels) == 1){
tolevels <- stringr::str_split(as.character(na.omit(stringr::str_match(edgename[2], possibleLevels))), "\\.", simplify = T)[1]
} else if (length(tolevels) >= 2){
tolevels <- stringr::str_split(as.character(na.omit(stringr::str_match(edgename[2], possibleLevels))), "\\.", simplify = T)[1,2]
} else if (length(tolevels) == 0){
tolevels <- NA
}
if(fromname == "location"){
fromlevels <- stringr::str_split(as.character(na.omit(stringr::str_match(edgename[2], possibleLevels))), "\\.", simplify = T)
} else {
fromlevels <- stringr::str_split(as.character(na.omit(stringr::str_match(edgename[1], possibleLevels))), "\\.", simplify = T)
if (length(fromlevels) == 1){
fromlevels <- stringr::str_split(as.character(na.omit(stringr::str_match(edgename[1], possibleLevels))), "\\.", simplify = T)[1]
} else if (length(fromlevels) >= 2){
fromlevels <- stringr::str_split(as.character(na.omit(stringr::str_match(edgename[1], possibleLevels))), "\\.", simplify = T)[1,2]
} else if (length(fromlevels) == 0){
fromlevels <- NA
}
}
# fix from and to names here to match in supportdag
if (fromname == "location"){
fromname <- "location.grouped"
} else if(fromname == "Positive"){
fromname <- "Positive.famillial.history"
} else if(fromname == "Multiple"){
fromname <- "Multiple.IAs"
}
if (toname == "location"){
toname <- "location.grouped"
} else if(toname == "Positive"){
toname <- "Positive.famillial.history"
} else if(toname == "Multiple"){
toname <- "Multiple.IAs"
}
# Fill missing from and to levels
if (is.na(fromlevels) & fromname != "intercept"){
fromlevels <- stringr::str_flatten(levels(abndata[,fromname]), collapse=", ")
}
if (is.na(tolevels) & fromname != "intercept"){
tolevels <- stringr::str_flatten(levels(abndata[,toname]), collapse=", ")
}
# Retrieve arc strength from supportdag
fromidx <- which(colnames(supportdag) == fromname)
toidx <- which(colnames(supportdag) == toname)
if(identical(fromidx, integer(0)) | identical(toidx, integer(0))){
support <- c(support, NA)
} else {
support <- c(support, round(supportdag[toidx, fromidx], 2))
}
# collect results
from_names <- c(from_names, fromname)
from_levels <- c(from_levels, fromlevels)
to_names <- c(to_names, toname)
to_levels <- c(to_levels, tolevels)
}
# put all together
result.table <- data.frame(cornames = cornames,
from = from_names,
fromLevels = from_levels,
to = to_names,
toLevels = to_levels,
coefficient = unlist(fabn.boot.th.mle$coef, use.names = F),
SE = unlist(fabn.boot.th.mle$Stderror, use.names = F),
interpretation= interpret,
support = support)
return(result.table)
} #EOF basicresulttable
result.table.basic <- basicresulttable(FITABN=fabn.boot.th.mle, SUPPORTDAG=dag.mcmc.boot)
result.table.basic
```
```{r}
result.table <- result.table.basic %>%
# Fix toLevels of Ruptured_IA
mutate(toLevels = case_when(to == "Ruptured_IA" ~ "No, Yes",
TRUE ~ toLevels)) %>%
# Handle intercepts: fromLevels = intercept and from = to-label (this happens due to some bad algorithm design above)
mutate(from = case_when(stringr::str_detect(cornames, "intercept") & is.na(fromLevels) & is.na(toLevels) ~ to,
stringr::str_detect(cornames, "intercept") & is.na(fromLevels) & !is.na(toLevels) ~ paste(to, toLevels, sep = "="),
(from %in% c("location.grouped", "Smoking_Current_Former_No")) & (fromLevels %in% c("Low", "Medium", "High", "Current", "Former", "No")) ~ paste(from, fromLevels, sep = "="),
TRUE ~ from),
fromLevels = case_when(stringr::str_detect(cornames, "intercept") & is.na(fromLevels) ~ "intercept",
fromLevels %in% c("Low", "Medium", "High", "Current", "Former", "No") ~ "No, Yes",
TRUE ~ fromLevels),
to = case_when(stringr::str_detect(cornames, "intercept") ~ NA_character_,
(to %in% c("location.grouped", "Smoking_Current_Former_No")) & (toLevels %in% c("Low", "Medium", "High", "Current", "Former", "No")) ~ paste(to, toLevels, sep = "="),
TRUE ~ to),
toLevels = case_when((fromLevels == "intercept") ~ NA_character_,
toLevels %in% c("Low", "Medium", "High", "Current", "Former", "No") ~ "No, Yes",
TRUE ~ toLevels)) %>%
# Clean up node labels
mutate(fromDirty = from,
toDirty = to,
from = case_when(from == "Gender" ~ "Sex",
from == "AgeDiag" ~ "Age at Diagnosis",
from == "Positive.famillial.history" ~ "Pos. Fam. History",
from == "Smoking_Current_Former_No" ~ "Smoking Status",
from == "Smoking_Current_Former_No=Current" ~ "Smoking Status=Current",
from == "Smoking_Current_Former_No=Former" ~ "Smoking Status=Former",
from == "Smoking_Current_Former_No=No" ~ "Smoking Status=No",
from == "location.grouped" ~ "IA Location",
from == "location.grouped=Low" ~ "IA Location=Low risk",
from == "location.grouped=Medium" ~ "IA Location=Medium risk",
from == "location.grouped=High" ~ "IA Location=High risk",
from == "Multiple.IAs" ~ "Multiple IAs",
from == "IAsize_log" ~ "IA Size",
from == "Ruptured_IA" ~ "Ruptured IA",
TRUE ~ from),
to = case_when(to == "Gender" ~ "Sex",
to == "AgeDiag" ~ "Age at Diagnosis",
to == "Positive.famillial.history" ~ "Pos. Fam. History",
to == "Smoking_Current_Former_No" ~ "Smoking Status",
to == "Smoking_Current_Former_No=Current" ~ "Smoking Status=Current",
to == "Smoking_Current_Former_No=Former" ~ "Smoking Status=Former",
to == "Smoking_Current_Former_No=No" ~ "Smoking Status=No",
to == "location.grouped" ~ "IA Location",
to == "location.grouped=Low" ~ "IA Location=Low risk",
to == "location.grouped=Medium" ~ "IA Location=Medium risk",
to == "location.grouped=High" ~ "IA Location=High risk",
to == "Multiple.IAs" ~ "Multiple IAs",
to == "IAsize_log" ~ "IA Size",
to == "Ruptured_IA" ~ "Ruptured IA",
TRUE ~ to)) %>%
# split levels in two columns
separate(col = fromLevels, into = c("fromLevels0", "fromLevels1")) %>%
separate(col = toLevels, into = c("toLevels0", "toLevels1")) %>%
# add odds ratio
mutate(exp.coefficient = exp(coefficient),
exp.CI = paste(round(exp(coefficient-SE), 2), "-", round(exp(coefficient+SE), 2)),
interpretation.exp.coef = case_when(interpretation == "log odds ratio" ~ "odds ratio",
interpretation == "correlation" ~ "exp. corr. coef.",
TRUE ~ NA_character_)) %>%
# add probabilities
mutate(prob = exp(coefficient)/(1+exp(coefficient)) # odds/(1+odds)
)
# Probability interpretation
for (i in 1:nrow(result.table)){
if (stringr::str_detect(result.table$cornames[i], "intercept") & !(result.table$from[i] %in% c("Age at Diagnosis", "IA Size", "Smoking Status=Former", "Smoking Status=No", "IA Location", "IA Location=Low risk", "IA Location=Medium risk", "IA Location=High risk"))){
# Intercept
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], "=", levels(abndata[ , which(colnames(abndata) == stringr::str_split(result.table$fromDirty[i], "=", simplify = T)[1])])[2], ")")
} else if (stringr::str_detect(result.table$cornames[i], "intercept") & result.table$from[i] %in% c("Age at Diagnosis", "IA Size")){
# Intercept continuous
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], ")")
} else if (stringr::str_detect(result.table$cornames[i], "intercept") & result.table$from[i] %in% c("Smoking Status=Former", "Smoking Status=No", "IA Location", "IA Location=Low risk", "IA Location=Medium risk", "IA Location=High risk")){
# Intercept multinomial
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], ")")
} else if(result.table$fromLevels0[i] == "") {
# continuous: From
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], "= cont. |", result.table$to[i], "=", result.table$toLevels1[i],")")
} else if(result.table$toLevels0[i] == "") {
# continuous: to
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], "=", result.table$fromLevels1[i], "|", result.table$to[i], "= cont.)")
} else if(result.table$fromLevels0[i] == "") {
# continuous: From and to
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], "= cont. |", result.table$to[i], "= cont.)")
} else if((is.na(result.table$fromLevels1[i])) & (!is.na(result.table$toLevels1[i]))) {
# Multinomial or continuous: From
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], "=", result.table$fromLevels0[i], "|", result.table$to[i], "=", result.table$toLevels1[i],")")
} else if((!is.na(result.table$fromLevels1[i])) & (is.na(result.table$toLevels1[i]))) {
# Multinomial or continuous: to
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], "=", result.table$fromLevels1[i], "|", result.table$to[i], "=", result.table$toLevels0[i],")")
} else if (is.na(result.table$fromLevels1[i]) & is.na(result.table$toLevels1[i])) {
# Multinomial or continuous: From and to
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], "=", result.table$fromLevels0[i], "|",result.table$to[i], "=", result.table$toLevels0[i],")")
} else if (!is.na(result.table$fromLevels1[i]) & !is.na(result.table$toLevels1[i])) {
# Dichotome/Binomial
result.table$interpretation.prob[i] <- paste0("P(", result.table$from[i], "=", result.table$fromLevels1[i], "|", result.table$to[i], "=", result.table$toLevels1[i],")")
} else {
result.table$interpretation.prob[i] <- paste0("WARNING UNCATCHED CASE: P(", result.table$from[i], "=", result.table$fromLevels1[i], "|", result.table$to[i], "=", result.table$toLevels1[i],")")
}
}
result.table
```
1/(1+exp(fabn.maxpar$coef$Hypertension[1])) * (1-1/(1+exp(fabn.maxpar$coef$Hypertension[2]))) * 1/(1+exp(fabn.maxpar$coef$Hypertension[3]))
```{r}
result.table.mle <- result.table %>%
# round to 2 digits
mutate(across(where(is.numeric), function(x){round(x, 2)})) %>%
# selct specific columns and reorder
select(c(from, fromLevels0, fromLevels1, to, toLevels0, toLevels1, support, exp.coefficient, exp.CI, interpretation.exp.coef, coefficient, SE, interpretation, prob, interpretation.prob, cornames))
if (SAVEPLOTS){
write.csv(result.table.mle, file = paste0(getwd(), PLOTPATH, FILENAME, "results_table_mle.csv"), row.names = F)
# write.csv(supportdag, file = paste0(getwd(), PLOTPATH, FILENAME, "individual_arc_support.csv"))
} else {
print(result.table.mle)
}
```
#### As LaTeX table
```{r}
linesep<-function(x,y=character()){
if(!length(x))
return(y)
linesep(x[-length(x)], c(rep('',x[length(x)]-1),'\\addlinespace',y))
}
result.table.mle.basicLatex <- result.table.mle %>%
# sort by parent nodes first and then by child nodes
arrange(from, fromLevels0, fromLevels1, to, toLevels0, toLevels1) %>%
# Clean column names
rename("From" = "from",
"From State 0" = "fromLevels0",
"From State 1" = "fromLevels1",
"To" = "to",
"To State 0" = "toLevels0",
"To State 1" = "toLevels1",
"Coefficient" = "exp.coefficient",
"95% CI" = "exp.CI",
"Interpretation" = "interpretation.exp.coef",
"log coefficient" = "coefficient",
"log coef. Interpretation" = "interpretation",
"Probability" = "prob",
"Probability Interpretation" = "interpretation.prob",
"Arc-strength" = "support") %>%
# Display NAs as empty cells
mutate(across(where(is.character), ~replace_na(.x, ""))) %>%
mutate(`Arc-strength` = as.character(sprintf("%.2f", `Arc-strength`))) %>% # keep tailing zeros of arc-strength
mutate(`Arc-strength` = case_when(`Arc-strength` == "NA" ~ "", # replace NAs manually
TRUE ~ `Arc-strength`)) %>%
# Finishing
select(-c(cornames)) %>%
tibble::remove_rownames()
result.table.mle.basicLatex
```
show everything.
```{r}
result.table.mle.basicLatex %>%
# Latex table
kableExtra::kbl(format = "latex",
label = "expABNmultinom_100k_results_table_mle",
booktabs = T,
linesep = linesep(c(4, 5, 3, 1, 3, 1, 4)), # manually add linespace after each group in "From" by group size.
caption = "Result Table Caption"
) %>%
kableExtra::kable_styling(latex_options=c("striped", "hold_position", "scale_down")) %>%
kableExtra::add_header_above(c("Parent" = 3, "Children" = 3, "Association" = 8, " " = 1)) %>%
kableExtra::landscape() %>%
cat(., file = paste0(Sys.getenv("PLOTPATH"), "/", "results_table_mle_full.tex"))
```
Only odds ratios and log odds
```{r}
result.table.mle.basicLatex %>%
select(-c(Probability, `Probability Interpretation`)) %>%
# rename(" " = "From",
# "State 0" = "From State 0",
# "State 1" = "From State 1",
# " " ="To",
# "State 0" = "To State 0",
# "State 1" = "To State 1") %>%
# Latex table
kableExtra::kbl(format = "latex",
label = "expABNmultinom_100k_results_table_mle",
booktabs = T,
linesep = linesep(c(5, 4, 3, 2, 2, 2, 5, 6, 2)), # manually add linespace after each group in "From" by group size.
caption = "Result Table Caption"
) %>%
kableExtra::kable_styling(latex_options=c("striped", "hold_position", "scale_down")) %>%
kableExtra::add_header_above(c("Parent" = 3, "Children" = 3, "Association" = 6, " " = 1), align = "l") %>%
kableExtra::landscape() %>%
cat(., file = paste0(Sys.getenv("PLOTPATH"), "/", "results_table_mle_short.tex"))
```
For presentation
```{r}
result.table.mle.basicLatex %>%
select(-c(Probability, `Probability Interpretation`, `log coefficient`, SE, `log coef. Interpretation`)) %>%
filter(`From State 0` != "intercept") %>%
# mutate(`Interpretation` = case_when(`From State 0` == "" ~ "exp. corr. coefficient",
# # `Interpretation` == "exp. corr. coef." ~ "corr. coefficient",
# TRUE ~ `Interpretation`)) %>%
relocate(`Arc-strength`, .after = `Interpretation`) %>%
# rename(" " = "From",
# "State 0" = "From State 0",
# "State 1" = "From State 1",
# " " ="To",
# "State 0" = "To State 0",
# "State 1" = "To State 1") %>%
# Latex table
kableExtra::kbl(format = "latex",
label = "expABNmultinom_100k_results_table_mle",
booktabs = T,
linesep = linesep(c(5, 4, 3, 2, 2, 2, 5, 6, 2)), # manually add linespace after each group in "From" by group size.
caption = "Result Table Caption"
) %>%
kableExtra::kable_styling(latex_options=c("striped", "hold_position", "scale_down")) %>%
kableExtra::add_header_above(c("Parent" = 3, "Children" = 3, "Association" = 3, " " = 1), align = "l") %>%
kableExtra::landscape() %>%
cat(., file = paste0(Sys.getenv("PLOTPATH"), "/", "results_table_mle_pres.tex"))
```
For paper
```{r}
result.table.mle.basicLatex %>%
select(-c(Probability, `Probability Interpretation`, `Coefficient`, `95% CI`, `Interpretation`)) %>%
filter(`From State 0` != "intercept") %>%
relocate(`Arc-strength`, .after = `log coef. Interpretation`) %>%
rename("Interpretation" = "log coef. Interpretation",
"Coefficient" = "log coefficient") %>%
# Latex table
kableExtra::kbl(format = "latex",
label = "expABNmultinom_100k_results_table_mle",
booktabs = T,
linesep = linesep(c(5, 4, 3, 2, 2, 2, 5, 6, 2)), # manually add linespace after each group in "From" by group size.
caption = "Result Table Caption"
) %>%
kableExtra::kable_styling(latex_options=c("striped", "hold_position", "scale_down")) %>%
kableExtra::add_header_above(c("Parent" = 3, "Children" = 3, "Association" = 3, " " = 1), align = "l") %>%
kableExtra::landscape() %>%
cat(., file = paste0(Sys.getenv("PLOTPATH"), "/", "results_table_mle_paper.tex"))
```
### Interpretations
```{r}
str(abndata)
```
```{r}
result.table.mle
```
### Arc-histogram
```{r}
# Arc histogram
arcsdist <- apply(list.mc.out.burn.thin.dag, 3, sum)
df.archist <- as.data.frame(table(arcsdist)) %>%
mutate(totalarcs = case_when(arcsdist == sum(dag.maxpar$dag) ~ "1",
TRUE ~ "0"))
plt.archist <- ggplot(df.archist, aes(x = arcsdist, y = Freq, fill=totalarcs)) +
geom_col(width=0.5) +
labs(title="Arc Histogram")+
xlab("Number of arcs in the DAG")+
ylab("Number of DAGs") +
scale_fill_manual( values = c("1"="red", "0"="darkgray"), guide = "none")+
theme_minimal()
if (SAVEPLOTS){
PLOTNAME <- "_arc_histogram"
ggsave(path = paste0(getwd(), PLOTPATH), filename = paste0(FILENAME, PLOTNAME, ".png"),
plt.archist)
} else {
plt.archist
}
```
### Most frequent DAGS in MCMC sample
```{r WARNING_takes_long_time, cache=TRUE}
u.list.dag <- unique.array(x = list.mc.out.burn.thin.dag,MARGIN = 3) # remove duplicate elements/rows
num_100 <- apply(X = u.list.dag, MARGIN = 3, FUN = function(x){
sum(apply(X = list.mc.out.burn.thin.dag,MARGIN = 3,FUN = function(y){
if(identical(x,y)){1}else{0}
}))
})
max(which((cumsum(sort(num_100,decreasing = FALSE)))/1000<0.80,arr.ind = TRUE))
```