# Simple Bayesian network

I have three variables:

• GENDER {“Male”, “Female”}
• AGE {“0-18”, “19-30”, “30-60”, “60+”}
• REGION {“Europe”, “Asia”, “Africa”, “America”}

From the literature I “know” that:

Males who live in Asia and who fall into 19-30 age group have 5% probability of having certain disease. Males in general have 3% probability of having the same disease Asian .. teenagers have average of 2% Americans 4% etc.

The point is that I have some knowledge and I want to record it in a network “format” and I would like to use that network in future.

I’m no R expert but I was reading lately about “bnlearn” package. It looks like it is exactly what I need but I can’t get my head around it! Am I on the right track? How do I specify my probabilities?

library(bnlearn);
net <- model2network("
[Gender.Male]
[Gender.Female]

[Region.Asia]
[Region.Europe]
[Region.America]

[Age.18.24]
[Age.24.30]
[Age.30.35]
[Age.45.50]
[Age.50.plus]
");
plot(net);


I actually have ~30 variables with up to 10 levels each.

• @william, please stop spamming the site w/ tag edits. This shouldn't be done this way. You should raise the issue on meta.CV & we can make it a synonym. – gung - Reinstate Monica May 1 '16 at 19:32

I believe that a package like gRain may be more what you're looking for. This package has an article explaining it that should give you a flavor of how it works.

EDIT: I think you would do something like:

library (gRain)
region <- cptable (~region, values=c(0, 0, 0, 0), levels=c("Europe", "Asia", "Africa", "America"))
gender <- cptable (~gender + region, values=c(0, 0, 0, 0, 0, 0, 0, 0), levels=c("Male", "Female"))
age    <- cptable (~age + region, values=c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), levels=c("0-18", "19-30", "30-60", "60+"))
disease <- cptable (~disease + region + gender + age, values=rep (0, 2*4*2*4), levels=c("Y", "N"))

plist <- compileCPT (list (region, gender, age, disease))
grn1 <- grain (plist)

summary (grn1)
plot (grn1)


Where the 0 values should actually be your percentages.

• Still no good. I spent hours trying to get it to work. Any other ideas? – user13467 Jul 12 '13 at 14:19
• Do I need a state for each attribute of the variable or is it could be achieved in some other way... – user13467 Jul 12 '13 at 14:20
• gRain seems to be dependent on graph package which is not available anymore. I hope bnlearn or similar packages can do the same – user13467 Jul 22 '13 at 10:12
• You get graph from the Bioconductor package. In the R Package Installer (Mac version, at least) choose BioConductor(binaries). – Wayne Jul 22 '13 at 12:37
• Hi Wayne. Thanks for that. I searched in the list of packages (from R installer) and there is no package called Bioconductor... do you have a direct link maybe? – user13467 Jul 22 '13 at 12:52