Marcio Pupin Mello
2011-Sep-28 12:03 UTC
[R] how to solve a simple discrete Bayesian Belief Network?
Can somebody save-me? Thanks in advance!
#R script:
#trying to find out how solve a discrete Bayesian Belief Network.
#option: using 'catnet' package
#BEGIN
library(catnet)
cnet <- cnNew(nodes = c("a", "b", "c"), cats =
list(c("1", "2"),
c("1", "2"), c("1", "2")), parents =
list(NULL, c(1), c(1,
2)), probs = list(c(0.2, 0.8), list(c(0.6, 0.4), c(0.4, 0.6)),
list(list(c(0.3, 0.7), c(0.7, 0.3)), list(c(0.9, 0.1), c(0.1,
0.9)))))
#what is the probability of a="1"?
cnNodeMarginalProb(cnet,node=1)[1]
#0.2
#what is the probability of b="2"?
cnNodeMarginalProb(cnet,node=2)[2]
#0.56
#what is the probability of c="1"?
cnNodeMarginalProb(cnet,node=3)[1]
#0.428
#but how can I answer questions like:
#what is the probability of a="1" given that c="1"? i.e.
P(a="1"|c="1")?
#or what is the probability of a="1" given that b="2" and
c="1"? i.e.
P(a="1"|b="2",c="1")?
#END
--
Marcio Pupin Mello
Survey Engineer
PhD 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
Marcio Pupin Mello
2011-Sep-28 18:42 UTC
[R] how to solve a simple discrete Bayesian Belief Network?
I found a solution using the gRain package... see (but using another
example)..
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)
#answering the 1st question:
# 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")
#answering the 2nd question:
# 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")
#answering the 3rd question:
# 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 28/09/2011 09:03, Marcio Pupin Mello wrote:> Can somebody save-me? Thanks in advance!
>
>
> #R script:
> #trying to find out how solve a discrete Bayesian Belief Network.
> #option: using 'catnet' package
>
> #BEGIN
> library(catnet)
> cnet <- cnNew(nodes = c("a", "b", "c"),
cats = list(c("1", "2"),
> c("1", "2"), c("1", "2")), parents
= list(NULL, c(1), c(1,
> 2)), probs = list(c(0.2, 0.8), list(c(0.6, 0.4), c(0.4, 0.6)),
> list(list(c(0.3, 0.7), c(0.7, 0.3)), list(c(0.9, 0.1), c(0.1,
> 0.9)))))
>
> #what is the probability of a="1"?
> cnNodeMarginalProb(cnet,node=1)[1]
> #0.2
>
> #what is the probability of b="2"?
> cnNodeMarginalProb(cnet,node=2)[2]
> #0.56
>
> #what is the probability of c="1"?
> cnNodeMarginalProb(cnet,node=3)[1]
> #0.428
>
>
> #but how can I answer questions like:
>
> #what is the probability of a="1" given that c="1"?
i.e. P(a="1"|c="1")?
>
> #or what is the probability of a="1" given that b="2"
and c="1"? i.e.
> P(a="1"|b="2",c="1")?
> #END
>
--
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