Answer To: Microsoft Word - SIT743-Assignment-2 Page 1 of 8 SIT743 Bayesian Learning and Graphical Models...
Abr Writing answered on May 26 2021
assignment.R
library(bnlearn)
# source("https://bioconductor.org/biocLite.R")
library(BiocManager)
# BiocManager::install(c("gRain", "RBGL", "gRbase", "Rgraphviz))
library(RBGL)
library(gRbase)
library(gRain)
library(Rgraphviz)
library(visNetwork)
# Problem 1
## Problem 1.5
bn <- model2network("[H][M|H][W|H][N|W:H][R|N:H][S|N]")
plot(bn)
dsep(bn, "M", "S")
dsep(bn, "W", "R", c("N", "H"))
dsep(bn, "R", "W", "H")
dsep(bn, "S", "W", "H")
# Problem 2
## Problem 2.1
### Problem 2.1.a
wd <- c("wet", "dry")
lh <- c("low", "high")
bc <- c("Bass", "Cod")
lmd <- c("light", "medium", "dark")
wt <- c("wide", "thin")
a <- cptable(~A, values=c(30, 70),levels=wd)
b <- cptable(~B, values=c(80, 20),levels=lh)
c.ab <- cptable(~C|A:B,
values=c(40, 60, 50, 50, 60, 40, 30, 70),
levels = bc)
d.c <- cptable(~D|C,
values=c(20, 40, 40, 50, 30, 20),
levels=lmd)
e.c <- cptable(~E|C,
values=c(60, 40, 40, 60),
levels=wt)
plist <- compileCPT(list(a, b, c.ab, d.c, e.c))
plist
net <- grain(plist)
net
### Problem 2.1.b
plist$A
plist$B
plist$C
plist$D
plist$E
## Problem 2.2
### Problem 2.2.a
querygrain(setEvidence(net,
evidence = list(
B="low"
)),
nodes = c("E"),
type = "marginal")
### Problem 2.2.b
querygrain(setEvidence(net,
evidence = list(
D="dark",
A="dry"
)),
nodes = c("C"),
type = "marginal")
### Problem 2.2.c
querygrain(net, nodes = c("C", "D"), type="joint")
### Problem 2.2.d
querygrain(net, nodes = c("C"), type="marginal")
# Problem 3
## Problem 3.1
bn <- model2network("[A][B][C|A:B][D|B:C]")
plot(bn)
dsep(bn, "A", "B")
## Problem 3.3
df <- data.frame(
a = c(0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1),
b = c(1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0),
c = c(1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0),
d = c(1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1)
)
df
cat("Alpha = ",
sum(df$a == 0)/nrow(df),
"\n",
sep = "")
cat("Beta = ",
sum(df$d == 0 & df$b == 1 & df$c == 1)/sum(df$b == 1 & df$c == 1),
"\n",
sep =...