Query an independence network, i.e. obtain the conditional distribution of a set of variables - possibly (and typically) given finding (evidence) on other variables.

querygrain(
  object,
  nodes = nodeNames(object),
  type = "marginal",
  evidence = NULL,
  exclude = TRUE,
  normalize = TRUE,
  simplify = FALSE,
  result = "array",
  details = 0
)

Arguments

object

A grain object.

nodes

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

type

Valid choices are "marginal" which gives the marginal distribution for each node in nodes; "joint" which gives the joint distribution for nodes and "conditional" which gives the conditional distribution for the first variable in nodes given the other variables in nodes.

evidence

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

exclude

If TRUE then nodes on which evidence is given will be excluded from nodes (see above).

normalize

Should the results be normalized to sum to one.

simplify

Should the result be simplified (to a dataframe) if possible.

result

If "data.frame" the result is returned as a data frame (or possibly as a list of dataframes).

details

Debugging information

Value

A list of tables with potentials.

Note

setEvidence() is an improvement of setFinding() (and as such setFinding is obsolete). Users are recommended to use setEvidence() in the future.

setEvidence() allows to specification of "hard evidence" (specific values for variables) and likelihood evidence (also known as virtual evidence) for variables.

The syntax of setEvidence() may change in the future.

References

Søren Højsgaard (2012). Graphical Independence Networks with the gRain Package for R. Journal of Statistical Software, 46(10), 1-26. https://www.jstatsoft.org/v46/i10/.

Author

Søren Højsgaard, sorenh@math.aau.dk

Examples


testfile <- system.file("huginex", "chest_clinic.net", package = "gRain")
chest <- loadHuginNet(testfile, details=0)
qb <- querygrain(chest)
qb
#> $Smoker
#> Smoker
#> yes  no 
#> 0.5 0.5 
#> 
#> $Hasbronchitis
#> Hasbronchitis
#>  yes   no 
#> 0.45 0.55 
#> 
#> $Haslungcancer
#> Haslungcancer
#>   yes    no 
#> 0.055 0.945 
#> 
#> $Tuberculosisorcancer
#> Tuberculosisorcancer
#>      yes       no 
#> 0.064828 0.935172 
#> 
#> $Dyspnoea
#> Dyspnoea
#>       yes        no 
#> 0.4359706 0.5640294 
#> 
#> $Hastuberculosis
#> Hastuberculosis
#>    yes     no 
#> 0.0104 0.9896 
#> 
#> $VisittoAsia
#> VisittoAsia
#>  yes   no 
#> 0.01 0.99 
#> 
#> $PositiveXray
#> PositiveXray
#>     yes      no 
#> 0.11029 0.88971 
#> 

lapply(qb, as.numeric) # Safe
#> $Smoker
#> [1] 0.5 0.5
#> 
#> $Hasbronchitis
#> [1] 0.45 0.55
#> 
#> $Haslungcancer
#> [1] 0.055 0.945
#> 
#> $Tuberculosisorcancer
#> [1] 0.064828 0.935172
#> 
#> $Dyspnoea
#> [1] 0.4359706 0.5640294
#> 
#> $Hastuberculosis
#> [1] 0.0104 0.9896
#> 
#> $VisittoAsia
#> [1] 0.01 0.99
#> 
#> $PositiveXray
#> [1] 0.11029 0.88971
#> 
sapply(qb, as.numeric) # Risky
#>      Smoker Hasbronchitis Haslungcancer Tuberculosisorcancer  Dyspnoea
#> [1,]    0.5          0.45         0.055             0.064828 0.4359706
#> [2,]    0.5          0.55         0.945             0.935172 0.5640294
#>      Hastuberculosis VisittoAsia PositiveXray
#> [1,]          0.0104        0.01      0.11029
#> [2,]          0.9896        0.99      0.88971