## Graphical model from Dawid and Evett (1997)
## http://www.math.leidenuniv.nl/~gillrd/teaching/graphical/DawidEvett.net
## http://www.math.leidenuniv.nl/~gillrd/teaching/graphical/DawidEvettSN.net

## "SN" stands for "short names", better for some plots


library(Rgraphviz)  ## From bioconductor repository
## Loading required package: graph
## Loading required package: grid
library(gRain)      ## From CRAN repository
## Loading required package: gRbase
network <- loadHuginNet("DawidEvett.net")  ## Created by me with GeNIe
networkSN <- loadHuginNet("DawidEvettSN.net")
plot(network)

network
## Independence network: Compiled: FALSE Propagated: FALSE 
##   Nodes: chr [1:13] "NumberOffenders" "CSuspectOneOfOffenders" ...
names(network)
##  [1] "universe"      "data"          "dag"           "cptlist"      
##  [5] "isInitialized" "isCompiled"    "isPropagated"  "finding"      
##  [9] "control"       "details"
plot(networkSN)

networkSN
## Independence network: Compiled: FALSE Propagated: FALSE 
##   Nodes: chr [1:13] "N" "C" "W" "G1" "B" "A" "X1" "X2" "G2" ...
names(networkSN)
##  [1] "universe"      "data"          "dag"           "cptlist"      
##  [5] "isInitialized" "isCompiled"    "isPropagated"  "finding"      
##  [9] "control"       "details"
networkC <- compile(network)
networkC
## Independence network: Compiled: TRUE Propagated: FALSE 
##   Nodes: chr [1:13] "NumberOffenders" "CSuspectOneOfOffenders" ...
names(networkC)
##  [1] "universe"      "data"          "dag"           "cptlist"      
##  [5] "isInitialized" "isCompiled"    "isPropagated"  "finding"      
##  [9] "control"       "details"       "rip"           "ug"           
## [13] "equilCQpot"    "tempCQpot"     "origCQpot"     "details"
networkCP <- propagate(networkC)
# plot(networkCP)
networkCP
## Independence network: Compiled: TRUE Propagated: TRUE 
##   Nodes: chr [1:13] "NumberOffenders" "CSuspectOneOfOffenders" ...
names(networkCP)
##  [1] "universe"      "data"          "dag"           "cptlist"      
##  [5] "isInitialized" "isCompiled"    "isPropagated"  "finding"      
##  [9] "control"       "details"       "rip"           "ug"           
## [13] "equilCQpot"    "tempCQpot"     "origCQpot"     "details"
names(network$cptlist)
##  [1] "NumberOffenders"           "CSuspectOneOfOffenders"   
##  [3] "WArrestingPoliceOfficer"   "Guard1HowManyOffenders"   
##  [5] "BWhoPunchedGuard"          "AWhoLeftFibresOnGrill"    
##  [7] "X1SuspectsBloodtype"       "X2GuardsBloodtype"        
##  [9] "Guard2WasPunchedOnTheNose" "RBloodstainOnJumper"      
## [11] "Y2BloodtypeSprayOnJumper"  "X3TypeOfSuspectsJumper"   
## [13] "Y1TypeOfFibreTuft"
network$cptlist[10]
## $RBloodstainOnJumper
## , , Guard2WasPunchedOnTheNose = Yes
## 
##                    BWhoPunchedGuard
## RBloodstainOnJumper Suspect Other
##     From_nose_punch     0.9  0.01
##     Other               0.1  0.99
## 
## , , Guard2WasPunchedOnTheNose = No
## 
##                    BWhoPunchedGuard
## RBloodstainOnJumper Suspect Other
##     From_nose_punch     0.5   0.5
##     Other               0.5   0.5
networkCP
## Independence network: Compiled: TRUE Propagated: TRUE 
##   Nodes: chr [1:13] "NumberOffenders" "CSuspectOneOfOffenders" ...
querygrain(networkCP,"CSuspectOneOfOffenders")
## $CSuspectOneOfOffenders
## CSuspectOneOfOffenders
##        Yes         No 
## 0.00990099 0.99009901
networkCPev <-setFinding(networkCP,"Y1TypeOfFibreTuft","Special")

networkCPev <-setFinding(networkCPev,"Y2BloodtypeSprayOnJumper","Special")

networkCPev <-setFinding(networkCPev,"X1SuspectsBloodtype","Other")

networkCPev <-setFinding(networkCPev,"X2GuardsBloodtype","Special")

networkCPev <-setFinding(networkCPev,"X3TypeOfSuspectsJumper","Special")

networkCPev <-setFinding(networkCPev,"NumberOffenders","Num_4")

networkCPev <-setFinding(networkCPev,"WArrestingPoliceOfficer","Suspect_was_offender")

networkCPev <-setFinding(networkCPev,"Guard1HowManyOffenders","Says_4")

networkCPev <-setFinding(networkCPev,"Guard2WasPunchedOnTheNose","Yes")

querygrain(networkCPev,"CSuspectOneOfOffenders")
## $CSuspectOneOfOffenders
## CSuspectOneOfOffenders
##      Yes       No 
## 0.832565 0.167435