Title: | Bayes Nets: 'RHugin' Emulation with 'gRain' |
---|---|
Description: | Wrappers for functions in the 'gRain' package to emulate some 'RHugin' functionality, allowing the building of Bayesian networks consisting on discrete chance nodes incrementally, through adding nodes, edges and conditional probability tables, the setting of evidence, both 'hard' (boolean) or 'soft' (likelihoods), querying marginal probabilities and normalizing constants, and generating sets of high-probability configurations. Computations will typically not be so fast as they are with 'RHugin', but this package should assist users without access to 'Hugin' to use code written to use 'RHugin'. |
Authors: | Peter Green [aut, cre], Therese Graversen [ctb], Soren Hojsgaard [ctb] |
Maintainer: | Peter Green <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.1.10 |
Built: | 2024-11-11 03:29:00 UTC |
Source: | https://github.com/cran/gRaven |
Wrappers for functions in the 'gRain' package to emulate some 'RHugin' functionality, allowing the building of Bayesian networks consisting on discrete chance nodes incrementally, through adding nodes, edges and conditional probability tables, the setting of evidence, both 'hard' (boolean) or 'soft' (likelihoods), querying marginal probabilities and normalizing constants, and generating sets of high-probability configurations. Computations will typically not be so fast as they are with 'RHugin', but this package should assist users without access to 'Hugin' to use code written to use 'RHugin'.
Maintainer: Peter Green <[email protected]>
library(gRaven) rm(list=ls(all=TRUE)) yn <- c("yes","no") chest<-hugin.domain() add.node(chest,"asia",states=yn) add.node(chest,"tub",states=yn) add.node(chest,"smoke",states=yn) add.node(chest,"lung",states=yn) add.node(chest,"bronc",states=yn) add.node(chest,"either",states=yn) add.node(chest,"xray",states=yn) add.node(chest,"dysp",states=yn) add.edge(chest,"tub","asia") add.edge(chest,"lung","smoke") add.edge(chest,"bronc","smoke") add.edge(chest,"either","lung") add.edge(chest,"either","tub") add.edge(chest,"xray","either") add.edge(chest,"dysp","bronc") add.edge(chest,"dysp","either") set.table(chest,"asia",c(0.01,0.99)) set.table(chest,"tub",c(0.05,0.95,0.01,0.99)) set.table(chest,"smoke",c(0.5,0.5)) set.table(chest,"lung",c(0.1,0.9,0.01,0.99)) set.table(chest,"bronc",c(0.6,0.4,0.3,0.7)) set.table(chest,"either",c(1,0,1,0,1,0,0,1)) set.table(chest,"xray",c(0.98,0.02,0.05,0.95)) set.table(chest,"dysp",c(0.9,0.1,0.7,0.3,0.8,0.2,0.1,0.9)) chest get.nodes(chest) chest$states sapply(get.nodes(chest),function(x) get.parents(chest,x)) compile(chest) chest get.belief(chest,"asia") get.belief(chest,"tub") get.belief(chest,"lung") get.belief(chest,"bronc") sapply(get.nodes(chest),function(x) get.belief(chest,x)) c2<-clone.domain(chest) set.finding(chest,"asia","yes") set.finding(chest,"dysp","yes") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub") get.belief(chest,"lung") get.belief(chest,"bronc") pEvidence(chest$net) get.normalization.constant(chest) pEvidence(c2$net,evidence=list(asia="yes",dysp="yes")) get.normalization.constant(c2)
library(gRaven) rm(list=ls(all=TRUE)) yn <- c("yes","no") chest<-hugin.domain() add.node(chest,"asia",states=yn) add.node(chest,"tub",states=yn) add.node(chest,"smoke",states=yn) add.node(chest,"lung",states=yn) add.node(chest,"bronc",states=yn) add.node(chest,"either",states=yn) add.node(chest,"xray",states=yn) add.node(chest,"dysp",states=yn) add.edge(chest,"tub","asia") add.edge(chest,"lung","smoke") add.edge(chest,"bronc","smoke") add.edge(chest,"either","lung") add.edge(chest,"either","tub") add.edge(chest,"xray","either") add.edge(chest,"dysp","bronc") add.edge(chest,"dysp","either") set.table(chest,"asia",c(0.01,0.99)) set.table(chest,"tub",c(0.05,0.95,0.01,0.99)) set.table(chest,"smoke",c(0.5,0.5)) set.table(chest,"lung",c(0.1,0.9,0.01,0.99)) set.table(chest,"bronc",c(0.6,0.4,0.3,0.7)) set.table(chest,"either",c(1,0,1,0,1,0,0,1)) set.table(chest,"xray",c(0.98,0.02,0.05,0.95)) set.table(chest,"dysp",c(0.9,0.1,0.7,0.3,0.8,0.2,0.1,0.9)) chest get.nodes(chest) chest$states sapply(get.nodes(chest),function(x) get.parents(chest,x)) compile(chest) chest get.belief(chest,"asia") get.belief(chest,"tub") get.belief(chest,"lung") get.belief(chest,"bronc") sapply(get.nodes(chest),function(x) get.belief(chest,x)) c2<-clone.domain(chest) set.finding(chest,"asia","yes") set.finding(chest,"dysp","yes") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub") get.belief(chest,"lung") get.belief(chest,"bronc") pEvidence(chest$net) get.normalization.constant(chest) pEvidence(c2$net,evidence=list(asia="yes",dysp="yes")) get.normalization.constant(c2)
Add or delete a directed edge from one or more parents.
add.edge(domain, child, parent) delete.edge(domain, child, parent)
add.edge(domain, child, parent) delete.edge(domain, child, parent)
domain |
name of a gRaven domain |
child |
a character string containing the name of the child node. |
parent |
a character string specifying by name the parent nodes |
Emulates function of the same name in the RHugin
package by calls to gRain
functions
a NULL value is invisibly returned.
Deleting an edge nullifies the CPT for node n, if any.
Peter J. Green, [email protected]
demo(chest.setup,package="gRaven") chest delete.edge(chest,"tub","asia") add.edge(chest,"bronc","asia") chest
demo(chest.setup,package="gRaven") chest delete.edge(chest,"tub","asia") add.edge(chest,"bronc","asia") chest
Add or delete a node in a gRaven domain.
add.node(domain, name, category = c("chance", "decision", "utility", "function"), kind = c("discrete", "continuous", "other"), subtype, states) delete.node(domain, name)
add.node(domain, name, category = c("chance", "decision", "utility", "function"), kind = c("discrete", "continuous", "other"), subtype, states) delete.node(domain, name)
domain |
name of a gRaven domain |
name |
a character string containing the name of the node to be added or deleted |
category |
a character string specifying the category of the node |
kind |
a character string specifying the kind of the node |
subtype |
a character string, 'labeled', 'numbered' or 'boolean' |
states |
a character, numeric or logical vector listing the states for this node |
Emulates functions of the same name in the RHugin
package by calls to gRain
functions, delete.node
deletes the specified node and all incident edges; nullifies all corresponding CPTs.
a NULL value is invisibly returned.
Only discrete chance nodes are currently handled in gRaven
.
Peter J. Green, [email protected]
demo(chest.setup,package="gRaven") chest add.node(chest,'dummy',states=1:3) delete.node(chest,'either') chest
demo(chest.setup,package="gRaven") chest add.node(chest,'dummy',states=1:3) delete.node(chest,'either') chest
Compile a gRaven domain
## S3 method for class 'gRaven' compile(object, ...) check.compiled(object)
## S3 method for class 'gRaven' compile(object, ...) check.compiled(object)
object |
name of gRaven domain |
... |
additional arguments to |
check.compiled
compiles the domain if necessary, having first ensured that necessary information such as cptables
is in place. If the domain is already compiled, compile
triggers a warning, but proceeds to re-initialise the grain
object domain$net
, in particular deleting any evidence set.
a NULL value is invisibly returned.
Peter J. Green, [email protected]
chest<-hugin.domain() add.node(chest,"asia",states=c("yes","no")) add.node(chest,"tub",states=c("yes","no")) add.edge(chest,"tub","asia") chest compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) chest$nodes chest$states chest$parents
chest<-hugin.domain() add.node(chest,"asia",states=c("yes","no")) add.node(chest,"tub",states=c("yes","no")) add.edge(chest,"tub","asia") chest compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) chest$nodes chest$states chest$parents
Dummy routine.
compress(domain)
compress(domain)
domain |
name of gRaven domain |
compression in the sense used in Hugin is not available in gRain, so this function has no effect.
1
Peter J. Green, [email protected]
demo("chest",package="gRaven",echo=FALSE) compile(chest) compress(chest)
demo("chest",package="gRaven",echo=FALSE) compile(chest) compress(chest)
Get beliefs (marginal joint probabilities) in a gRaven domain
get.belief(domain, nodes) get.marginal(domain, nodes, class = c("data.frame", "table", "ftable", "numeric"))
get.belief(domain, nodes) get.marginal(domain, nodes, class = c("data.frame", "table", "ftable", "numeric"))
domain |
name of gRaven domain |
nodes |
character vector of names of nodes |
class |
desired class of output |
Emulates functions of the same name in the RHugin
package by calls to gRain
functions. Unlike with RHugin
, gRaven
conditions on all entered evidence in reporting probabilities, not only propagated evidence.
For get.marginal
, a list with one component "table" which is a data frame, table, flat table or numeric vector of marginal joint probabilities, as specified by class
.
For get.belief
, a vector in the case of a single node (if there is no evidence on that node. a vector of ones), otherwise as get.marginal
.
get.belief
handles more than one node at a time.
Peter J. Green, [email protected]
demo("chest",package="gRaven",echo=FALSE) compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub") get.marginal(chest,c('asia','xray','tub'))
demo("chest",package="gRaven",echo=FALSE) compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub") get.marginal(chest,c('asia','xray','tub'))
Get the nodes of a gRaven domain
get.nodes(domain)
get.nodes(domain)
domain |
name of gRaven domain |
Emulates function of the same name in the RHugin
package by calls to gRain
functions
Character vector of node names.
Peter J. Green, [email protected]
demo("chest",package="gRaven",echo=FALSE) get.nodes(chest) chest$states sapply(get.nodes(chest),function(x) get.parents(chest,x))
demo("chest",package="gRaven",echo=FALSE) get.nodes(chest) chest$states sapply(get.nodes(chest),function(x) get.parents(chest,x))
Get the normalisation constant of a gRaven domain
get.normalization.constant(domain, log = FALSE)
get.normalization.constant(domain, log = FALSE)
domain |
name of a gRaven domain |
log |
logical, should constant be returned on log scale? |
numeric, the value of the normalisation constant (or its logarithm). If no evidence has been entered, the constant is defined as 1.
Peter J. Green, [email protected]
yn <- c("yes","no") chest<-hugin.domain() add.node(chest,"asia",states=yn) add.node(chest,"tub",states=yn) add.node(chest,"smoke",states=yn) add.node(chest,"lung",states=yn) add.node(chest,"bronc",states=yn) add.node(chest,"either",states=yn) add.node(chest,"xray",states=yn) add.node(chest,"dysp",states=yn) add.edge(chest,"tub","asia") add.edge(chest,"lung","smoke") add.edge(chest,"bronc","smoke") add.edge(chest,"either","lung") add.edge(chest,"either","tub") add.edge(chest,"xray","either") add.edge(chest,"dysp","bronc") add.edge(chest,"dysp","either") set.table(chest,"asia",c(0.01,0.99)) set.table(chest,"tub",c(0.05,0.95,0.01,0.99)) set.table(chest,"smoke",c(0.5,0.5)) set.table(chest,"lung",c(0.1,0.9,0.01,0.99)) set.table(chest,"bronc",c(0.6,0.4,0.3,0.7)) set.table(chest,"either",c(1,0,1,0,1,0,0,1)) set.table(chest,"xray",c(0.98,0.02,0.05,0.95)) set.table(chest,"dysp",c(0.9,0.1,0.7,0.3,0.8,0.2,0.1,0.9)) compile(chest) get.normalization.constant(chest) set.finding(chest,"asia","yes") set.finding(chest,"dysp","yes") get.normalization.constant(chest)
yn <- c("yes","no") chest<-hugin.domain() add.node(chest,"asia",states=yn) add.node(chest,"tub",states=yn) add.node(chest,"smoke",states=yn) add.node(chest,"lung",states=yn) add.node(chest,"bronc",states=yn) add.node(chest,"either",states=yn) add.node(chest,"xray",states=yn) add.node(chest,"dysp",states=yn) add.edge(chest,"tub","asia") add.edge(chest,"lung","smoke") add.edge(chest,"bronc","smoke") add.edge(chest,"either","lung") add.edge(chest,"either","tub") add.edge(chest,"xray","either") add.edge(chest,"dysp","bronc") add.edge(chest,"dysp","either") set.table(chest,"asia",c(0.01,0.99)) set.table(chest,"tub",c(0.05,0.95,0.01,0.99)) set.table(chest,"smoke",c(0.5,0.5)) set.table(chest,"lung",c(0.1,0.9,0.01,0.99)) set.table(chest,"bronc",c(0.6,0.4,0.3,0.7)) set.table(chest,"either",c(1,0,1,0,1,0,0,1)) set.table(chest,"xray",c(0.98,0.02,0.05,0.95)) set.table(chest,"dysp",c(0.9,0.1,0.7,0.3,0.8,0.2,0.1,0.9)) compile(chest) get.normalization.constant(chest) set.finding(chest,"asia","yes") set.finding(chest,"dysp","yes") get.normalization.constant(chest)
Get the parents of a node in a gRaven domain
get.parents(domain, n, type = "parents")
get.parents(domain, n, type = "parents")
domain |
name of gRaven domain |
n |
name of node |
type |
only "parents" is currently accepted |
A named list of character vectors of names of parents.
Peter J. Green, [email protected]
demo("chest",package="gRaven",echo=FALSE) get.nodes(chest) chest$states sapply(get.nodes(chest),function(x) get.parents(chest,x))
demo("chest",package="gRaven",echo=FALSE) get.nodes(chest) chest$states sapply(get.nodes(chest),function(x) get.parents(chest,x))
Get states, edges and children in a gRaven domain
get.states(domain, nodes=domain$nodes) get.children(domain, nodes) get.edges(domain, nodes=domain$nodes)
get.states(domain, nodes=domain$nodes) get.children(domain, nodes) get.edges(domain, nodes=domain$nodes)
domain |
name of gRaven domain |
nodes |
name of node(s) |
Emulates functions of the same name in the RHugin
package by calls to gRain
functions
For get.states
, vector of state values. For get.edges
,
a list with one element for each node in domain
. Each element is in turn a list with a single element edges
which is a character vector of names of the node's children.
An empty vector indicates that the node has no children. For get.children
,
character vector of children's node names in the case of a single node, otherwise list of children of each node, each element being a character vector of names of the node's children.
get.states and get.children allow more than one node; get.edges allows selecting nodes.
Peter J. Green, [email protected]
demo("chest",package="gRaven",echo=FALSE) get.states(chest,"asia") get.children(chest,"asia") get.edges(chest)
demo("chest",package="gRaven",echo=FALSE) get.states(chest,"asia") get.children(chest,"asia") get.edges(chest)
Create or Clone a gRaven domain object.
hugin.domain() clone.domain(domain)
hugin.domain() clone.domain(domain)
domain |
name of gRaven domain |
Emulates functions of the same name in the RHugin
package. A gRaven domain is an environment, with additional class attribute 'gRaven'. The environment holds structures such as nodes
, states
, parents
and cptables
, populated incrementally by functions in the package, preparing the information needed by the gRain
package to create and manipulate the structure net
in the domain, which is a grain
object.
character string naming a gRaven domain
Peter J. Green, [email protected]
chest<-hugin.domain() yn <- c("yes","no") add.node(chest,"asia",states=yn) add.node(chest,"smoke",states=yn) add.node(chest,"tub",states=yn) add.node(chest,"lung",states=yn) add.node(chest,"either",states=yn) add.edge(chest,"tub","asia") add.edge(chest,"lung","smoke") add.edge(chest,"either",c("lung","tub")) set.table(chest,"asia",c(0.01,0.99)) set.table(chest,"tub",c(0.05,0.95,0.01,0.99)) set.table(chest,"smoke",c(0.5,0.5)) set.table(chest,"lung",c(0.1,0.9,0.01,0.99)) set.table(chest,"either",c(1,0,1,0,1,0,0,1)) chest chest2<-clone.domain(chest) add.node(chest2,"bronc",states=yn) add.node(chest2,"dysp",states=yn) add.node(chest2,"xray",states=yn) add.edge(chest2,"bronc","smoke") add.edge(chest2,"dysp",c("bronc","either")) add.edge(chest2,"xray","either") set.table(chest2,"bronc",c(0.6,0.4,0.3,0.7)) set.table(chest2,"dysp",c(0.9,0.1,0.7,0.3,0.8,0.2,0.1,0.9)) set.table(chest2,"xray",c(0.98,0.02,0.05,0.95)) chest2
chest<-hugin.domain() yn <- c("yes","no") add.node(chest,"asia",states=yn) add.node(chest,"smoke",states=yn) add.node(chest,"tub",states=yn) add.node(chest,"lung",states=yn) add.node(chest,"either",states=yn) add.edge(chest,"tub","asia") add.edge(chest,"lung","smoke") add.edge(chest,"either",c("lung","tub")) set.table(chest,"asia",c(0.01,0.99)) set.table(chest,"tub",c(0.05,0.95,0.01,0.99)) set.table(chest,"smoke",c(0.5,0.5)) set.table(chest,"lung",c(0.1,0.9,0.01,0.99)) set.table(chest,"either",c(1,0,1,0,1,0,0,1)) chest chest2<-clone.domain(chest) add.node(chest2,"bronc",states=yn) add.node(chest2,"dysp",states=yn) add.node(chest2,"xray",states=yn) add.edge(chest2,"bronc","smoke") add.edge(chest2,"dysp",c("bronc","either")) add.edge(chest2,"xray","either") set.table(chest2,"bronc",c(0.6,0.4,0.3,0.7)) set.table(chest2,"dysp",c(0.9,0.1,0.7,0.3,0.8,0.2,0.1,0.9)) set.table(chest2,"xray",c(0.98,0.02,0.05,0.95)) chest2
Re-initialise a gRaven domain
initialize.domain(domain)
initialize.domain(domain)
domain |
name of gRaven domain |
Emulates function of the same name in the RHugin
package.
Restores the domain to the state it was in after the call to compile
, in particular erasing all entered evidence.
a NULL value is invisibly returned.
Peter J. Green, [email protected]
demo("chest",package="gRaven",echo=FALSE) compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) sapply(get.nodes(chest),function(x) get.belief(chest,x)) get.normalization.constant(chest) initialize.domain(chest) sapply(get.nodes(chest),function(x) get.belief(chest,x)) get.normalization.constant(chest)
demo("chest",package="gRaven",echo=FALSE) compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) sapply(get.nodes(chest),function(x) get.belief(chest,x)) get.normalization.constant(chest) initialize.domain(chest) sapply(get.nodes(chest),function(x) get.belief(chest,x)) get.normalization.constant(chest)
List gRaven domain Objects
list.domains(print=TRUE)
list.domains(print=TRUE)
print |
logical, should list of domains be output to console |
gRaven domains are R environment objects, with named slots typically including nodes, parents, cptables and net, where the net component is a gRain
grain
object. The domains that this function finds and lists are all the objects in .GlobalEnv of class "gRaven", and all those objects in this class that are components of a component named 'domains' of a list object in .GlobalEnv.
The function invisibly returns a character vector of the domain names found.
Peter J. Green, [email protected]
demo(chest,package="gRaven") chest chest2<-clone.domain(chest) chest2 set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub") propagate(chest2) get.belief(chest2,"asia") get.belief(chest2,"tub") list.domains()
demo(chest,package="gRaven") chest chest2<-clone.domain(chest) chest2 set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub") propagate(chest2) get.belief(chest2,"asia") get.belief(chest2,"tub") list.domains()
Find the configurations of the specified nodes that occur with probability pmin
or greater. These configurations are known as most probable configurations or maximum a posteriori (MAP) configurations.
map.configurations(domain, nodes, pmin)
map.configurations(domain, nodes, pmin)
domain |
name of gRaven domain |
nodes |
character vector of names of nodes |
pmin |
a single numeric value between 0 and 1 specifying the minimum probability for the most probable configurations |
Emulates function of the same name in the RHugin
package by calls to gRain
functions
Data.frame with one column for each node in nodes
. Each row contains a most probable configuration. The final column of the data.frame (Prob
) gives the probability of the configuration.
Therese Graversen, [email protected]
demo("chest",package="gRaven",echo=FALSE) set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") map.configurations(chest,c("lung","bronc"),0.0001)
demo("chest",package="gRaven",echo=FALSE) set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") map.configurations(chest,c("lung","bronc"),0.0001)
Print method for a gRaven domain
## S3 method for class 'gRaven' print(x, ...)
## S3 method for class 'gRaven' print(x, ...)
x |
gRaven domain |
... |
additional arguments to |
Prints summary information describing the domain.
a NULL value is invisibly returned.
Peter J. Green, [email protected]
demo(chest,package="gRaven") print(chest)
demo(chest,package="gRaven") print(chest)
Propagate method for a gRaven domain
## S3 method for class 'gRaven' propagate(object, ...)
## S3 method for class 'gRaven' propagate(object, ...)
object |
character string, name of gRaven domain |
... |
additional arguments to |
Propagates all previously entered evidence through the network. This call is required by functions such as get.belief
and map.configurations
, but not before get.normalization.constant
. In the implementation, findings will have been accumulated in the variable net$cache in the domain, by calls to set.finding
, and are moved to net$evid by the propagate
function.
a NULL value is invisibly returned.
Peter J. Green, [email protected]
demo("chest",package="gRaven",echo=FALSE) compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub")
demo("chest",package="gRaven",echo=FALSE) compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub")
Set, retract and inspect findings
set.finding(domain, node, finding) retract(domain, nodes=domain$nodes) get.finding(domain, nodes=domain$nodes, type = c("entered", "propagated"), namestates=FALSE)
set.finding(domain, node, finding) retract(domain, nodes=domain$nodes) get.finding(domain, nodes=domain$nodes, type = c("entered", "propagated"), namestates=FALSE)
domain |
name of gRaven domain |
node |
name of node |
nodes |
character vector naming one or mode nodes |
finding |
either a state of node |
type |
character vector of one or more options to select between evidence entered before or after most recent propagation |
namestates |
logical, should output have the states labelled |
For type
, partial matching is used.
For set.finding
and retract
, a NULL value is invisibly returned. For get.finding
, the likelihood vector is invisibly returned for each specified node, in a format governed by namestates
. This is a named vector in the case of a single node, otherwise a list of such vectors. For a single node, in the absence of any evidence, a named vectors of 1's is returned.
Findings (or evidence) are handled differently in gRaven
(and gRain
) than in RHugin
, and that is reflected in differences in results from these functions, especially get.finding
. For set.finding
in gRaven
, the case
argument is not supported. For get.finding
in both packages, previously-set evidence is displayed as a non-negative vector indexed by the states, typically a probability distribution; two enhancements in gRaven
are that findings on more than one node can be displayed, and that the format of the output can be controlled by namestates
. In gRaven
when new evidence is set by set.finding
, it replaces any existing evidence on the same node, and all existing evidence on all nodes is "unpropagated". Evidence is held in a data structure cache
until propagation, when it is moved to evid
; these names are used in labelling the output from get.finding
. Finally if no evidence has been entered on a node, RHugin
reports a vector of all ones, while gRaven
returns such a vector invisibly.
Peter J. Green, [email protected]
demo(chest,package="gRaven",echo=FALSE) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub") get.finding(chest) retract(chest,"dysp") get.belief(chest,"tub") get.finding(chest)
demo(chest,package="gRaven",echo=FALSE) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") propagate(chest) get.belief(chest,"asia") get.belief(chest,"tub") get.finding(chest) retract(chest,"dysp") get.belief(chest,"tub") get.finding(chest)
Set or get CPT in a gRaven domain
set.table(domain, n, tab = 1, type = c("cpt", "experience", "fading")) get.table(domain, n, type = c("cpt", "experience", "fading"), class = c("data.frame", "table", "ftable", "numeric"))
set.table(domain, n, tab = 1, type = c("cpt", "experience", "fading")) get.table(domain, n, type = c("cpt", "experience", "fading"), class = c("data.frame", "table", "ftable", "numeric"))
domain |
name of gRaven domain |
n |
name of node |
tab |
values of conditional probabilties |
type |
a character string specifying the type of table to set. |
class |
a character string specifying the class of the returned table |
For set.table
, a NULL value is invisibly returned; for get.table
, an object of required class.
Only type="cpt" and class="data.frame" are currently implemented in gRaven
.
Peter J. Green, [email protected]
chest<-hugin.domain() add.node(chest,"asia",states=c("yes","no")) add.node(chest,"tub",states=c("yes","no")) add.edge(chest,"tub","asia") compile(chest) chest get.table(chest,"asia") set.table(chest,"asia",c(0.01,0.99)) get.table(chest,"asia")
chest<-hugin.domain() add.node(chest,"asia",states=c("yes","no")) add.node(chest,"tub",states=c("yes","no")) add.edge(chest,"tub","asia") compile(chest) chest get.table(chest,"asia") set.table(chest,"asia",c(0.01,0.99)) get.table(chest,"asia")
Simulate method for a gRaven domain
## S3 method for class 'gRaven' simulate(object, nsim = 1, seed = NULL, ...)
## S3 method for class 'gRaven' simulate(object, nsim = 1, seed = NULL, ...)
object |
character string, name of gRaven domain |
nsim |
Number of cases to simulate |
seed |
An optional integer controlling the random number generatation |
... |
additional arguments to |
a data frame.
Peter J. Green, [email protected]
demo("chest",package="gRaven",echo=FALSE) compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") newdata<-simulate(chest,100) head(newdata)
demo("chest",package="gRaven",echo=FALSE) compile(chest) chest set.finding(chest,"asia","yes") set.finding(chest,"dysp","no") newdata<-simulate(chest,100) head(newdata)
Summary method for a gRaven domain
## S3 method for class 'gRaven' summary(object, domain = TRUE, nodes = FALSE, jt = FALSE, print.cliques = FALSE, ...)
## S3 method for class 'gRaven' summary(object, domain = TRUE, nodes = FALSE, jt = FALSE, print.cliques = FALSE, ...)
object |
character string, name of gRaven domain |
domain |
Logical, should domain be summarised |
jt |
Logical, should junction tree be summarised |
nodes |
Logical, should nodes be summarised |
print.cliques |
Logical, should cliques be printed |
... |
additional arguments to |
a data frame.
In gRaven
, not all of the options are yet implemented, and generally the information delivered is less detailed than in the RHugin
version.
Peter J. Green, [email protected]
demo("chest",package="gRaven",echo=FALSE) chest summary(chest,jt=TRUE)
demo("chest",package="gRaven",echo=FALSE) chest summary(chest,jt=TRUE)