create-model

library(agena.ai)

#Creating the variables, all of which are binary False/True nodes

A <- new("Node", id="A", name="Visit to Asia?")
S <- new("Node", id="S", name="Smoker?")

TB <- new("Node", id="T", name="Has tuberculosis")
L <- new("Node", id="L", name="Has lung cancer")
B <- new("Node", id="B", name="Has bronchitis")

TBoC <- new("Node", id="TBoC", name="Tuberculosis or cancer")

X <- new("Node", id="X", name="Positive X-ray?")
D <- new("Node", id="D", name="Dyspnoea?")

#Creating the arcs between parent and child nodes by adding parents

TB$add_parent(A)
#> Node A has been added to the parents list of T
#> NPT values for T are reset to uniform
L$add_parent(S)
#> Node S has been added to the parents list of L
#> NPT values for L are reset to uniform
B$add_parent(S)
#> Node S has been added to the parents list of B
#> NPT values for B are reset to uniform
TBoC$add_parent(TB)
#> Node T has been added to the parents list of TBoC
#> NPT values for TBoC are reset to uniform
TBoC$add_parent(L)
#> Node L has been added to the parents list of TBoC
#> NPT values for TBoC are reset to uniform
X$add_parent(TBoC)
#> Node TBoC has been added to the parents list of X
#> NPT values for X are reset to uniform
D$add_parent(TBoC)
#> Node TBoC has been added to the parents list of D
#> NPT values for D are reset to uniform
D$add_parent(B)
#> Node B has been added to the parents list of D
#> NPT values for D are reset to uniform

Defining the NPT values

A$set_probabilities(list(0.99, 0.01))
TB$set_probabilities(list(c(0.99,0.01),c(0.95,0.05)),by_rows = FALSE)
L$set_probabilities(list(c(0.9,0.1),c(0.99,0.01)),by_rows = FALSE)
B$set_probabilities(list(c(0.7,0.3), c(0.4,0.6)),by_rows = FALSE)
TBoC$set_probabilities(list(c(1,0),c(0,1),c(0,1),c(0,1)),by_rows = FALSE)
X$set_probabilities(list(c(0.95,0.05), c(0.02,0.98)),by_rows = FALSE)
D$set_probabilities(list(c(0.9,0.1),c(0.2,0.8),c(0.3,0.7),c(0.1,0.9)),by_rows = FALSE)

Creating a network and a model for the nodes

asia_net = new("Network", id="asia_net", nodes=c(A,S,TB,L,B,TBoC,X,D))
asia_model = new("Model", networks = list(asia_net))

Saving the model as a .cmpx file

asia_model$to_cmpx()