dagitty パッケージのダウンロード
install.packages("dagitty")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
daittyをロード
library(dagitty)
1. 単純な例
# DAGの定義
dag1 <- dagitty("dag {
Z -> Y
Z -> X
X -> Y
}")
# DAGのプロット
plot(dag1)
## Plot coordinates for graph not supplied! Generating coordinates, see ?coordinates for how to set your own.

# AからCへの影響を調べる場合に調整すべき変数のセットを求める
adjustment_sets <- adjustmentSets(dag1, exposure = "X", outcome = "Y")
print(adjustment_sets)
## { Z }
2. もうすこし複雑な例
DAGの定義
# DAGの定義
dag2 <- dagitty("dag {
E -> I
A -> E
A -> I
F -> E
F -> A
G -> A
}")
DAGを図示
# DAGのプロット
plot(dag2)
## Plot coordinates for graph not supplied! Generating coordinates, see ?coordinates for how to set your own.

制御すべき交絡因子
# EからIへの影響を調べる場合に調整すべき変数のセットを求める
adjustment_sets <- adjustmentSets(dag2, exposure = "E", outcome = "I")
print(adjustment_sets)
## { A }
3. さらに複雑な例
# DAGの定義
dag3 <- dagitty("dag {
X -> Y
X -> V
Z -> X
Z -> Y
W -> Z
W -> O
O -> Y
V -> Y
O -> X
S -> Z
}")
plot(dag3)
## Plot coordinates for graph not supplied! Generating coordinates, see ?coordinates for how to set your own.

# XからYへの影響を調べる場合に調整すべき変数のセットを求める
adjustment_sets <- adjustmentSets(dag3, exposure = "X", outcome = "Y")
print(adjustment_sets)
## { O, Z }
# DAGの定義
dag4 <- dagitty("dag {
O -> X
O -> Y
S -> Z
V -> Y
W -> O
W -> Z
X -> S
X -> V
X -> Y
Z -> Y
}")
plot(dag4)
## Plot coordinates for graph not supplied! Generating coordinates, see ?coordinates for how to set your own.

# XからYへの影響を調べる場合に調整すべき変数のセットを求める
adjustmentSets(dag4, exposure = "X", outcome = "Y")
## { O }