# なんちゃって病院でベイジアンネットワーク

• 先日、法廷でベイジアンネットワークという記事を書いた
• 病院だとどうなるか？
• ごく簡単に
• ネットワークを作るとき、病気である事前確率は有病率などで決まる。問診の結果で有病率推定事前確率を決めてもよい
• 検査をする
• PPV、NPVで出す
• これは・・・
```library(gRain)
D <- cptable(~D,values = c(0.5,0.5),levels=c("disease","health"))
T1 <- cptable(~T1|D,values=c(80,20,30,70),levels=c("+","-"))
plist <- compileCPT(list(D,T1))
net <- grain(plist)
net2 <- setEvidence(net,nodes = c("T1"),states=c("+"))
querygrain(net2,nodes=c("D"))
net2 <- setEvidence(net,nodes = c("T1"),states=c("-"))
querygrain(net2,nodes=c("D"))
querygrain(net,nodes=c("T1","D"),type="joint")
```
```> querygrain(net2,nodes=c("D"))
\$D
D
disease    health
0.7272727 0.2727273

> net2 <- setEvidence(net,nodes = c("T1"),states=c("-"))
> querygrain(net2,nodes=c("D"))
\$D
D
disease    health
0.2222222 0.7777778
> querygrain(net,nodes=c("T1","D"),type="joint")
D
T1  disease health
+     0.4   0.15
-     0.1   0.35
```
• 検査を二つにし、２つの検査が独立だとすると…
```library(gRain)
D <- cptable(~D,values = c(0.5,0.5),levels=c("disease","health"))
T1 <- cptable(~T1|D,values=c(80,20,30,70),levels=c("+","-"))
T2 <- cptable(~T2|D,values=c(80,20,30,70),levels=c("+","-"))
plist <- compileCPT(list(D,T1,T2))
net <- grain(plist)
net2 <- setEvidence(net,nodes = c("T1","T2"),states=c("+","+"))
querygrain(net2,nodes=c("D"))
net2 <- setEvidence(net,nodes = c("T1","T2"),states=c("+","-"))
querygrain(net2,nodes=c("D"))
net2 <- setEvidence(net,nodes = c("T1","T2"),states=c("-","+"))
querygrain(net2,nodes=c("D"))
net2 <- setEvidence(net,nodes = c("T1","T2"),states=c("-","-"))
querygrain(net2,nodes=c("D"))
querygrain(net,nodes=c("T1","T2","D"),type="joint")
```
```> library(gRain)
> D <- cptable(~D,values = c(0.5,0.5),levels=c("disease","health"))
> T1 <- cptable(~T1|D,values=c(80,20,30,70),levels=c("+","-"))
> T2 <- cptable(~T2|D,values=c(80,20,30,70),levels=c("+","-"))
> plist <- compileCPT(list(D,T1,T2))
> net <- grain(plist)
> net2 <- setEvidence(net,nodes = c("T1","T2"),states=c("+","+"))
> querygrain(net2,nodes=c("D"))
\$D
D
disease    health
0.8767123 0.1232877

> net2 <- setEvidence(net,nodes = c("T1","T2"),states=c("+","-"))
> querygrain(net2,nodes=c("D"))
\$D
D
disease    health
0.4324324 0.5675676

> net2 <- setEvidence(net,nodes = c("T1","T2"),states=c("-","+"))
> querygrain(net2,nodes=c("D"))
\$D
D
disease    health
0.4324324 0.5675676

> net2 <- setEvidence(net,nodes = c("T1","T2"),states=c("-","-"))
> querygrain(net2,nodes=c("D"))
\$D
D
disease    health
0.0754717 0.9245283

> querygrain(net,nodes=c("T1","T2","D"),type="joint")
, , D = disease

T2
T1     +    -
+ 0.32 0.08
- 0.08 0.02

, , D = health

T2
T1      +     -
+ 0.045 0.105
- 0.105 0.245
```