Learn R Programming

bnspatial (version 0.9)

bnspatial: Spatialize the Bayesian network

Description

This function wraps most bnspatial package functions to ease the spatial implementation of Bayesian networks with minimal user interaction.

Usage

bnspatial(network, target, spatialData, lookup, msk = NULL, what = c("class", "entropy"), midvals = NULL, targetState = NULL, spatial = TRUE, inparallel = FALSE, exportRaster = FALSE, path = NULL, verbose = TRUE, ...)

Arguments

network
The Bayesian network. An object of class "grain" (package gRain), or a character (the path to the ".net" file to be imported)
target
character. The node of interest to be modelled and mapped.
spatialData
character. The raster files corresponding to nodes
lookup
character or a formatted list. This argument can be provided as path to a comma separated file or a formatted list (see setClasses )
msk
a character (path to raster file), a raster (object of class "RasterLayer"), or a list of rasters. The reference raster(s) to be used as mask. All model outputs will have the same resolution and same extent as this raster(s). All locations with no data (i.e. NA) cells in msk will be ignored as well.
what
character. The required output, one or more of these values are valid:
  • "class" returns the relatively most likely states.
  • "entropy" calculates the Shannon index and returns the entropy given the state probabilities.
  • "probability" returns an object for each state of the target node, with associated probability.
  • "expected" gives the expected value for the target node (see Details). Only valid for continuous target nodes. midValues argument must be provided.
  • "variation" returns the coefficient of variation, as a measure of uncertainty.
midvals
vector of length equal to the number of states of the target node. Applies only if the target node is a continuous variable, in which case midvals must contain the mid values for each of the intervals
targetState
character. One or more states of interest from the target node. Applies only when argument what includes 'probability'. Default is set to all states of the node.
spatial
logical. Should the output be spatially explicit -i.e. a georeferenced raster? Default is TRUE, returning an object of class "RasterLayer". If FALSE, returns a data.frame with one row for each non NA cell in msk raster and in columns the output required by mask argument.
inparallel
logical or integer. Should the function use parallel processing facilities? Default is FALSE: a single process will be launched. If TRUE, all cores/processors but one will be used. Alternatively, an integer can be provided to dictate the number of cores/processors to be used.
exportRaster
Logical or character. Should the spatial output be exported to a raster file? Applies only if argument spatial=TRUE. When exportRaster=TRUE, rasters will be exported in .tif format. A character specifying another extension can be provided, in which case the raster will be exported in that format. Only formats listed by writeFormats are valid.
path
The directory to store the output files, when exportRaster is not FALSE. Default is the working directory (getwd()). File names are set by a default naming convention, see Details.
verbose
logical. If verbose = TRUE a summary of class boundaries and associated nodes and data will be printed to screen for quick checks.
...
Additional arguments to fix a state (i.e. setting evidence) to one or more nodes, as known and independent from any spatial data (e.g. the case of non-spatial variables which are equal everywhere). Node name is provided as argument and the associated fixed state as character; both node and state names must be typed accordingly to their names in the network.

Value

A list of "RasterLayer" objects or a data.frame, depending on input arguments: see mapTarget. Some basic information about discretization and network/data link are printed on screen during execution.

Details

bnspatial

The expected value is calculated by summing the mid values of target node states weighted by their probability: p1 * midVal_1 + p2 * midval_2 + ... + pn * midval_n When a RasterLayer is exported to a file, the file name is set by default, accordingly to the following naming convention:

  • "class" _Class.
  • "entropy" _ShanEntropy.
  • "probability" _Probability_..
  • "expected" _ExpectedValue.
  • "variation" _CoefVariation.

An additional comma separated file (.csv) is written to the same directory when "class", providing a key to interpret the raster values and the state they refer to.

See Also

setClasses; mapTarget; linkNode; loadNetwork

Examples

Run this code
data(ConwyData)
network <- LandUseChange
spatialData <- c(currentLU, slope, status)
lookup <- LUclasses

bn <- bnspatial(network, 'FinalLULC', spatialData, lookup)
bn

Run the code above in your browser using DataLab