SpatialGEV
SpatialGEV: Fast Bayesian inference for spatial extreme value models in R - Published in JOSS (2024)
Science Score: 93.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 1 DOI reference(s) in JOSS metadata -
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
✓JOSS paper metadata
Published in Journal of Open Source Software
Scientific Fields
Engineering
Computer Science -
40% confidence
Last synced: 6 months ago
·
JSON representation
Repository
An efficient computational method for fitting spatial extreme value models
Basic Info
Statistics
- Stars: 2
- Watchers: 2
- Forks: 3
- Open Issues: 4
- Releases: 2
Created over 5 years ago
· Last pushed over 1 year ago
Metadata Files
Readme
Contributing
License
README.Rmd
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
tidy = "styler"
)
```
# SpatialGEV
[](https://cran.r-project.org/package=SpatialGEV)
[](https://github.com/meixichen/SpatialGEV/actions)
*Meixi Chen, Martin Lysy, Reza Ramezan*
---
## Description
A fast Bayesian inference method for spatial random effects modelling of weather extremes.
The latent spatial variables are efficiently marginalized by a Laplace approximation using the
[***TMB***](https://github.com/kaskr/adcomp) library, which leverages efficient automatic
differentiation in C++. The models are compiled in C++, whereas the optimization step is carried
out in R. With this package, users can fit spatial GEV models with different complexities to
their dataset without having to formulate the model using C++. This package also offers a
method to sample from the approximate posterior distributions of both fixed and random effects,
which will be useful for downstream analysis.
## Installation
Before installing ***SpatialGEV***, make sure you have ***TMB*** installed following the instructions [here](https://github.com/kaskr/adcomp/wiki/Download).
***SpatialGEV*** uses several functions from the ***INLA*** package for SPDE approximation to the Matérn covariance as well as mesh creation on the spatial domain. If the user would like to use the SPDE method (i.e. `kernel="spde"` in `spatialGEV_fit()`), please first install package ***INLA***. Since ***INLA*** is not on CRAN, it needs to be downloaded following their instruction [here](https://www.r-inla.org/download-install).
To download the stable version of this package, run
```{r install-pkg-cran, eval=FALSE}
install.packages("SpatialGEV")
```
To download the development version of this package, run
```{r install-pkg-github, eval=FALSE}
devtools::install_github("meixichen/SpatialGEV")
```
## Example
Using the simulated data set `simulatedData2` provided in the package, we demonstrate how to use this package. Spatial variation of the GEV parameters are plotted below.
```{r show-data, fig.height=4, fig.width=4.5, out.width="50%"}
library(SpatialGEV)
# GEV parameters simulated from Gaussian random fields
a <- simulatedData2$a # location
logb <- simulatedData2$logb # log scale
logs <- simulatedData2$logs # log shape
locs <- simulatedData2$locs # coordinate matrix
n_loc <- nrow(locs) # number of locations
y <- Map(evd::rgev, n=sample(50:70, n_loc, replace=TRUE),
loc=a, scale=exp(logb), shape=exp(logs)) # observations
filled.contour(
x = unique(locs$x),
y = unique(locs$y),
z = matrix(a, ncol=sqrt(n_loc)),
color.palette = terrain.colors,
xlab="Longitude", ylab="Latitude",
main="Spatial variation of a",
cex.lab=1,cex.axis=1
)
filled.contour(
x = unique(locs$x),
y = unique(locs$y),
z = matrix(exp(logb), ncol=sqrt(n_loc)),
color.palette = terrain.colors,
xlab="Longitude", ylab="Latitude",
main="Spatial variation of b",
cex.lab=1,cex.axis=1
)
filled.contour(
x = unique(locs$x),
y = unique(locs$y),
z = matrix(exp(logs), ncol=sqrt(n_loc)),
color.palette = terrain.colors,
xlab="Longitude", ylab="Latitude",
main="Spatial variation of s",
cex.lab=1,cex.axis=1
)
```
To fit a GEV-GP model to the simulated data, use the `spatialGEV_fit()` function. We use `random="abs"` to indicate that all three GEV parameters are treated as random effects. The shape parameter `s` is constrained to be positive (log transformed) by specifying `reparam_s="positive"`. The covariance kernel function used here is the SPDE-approximated Matérn kernel `kernel="spde"`. Initial parameter values are passed to `init_param` using a list.
```{r fit}
fit <- spatialGEV_fit(
data = y, locs = locs, random = "abs",
init_param = list(
a = rep(60, n_loc),
log_b = rep(2,n_loc),
s = rep(-3,n_loc),
beta_a = 60, beta_b = 2, beta_s = -2,
log_sigma_a = 1.5, log_kappa_a = -2,
log_sigma_b = 1.5, log_kappa_b = -2,
log_sigma_s = -1, log_kappa_s = -2
),
reparam_s = "positive",
kernel="spde",
silent = TRUE
)
class(fit)
print(fit)
```
Posterior samples of the random and fixed effects are drawn using `spatialGEV_sample()`. Specify `observation=TRUE` if we would also like to draw from the posterior predictive distribution.
```{r sample}
sam <- spatialGEV_sample(model = fit, n_draw = 1e4, observation = TRUE)
print(sam)
```
To get summary statistics of the posterior samples, use `summary()` on the sample object.
```{r sample-summary}
pos_summary <- summary(sam)
pos_summary$param_summary[1:5,]
pos_summary$y_summary[1:5,]
```
One can also plot the full posteriors using e.g., the [***bayespolot***](https://mc-stan.org/bayesplot/) package.
```{r sample-plot, fig.width = 7, fig.height = 12, out.width = "100%"}
library(bayesplot)
library(ggplot2)
mcmc_areas(
x = sam$parameter_draws[,1:5],
prob = .95,
point_est = "mean"
) +
ggtitle(
"Posterior distributions of a1 - a5",
"with posterior means and 95% credible intervals"
)
```
# TODO
- [ ] Consider a shorter name, e.g., `sgev_*`, than `spatialGEV_*`.
- [ ] Argument `init_params` adds a lot of complexity to `spatialGEV_fit()`. Perhaps this function can be broken down into two parts: `spatialGEV_adfun()` which returns the `adfun` object, and then `spatialGEV_fit()` which does the fitting. The advantage is that the `adfun$env$parameters` object tells you the dimension of the parameter values, which can be useful for initialization. Also, note that `adfun$env$data` object contains the entire data list for browsing.
- [ ] Write some tests for the `spde` kernel. Construct the sparse precision matrix using `get_spde_prec()`, invert it using `Matrix::solve()`, apply the scale factor, and pass as variance to `dmvnorm()`.
Owner
- Name: Meixi Chen
- Login: meixichen
- Kind: user
- Repositories: 1
- Profile: https://github.com/meixichen
JOSS Publication
SpatialGEV: Fast Bayesian inference for spatial extreme value models in R
Published
November 08, 2024
Volume 9, Issue 103, Page 6878
Authors
Tags
Bayesian inference spatial model extreme valueGitHub Events
Total
- Release event: 4
- Watch event: 1
- Delete event: 1
- Push event: 4
- Create event: 3
Last Year
- Release event: 4
- Watch event: 1
- Delete event: 1
- Push event: 4
- Create event: 3
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 5
- Total pull requests: 20
- Average time to close issues: 14 days
- Average time to close pull requests: 9 days
- Total issue authors: 3
- Total pull request authors: 4
- Average comments per issue: 2.6
- Average comments per pull request: 0.25
- Merged pull requests: 15
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 1
- Pull requests: 2
- Average time to close issues: N/A
- Average time to close pull requests: about 6 hours
- Issue authors: 1
- Pull request authors: 1
- Average comments per issue: 0.0
- Average comments per pull request: 1.0
- Merged pull requests: 2
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- fabian-s (3)
- jasonelaw (1)
- fernandomayer (1)
Pull Request Authors
- meixichen (17)
- mlysy (13)
- jasonelaw (2)
- olivroy (2)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
.github/workflows/R-CMD-check.yaml
actions
- actions/checkout v2 composite
- r-lib/actions/check-r-package v1 composite
- r-lib/actions/setup-pandoc v1 composite
- r-lib/actions/setup-r v1 composite
- r-lib/actions/setup-r-dependencies v1 composite
DESCRIPTION
cran
- R >= 3.5.0 depends
- TMB >= 1.7.16 imports
- evd * imports
- mvtnorm * imports
- stats * imports
- INLA * suggests
- knitr * suggests
- rmarkdown * suggests
- testthat * suggests
