Learn R Programming

snapKrig

An R package for fast spatial analysis and kriging on grids

snapKrig uses a computationally lean implementation of a 2-dimensional spatial correlation model for gridded data. By restricting to models with (separable) Kronecker covariance, the package can speed computations on certain likelihood and kriging problems by orders of magnitude compared to alternatives like gstat, fields, geoR, spatial, and LatticeKrig.

Here are some benchmarking results for computation time to downscale a 32 x 43 raster onto grids of increasing size by ordinary kriging.

code and instructions to reproduce these results can be found here

Installation

snapKrig is on CRAN. Install it with

install.packages('snapKrig')

or use devtools to install the latest development version

devtools::install_github('deankoch/snapKrig')

Check out the introduction vignette for a worked example with the Meuse soils data, or try the code below to get started right away. Some other code examples be found here. We plan to publish a more detailed tutorial and benchmarking study in an upcoming paper.

Example

To get started define an empty grid

library(snapKrig)

# simulate data on a rectangular grid
g_empty = sk(c(100, 200))
g_empty
#> 100 x 200 empty

Generate some random auto-correlated data for this grid

# set a random seed
set.seed(1234567)

# simulate data on a square grid
pars = sk_pars(g_empty)
g_sim = sk_sim(g_empty, pars)

# plot
plot(g_sim, main='snapKrig simulation')
g_sim
#> 100 x 200 complete

Downscale and filter noise by simple kriging

# downscale and predict, timing computations
t_start = Sys.time()
g_down = sk_rescale(g_sim, down=10)
g_pred = sk_cmean(g_down, pars, X=0)
#> 100 x 200 complete sub-grid detected

# print time elapsed in computation
t_end = Sys.time()
t_end - t_start
#> Time difference of 0.7008011 secs
# plot grid
plot(g_pred, main='snapKrig prediction at 10X resolution')

# print summary
summary(g_pred)
#> complete sk grid
#> 1973081 points
#> range [-1.95, 2.26]
#> ..............................
#> dimensions : 991 x 1991
#> resolution : 0.1 x 0.1
#>     extent : [0, 99] x [0, 199]

Technical Features

snapKrig’s computational efficiency makes it useful in situations where interpolation or down-scaling would pose a problem due to long computation times and/or high memory demands. Such problems are common when working with geo-referenced data in earth sciences. snapKrig’s features include:

  • anisotropic Gaussian processes on 2-dimensional regular grids for a choice of covariance kernels
  • optimized computation of the likelihood function, generalized least squares, and kriging predictor/variance
  • fast computations with missing data problems, and even faster in the complete data case
  • automated maximum likelihood model fitting and support for sample semi-variograms
  • user friendly helper functions for raster down-scaling and point interpolation

snapKrig depends only on packages included by default in R (like graphics and stats), but supports raster and geometry classes from sf and terra.

History

An earlier implementation of snapKrig was called pkern. snapKrig is a redesigned version that uses a more user-friendly S3 grid object class.

pkern was an R implementation of some methods I developed in my thesis for speeding up geostatistical computations involving large covariance matrices. The central idea is to model spatial dependence using a separable 2-dimensional covariance kernel, defined as the product of two (1-dimensional) univariate covariance kernels. This introduces special symmetries and structure in the covariance matrix, which are exploited in this package for fast and memory-efficient computations.

I developed snapKrig to support a project to interpolate weather data, but the methods underlying snapKrig are applicable more generally. See also [1], where I use product kernels to study directions of anisotropy in a non-stationary random fields, and [2, 3], where I apply it to fit a covariance structure, and to speed up calculations of dispersal kernel convolutions.

Copy Link

Version

Install

install.packages('snapKrig')

Monthly Downloads

158

Version

0.0.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Dean Koch

Last Published

May 6th, 2023

Functions in snapKrig (0.0.2)

is.na.sk

Indices of grid points with missing data (NAs)
mean.sk

Calculate the mean value in a grid
plot.sk

Heatmap plots
sk_GLS

Generalized least squares (GLS) with Kronecker covariances for sk grids
length.sk

The number of grid-points
sk

Make a snapKrig grid list object
print.sk

Auto-printing
sk_LL

Likelihood of covariance model pars given the data in sk grid g
sk_bds

Set default parameter covariance parameter bounds for a Kronecker covariance model
sk_corr

Stationary 1D correlation kernels
sk_add_bins

Add bin labels to a variogram data frame
sk_export

Convert "sk" grid to SpatRaster
sk_cmean

Compute kriging predictor (or variance) for an sk grid
sk_coords

Return coordinates of a grid of points in column-vectorized order
sk_fit

Fit a covariance model to an sk grid by maximum likelihood
sk_kp

Return named vector of Kronecker covariance parameters initialized to NA
sk_mat2vec

Column-vectorization indices
sk_nLL

Negative log-likelihood for parameter vector p
sk_make

Make a sk grid object
sk_sample_pt

Sub-grid point sampler for grid data
sk_sim

Random draw from multivariate normal distribution for sk grids
sk_sample_vg

Sample point pair absolute differences for use in semi-variogram estimation
sk_rescale

Up or down-scale a sk grid by an integer factor
sk_plot_pars

Plot the covariance structure of a snapKrig model
sk_plot_semi

Plot a semi-variogram
sk_pars

Initialize Kronecker covariance function parameters for a sk grid
sk_pars_make

Build a parameter list defining the 2d spatial Kronecker covariance model
sk_corr_mat

Construct 1D stationary correlation matrices for regularly spaced data
sk_plot

Plot grid data
sk_sub_find

Find complete regular sub-grids in a sk grid object
sk_to_string

Extract Kronecker covariance parameters as plot-friendly strings
sk_validate

Check compatibility of entries in a sk grid object, and fill in any missing ones
sk_pars_update

Convert covariance parameter list to/from vectorized form
sk_toep_mult

Efficiently compute yzx for symmetric Toeplitz matrices y and x
sk_snap

Snap a set of points to a "sk" grid
sk_sub

Return a sub-grid of a sk grid object
sk_var

Generate a covariance matrix or its factorization
[[.sk<-

sk_methods.R Dean Koch, 2022 S3 methods for sk grid list objects
[<-.sk

Single-bracket assign
sk_vec2mat

Invert column-vectorization indices
[.sk

Extract a sk list element (single-bracket access)
sk_sub_idx

Find column-vectorized index of a sub-grid
sk_vario_fun

Theoretical variogram function
sk_var_mult

Multiply a vector by a power of the covariance matrix
as.integer.sk

Coerce grid values to integer
dim.sk

Grid dimensions
Math.sk

Math group generics
as.logical.sk

Coerce grid values to logical
as.matrix.sk

convert to matrix
Ops.sk

Operations group generics
summary.sk

Grid summary
anyNA.sk

Check for presence of grid points with missing data (NAs)
as.vector.sk

Convert grid data to vector of specified mode
as.double.sk

Coerce grid values to numeric (double type)