querygrain.Rd
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
)
A grain
object.
A vector of nodes; those nodes for which the (conditional) distribution is requested.
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
.
An alternative way of specifying findings (evidence), see examples below.
If TRUE
then nodes on which evidence is given
will be excluded from nodes
(see above).
Should the results be normalized to sum to one.
Should the result be simplified (to a dataframe) if possible.
If "data.frame" the result is returned as a data frame (or possibly as a list of dataframes).
Debugging information
A list of tables with potentials.
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.
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/.
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