Learn R Programming

gRain (version 1.3-0)

grain-evi: Set evidence in grain objects

Description

Setting and removing evidence in grain objects.

Usage

setEvi(object, nodes = NULL, states = NULL, evidence = NULL,
  propagate = TRUE, details = 0)

setEvi_(object, evidence = NULL, propagate = TRUE, details = 0)

retractEvi(object, items = NULL, propagate = TRUE)

retractEvi_(object, items = NULL, propagate = TRUE)

absorbEvi(object, propagate = TRUE)

absorbEvi_(object, propagate = TRUE)

pEvidence(object)

getEvidence(object)

dropEvi(object) <- value

addEvi(object) <- value

evidence(object)

# S3 method for grain evidence(object)

evidence(object) <- value

# S3 method for grain evidence(object) <- value

addEvi(object, nodes = NULL, states = NULL, evidence = NULL, propagate = TRUE, details = 0)

dropEvi(object, items = NULL, propagate = TRUE)

getEvi(object)

insertEvi(evi.list, pot, hostclique)

getHostClique(set.list, cliques)

Arguments

object

A "grain" object

nodes

A vector of nodes; those nodes for which the (conditional) distribution is requested.

states

A vector of states (of the nodes given by 'nodes')

evidence

An alternative way of specifying findings (evidence), see examples below.

propagate

Should the network be propagated?

details

Debugging information

items

Items in the evidence list to be removed. Here, NULL means remove everything. If items is a character vector (of nodes) then evidence on these nodes is removed. If items is a numeric vector then those items in the evidence list is removed. Notice that 0 means nothing is removed.

value

The evidence in the form of a named list or an evidence-object.

evi.list

A "grain_ev" object.

pot

A list of clique potentials (a potential is an array).

hostclique

A numerical vector indicating in which element of 'pot' each eviendence item in 'evi.list' should be inserted in.

set.list

A list of sets (a set is a character vector).

cliques

A list of sets (a set is a character vector).

References

S<U+00F8>ren H<U+00F8>jsgaard (2012). Graphical Independence Networks with the gRain Package for R. Journal of Statistical Software, 46(10), 1-26. http://www.jstatsoft.org/v46/i10/.

See Also

setEvidence getEvidence retractEvidence pEvidence setFinding getFinding retractFinding pFinding

Examples

Run this code
# NOT RUN {
## setFinding / setEvidence

yn <- c("yes","no")
a    <- cptable(~asia, values=c(1,99),levels=yn)
t.a  <- cptable(~tub+asia, values=c(5,95,1,99),levels=yn)
s    <- cptable(~smoke, values=c(5,5), levels=yn)
l.s  <- cptable(~lung+smoke, values=c(1,9,1,99), levels=yn)
b.s  <- cptable(~bronc+smoke, values=c(6,4,3,7), levels=yn)
e.lt <- cptable(~either+lung+tub,values=c(1,0,1,0,1,0,0,1),levels=yn)
x.e  <- cptable(~xray+either, values=c(98,2,5,95), levels=yn)
d.be <- cptable(~dysp+bronc+either, values=c(9,1,7,3,8,2,1,9), levels=yn)
plist <- compileCPT(list(a, t.a, s, l.s, b.s, e.lt, x.e, d.be))
bn <- grain(plist)
 
## 1) These forms are identical

e1 <- list(dysp="no", xray="no")

setEvi(bn, evidence=e1)
setEvi(bn, nodes=c("dysp","xray"), states=c("no", "no"))
setEvidence(bn, nodes=c("dysp","xray"), states=c("no", "no"))

# Notice: setFinding is old school but it was used in the
# "Graphical Models with R" book.
setFinding(bn, nodes=c("dysp","xray"), states=c("no", "no"))

## 2) Updating evidence
# Notice that only 'asia' is set because 'dysp' was set earlier

e2 <- list(dysp="yes", asia="yes")
bn1 <- setEvi(bn, evidence=e1)
bn1
bn2 <- setEvi(bn1, evidence=e2)
bn2

## 3) Shorter forms

bn2 <- bn
evidence(bn2)
evidence(bn2) <- e1
evidence(bn2)
evidence(bn2) <- e2
evidence(bn2)
evidence(bn2) <- NULL
evidence(bn2)


## 4) Alternative forms:

setEvi(bn, evidence=list("asia"=c(1, 0), "xray"="yes"))

## 5) Suppose we do not know with certainty whether a patient has
## recently been to Asia. We can then introduce a new variable
## "guess.asia" with "asia" as its only parent. Suppose
## p(guess.asia=yes|asia=yes)=.8 and p(guess.asia=yes|asia=no)=.1
## If the patient is e.g. unusually tanned we may set
## guess.asia=yes and propagate. This corresponds to modifying the
## model by the likelihood (0.8, 0.1) as

b =setEvi(bn, nodes=c("asia","xray"), states=list(c(0.8,0.1), "yes"))
as.data.frame( evidence( b ) )

# }

Run the code above in your browser using DataLab