bnspatial: Spatialize the Bayesian network

Description Usage Arguments Details Value See Also Examples

View source: R/bnspatial.R

Description

This function wraps most package functions, to ease the spatial implementation of Bayesian networks with minimal coding.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
bnspatial(
  network,
  target,
  spatialData,
  lookup,
  msk = NULL,
  what = c("class", "entropy"),
  midvals = NULL,
  targetState = NULL,
  spatial = TRUE,
  inparallel = FALSE,
  export = FALSE,
  path = NULL,
  field = NULL,
  verbose = TRUE,
  ...,
  exportRaster = FALSE
)

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.

spatialData

character with path to one or more raster files or to a single spatial vector file, or a list of objects of class 'RasterLayer' (for raster), or a single object of class 'sf' or 'SpatialPolygonsDataFrame' (for spatial vector). The spatial data associated to given network nodes, provided as file paths or as (list of) spatial objects. Items must be ordered accordingly to the corresponding nodes listed in lookup, or provided as named list, where names match exactly to the corresponding nodes name.

lookup

character (path to file) 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 or vector file), or a bounding box as numeric (xmin,xmax,ymin,ymax), or one or more (as list of) rasters of class "RasterLayer", or a single object of class "sf" or "SpatialPolygonsDataFrame". The reference data (raster or vector) to be used as mask. All model outputs will have the same extent (outline) as this object. All locations with no data (i.e. NA) cells in msk input will be ignored as well.

what

character. The required output, one or more of these are valid:

  • "class" returns the relatively most likely state.

  • "entropy" calculates the Shannon index and returns the entropy, given the node probabilities.

  • "probability" returns an object for each state of the target node, with its probability.

  • "expected" gives the expected value for the target node (see Details). Only valid for target nodes of continuous values. midValues argument must be provided.

  • "variation" returns the coefficient of variation, as a measure of uncertainty. Only valid for target nodes of continuous values.

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 or spatial vector? Default is TRUE, returning an object of class "RasterLayer" or "sf" for polygons. If FALSE, returns a data frame with one row for each valid cell/feature from msk and in columns the output required by what 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.

export

Logical or character. Should the spatial output be exported to file? Applies only if argument spatial=TRUE. When export=TRUE, output will be exported in .tif (raster) or .shp (vector) format. For rasters, 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. Argument exportRaster is deprecated.

path

The directory to store the output files, when export is not FALSE. Default is the working directory as from getwd(). File names are set by a default naming convention, see Details.

field

character. Only for spatial vector data (e.g. shapefile), the field/column names in the attribute table corresponding to the nodes, ordered accordingly.

verbose

logical. If verbose = TRUE a summary of class boundaries and associated nodes and data will be printed to screen for checks.

...

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.

exportRaster

deprecated, use export instead.

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 spatial object is exported to a file, the file name is set by default, accordingly to the following naming convention:

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

Value

A list of "RasterLayer" or "SpatialPolygonsDataFrame" 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.

See Also

setClasses; mapTarget; linkNode; loadNetwork

Examples

1
2
3
4
5
6
7
8
list2env(ConwyData, environment())

network <- LandUseChange
spatialData <- c(ConwyLU, ConwySlope, ConwyStatus)
lookup <- LUclasses

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

dariomasante/bnspatial documentation built on Aug. 25, 2020, 4:07 p.m.