Introduction to Spatio-Temporal Variography

November 21, 2026

1 Introduction

Since gstat package version 1.0-0, a dependency of gstat on the R package spacetime was introduced, allowing the code in gstat to exploit spatio-temporal data structures from that package. This vignette describes the possibilities and limitations of the package for spatio-temporal geostatistics.

To understand some of the possibilities and limitations, some knowledge of the history of the software is needed. The original gstat software (Pebesma and Wesseling, 1998) was a standalone computer program written in around 25,000 lines of C code, and would do geostatistical modelling, prediction and simulation. The gstat R package (Pebesma, 2004) consisted mostly of an R interface to this C code, together with convenience functions to use R’s modelling interface (formula’s, see ?lm) and graphic capabilities (trellis graphics in package lattice to show cross variogram as matrix plots; interaction with variogram clouds using base plots).

Starting 2003, a group of programmers developed a set of classes and methods for dealing with spatial data in R (points, lines, polygons, grids), which was supported by the publications of the well-known ASDAR book (Bivand et al. 2008; see also https://asdar-book.org/) and helped convergence in the user community, with in 2011 over 2000 subscribers on the r-sig-geo mailing list. Package gstat was one of the first packages that adopted and benefited from these classes.

To realize a particular idea, writing code in C typically takes about 10-20 times as long as writing it in R. C code can be more efficient, gives more control over memory usage, but is also more error prone–mistakes in C code make an R session crash, something that is hard to do when writing R code.

The original C code of gstat (Pebesma and Wesseling, 1998) provides all kriging varieties (universal, ordinary, simple; univariable, or multivariable as in cokriging) for two- or three-dimensional data. When the spatial domain is constrained to two dimensions (and this might cover over 99% of the use cases!), the third dimension might be used to represent time. As such, the metric variogram model, which allows for geometric anisotropy definition in three dimensions, can be used for spatio-temporal kriging. When defining the three-dimensional variogram as the sum of 2 or more nested variogram (summed) models, one can choose anisotropy coefficients for a single model such that this model is effectively zero in some directions, e.g. in space or in time; this allows one to approximate the so-called space-time sum model. It should be noted that at the C code there is no knowledge whether a third dimension represents space, or time. As such, particular characteristics of time cannot be taken care of.

Since the second half of 2010, the development of an R package spacetime started. It provides methods and classes for spatio-temporal data, and builds on the spatial data classes in sp and time series classes in xts. This document will explain how data in this form, and methods provided by this package, can be used for spatio-temporal geostatistics.

We will work with a data set with air quality (PM10) measurements over germany, taken from rural background stations available in the data sets provided by the European Environmental Agency.

> library(spacetime)> rm(list = ls())> data(air)> ls()

[1] "air" "dates" "DE" "DE_NUTS1" "stations"

2 Variography

2.1 Temporal autocorrelation and cross correlation

We will look into a subset of the data, ranging from 2005 to 2010, and remove stations that have only missing values in this period:

> if (!exists("rural"))+ rural = STFDF(stations, dates, data.frame(PM10 = as.vector(air)))> rr = rural[,"2005::2010"]> unsel = which(apply(as(rr, "xts"), 2, function(x) all(is.na(x))))> r5to10 = rr[-unsel,]> summary(r5to10)

Length Class Mode 96778 STFDF S4Next, we will (rather arbitrarily) select four stations, which have the following labels:

> rn = row.names(r5to10@sp)[4:7]> rn

[1] "DEBE056" "DEBE032" "DEHE046" "DENW081"

In the following, autocorrelation functions are computed and plotted. The resulting plot is shown in Figure 1.

> par(mfrow=c(2,2))> # select 4, 5, 6, 7> for (i in rn) {+ acf(na.omit(r5to10[i,]$PM10), main = i)+ }> par(mfrow=c(1,1))

> par(mfrow=c(2,2))

> # select 4, 5, 6, 7

> rn = row.names(r5to10@sp)[4:7]

> for (i in rn) {

+ x <- as.numeric(na.omit(r5to10[i, ]))

+ acf(x, main = i)

+ }

Figure 1: Autocorrelations for PM10; time lag unit in days.

Auto- and cross correlations can be computed when a multivariate time series object is passed to acf:

> acf(na.omit(as(r5to10[rn,], "xts")))The resulting plot is shown in Figure 2.

Figure 2: autocorrelations (diagonal) and cross correlations (off-diagonal) for the four stations selected; time lag unit in days.

From these graphs one should be able to observe the following

  • •

    autocorrelations for lag 0 are always 1

  • •

    cross correlations for lag 0 are not always 1

  • •

    cross correlations can be asymmetric, meaning that when ρA⁢B⁢(h) is the correlation between Z⁡(sA,t) and Z⁡(sB,t+h),

    ρA⁢B⁢(h)=ρB⁢A⁢(−h)≠ρA⁢B⁢(−h)

    with sA and sB the two stations between which a cross correlation is computed, and h the (directional!) lag between the series.

The plot further more shows that for these four stations the asymmetry is not very strong, but that cross correlations are fairly strong and of a similar form of autocorrelations.

This kind of plot does not work very well in layouts of e.g. 10 x 10 sub-plots; acf automatically chooses 4 x 4 as the maximum a single plot. To try this out, do a 7 x 7 plot

> acf(na.omit(as(r5to10[4:10,], "xts")))and note that here we see in the last figure (DESH & DESN04) a pair of plots with nearly no cross correlation. This might have to do with the spatial distance between these two stations:

> library(sp)> print(spDists(r5to10[4:10,]@sp), digits=3)

[,1] [,2] [,3] [,4] [,5] [,6] [,7][1,] 0.0 28.8 344 468 292 236 135[2,] 28.8 0.0 317 440 269 231 131[3,] 343.6 317.3 0 150 309 282 285[4,] 467.9 439.7 150 0 336 432 430[5,] 291.8 268.6 309 336 0 438 362[6,] 235.6 231.1 282 432 438 0 101[7,] 134.6 131.0 285 430 362 101 0(What is the spatial distance between stations DESH and DESN04?)

2.2 Spatial correlation, variograms

In the next steps, we will sample 100 time instances randomly,

> rs = sample(dim(r5to10)[2], 100)we select these instances as a SpatialPointsDataFrame and add a time index to them. After this we bind them together in a single SpatialPointsDataFrame which has a time index ti:

> lst = lapply(rs, function(i) { x = r5to10[,i]; x$ti = i; rownames(x@coords) = NULL; x} )> pts = do.call(rbind, lst)Then, we can compute the pooled variogram

> library(gstat)> v = variogram(PM10~ti, pts[!is.na(pts$PM10),], dX=0)and plot it (Figure 3):

> # plot(v, fit.variogram(v, vgm(1, "Exp", 200, 1)))> vmod = fit.variogram(v, vgm(100, "Exp", 200))> plot(v, vmod)

Figure 3: sample spatial variogram, averaged over 100 randomly chosen time steps

The fitted model is this:

> vmod

model psill range1 Exp 64.0804 66.99337One should note that the fit is rather poor, and not forget that we only have 53 stations selected. The time resolution is rich (1862 days) but the number of stations is small:

> dim(r5to10)

space time variables 53 1826 1

We can fit a spatio-temporal variogram the usual way, by passing an object of class STFDF (Pebesma, 2012):

> vv = variogram(PM10~1, r5to10, width=20, cutoff = 200, tlags=0:5)Alternatively, if this takes too long, a temporal subset can be taken, e.g. using the first 200 days:

> vv = variogram(PM10~1, r5to10, width=20, cutoff = 200, tlags=0:5)taking random days from the full period will lead to the a wrong assumption that every time index increment reflect a constant lag increase. As an alternative, we will here load the precomputed S/T variogram:

> data(vv)Plotting this object can be done in several ways, two 2D-plots are shown in Figure 4 and a 3D wireplot is shown in Figure 7:

> plot(vv)> plot(vv, map = FALSE)

Figure 4: Spatio-temporal sample variogram map (top) and sample variograms for each time lag (bottom); both figures depict the information of object vv.

2.3 Fitting a spatio-temporal variogram model

At first, we try to fit a metric model with spatio-temporal anisotropy:

> metricVgm <- vgmST("metric",+ joint=vgm(50,"Exp",100,0),+ stAni=50)> metricVgm <- fit.StVariogram(vv, metricVgm)As numerical criterion to judge the goodness of fit of model and sample variogram, the root-mean-squared-difference between the surfaces can be obtained by:

> attr(metricVgm, "optim")$value

[1] 60.60853

The final model can be plotted with the sample variogram (Figure 5):

> plot(vv, metricVgm)

Figure 5: Sample variogram map (left) and fitted metric model (right).

Now, let us try to fit and plot a separable model (Figure 6):

> sepVgm <- vgmST("separable",+ space=vgm(0.9,"Exp", 123, 0.1),+ time =vgm(0.9,"Exp", 2.9, 0.1),+ sill=100)> sepVgm <- fit.StVariogram(vv, sepVgm, method = "L-BFGS-B",+ lower = c(10,0,0.01,0,1),+ upper = c(500,1,20,1,200))

To compare this model with the previous one, we look at the optimized root-mean-squared-differences between the two surfaces and plot sample and both models:

> attr(sepVgm, "optim")$value

[1] 46.39099

> plot(vv, list(sepVgm, metricVgm))

Figure 6: Sample variogram map (left), fitted separable model (middle) and fittted metric model (right).

A wireframe (3D) plot of sample variogram and fitted variogram models can be obtained e.g. by

> library(lattice)> plot(vv, list(sepVgm, metricVgm), all=T, wireframe=T, zlim=c(0,120),+ zlab=NULL,+ xlab=list("distance (km)", rot=30),+ ylab=list("time lag (days)", rot=-35),+ scales=list(arrows=F, z = list(distance = 5)))which is shown in Figure 7. Further spatio-temporal model definitions can be found in the help pages of fit.StVariogram and variogramSurface. The demo stkrige presents further examples and illustrates an interactive 3D-plot of sample variogram and the fitted variogram model.

Figure 7: Wireframe plots of sample and fitted space-time variograms.

3 Spatio-temporal prediction

The vignette in package spacetime gives an example of using the gstat function krigeST for spatio-temporal kriging of the Irish wind data. The krigeST function uses global kriging, but only needs to invert the purely spatial and purely time covariance matrices in the separable case.

For more generic spatio-temporal kriging where space is two-dimensional, one could use krige, defining the observations and prediction locations as three-dimensional data sets, see for an example

> demo(gstat3D)It needs to be pointed out that in that case, the time (typically the third dimension) needs to be numeric, and three-dimensional anisotropy needs to be defined properly (see ?vgm).

In case the data set is too large for global kriging, one could try to use local kriging, and select data within some distance, or by specifying nmax (the nearest n observations). In both cases, it is advisable to transform time such that one can use an isotropic variogram model in the three dimensions, as only in that case the nearest n observations correspond to the n most correlated observations. krigeST provides a solution where a bufferNmax-times larger neighbourhood is evaluated within the covariance model and the strongest correlated nmax neighbours are selected.

An additional consideration is that in space-time, observations may not be regularly spaced. In some cases, the nearest n observations may come from a single measurement location, which may lead to sharp jumps/boundaries in the interpolated values. This might be solved by using larger neighbourhoods, or by setting the omax in krige or gstat calls to the neighbourhood size to select per octant (this should be combined with specifying maxdist).

References

  • •

    Bivand, R., E. Pebesma and V. Gomez-Rubio, 2008. Applied Spatial Data Analysis with R. Springer.

  • •

    Cressie, N.A.C., 1993. Statistics for Spatial Data. Wiley.

  • •

    Cressie, N. and C. Wikle, 2011. Statistics for Spatio-temporal Data. Wiley.

  • •

    Pebesma, E., 2012. spacetime: Spatio-Temporal Data in R. Journal of Statistical Software, volume 51, issue 7; 1-30.

  • •

    Pebesma, E.J., Wesseling, C.G., 1998. Gstat, a program for geostatistical modelling, prediction and simulation. Computers & Geosciences, 24 (1), pp. 17–31.

  • •

    Pebesma, E.J., 2004. Multivariable geostatistics in S: the gstat package. Computers & Geosciences 30: 683-691

  • •

    Ver Hoef, J.M., Cressie, N.A.C, 1993. Multivariable Spatial Prediction. Mathematical Geology, 25 (2), pp. 219–240.