# [R] compute probabilities on a Bayesian Network (SOLVED)

Marcio Pupin Mello mello at ieee.org
Wed Sep 28 20:41:06 CEST 2011

```I studied a little (I mean "a lot") and found out a solution. Then I
decided to post here aiming to help people who are interested...
The solution uses the "gRain" package... but because one dependence (the
"graph" package) has been removed from the CRAN repository you have to
use the follow script to install "gRain" package (I tested it on Windows 7):

#run to install "gRain" and "graph" packages
chooseBioCmirror() #then choose the nearest one
setRepositories() #then use "Ctrl" key to select all
install.packages(c("graph","gRain"),dependencies=TRUE))

Then you can run the code to build the Bayesian Network structure of the
figure in http://www.dsr.inpe.br/~mello/1727/BNgrapmodel.png

#run to build the Bayesian Network shown in Figure
library(gRain)
v.L<-cptable(~L,values=c(0.35,(1-0.35)),levels=c("T","F"),normalize=T,smooth=0)
v.R<-cptable(~R,values=c(0.3,(1-0.3)),levels=c("T","F"),normalize=T,smooth=0)
v.H<-cptable(~H|D,values=c(0.75,(1-0.75),0.2,(1-0.2)),levels=c("T","F"),normalize=T,smooth=0)
v.D<-cptable(~D,values=c(0.05,(1-0.05)),levels=c("T","F"),normalize=T,smooth=0)
v.C<-cptable(~C|S,values=c(0.9,(1-0.9),0.12,(1-0.12)),levels=c("T","F"),normalize=T,smooth=0)
v.S<-cptable(~S|D:H:R:L,values=c(0.60,(1-0.60),0.65,(1-0.65),0.62,(1-0.62),0.67,(1-0.67),0.61,(1-0.61),0.64,(1-0.64),0.61,(1-0.61),0.70,(1-0.70),0.05,(1-0.05),0.09,(1-0.09),0.07,(1-0.07),0.10,(1-0.10),0.06,(1-0.06),0.08,(1-0.08),0.07,(1-0.07),0.12,(1-0.12)),levels=c("T","F"),normalize=T,smooth=0)
CPT<-compileCPT(list(v.L,v.R,v.H,v.D,v.C,v.S))
BN<-grain(CPT,smooth=0)

# what is the probability of S=T given C=T, L=T, R=F, H=F and D=F?
scenaria.q1<-list(nodes=c("C","L","R","H","D"),states=c("T","T","F","F","F"))
querygrain(setFinding(BN,nodes=scenaria.q1\$nodes,states=scenaria.q1\$states),nodes="S")

# what is the probability of S=T given C=F, L=F, R=T, H=T, and D=T?
scenaria.q2<-list(nodes=c("C","L","R","H","D"),states=c("F","F","T","T","T"))
querygrain(setFinding(BN,nodes=scenaria.q2\$nodes,states=scenaria.q2\$states),nodes="S")

# what is the probability of what is the probability of S=T when my only
one evidence is that C=T?
scenaria.q3<-list(nodes=c("C"),states=c("T"))
querygrain(setFinding(BN,nodes=scenaria.q3\$nodes,states=scenaria.q3\$states),nodes="S")

On 26/09/2011 18:42, Marcio Pupin Mello wrote:
> Deal R Users,
> I'm trying to find out how can I compute probabilities on a Bayesian
> Network using R. The Bayesian Network I modelled is shown at
> http://www.dsr.inpe.br/~mello/1727/BNgrapmodel.png and I'd like to know
> how can I proceed (commands on R) to answer questions like: (1) what is
> the probability of S=T given C=T, L=T, R=F, H=F and D=F; or (2) what is
> the probability of S=T given C=F, L=F, R=T, H=T, and D=T; or even (3)
> what is the probability of S=T when my only one evidence is that C=T
> (i.e I don't know the other variables values). I used Microsoft Belief
> Networks to compute the results but I don't know how to do it with R.
> Looking forward to receiving replies!
>
> Marcio Pupin Mello
>

--
Marcio Pupin Mello

Survey Engineer
Ph.D candidate in Remote Sensing
National Institute for Space Research (INPE) - Brazil
Laboratory of Remote Sensing in Agriculture and Forestry (LAF)
www.dsr.inpe.br/~mello

```