Bayes Rule

Bayesian Networks

Problem Set 2

In the Grade Network that we looked at in the notes, what happens to the probability of Difficulty of Course when you present the evidence that the received recommendation letter was good? In addition, now present the evidence that both SAT scores were good and the letter of recommendation was good, What is the probability of the Difficulty of Course now? You should use the gRain package in R to build your network and perform these calculations. You may need to install RBGL package from BioConductor in R to get gRain working. See http://www.bioconductor.org/packages/release/bioc/html/RBGL.html for instructions on RBGL.

#source("http://bioconductor.org/biocLite.R")
#biocLite("RBGL")
#biocLite("Rgraphviz")
suppressWarnings(suppressMessages(require(gRain)))
suppressWarnings(suppressMessages(require(Rgraphviz)))

ny <- c("no","yes")
lh <- c("low","high")

d <- cptable(~difficulty, value = c(0.3,0.7),levels =ny)
i <- cptable(~intelligence, values= c(0.8,0.2), levels=ny)
s.i <- cptable(~sat|intelligence, values= c(0.9,0.1,0.2,0.8), levels=lh)
g.di <-cptable(~grade|difficulty:intelligence, values =c(0.6,0.4,0.01,0.99,0.8,0.2,0.1,0.9), levels=lh)
l.g <-cptable(~letter|grade, value =c(0.9,0.1,0.05,0.95), levels=lh)

bn.grade <-compileCPT(list(d,i,s.i,g.di,l.g))

bas.Graph <-grain(bn.grade)

## Compute marginal probabilities

querygrain(bas.Graph,node=c("difficulty","intelligence","sat","grade","letter"),type="marginal")
## $difficulty
## difficulty
##  no yes 
## 0.3 0.7 
## 
## $intelligence
## intelligence
##  no yes 
## 0.8 0.2 
## 
## $grade
## grade
##    low   high 
## 0.2116 0.7884 
## 
## $sat
## sat
##  low high 
## 0.76 0.24 
## 
## $letter
## letter
##     low    high 
## 0.22986 0.77014