gRain.R

richard — Mar 18, 2014, 11:30 AM

library(Rgraphviz)
Loading required package: graph
Loading required package: grid
library(gRain)
Loading required package: gRbase
network <- loadHuginNet("DawidEvett250214.net")
plot(network)

plot of chunk unnamed-chunk-1

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"      
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.009901 0.990099 

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.8326 0.1674