Giter Club home page Giter Club logo

bnspatial's Introduction

bnspatial

Package for the spatial implementation of Bayesian Networks and mapping in geographical space. Outputs are GIS ready maps of expected value (or most likely state) given known and unknown conditions, maps of uncertainty measured as both coefficient of variation or Shannon index (entropy), maps of probability associated to any states of any node of the network. Some additional features are provided as well, such as parallel processing options, data discretization routines and function wrappers designed for users with minimal knowledge of the R programming language.

URL: https://cran.r-project.org/package=bnspatial

To install, open a R session and enter the following commands, which will install some mandatory functions underlying gRain:

# For R version 3.6 and later
if (!requireNamespace("BiocManager")) install.packages("BiocManager")
BiocManager::install('RBGL','Rgraphviz')

# Use this for R version 3.5
if (!requireNamespace("BiocManager")) install.packages("BiocManager")
BiocManager::install('RBGL', version = "3.8")

# ...or this for older versions:
source("http://bioconductor.org/biocLite.R")
biocLite("RBGL")

Then install bnspatial, by selecting it from the packages list, or typing in the console:

install.packages("bnspatial")

Alternatively, to install from source:

  • download the source file (.tar.gz) to the R working directory (or any other directory)
  • start an R session
  • run previous Biocmanager installations and then execute these commands:
install.packages("gRain", repos="http://cran.uk.r-project.org/", dependencies=T, clean=T)
install.packages("raster", repos="http://cran.uk.r-project.org/", dependencies=T, clean=T)

## Install bnspatial (full path to the file, if not in the R working directory)
install.packages("~/bnspatial_[LATEST_VERSION].tar.gz", repos = NULL, type="source")

Reference manual: bnspatial.pdf

Vignette with guided example: Overview of package 'bnspatial'

Also from R console:

help(package=bnspatial) ## opens package index
?bnspatial ## help file for the main function

bnspatial's People

Contributors

dariomasante avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bnspatial's Issues

running bnspatial in R.4

Hi Dario, I updated my version of R and all the dependencies that are running with bnspatial. I had a perfectly working network in the previous version of R3.6 but now I get this message when running the model:

Attaching package: �gRbase�

The following objects are masked from �package:bnlearn�:

ancestors, children, parents

no loop for break/next, jumping to top level

Could you please help me with some explanation and how to fix this problem?

Many thanks

Miriam

documentation example failing

I am trying to runt he example from the documentation. The code is:

library(bnspatial)
data(ConwyData)
list2env(ConwyData, environment())

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

## Run a spatial query on the Bayesian network 
## A summary will be printed on screen to check whether spatial data correspond 
## to network nodes as intended.
bn <- bnspatial(network, target, spatialData, lookup)
bn

## Plot output maps
library(raster)
par(mfrow=c(1,2))
plot(bn$Class, main='Most likely class')
plot(bn$Entropy, main='Uncertainty (Shannon index)')

Everything runs fine up to running bnspatial which fails with:

Error in NextMethod("compile") : no method to invoke

R sessionInfo:

Platform: x86_64-conda_cos6-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS

Matrix products: default
BLAS/LAPACK: /mnt/d/linux_home/envs/camilla_Ptool/lib/libopenblasp-r0.3.10.so

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8        LC_COLLATE=C.UTF-8    
 [5] LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8    LC_PAPER=C.UTF-8       LC_NAME=C             
 [9] LC_ADDRESS=C           LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] graph_1.64.0        BiocGenerics_0.32.0 bnspatial_1.1.1     forcats_0.5.0       stringr_1.4.0      
 [6] dplyr_1.0.0         purrr_0.3.4         readr_1.3.1         tidyr_1.1.0         tibble_3.0.3       
[11] ggplot2_3.3.2       tidyverse_1.3.0     raster_3.3-7        sp_1.4-2           

loaded via a namespace (and not attached):
 [1] gRbase_1.8-3.4   tidyselect_1.1.0 xfun_0.15        haven_2.3.1      lattice_0.20-41  colorspace_1.4-1
 [7] vctrs_0.3.2      generics_0.0.2   stats4_3.6.1     yaml_2.2.1       RBGL_1.62.1      blob_1.2.1      
[13] rlang_0.4.7      functional_0.6   pillar_1.4.6     glue_1.4.1       withr_2.2.0      DBI_1.1.0       
[19] gRain_1.3-0      dbplyr_1.4.4     modelr_0.1.8     readxl_1.3.1     lifecycle_0.2.0  munsell_0.5.0   
[25] gtable_0.3.0     cellranger_1.1.0 rvest_0.3.5      codetools_0.2-16 knitr_1.29       fansi_0.4.1     
[31] broom_0.7.0      Rcpp_1.0.5       scales_1.1.1     backports_1.1.8  jsonlite_1.7.0   fs_1.4.2        
[37] hms_0.5.3        stringi_1.4.3    grid_3.6.1       cli_2.0.2        tools_3.6.1      magrittr_1.5    
[43] crayon_1.3.4     pkgconfig_2.0.3  Matrix_1.2-18    ellipsis_0.3.1   xml2_1.3.2       reprex_0.3.0    
[49] lubridate_1.7.9  assertthat_0.2.1 httr_1.4.1       rstudioapi_0.11  R6_2.4.1         igraph_1.2.4.1  
[55] compiler_3.6.1 ```

Running bnspatial on vector layers

Dear Dario, could you please clarify whether there is now a facility to run bnspatial on shape files instead of rasters? thanks Miriam

import BBN into R without spatial data

Hey, it's possible to import and load a bayesian network into R without spatial data? I have a huge and complex BN. I tried the bnspatial package, but I actually don't have spatial information. So I have problems by creating the (lookup-) text file (special classBoundaries) and plot the output map. I just wanna know if the BN with the nodes and node states is really inside. Somebody maybe have a tip or a few links for me, where I can find a solution? Couldn't find informations about importing BNs without spatial data. Thanks!

P.S. Sorry for my bad English. :D

conda package?

r has a great system of conda packages availble now, but bnspatial is a notable exception to this. Any chance of an official conda package for it?

decision and non-spatial nodes

I have constructed a BBN in GeNIe. It contains a combination of spatially explicit nodes (rasters) as well as non-spatial information, such as expert opinion or decision nodes. Is it posible to conduct the analysis using bnspatial?

I tried combining non-spatial information and spatial information on a smaller network of only 4 nodes:
hazard.
I imported the network (hazard_subnetwork), corresponding discretized raster files: ekin.raster.clases, reachPr.raster.clases and lookup .txt file. When running the query, I get an error:

# loading data 
  spatialData <- c(ekin.raster.clases, reachPr.raster.clases)
  lookup <- "C:/...path.../lookup_hazard.txt"
  target <- 'hazard'
  
  bn <- bnspatial(hazard_subnetwork, target, spatialData, lookup)

Read 6 items

"Intensity" points to:
 ->   

With states:
low    moderate    high

Represented by integer values:
1 <-> 2 <-> 3
----------------------------------

"probability" points to:
 ->   

With states:
low    moderate    high

Represented by integer values:
1 <-> 2 <-> 3
----------------------------------
Lookup table to interpret "hazard" values:
    hazard cell_ID
1      low       1
2 moderate       2
3     high       3

Problem:

  1. Node probability and Intensity point to nowhere, despite that according to the model they should be pointing to the target variable hazard. (This issue is probably related the the following point).

  2. I have not specified node rock mass scenario in any way. It has 4 states: none, 30yrs, 100yers, 300yrs and according to the state, different ekin.raster.clases, reachPr.raster.clases should be retrieved. Can this be modeled in bnspatial?

Error in linkNode(spatialData[names(lookup) == nm][[1]], network = network, : Integer values in categorical data do not match categories provided.

Hi,

I'm getting the above error message when trying to run the bnspatial function. The problem seems to be with setting states in the lookup file. The function runs no problem when I use only continuous discretised variables and set the minimum, maximum and break point values in the lookup text file.
The problem arises when I include a categorical variable as one of the explanatory variables so I'm assuming the problem is with how the states are set in the lookup file but I can't figure out how to fix the problem. Do you have any what it is I could be doing wrong?

I have attached a pdf file with screenshots of the data, lookup file and r output.
help_doc_jul_2018.pdf

Thank you,

Eimear

Bnspatial package compatible with Rversion 4.33

Goodmorning, is there bnspatial package compatible with R version 4.33? Its currently not available in the library for this R version but am getting a message saying it maybe available elsewhere. How can I install it?

Please remove dependencies on **rgdal**, **rgeos**, and/or **maptools**

This package depends on (depends, imports or suggests) raster and one or more of the retiring packages rgdal, rgeos or maptools (https://r-spatial.org/r/2022/04/12/evolution.html). Since raster 3.6.3, all use of external FOSS library functionality has been transferred to terra, making the retiring packages very likely redundant. It would help greatly if you could remove dependencies on the retiring packages as soon as possible.

lookup states

Hi,
Is there any way to avoid defining states for the 'lookup' argument in bnspatial.
The data on which my bn model is trained contains vector and spatial data, all continuous variables. To generate a network using bnlearn package, I first discretise the variables in R. I end up with a dataset with classBoundaries defined, but no defined states. I've tried assigning labels to levels within the variables after they've been discretised, but have gotten the following error - " evi_med " missing from network node : " EVI_Jul_recode_100_10km.tif "Names of states provided do not match the node states from the network.

Thanks,

Eimear

Some values in the spatial data do not have an associated state in the network node.

Hi Dario,

I am using bnspatial for the first time with a spatial layer with continuous values. I get this message:

Some values in the spatial data do not have an associated state in the network node. The following values will be treated as NA: 8.00602000427246, 8.05875575421593, 8.09402885559474, 8.15999984741211, 8.16289984703064, 8.17201171194823, 8.17608467789083, 8.2011417617926, 8.25, 8.25238804870777, 8.27000045776367, 8.28477827755444, 8.28637875609543, 8.28999996185303, 8.30963083413931, 8.31822148906794, 8.31999969482422, 8.32239982604981, 8.32323140498621, 8.32366436004639no non-missing arguments to min; returning Infno non-missing arguments to max; returning -Inf

I'm not a 100% sure what is happening, but I have checked that the names of layers, nodes and states correspond. I am also using -Inf and Inf as lower and upper boundary. The values stated here would be in the state defined as -Inf, 20.
The code runs but I believe the results are not credible. Do you have insight on what is going on and how I could correct this?

Many thanks and best wishes,
Camilla

mapping the results of a bayesian decision network

Can anyone help me with some information on how to generate a predictive risk map based on the results of a bayesian network generated in R using bnlearn? BN spatial requires a .net bn file so I can't use that. I've also tried spatial.ly and ggplot but can't get them to work either. I'm fairly new to R so I'm still trying to get my head around a lot of the coding/packages.
My Bayesian network is a directed graph related to a binary outcome variable, with discretised explanatory variable, the format of which are both vector and raster data. I have tried generating a map from a model using raster files only but am still running into problems.
Any help would be most appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.