Learn R Programming

⚠️There's a newer version (1.1.1) of this package.Take me there.

spStack

Overview

spStack is an R package that delivers Bayesian inference for point-referenced spatial data by assimilating posterior inference over a collection of candidate models using stacking of predictive densities. Currently, it supports point-referenced Gaussian, Poisson, binomial and binary outcomes. Users can supply candidate values of spatial process parameters and certain auxiliary model parameters, based on which the collection of models will be created. spStack utilizes the Bayesian conjugate linear modelling framework for Gaussian data and the generalized conjugate multivariate distribution theory for non-Gaussian exponential family data. Learn more in vignette("spStack"). Technical details of the methodology are available in Zhang, Tang, and Banerjee 2024 and Pan, Zhang, Bradley, and, Banerjee 2024.

Installation

For a quick installation of the development version, run the following command in R.

# Install development version from GitHub
# install.packages("pak")
pak::pak("SPan-18/spStack-dev")

If installing from CRAN, use the following.

install.packages("spStack")

Installing from source

To install the package from source, download the tarball spStack_X.X.XX.tar.gz file. After setting the working directory at the file location, either issue R CMD install spStack_X.X.XX.tar.gz in the terminal, or run the following command in R to install the package.

install.packages("spStack_X.X.XX.tar.gz", type = "source", repos = NULL)

Note that the package is written in C++ with calls to FORTRAN routines and hence contains a Makevars file for cross-platform portability. So, it is important to set the correct path to FORTRAN libraries as well as BLAS and LAPACK on your computer. For example, if you are working on MacOS, create a file ~.R/Makevars and set global configurations for the libraries to link with R. The following is an example of such a Makevars file.

# Set Fortran library paths
FLIBS = -L/opt/homebrew/opt/gcc/lib/gcc/14 -lgfortran -lquadmath -lm

# BLAS and LAPACK libraries (using Accelerate framework on macOS)
BLAS_LIBS = -L/System/Library/Frameworks/Accelerate.framework/Versions/Current/ -framework Accelerate
LAPACK_LIBS = -L/System/Library/Frameworks/Accelerate.framework/Versions/Current/ -framework Accelerate

It tells R to use the Accelerate framework, that comes pre-installed with Mac for BLAS and LAPACK functions. If you do not have gfortran, simply run brew install gcc on the terminal which will install the gcc compiler and gfortran comes bundled with gcc. If gcc is installed using Homebrew, then the path should be the same as above, otherwise the path for gfortran needs to set correctly.

Usage

Once successfully installed, load the library in R.

library(spStack)

View the vignette by running vignette("spStack") and see example code to implement predictive stacking for different Bayesian hierarchical spatial models.

Acknowledgement

The graphics used in the logo has been obtained from the page Gaussian RF (Boris Kozintsev, 1999). It represents a realization of an isotropic Gaussian random field under the Matérn correlation function with decay and smoothness parameters 5 and 2, respectively.

Copy Link

Version

Install

install.packages('spStack')

Monthly Downloads

293

Version

1.1.0

License

GPL-3

Maintainer

Soumyakanti Pan

Last Published

July 12th, 2025

Functions in spStack (1.1.0)

stvcGLMstack

Bayesian spatially-temporally varying coefficients generalized linear model using predictive stacking
surfaceplot

Make a surface plot
surfaceplot2

Make two side-by-side surface plots
cholUpdate

Different Cholesky factor updates
candidateModels

Create a collection of candidate models for stacking
simGaussian

Synthetic point-referenced Gaussian data
get_stacking_weights

Optimal stacking weights
posteriorPredict

Prediction of latent process at new spatial or temporal locations
recoverGLMscale

Recover posterior samples of scale parameters of spatial/spatial-temporal generalized linear models
simPoisson

Synthetic point-referenced Poisson count data
iDist

Calculate distance matrix
simBinary

Synthetic point-referenced binary data
simBinom

Synthetic point-referenced binomial count data
stvcGLMexact

Bayesian spatially-temporally varying generalized linear model
sim_spData

Simulate spatial data on unit square
spStack-package

spStack: Bayesian Geostatistics Using Predictive Stacking
stackedSampler

Sample from the stacked posterior distribution
spLMstack

Bayesian spatial linear model using predictive stacking
spLMexact

Univariate Bayesian spatial linear model
spGLMexact

Univariate Bayesian spatial generalized linear model
spGLMstack

Bayesian spatial generalized linear model using predictive stacking
sim_stvcPoisson

Synthetic point-referenced spatial-temporal Poisson count data simulated using spatially-temporally varying coefficients