Learn R Programming

bnspatial (version 1.1.1)

queryNet: Query the Bayesian network

Description

This function queries the Bayesian network and returns the probabilities for each state of the target node. Available input variables are set as evidence. queryNetParallel works as queryNet, but makes use of multi cores/processors facilities for big network queries, by splitting data into chunks and processing them in parallel.

Usage

queryNet(network, target, evidence, ...)

queryNetParallel(network, target, evidence, inparallel = TRUE, ...)

Arguments

network

The Bayesian network. An object of class grain, or a character (the path to the .net file to be loaded)

target

character. The node of interest to be modelled and mapped.

evidence

matrix or data.frame. Named columns are the known input variables; rows are the discrete states associated to them for each record (NA allowed).

...

Additional arguments to force one or more nodes to a state (i.e. fixing evidence). If the node is associated to any input spatial data, the latter will be ignored, thus resulting spatially equal everywhere. Node name must be provided as argument and the associated fixed state as character; both node and state names must be typed exactly as their names in the network.

inparallel

logical or integer. Number of cores to be used by queryNetParallel. Default is TRUE, so the maximum number available minus one is set.

Value

A matrix of probabilities: columns are the states of the target node and rows are the probabilities associated to each record (i.e. spatial locations) from evidence.

Examples

Run this code
# NOT RUN {
list2env(ConwyData, environment())

network <- LandUseChange

q <- queryNet(network, 'FinalLULC', evidence)
head(q)

## Fix a given node on a state (i.e. fixed evidence) by providing an additional argument
q <- queryNet(network, 'FinalLULC', evidence, Stakeholders = 'farmers')
head(q)

## Fix evidence for two nodes, including one of the spatial inputs (i.e. overriden by evidence)
q <- queryNet(network, 'FinalLULC', evidence, Stakeholders = 'farmers', CurrentLULC = 'forest')
head(q)
## For a programmatic approach, the arguments could be passed as named list:
# lst <- list(Stakeholders = 'farmers', CurrentLULC = 'forest')
# queryNet(network, 'FinalLULC', evidence, lst)

## Use parallel processing
q <- queryNetParallel(network, 'FinalLULC', evidence, inparallel=2)
head(q)

# }

Run the code above in your browser using DataLab