-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.qmd
1705 lines (1403 loc) · 41.2 KB
/
index.qmd
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: "R, DOT, and TikZ code to plot directed acyclic graphs and single world intervention graphs for Mendelian randomization analyses"
author: "Tom Palmer"
date: "2019-01-24"
date-modified: "`r Sys.Date()`"
format:
html:
monofont: "Fira Mono"
bibliography: refs.bib
---
```{=html}
<!-- HTML -->
<!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/distr/fira_code.css"> -->
<link rel="stylesheet" href="https://code.cdn.mozilla.net/fonts/fira.css">
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(
cache = TRUE,
dev = "svg",
fig.align = "center",
collapse = TRUE,
comment = '#>'
)
# pgf for tikz
if (!tinytex::check_installed('pgf')) tinytex::tlmgr_install("pgf")
# tikz-cd
if (!tinytex::check_installed('tikz-cd')) tinytex::tlmgr_install("tikz-cd")
# tikz-swigs
if (!tinytex::check_installed('tikz-swigs')) tinytex::tlmgr_install("tikz-swigs")
# Using advice from Andrew Heiss blogpost to use SVGs in HTML from TikZ
# https://www.andrewheiss.com/blog/2021/08/27/tikz-knitr-html-svg-fun/#connecting-dvisvgm-to-ghostscript-on-macos
# For svg figures we require the latex package dvisvgm
if (!tinytex::check_installed("dvisvgm")) tinytex::tlmgr_install("dvisvgm")
# On macOS dvisvgm needs the ghostscript library dynamically linked to it via
# either the LIBGS environment variable or the --libgs flag.
# The libgs.dylib from Homebrew (brew install ghostscript) is sufficient.
if (Sys.info()[["sysname"]] == "Darwin") {
# Ghostscript from Homebrew
Sys.setenv(LIBGS = "/opt/homebrew/lib/libgs.dylib")
if (Sys.info()[["machine"]] == "arm64" && compareVersion(as.character(quarto::quarto_version()), "1.2.56") == -1) {
# Install Ghostscript from https://pages.uoregon.edu/koch/Ghostscript-10.00-Full.pkg
Sys.setenv(LIBGS = "/usr/local/lib/libgs.dylib")
}
}
```
## Introduction
This webpage shows some R code for generating figures, directed acyclic graphs (DAGs), and single world intervention graphs (SWIGs) for Mendelian randomization (MR) analyses.
First, we load in the tidyverse collection of packages to have access to the pipe and the ggplot2 package, amongst other features.
```{r tdyvrs, message=FALSE, warning=FALSE}
library(tidyverse)
```
If you do not already one you will need a LaTeX installation. In R it is easiest to install [TinyTeX](https://yihui.org/tinytex/) as follows.
```{r tnytx, eval=FALSE}
install.packages("tinytex")
tinytex::install_tinytex()
```
## DiagrammeR package
The website for DiagrammeR is here: <http://rich-iannone.github.io/DiagrammeR/> .
We will draw the DAGs using Graphviz which uses the DOT language to describe graphs.
### Single genotype DAG
```{r dg-single-dag}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB]
node [shape=ellipse]
U [label='Confounders']
node [shape=box, height=0.3, width=0.3]
G [label='Genotype']
X [label='Phenotype']
Y [label='Outcome']
{ rank = same; G X Y }
G -> X [minlen=3]
U -> X
U -> Y
X -> Y [minlen=3]
}
", height = 200)
```
#### Version without borders around observed variables
```{r dg-single-dag-2}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB]
node [shape=ellipse]
U [label='Confounders']
node [shape=plaintext, height=0.3, width=0.3]
G [label='Genotype']
X [label='Phenotype']
Y [label='Outcome']
{ rank = same; G X Y }
G -> X [minlen=3]
U -> X
U -> Y
X -> Y [minlen=3]
}
", height = 200)
```
### Multiple genotype DAG
```{r dg-multiple-dag}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=box, height=0.3, width=0.3]
G1 [label='<I>G@_{1}</I>', pos='0,0.75!']
G2 [label='<I>G@_{2}</I>', pos='0,0!']
G3 [label='<I>G@_{3}</I>', pos='0,-0.75!']
X [label='Phenotype', pos='2,0!']
Y [label='Outcome', pos='4,0!']
G1 -> X
G2 -> X
G3 -> X
U -> X
U -> Y
X -> Y
}
", height = 300)
```
This could be extended for more genotypes.
#### Version without borders around observed variables
```{r dg-multiple-dag-2}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
G1 [label='<I>G@_{1}</I>', pos='0,0.75!']
G2 [label='<I>G@_{2}</I>', pos='0,0!']
G3 [label='<I>G@_{3}</I>', pos='0,-0.75!']
X [label='Phenotype', pos='2,0!']
Y [label='Outcome', pos='4,0!']
G1 -> X
G2 -> X
G3 -> X
U -> X
U -> Y
X -> Y
}
", height = 300)
```
### MVMR DAGs
We can plot the DAGs in Figure 4 of @sanderson-ije-2019 [here](https://academic.oup.com/view-large/figure/162152273/dyy262f4.tif).
#### Scenario 1: $X_2$ confounder of $X_1$ and $Y$
```{r mvmr-dag-1}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
G1 [label='<I>G@_{1}</I>', pos='0,0!']
X1 [label='<I>X@_{1}</I>', pos='2,0!']
Y [label=<<I>Y</I>>, pos='4,0!']
G2 [label='<I>G@_{2}</I>', pos='0,-1!']
X2 [label='<I>X@_{2}</I>', pos='2,-1!']
G1 -> X1
G2 -> X2
U -> X1
U -> Y
U -> X2
X1 -> Y
X2 -> Y
X2 -> X1
}
", height = 300)
```
#### Scenario 2: $X_2$ collider of $X_1$ and $Y$
```{r mvmr-dag-2}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
G1 [label='<I>G@_{1}</I>', pos='0,0!']
X1 [label='<I>X@_{1}</I>', pos='2,0!']
Y [label=<<I>Y</I>>, pos='4,0!']
G2 [label='<I>G@_{2}</I>', pos='0,-1!']
X2 [label='<I>X@_{2}</I>', pos='2,-1!']
G1 -> X1
G2 -> X2
U -> X1
U -> Y
U -> X2
X1 -> Y
Y -> X2
X1 -> X2
}
", height = 300)
```
#### Scenario 3: $X_2$ on a pleiotropic pathway
```{r mvmr-dag-3}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
G1 [label='<I>G@_{1}</I>', pos='0,0!']
X1 [label='<I>X@_{1}</I>', pos='2,0!']
Y [label=<<I>Y</I>>, pos='4,0!']
G2 [label='<I>G@_{2}</I>', pos='0,-1!']
X2 [label='<I>X@_{2}</I>', pos='2,-1!']
G1 -> X1
G2 -> X2
U -> X1
U -> Y
U -> X2
X1 -> Y
X2 -> Y
}
", height = 300)
```
#### Scenario 4: $X_2$ mediator of the effect of $X_1$ on $Y$
```{r mvmr-dag-4}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
G1 [label='<I>G@_{1}</I>', pos='0,0!']
X1 [label='<I>X@_{1}</I>', pos='2,0!']
Y [label=<<I>Y</I>>, pos='4,0!']
G2 [label='<I>G@_{2}</I>', pos='0,-1!']
X2 [label='<I>X@_{2}</I>', pos='2,-1!']
edge [arrowhead='vee']
G1 -> X1
G2 -> X2
U -> X1
U -> Y
U -> X2
X1 -> Y
X2 -> Y
X1 -> X2
}
", height = 300)
```
### Bidirectional MR figure of combined DAGs
For a bidirectional MR analysis there are 2 separate DAGs which are as follows.
```{r dg-bidir-fig-1}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
GX [label='Genotypes \n for phenotype', pos='0,0!']
X [label='Phenotype', pos='2,0!']
Y [label='Outcome', pos='4,0!']
GX -> X [penwidth=2]
U -> X [penwidth=2]
U -> Y [penwidth=2]
X -> Y [penwidth=2]
}
", height = 200)
```
```{r bg-bidir-fig-2}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
X [label='Phenotype', pos='2,0!']
Y [label='Outcome', pos='4,0!']
GY [label='Genotypes \n for outcome', pos='6,0!']
U -> X [penwidth=2]
U -> Y [penwidth=2]
Y -> X [penwidth=2]
GY -> Y [penwidth=2]
}
", height = 200)
```
We can combine these into a single figure as follows.
```{r dg-bidir-fig}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
GX [label='Genotypes \n for phenotype', pos='0,0!']
X [label='Phenotype', pos='2,0!']
Y [label='Outcome', pos='4,0!']
GY [label='Genotypes \n for outcome', pos='6,0!']
{ rank = same; GX X Y GY }
GX -> X [penwidth=2]
U -> X [penwidth=2]
U -> Y [penwidth=2]
X -> Y [penwidth=2]
Y -> X [color=blue, penwidth=2]
GY -> Y [color=blue, penwidth=2]
}
", height = 200)
```
Arguably we could add additional blue arrows from the Confounders node.
If you prefer a greyscale figure replace `blue` with `grey`/`DimGrey`/`grey30` (or change the number as required. See [here](https://rich-iannone.github.io/DiagrammeR/graphviz_and_mermaid.html#colors) for more colors.
### Pleiotropy DAGs
There are many types of pleiotropy. Let's plot some of the pleiotropy DAGs from @sanderson-nrmp-2022.
#### Horizontal pleiotropy (causes bias)
```{r dg-pleio-1}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB]
node [shape=ellipse]
U [label='Confounders']
node [shape=plaintext, height=0.3, width=0.3]
G [label='Genotype']
X [label='Phenotype']
Y [label='Outcome']
C [label='Other phenotype']
{ rank = same; G X Y }
G -> X [minlen=3]
G -> C
U -> X
U -> Y
X -> Y [minlen=3]
C -> Y
}
", height = 300)
```
#### Horizontal pleiotropy (no bias)
```{r dg-pleio-2}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
G [label='Genotype', pos='0,0!']
X [label='Phenotype', pos='2,0!']
Y [label='Outcome', pos='4,0!']
C [label='Other phenotype', pos='2,-1!']
G -> X
G -> C
U -> X
U -> Y
X -> Y
}
", height = 300)
```
#### Confounding via linkage disequilibrium
```{r dg-pleio-3}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
G1 [label='Genotype 1', pos='0,0!']
G2 [label='Genotype 2', pos='0,-1!']
X [label='Phenotype', pos='2,0!']
Y [label='Outcome', pos='4,0!']
C [label='Other phenotype', pos='2,-1!']
G1 -> X
G1 -> G2 [dir='both']
G2 -> C
U -> X
U -> Y
X -> Y
C -> Y
}
", height = 300)
```
#### Vertical pleiotropy
```{r dg-pleio-4}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB]
node [shape=ellipse]
U [label='Confounders']
node [shape=plaintext, height=0.3, width=0.3]
G [label='Genotype']
X [label='Phenotype 1']
Y [label='Outcome']
C [label='Phenotype 2']
{ rank = same; G X C Y }
G -> X [minlen=2]
X -> C [minlen=2]
U -> X
U -> Y
C -> Y [minlen=2]
}
", height = 200)
```
#### Misspecification of the primary phenotype
```{r dg-pleio-5}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
G [label='Genotype', pos='0,0!']
X [label='Phenotype 1', pos='2,0!']
Y [label='Outcome', pos='4,0!']
C [label='Phenotype 2', pos='2,-1!']
G -> C
C -> X
U -> X
U -> Y
X -> Y
C -> Y
}
", height = 300)
```
#### Correlated pleiotropy
```{r dg-pleio-6}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse]
U [label='Confounders', pos='3,1!']
node [shape=plaintext, height=0.3, width=0.3]
G [label='Genotype', pos='0,0!']
X [label='Phenotype 1', pos='2,0!']
Y [label='Outcome', pos='4,0!']
C [label='Phenotype 2', pos='2,-1!']
G -> X
G -> C
C -> X
U -> X
U -> Y
X -> Y
C -> Y
}
", height = 300)
```
### Single genotype path diagram
#### Explicitly including the confounder
```{r dg-single-path}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse, height=0.3, width=0.3]
U [label=<<I>U</I>>, pos='3,1!']
node [shape=box, height=0.3, width=0.3]
G [label=<<I>G</I>>, pos='0,0!']
X [label=<<I>X</I>>, pos='2,0!']
Y [label=<<I>Y</I>>, pos='4,0!']
node [shape=circle, height=0.35, fixedsize=true]
Ex [label='<I>ε@_{X}</I>', pos='2,1!']
Ey [label='<I>ε@_{Y}</I>', pos='4,1!']
G -> X [label='<I>β@_{GX}</I>']
Ex -> X [label=1]
Ey -> Y [label=1]
U -> X [label='<I>β@_{UX}</I>']
U -> Y [label='<I>β@_{UY}</I>']
X -> Y [label='<I>β@_{XY}</I>']
U -> U [dir='both', headport='n', tailport='n']
G -> G [dir='both', headport='w', tailport='w']
}
", height = 250)
```
#### Including variance terms
Note in the diagram below $\sigma_{X}^2$ and $\sigma_{Y}^2$ represent the variance of the respective error/residual terms ($\varepsilon_X$, $\varepsilon_Y$) and not the variance of $X$ and $Y$.
```{r dg-single-path-variance}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato]
node [shape=ellipse, height=0.3, width=0.3]
U [label=<<I>U</I>>, pos='3,1!']
node [shape=box, height=0.3, width=0.3]
G [label=<<I>G</I>>, pos='0,0!']
X [label=<<I>X</I>>, pos='2,0!']
Y [label=<<I>Y</I>>, pos='4,0!']
node [shape=circle, height=0.35, fixedsize=true]
Ex [label='<I>ε@_{X}</I>', pos='2,1!']
Ey [label='<I>ε@_{Y}</I>', pos='4,1!']
G -> X [label='<I>β@_{GX}</I>']
Ex -> X [label=1]
Ey -> Y [label=1]
U -> X [label='<I>β@_{UX}</I>']
U -> Y [label='<I>β@_{UY}</I>']
X -> Y [label='<I>β@_{XY}</I>']
U -> U [dir='both', headport='n', tailport='n', label=<var(<I>U</I>)>]
G -> G [dir='both', headport='w', tailport='w', label=<var(<I>G</I>)>]
Ex -> Ex [dir='both', headport='n', tailport='n', label=<<I>σ@_{X}@^{2}</I>>]
Ey -> Ey [dir='both', headport='n', tailport='n', label=<<I>σ@_{Y}@^{2}</I>>]
}
", height = 250)
```
#### Including correlated error terms between exposure and outcome
```{r dg-single-path-2}
DiagrammeR::grViz("
digraph mrdag {
graph [rankdir=TB, layout=neato, splines=true]
node [shape=box, height=0.3, width=0.3]
G [label=<<I>G</I>>, pos='0,0!']
X [label=<<I>X</I>>, pos='2,0!']
Y [label=<<I>Y</I>>, pos='4,0!']
node [shape=circle, height=0.35, fixedsize=true]
Ex [label='<I>ε@_{X}</I>', pos='2,1!']
Ey [label='<I>ε@_{Y}</I>', pos='4,1!']
dummy [label='', pos='3,1!', color=white, height=0.5]
G -> X [label='<I>β@_{GX}</I>']
Ex -> X [label=1]
Ey -> Y [label=1]
X -> Y [label='<I>β@_{XY}</I>']
Ex -> Ey [dir='both', label=<<I>ρ</I>>]
G -> G [dir='both', headport='w', tailport='w']
}
", height = 250)
```
## dagitty package
The website for DAGitty is <http://www.dagitty.net/>
```{r dgtty, warning=FALSE, message=FALSE}
library(dagitty)
```
### Using DOT syntax
The DAG for a single genotype.
```{r dgtty-single-dag}
#| fig.height=2
mrdag <- dagitty('dag {
G -> X -> Y
X <- U -> Y
G [pos="0,0"]
X [e, pos="1,0"]
U [pos="1.5,-0.4"]
Y [o, pos="2,0"]
}')
plot(mrdag)
```
### Using ggdag
```{r ggdg-single-dag, warning=FALSE, message=FALSE}
#| fig.height=3.5
library(ggdag)
mrdag <- dagitty('dag {
G -> X -> Y
X <- U -> Y
G [pos="0,0"]
X [e, pos="1,0"]
U [pos="1.5,0.4"]
Y [o, pos="2,0"]
}')
ggmrdag <- tidy_dagitty(mrdag)
ggdag(ggmrdag) + theme_dag()
```
This can be labelled as follows.
```{r ggdg-single-dag-2}
#| fig.height=3.5
mrdag <- mrdag %>%
dag_label(
labels = c(
"X" = "Exposure",
"Y" = "Outcome",
"G" = "Genotype",
"U" = "Confounders"))
ggdag(mrdag, use_labels = "label") + theme_dag()
```
Dagitty can identify the instrumental variable on the DAG.
```{r ggdg-single-dag-3}
#| fig.height=3.5
mrdag %>% ggdag_instrumental() + theme_dag()
```
It correctly identifies the genotype, $G$, as the instrumental variable.
### Using R model type syntax in ggdag
```{r ggdg-single-dag-4}
#| fig.height=3.5
coords <- list(x = c(G = 0, X = 1, Y = 2, U = 1.5),
y = c(G = 0, X = 0, Y = 0, U = 1))
mrdag <- dagify(
X ~ G + U,
Y ~ X + U,
labels = c(
"G" = "Genotype",
"X" = "Exposure",
"Y" = "Outcome",
"U" = "Confounders"
),
exposure = "X",
outcome = "Y",
coords = coords
)
ggdag(mrdag, use_labels = "label") + theme_dag()
```
Plotting directly with **ggplot2** functions.
```{r ggdg-single-dag-5}
#| fig.height=3.5
mrdag %>%
ggplot(aes(
x = x,
y = y,
xend = xend,
yend = yend
)) +
geom_dag_point() +
geom_dag_edges() +
geom_dag_text() +
theme_dag()
```
Showing the paths.
```{r ggdg-single-dag-6}
#| fig.height=3.5
set.seed(123)
dagify(Y ~ X + U,
X ~ U + G,
exposure = "X",
outcome = "Y") %>%
ggdag_paths() + theme_dag()
```
Adjustment set for the effect of $X$ on $Y$.
```{r ggdg-single-dag-7}
#| fig.height=3.5
dagify(Y ~ X + U,
X ~ U + G,
exposure = "X",
outcome = "Y",
coords = coords) %>%
ggdag_adjustment_set() + theme_dag()
```
This shows that the path from $G$ to $X$ is unconfounded. And that when $U$ is adjusted for the path from $X$ to $Y$ is unconfounded.
### Multiple genotype DAG
```{r ggdg-multiple-dag}
coords2 <- list(x = c(G1 = 0, G2 = 0, G3 = 0, X = 1, Y = 2, U = 1.5),
y = c(G1 = 0.5, G2 = 0, G3 = -0.5, X = 0, Y = 0, U = 1))
mrdag <- dagify(
X ~ G1,
X ~ G2,
X ~ G3,
Y ~ X,
X ~ U,
Y ~ U,
exposure = "X",
outcome = "Y",
coords = coords2
)
ggdag(mrdag) + theme_dag()
```
This could be extended for more genotypes.
## Eleanor Murray's TikZ diagrams using the tikz code chunk engine
I believe that tikz diagrams can be included in Rmarkdown documents using the tikz code chunk engine for output types `pdf_document` and `html_document`.
Tikz is a package for LaTeX and so pdf is its natural output. So far I have not been able to include these figures in either `html_notebook` or `word_document` output formats. I suspect that the images need conversion to png files for these to work under rmarkdown.
We will use some of the LaTeX code for drawing DAGs using tikz provided by Eleanor Murray [here](https://github.com/eleanormurray/causalgraphs_latex).
In its LaTeX preamble, this code loads in several TikZ libraries. As such for our tikz code chunks to compile in our Rmd file we need to define a new template file, e.g.`tikz-template.tex`, and call our tikz code chunks as follows.
````{verbatim}
```{tikz}
#| out.width="65%",
#| engine.opts=list(template = "tikz-template.tex",
#| dvisvgm.opts = "--font-format=woff")
\begin{tikzpicture}
% Your tikz code goes here ...
\end{tikzpicture}
```
````
The `tikz-template.tex` file includes the TikZ preamble code from Eleanor Murray's LaTeX code plus the required template code. The contents of the template file is as follows.
```{verbatim, lang='tex', file='tikz-template.tex'}
```
We can now plot our DAGs.
### Single genotype DAGs using TikZ
#### Specifying node coordinates
```{tikz tkz-single-dag}
#| out.width='65%',
#| engine.opts=list(template = "tikz-template.tex",
#| dvisvgm.opts = "--font-format=woff")
\begin{tikzpicture}
\node (1) at (0,0) {$G$};
\node (2) at (1.5,0) {$X$};
\node (3) at (3,0) {$Y$};
\node (4) at (2.25,1) {$U$};
\draw[Arrow] (1) -- (2);
\draw[Arrow] (2) -- (3);
\draw[Arrow] (4) -- (2);
\draw[Arrow] (4) -- (3);
\end{tikzpicture}
```
#### Single genotype DAG using tikz-cd
The tikz-cd package provides helpful syntax for making commutative diagrams which we can use to produce DAGs.
First install the package.
```r
tinytex::tlmgr_install("tikz-cd")
```
Then we can plot the single instrument DAG using the following code.
```{tikz tkz-single-dag-2}
#| out.width='65%',
#| engine.opts=list(template = "tikz-template.tex",
#| dvisvgm.opts = "--font-format=woff")
\usetikzlibrary{cd}
\tikzcdset{arrow style=tikz, diagrams={>=stealth}}
\begin{tikzcd}[column sep=tiny]
& & & & U \arrow[dl, thick] \arrow[dr, thick] & \\
G \arrow[thick]{rrr} & & & X \arrow[thick]{rr} & & Y
\end{tikzcd}
```
#### Single genotype DAG using simpler TikZ code
```{tikz tikz-single-dag-3}
#| out.width='65%',
#| engine.opts=list(dvisvgm.opts = "--font-format=woff")
\usetikzlibrary{positioning}
\tikzset{
> = stealth,
every path/.append style = {
arrows = ->,
thick
}
}
\tikz{
\node (g) {$G$};
\node (x) [right = of g] {$X$};
\node (y) [right = of x] {$Y$};
\node (u) [above right = 0.5cm and 0.25cm of x] {$U$};
\path (g) edge (x);
\path (x) edge (y);
\path (u) edge (x);
\path (u) edge (y);
}
```
### Multiple genotype DAGs using TikZ
#### Specifying node coordinates
```{tikz tkz-multiple-dag}
#| out.width='65%',
#| engine.opts=list(template = "tikz-template.tex",
#| dvisvgm.opts = "--font-format=woff")
\begin{tikzpicture}
\node (1) at (0,0) {$G_2$};
\node (2) at (1.5,0) {$X$};
\node (3) at (3,0) {$Y$};
\node (4) at (2.25,1) {$U$};
\node (5) at (0,0.5) {$G_1$};
\node (6) at (0,-0.5) {$G_3$};
\draw[Arrow] (1) -- (2);
\draw[Arrow] (2) -- (3);
\draw[Arrow] (4) -- (2);
\draw[Arrow] (4) -- (3);
\draw[Arrow] (5) -- (2);
\draw[Arrow] (6) -- (2);
\end{tikzpicture}
```
Again this could be extended for more genotypes.
#### Multiple genotype DAG using circularly spaced genotypes
```{tikz tkz-multiple-dag-circular}
#| out.width='65%',
#| engine.opts=list(template = "tikz-template.tex",
#| dvisvgm.opts = "--font-format=woff")
\begin{tikzpicture}
\node (1) at (0,0) {$G_2$};
\node (2) at (1.5,0) {$X$};
\node (3) at (3,0) {$Y$};
\node (4) at (2.25,1) {$U$};
\node (5) at ([shift=({160:1.5})]2) {$G_1$};
\node (6) at ([shift=({200:1.5})]2) {$G_3$};
\draw[Arrow] (1) -- (2);
\draw[Arrow] (2) -- (3);
\draw[Arrow] (4) -- (2);
\draw[Arrow] (4) -- (3);
\draw[Arrow] (5) -- (2);
\draw[Arrow] (6) -- (2);
\end{tikzpicture}
```
### Pleiotropy DAGs using TikZ
#### Horizontal pleiotropy (causes bias)
```{tikz tkz-pleio-1}
#| out.width='65%',
#| engine.opts=list(dvisvgm.opts = "--font-format=woff")
\usetikzlibrary{positioning}
\tikzset{
> = stealth,
every path/.append style = {
arrows = ->,
thick
}
}
\tikz{
\node (g) {$G$};
\node (x1) [right = of g] {$X_1$};
\node (y) [right = of x1] {$Y$};
\node (u) [above right = 0.5cm and 0.25cm of x1] {$U$};
\node (x2) [below = 0.5cm of x1] {$X_2$};
\path (g) edge (x1);
\path (x1) edge (y);
\path (u) edge (x1);
\path (u) edge (y);
\path (g) edge (x2);
\path (x2) edge (y);
}
```
#### Horizontal pleiotropy (no bias)
```{tikz tkz-pleio-2}
#| out.width='65%',
#| engine.opts=list(dvisvgm.opts = "--font-format=woff")
\usetikzlibrary{positioning}
\tikzset{
> = stealth,
every path/.append style = {
arrows = ->,
thick
}
}
\tikz{
\node (g) {$G$};
\node (x1) [right = of g] {$X_1$};
\node (y) [right = of x1] {$Y$};
\node (u) [above right = 0.5cm and 0.25cm of x1] {$U$};