-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunderstanding_dags.Rmd
860 lines (732 loc) · 26.9 KB
/
understanding_dags.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
---
title: "Understanding DAGs"
author: "Anders Sundelin"
date: "2022-12-21"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dagitty)
library(ggdag)
library(ggplot2)
#library(rethinking)
```
# Getting down to DAGs
Initial DAG:
```{r }
introd.dag <- dagitty("dag {
AUTHOR -> INTROD;
REPO -> INTROD;
TEAM -> INTROD;
REASON -> INTROD;
ADDED -> INTROD;
ISSUES -> INTROD;
COMPLEX -> INTROD;
FILE -> ISSUES;
FILE -> REPO;
FILE -> COMPLEX;
AUTHOR -> TEAM;
AUTHOR -> REPO;
TEAM -> REPO;
}")
coordinates(introd.dag) <- list(x=c(COMPLEX=0, FILE=1, ISSUES=1, ADDED=-1, INTROD=1, REPO=2, REASON=-1, AUTHOR=0, TEAM=2),
y=c(COMPLEX=0, FILE=0, ISSUES=1, ADDED=1 , INTROD=2, REPO=2, REASON=3 , AUTHOR=3, TEAM=3))
drawdag(introd.dag)
```
Explanation of the variables in the DAG:
* INTROD - this is the thing we are trying to model/predict. It is the number of *introduced duplicates* in a particular file, in a particular change (commit). Zero or larger (absolute upper bound is on the order of the total number of lines in all files - due to the way Sonar calculates duplicates, though this assumes duplicates are counted line per line - in practice the tool will cluster identical lines or modules)
* ADDED - Number of added lines in the particular file, in the particular commit, as measured via Git (--numstat)
* REASON - The reason for changing the file - factor levels include "Added functionality", "TR fix", "Refactoring/spontaneous improvement".
* AUTHOR - The author that started the change (Git Author).
* TEAM - The team that the author was part of, at the time of the commit first being created.
* REPO - The repository holding the files. Authors and teams are more or less familiar with individual repositories (depending on whether they have worked there before).
* ISSUES - The number of existing duplicates, in the file, as measured by Sonar, before the commit is applied.
* FILE - The changed file. Files always belong to a repo. So, given that I know what file it is, I know what repo is affected. But a repo has (most likely) many files.
* COMPLEX - The McCabe complexity of the file (as calculated by Sonar), before the change was made.
Causal reasoning:
* By modifying code, authors make changes to existing files, create new files, or delete existing files.
* Authors belong to a team, interact with team members (reviews, merges, discusses changes).
* Both authors and teams have various levels of understanding of code repositories.
* Repositories serve as the grouping level of files. Each file belong to one, and only one, repository, and if I know what file is affected, I also know what repository was affected. But the converse is not true.
* Individual files have individual number of existing issues (duplicates), and existing McCabe complexity. Both values start at 0, and are bounded by the number of lines in the file (at least same magnitude).
The purpose of the model is to ascertain how the model can be used to illustrate and predict "team behaviour", in particular in an ownership context.
Teams that "care for" their repo are likely to introduce less duplicates than other, less caring (or more junior) teams.
Questions:
* We could label the arrow between AUTHOR and REPO with "familiarity". How do we reason about the direction of the arrow? Should the causal effect flow "from AUTHOR, via REPO, to INTROD", or is it "from REPO, via AUTHOR, to INTROD"?
* Similar discussion for TEAM.
* This is of course super important, as it determines whether REPO is a collider or not.
* We could put ADDED and REASON inside a grouping level called COMMIT, as they are effectively tied together there. But it is unclear what that would give us - we are not comparing commits per se.
```{r}
adjustmentSets(introd.dag, exposure = c("TEAM", "ADDED", "COMPLEX", "ISSUES", "REASON"), outcome = "INTROD")
```
So, if I were to include TEAM, ADDED, COMPLEX, ISSUES and REASON in my causal model, I also need to include both AUTHOR and FILE. But not REPO (due to the collider bias).
If I also include "REPO":
```{r}
adjustmentSets(introd.dag, exposure = c("TEAM", "ADDED", "COMPLEX", "ISSUES", "REASON", "REPO"), outcome = "INTROD")
```
So, if I include REPO in my predictors, then I should include AUTHOR, but not FILE.
```{r}
impliedConditionalIndependencies(introd.dag)
```
# Modified DAG
If we instead reason that AUTHORs only introduces duplicates via FILEs, and not directly influences INTROD, then our DAG becomes:
```{r}
introd.dag.2 <- dagitty("dag {
AUTHOR -> FILE;
REPO -> INTROD;
TEAM -> INTROD;
REASON -> INTROD;
ADDED -> INTROD;
ISSUES -> INTROD;
COMPLEX -> INTROD;
FILE -> ISSUES;
FILE -> REPO;
FILE -> COMPLEX;
AUTHOR -> TEAM;
TEAM -> REPO;
}")
coordinates(introd.dag.2) <- list(x=c(COMPLEX=0, FILE=1, ISSUES=1, ADDED=-1, INTROD=1, REPO=2, REASON=-1, AUTHOR=2, TEAM=2),
y=c(COMPLEX=0, FILE=0, ISSUES=1, ADDED=1 , INTROD=2, REPO=3, REASON=3 , AUTHOR=0, TEAM=2))
drawdag(introd.dag.2)
```
```{r}
adjustmentSets(introd.dag.2, exposure = c("TEAM", "COMPLEX", "ISSUES", "ADDED", "REASON"), outcome = "INTROD")
```
## Criticism of the above model
Important to note that arrows in a DAG should refer to _causal_ relationships. A _causes_ B, not "A is part of B", or "A is an attribute of B".
Suggestion from mentor is to make a matrix (spreadsheet) with motivations, including arrows and direction for all plausible combinations.
But be sure differentiate between causal effects and correlations or hierarchical information.
With that said, we will revise the above DAGs.
AUTHOR has an effect on ADDED, but FILE does not have a causal relationship with REPO (though given that I know what file is affected, the repo is also given - but this is a correlation, not _causation_).
Suggestion from mentor is to model the relationships in a matrix form, write down all possible combinations, and also indicate the direction of the arrows.
In particular, given a DAG and an analysis, one may also perform sensitivity analysis (akin to physics' pertubation analysis), that is, reasoning about how many or how strong unobserved counfounds that would be needed to erase a particular effect. https://evalf21.classes.andrewheiss.com/example/confounding-sensitivity/
* AUTHOR (or, COMMITTER, as this is the person doing the final merge) would have a causal effect on ADDED.
* FILE would not have a causal effect on REPO, but either (or both) would have a causal effect on AUTHOR or COMMITTER.
People are more or less famililar with the functions, tests, structure of particular FILEs or REPOs (this is the ownership part).
* A FILE will _have_ an existing complexity (COMPLEX), but a particular FILE would not _cause_ that same COMPLEX. It is still plausible that COMPLEX would affect the AUTHOR (and/or TEAM) via causation (harder to make changes to complex files).
* In itself, neither FILE nor REPO would affect INTROD. The unfamiliarity would come through either AUTHOR/COMMITTER/TEAM.
This would indicate that if I want to predict INTROD based on the TEAM, COMPLEX, ISSUES, ADDED and REASON variables, I also need to include FILE in my model. But not REPO or AUTHOR.
```{r}
adjustmentSets(introd.dag.2, exposure = c("TEAM", "COMPLEX", "ISSUES", "ADDED", "REASON", "REPO"), outcome = "INTROD")
```
No further parameters should be added if my linear regression includes TEAM, COMPLEX, ISSUES, ADDED, REASON and REPO.
```{r}
adjustmentSets(introd.dag.2, exposure = c("AUTHOR", "COMPLEX", "ISSUES", "ADDED", "REASON"), outcome = "INTROD")
```
There does not seem to be any adjustmentSets for these parameters. Using FILE instead of ISSUES or COMPLEX will solve the problem, returning an empty set again.
```{r}
impliedConditionalIndependencies(introd.dag.2)
```
```{r}
introd.dag.3 <- dagitty("dag {
AUTHOR [exposure];
TEAM [exposure];
INTROD [outcome];
AUTHOR -> ADDED;
AUTHOR -> REMOVED;
TEAM -> AUTHOR;
TEAM -> ADDED;
TEAM -> REMOVED;
FILE -> AUTHOR;
FILE -> TEAM;
REPO -> AUTHOR;
REPO -> TEAM;
ISSUES -> AUTHOR;
ISSUES -> TEAM;
COMPLEX -> AUTHOR;
COMPLEX -> TEAM;
ADDED -> INTROD;
REASON -> AUTHOR;
REASON -> TEAM;
REMOVED -> INTROD;
}")
#coordinates(introd.dag.2) <- list(x=c(COMPLEX=0, FILE=1, ISSUES=1, ADDED=-1, INTROD=1, REPO=2, REASON=-1, AUTHOR=2, TEAM=2),
# y=c(COMPLEX=0, FILE=0, ISSUES=1, ADDED=1 , INTROD=2, REPO=3, REASON=3 , AUTHOR=0, TEAM=2))
drawdag(introd.dag.3)
```
```{r}
impliedConditionalIndependencies(introd.dag.3)
```
```{r}
adjustmentSets(introd.dag.3, exposure = c("AUTHOR", "TEAM", "COMPLEX", "ADDED", "ISSUES", "REPO"), outcome = "INTROD")
```
```{r}
adjustmentSets(introd.dag.3, exposure = c("AUTHOR"), outcome = "INTROD")
```
So, this tells us that if we want to examine the total way that TEAM and AUTHOR influences INTROD, we should also include REASON into our model.
This is becuase of the backdoor path through REMOVED. We assume that REASON will have an impact at least through the removal of files.
The model can be more graphically explored on http://www.dagitty.net/
This also includes coloring, and using node types, making the DAG a wee bit more understandable.
Would AUTHOR have *any* effect on INTROD except through ADDED or REMOVED? I think not? Likewise for TEAM... They might influence Author (committer, really).
# Only action through added/removed lines
We could plausibly argue that the only action an AUTHOR or a TEAM has on the number of issues is through the number of ADDED or REMOVED lines. These are, after all, the sole change that the AUTHOR (or other users) have on the file in question.
Does it change any inference?
```{r}
introd.dag.4 <- dagitty("dag {
AUTHOR [exposure];
TEAM [exposure];
INTROD [outcome];
AUTHOR -> ADDED;
AUTHOR -> REMOVED;
TEAM -> AUTHOR;
FILE -> AUTHOR;
FILE -> TEAM;
REPO -> AUTHOR;
REPO -> TEAM;
ISSUES -> AUTHOR;
ISSUES -> TEAM;
COMPLEX -> AUTHOR;
COMPLEX -> TEAM;
ADDED -> INTROD;
REASON -> AUTHOR;
REASON -> REMOVED;
REMOVED -> INTROD;
}")
#coordinates(introd.dag.4) <- list(x=c(COMPLEX=0, FILE=1, ISSUES=1, ADDED=-1, INTROD=1, REPO=2, REASON=-1, AUTHOR=2, TEAM=2),
# y=c(COMPLEX=0, FILE=0, ISSUES=1, ADDED=1 , INTROD=2, REPO=3, REASON=3 , AUTHOR=0, TEAM=2))
drawdag(introd.dag.4)
```
```{r}
adjustmentSets(introd.dag.4, exposure = c("TEAM", "AUTHOR"), outcome="INTROD")
```
```{r}
adjustmentSets(introd.dag.4, exposure = c("TEAM", "AUTHOR", "COMPLEX", "ISSUES", "REPO"), outcome="INTROD")
```
```{r}
impliedConditionalIndependencies(introd.dag.4)
```
We could add a COMMIT node, to indicate that both AUTHOR and TEAM influence commits, and commits cause ADDED or REMOVED lines.
```{r}
introd.dag.5 <- dagitty("dag {
AUTHOR [exposure];
TEAM [exposure];
INTROD [outcome];
COMMIT -> ADDED;
COMMIT -> REMOVED;
AUTHOR -> COMMIT;
TEAM -> COMMIT;
FILE -> AUTHOR;
FILE -> TEAM;
REPO -> AUTHOR;
REPO -> TEAM;
ISSUES -> AUTHOR;
ISSUES -> TEAM;
COMPLEX -> AUTHOR;
COMPLEX -> TEAM;
ADDED -> INTROD;
REASON -> AUTHOR;
REASON -> REMOVED;
REMOVED -> INTROD;
}")
#coordinates(introd.dag.4) <- list(x=c(COMPLEX=0, FILE=1, ISSUES=1, ADDED=-1, INTROD=1, REPO=2, REASON=-1, AUTHOR=2, TEAM=2),
# y=c(COMPLEX=0, FILE=0, ISSUES=1, ADDED=1 , INTROD=2, REPO=3, REASON=3 , AUTHOR=0, TEAM=2))
drawdag(introd.dag.5)
```
```{r}
adjustmentSets(introd.dag.5, exposure = c("AUTHOR", "TEAM", "COMPLEX", "REPO", "ISSUES"), outcome = "INTROD")
```
Adding COMMIT as a descendent of AUTHOR (and before ADDED and REMOVED) does not change the number of nodes that should be added to our graph. We should still add REASON, to close the backdoor path through REMOVED (i.e. the possibility that refactorings may bias the results, related to removed files).
Adding committer (as the person finally making the change to the repo). Committer and author MAY be the same person, but does not have to. Only the commiter influences the final result.
```{r}
introd.dag.6 <- dagitty("dag {
AUTHOR [exposure];
TEAM [exposure];
COMMITTER [exposure]
INTROD [outcome];
COMMITTER -> ADDED;
COMMITTER -> REMOVED;
AUTHOR -> COMMITTER;
TEAM -> COMMITTER;
FILE -> AUTHOR;
FILE -> TEAM;
REPO -> AUTHOR;
REPO -> TEAM;
ISSUES -> AUTHOR;
ISSUES -> TEAM;
COMPLEX -> AUTHOR;
COMPLEX -> TEAM;
ADDED -> INTROD;
REASON -> AUTHOR;
REASON -> REMOVED;
REMOVED -> INTROD;
}")
#coordinates(introd.dag.4) <- list(x=c(COMPLEX=0, FILE=1, ISSUES=1, ADDED=-1, INTROD=1, REPO=2, REASON=-1, AUTHOR=2, TEAM=2),
# y=c(COMPLEX=0, FILE=0, ISSUES=1, ADDED=1 , INTROD=2, REPO=3, REASON=3 , AUTHOR=0, TEAM=2))
drawdag(introd.dag.6)
```
```{r}
adjustmentSets(introd.dag.6, exposure = c("AUTHOR", "TEAM", "COMMITTER", "COMPLEX", "REPO", "ISSUES"), outcome = "INTROD")
```
Having committer in the DAG does not change anything.
```{r}
adjustmentSets(introd.dag.6, exposure = c("TEAM", "COMMITTER", "COMPLEX", "REPO", "ISSUES"), outcome = "INTROD")
```
If we swap AUTHOR for COMMITTER (person doing the final change), we still have to include REASON in our model. But having AUTHOR there is not needed, if we are only striving to assess COMMITTER
# Team-only model
We could model the author&team as one entity, due to our sparse data. That effectively collapses the interaction and questions what is the causal relation between authors and teams (e.g. does the author cause the team's behaviour, or vice versa).
```{r}
introd.dag.7 <- dagitty("dag {
TEAM [exposure];
REPO [exposure]
INTROD [outcome];
TEAM -> ADDED;
TEAM -> REMOVED;
FILE -> TEAM;
REPO -> TEAM;
ISSUES -> TEAM;
COMPLEX -> TEAM;
ADDED -> INTROD;
REASON -> TEAM;
REMOVED -> INTROD;
}")
drawdag(introd.dag.7)
```
Adjustment set is empty, as all causal paths flow through TEAM. We could of couse still investigate the impact of ISSUES, REASON, COMPLEX.
If we were to believe that some reasons (e.g. improvements or refactorings) would impact added lines, then that would become a collider and should be part of the model, even if it is not an outcome per se.
```{r}
introd.dag.8 <- dagitty("dag {
TEAM [exposure];
REPO [exposure]
INTROD [outcome];
TEAM -> ADDED;
TEAM -> REMOVED;
FILE -> TEAM;
REPO -> TEAM;
ISSUES -> TEAM;
COMPLEX -> TEAM;
ADDED -> INTROD;
REASON -> TEAM;
REASON -> ADDED;
REMOVED -> INTROD;
}")
drawdag(introd.dag.8)
```
But it seems unlikely that REASON would impact ADDED lines without first going through ADDED or REMOVED lines (e.g. does the REASON has a different cause than going through the AUTHOR/TEAM process?)
```{r}
adjustmentSets(introd.dag.8, exposure = "REPO", outcome = "INTROD", effect = "direct")
```
### New causal model
```{r}
testImplications <- function( covariance.matrix, sample.size ){
library(ggm)
tst <- function(i){ pcor.test( pcor(i,covariance.matrix), length(i)-2, sample.size )$pvalue }
tos <- function(i){ paste(i,collapse=" ") }
implications <- list(c("COMPLEX","FEEDBACK"),
c("COMPLEX","REQ"),
c("COMPLEX","NCOMMITS"),
c("DUP","FEEDBACK"),
c("DUP","REQ"),
c("DUP","NCOMMITS"),
c("FEEDBACK","INTROD","ADD","COMPLEX","DEL","DUP"),
c("FEEDBACK","REQ"),
c("FEEDBACK","TEAM_REPO"),
c("FEEDBACK","NCOMMITS"),
c("INTROD","REQ","DUP","COMPLEX","DEL","ADD"),
c("INTROD","TEAM_REPO","DEL","ADD","DUP","COMPLEX"),
c("INTROD","NCOMMITS","DUP","COMPLEX","DEL","ADD"),
c("REQ","TEAM_REPO"),
c("REQ","NCOMMITS"),
c("TEAM_REPO","NCOMMITS"))
data.frame( implication=unlist(lapply(implications,tos)),
pvalue=unlist( lapply( implications, tst ) ) )
}
introd.dag.9 <- dagitty("dag {
bb=\"-4.944,-4.291,4.847,5.173\"
ADD [pos=\"0.918,-0.890\"]
CARE [latent,pos=\"-2.168,0.674\"]
COMPLEX [pos=\"-2.590,-1.081\"]
DEL [pos=\"-1.062,-0.699\"]
DUP [pos=\"-1.826,-0.777\"]
EXP [latent,pos=\"-1.215,0.787\"]
FEEDBACK [pos=\"1.355,0.179\"]
INTROD [outcome,pos=\"-0.995,-2.778\"]
KNOW [latent,pos=\"0.428,0.919\"]
NCOMMITS [pos=\"1.165,1.326\"]
REQ [pos=\"0.923,-1.885\"]
TEAMREPO [exposure,pos=\"-1.262,1.688\"]
ADD -> INTROD
CARE -> COMPLEX
CARE -> DUP
CARE -> EXP
COMPLEX -> INTROD
DEL -> INTROD
DUP -> INTROD
EXP -> KNOW
FEEDBACK -> ADD
FEEDBACK -> DEL
FEEDBACK -> KNOW
KNOW -> ADD
KNOW -> DEL
NCOMMITS -> KNOW
REQ -> ADD
REQ -> DEL
TEAMREPO -> CARE
TEAMREPO -> KNOW
}")
drawdag(introd.dag.9)
```
```{r}
ggdag(introd.dag.9) + theme_dag_blank()
```
The backdoor criterion says we should exclude controls that are descendents of the treatment along paths to the outcome.
If our target is the Average Causal Effect, we want to leave all channels through which the causal effect flows untouched.
We should not control for mediators.
Neutral controls might work though. That is, when Z is a cause of the mediator, but not an effect of the treatment.
```{r}
introd.dag.10 <- dagitty("dag {
bb=\"-6.094,-5.092,6.372,5.521\"
ADD [pos=\"-0.796,0.553\"]
CARE [latent,pos=\"-1.863,-0.920\"]
COMPLEX [pos=\"-2.299,-2.207\"]
DEL [pos=\"-2.339,0.520\"]
DUP [pos=\"-2.902,-2.212\"]
EXP [latent,pos=\"-1.159,-0.920\"]
FEEDBACK [pos=\"1.121,-0.942\"]
INTROD [outcome,pos=\"-1.400,-3.971\"]
KNOW [latent,pos=\"-0.106,-0.931\"]
NCOMMITS [pos=\"-1.112,-1.558\"]
NTESTS [pos=\"1.155,-1.574\"]
REQ [exposure,pos=\"-1.782,1.553\"]
ADD -> CARE
ADD -> KNOW
CARE -> COMPLEX
CARE -> DUP
CARE -> INTROD
COMPLEX -> INTROD
DEL -> CARE
DEL -> KNOW
DUP -> INTROD
EXP -> KNOW
FEEDBACK -> ADD
FEEDBACK -> DEL
FEEDBACK -> INTROD
FEEDBACK -> KNOW
KNOW -> INTROD
NCOMMITS -> EXP
NTESTS -> FEEDBACK
REQ -> ADD
REQ -> DEL
}")
drawdag(introd.dag.10)
```
```{r}
adjustmentSets(introd.dag.10)
```
```{r}
introd.dag.11 <- dagitty("dag {
bb=\"-6.094,-5.092,6.372,5.521\"
ADD [exposure,pos=\"-0.636,-1.651\"]
CARE [latent,pos=\"-2.527,0.459\"]
COMPLEX [pos=\"-2.299,-2.207\"]
DEL [exposure,pos=\"-1.507,-1.662\"]
DUP [pos=\"-2.902,-2.212\"]
EXP [latent,pos=\"-0.159,0.184\"]
FEEDBACK [pos=\"-1.004,-0.503\"]
INTROD [outcome,pos=\"-1.400,-3.971\"]
KNOW [latent,pos=\"0.484,-1.410\"]
NCOMMITS [pos=\"-0.837,0.717\"]
NTESTS [pos=\"-1.601,0.151\"]
ADD -> INTROD
CARE -> ADD
CARE -> COMPLEX
CARE -> DEL
CARE -> DUP
COMPLEX -> INTROD
DEL -> INTROD
DUP -> INTROD
EXP -> KNOW
FEEDBACK -> ADD
FEEDBACK -> DEL
FEEDBACK -> KNOW
KNOW -> INTROD
NCOMMITS -> EXP
NTESTS -> FEEDBACK
}
")
drawdag(introd.dag.11)
```
```{r}
introd.dag.12 <- dagitty("
dag {
bb=\"-3.165,-3.602,3.813,3.596\"
ADD [exposure,pos=\"-1.894,-2.371\"]
COMPLEX [pos=\"0.784,2.997\"]
DUP [pos=\"3.232,0.427\"]
FEAR [latent,pos=\"0.028,0.052\"]
INTROD [outcome,pos=\"0.529,-3.002\"]
TESTS [pos=\"-2.584,1.942\"]
ADD -> INTROD
COMPLEX -> FEAR
DUP -> FEAR
FEAR -> ADD
FEAR -> INTROD
TESTS -> FEAR
}
")
drawdag(introd.dag.12)
```
```{r}
adjustmentSets(introd.dag.12)
```
```{r}
introd.dag.13 <- dagitty("dag {
bb=\"-3.165,-3.602,3.813,3.596\"
ADD [exposure,pos=\"-1.894,-2.371\"]
COMPLEX [pos=\"0.313,-1.217\"]
DUP [pos=\"1.504,-1.362\"]
INTROD [outcome,pos=\"0.529,-3.002\"]
TESTS [pos=\"-0.975,-1.011\"]
ADD -> INTROD
COMPLEX -> ADD
COMPLEX -> INTROD
DUP -> ADD
DUP -> INTROD
TESTS -> ADD
TESTS -> INTROD
}
")
drawdag(introd.dag.13)
```
```{r}
adjustmentSets(introd.dag.13)
```
We have four possible adjustment sets. We have to condition on X4, but as it is a collider (receiving arrows both from X1 and X2), this will open the causal path, introducing bias.
```{r}
pearl.dag.fig.3.4 <- dagitty("dag {
Xi [exposure]
Xj [outcome]
X1 -> X3
X1 -> X4
X2 -> X4
X2 -> X5
X3 -> Xi
X4 -> Xi
X4 -> Xj
X5 -> Xj
Xi -> X6
X6 -> Xj
}")
drawdag(pearl.dag.fig.3.4)
adjustmentSets(pearl.dag.fig.3.4)
```
If we make X4 a pipe instead of a collider, it is enough to condition on X4.
```{r}
pearl.dag.fig.3.4.pipe <- dagitty("dag {
Xi [exposure]
Xj [outcome]
X1 -> X3
X1 -> X4
X4 -> X2
X2 -> X5
X3 -> Xi
X4 -> Xi
X4 -> Xj
X5 -> Xj
Xi -> X6
X6 -> Xj
}")
drawdag(pearl.dag.fig.3.4.pipe)
adjustmentSets(pearl.dag.fig.3.4.pipe)
```
```{r}
my.dag <- dagitty("dag {
ADD [exposure]
INTROD [outcome]
ADD -> REM
ADD -> DUP
ADD -> COMPLEX
DUP -> COMPLEX
REM -> INTROD
DUP -> INTROD
COMPLEX -> INTROD
ADD -> INTROD
}")
drawdag(my.dag)
adjustmentSets(my.dag)
```
```{r}
d.0 <- dagitty("dag {
lang [exposure]
rank [outcome]
skill [unobserved]
lang -> rank
lang -> size
nick -> lang
nick -> size
nick -> skill
challenge -> nick
challenge -> rank
challenge -> skill
skill -> rank
}")
drawdag(d.0)
adjustmentSets(d.0)
impliedConditionalIndependencies(d.0)
```
```{r}
d.1 <- dagitty("dag {
lang [exposure]
rank [outcome]
skill [unobserved]
lang -> rank
lang -> size
nick -> lang
nick -> size
nick -> skill
challenge -> nick
challenge -> rank
challenge -> skill
skill -> rank
skill -> size
}")
drawdag(d.1)
adjustmentSets(d.1)
impliedConditionalIndependencies(d.1)
```
```{r}
d.2 <- dagitty("dag {
lang [exposure]
rank [outcome]
skill [unobserved]
lang -> rank
lang -> size
nick -> lang
nick -> size
nick -> skill
challenge -> nick
challenge -> rank
challenge -> skill
skill -> rank
skill -> size
nick -> size
}")
drawdag(d.2)
adjustmentSets(d.2)
impliedConditionalIndependencies(d.2)
```
```{r}
test.dag <- dagitty("dag {
ADD [exposure]
INTROD [outcome]
KNOW [unobserved]
CLEAN [unobserved]
CLEAN -> REM
CLEAN -> ADD
REM -> INTROD
DUP -> COMPLEX
KNOW -> COMPLEX
KNOW <- DUP
KNOW <- ADD
DUP -> ADD
COMPLEX -> ADD
ADD -> INTROD
}")
drawdag(test.dag)
adjustmentSets(test.dag)
impliedConditionalIndependencies(test.dag)
```
```{r}
test.dag <- dagitty("dag {
ADD [exposure]
INTROD [outcome]
KNOW [unobserved]
CLEAN [unobserved]
CLEAN -> REM
CLEAN -> ADD
REM -> INTROD
COMPLEX -> INTROD
DUP -> INTROD
DUP -> COMPLEX -> KNOW
DUP -> KNOW
KNOW -> ADD
DUP -> ADD
COMPLEX -> ADD
ADD -> INTROD
}")
ggdag(test.dag)
#drawdag(test.dag)
adjustmentSets(test.dag)
impliedConditionalIndependencies(test.dag)
```
```{r}
adjustmentSets(test.dag)
```
```{r}
updated_dag <- dagitty("dag {
bb=\"0,0,1,1\"
ADD [adjusted,pos=\"0.603,0.269\"]
CLEAN [latent,pos=\"0.186,0.482\"]
COMP [adjusted,pos=\"0.529,0.397\"]
DEL [adjusted,pos=\"0.594,0.490\"]
DUP [adjusted,pos=\"0.512,0.103\"]
INTR [outcome,pos=\"0.928,0.296\"]
KNOW [latent,pos=\"0.198,0.067\"]
TEAM [exposure,pos=\"0.065,0.279\"]
ADD -> INTR
CLEAN -> ADD
CLEAN -> DEL
COMP -> ADD
COMP -> INTR
COMP -> KNOW
DEL -> ADD
DEL -> INTR
DUP -> ADD
DUP -> COMP
DUP -> INTR
DUP -> KNOW
KNOW -> ADD
TEAM -> ADD
TEAM -> CLEAN
TEAM -> COMP
TEAM -> DEL
TEAM -> DUP
TEAM -> KNOW
}") |> tidy_dagitty()
ggdag(updated_dag, exposures = "TEAM") + theme_dag()
```
```{r}
coords <- data.frame(name=c("TEAM", "KNOW", "CLEAN", "DUP", "COMP", "ADD", "DEL", "INTR"),
x=c( 0, 0.2, 0.4, 0.4, 0.35, 0.55, 0.38, 1.0),
y=c( -0.03, 0.2, -0.08, 0.2, 0.03, 0.07, -0.2, 0))
dagified <- dagify( INTR ~ DEL + COMP + ADD + DUP + TEAM,
COMP ~ TEAM + DUP,
DEL ~ TEAM + CLEAN,
ADD ~ COMP + CLEAN + KNOW + DUP,
DUP ~ TEAM,
CLEAN ~ TEAM,
KNOW ~ TEAM + DUP + COMP,
exposure = "TEAM",
outcome = "INTR",
latent = c("CLEAN", "KNOW"),
coords=coords)
(p <- ggdag_status(dagified) + theme_dag() + theme(legend.position="bottom") + guides(color = guide_legend(override.aes = list(size = 2), title=NULL, direction="horizontal", nrow = 1))
)
ggsave("causal-dag.pdf", p, device = "pdf", dpi = 1200, width=15, height=17.8, units="cm")
```
```{r}
impliedConditionalIndependencies(dagified)
```
```{r}
(p <- ggdag_adjustment_set(dagified, shadow = T) + theme_dag() )
```
```{r}
coords <- data.frame(name=c("TEAM", "KNOW", "CLEAN", "DUP", "COMP", "ADD", "REM", "INTR"),
x=c( 0, 0.2, 0.4, 0.4, 0.35, 0.55, 0.38, 1.0),
y=c( -0.03, 0.2, -0.08, 0.2, 0.03, 0.07, -0.2, 0))
dagified <- dagify( INTR ~ REM + COMP + ADD + DUP + TEAM,
COMP ~ TEAM + DUP,
REM ~ TEAM + CLEAN,
ADD ~ COMP + CLEAN + KNOW + DUP,
DUP ~ TEAM,
CLEAN ~ TEAM,
KNOW ~ TEAM + DUP + COMP,
exposure = "TEAM",
outcome = "INTR",
latent = c("CLEAN", "KNOW"),
coords=coords)
(p <- ggdag_status(dagified) + theme_dag() + theme(legend.position="bottom") + guides(color = guide_legend(override.aes = list(size = 2), title=NULL, direction="horizontal", nrow = 1))
)
```
```{r}
figsave("causal-dag.pdf", p)
```