bmstdr: Bayesian Modeling of Spatio-Temporal Data with R

Sujit K. Sahu

University of Southampton
S.K.Sahu@soton.ac.uk

2022-04-05

Abstract

This is a vignette for the R package bmstdr. The package facilitates Bayesian modeling of both point reference and areal unit data with or without temporal replications. Three main functions in the package: Bspatial for spatial only point referene data, Bsptime for spatio-temporal point reference data and Bcartime for areal unit data, which may also vary in time, perform the main modeling and validation tasks. Computations and inference in a Bayesian modeling framework are done using popular R software packages such as spBayes, spTimer, spTDyn, CARBayes, CARBayesST and also code written using computing platforms INLA and rstan. Point reference data are modeled using the Gaussian error distribution only but a top level generalized linear model is used for areal data modeling. The user of bmstdr is afforded the flexibility to choose an appropriate package and is also free to name the rows of their input data frame for validation purposes. The package incorporates a range of prior distributions allowable in the nominated packages with default hyperparameter values. The package allows quick comparison of models using both model choice criteria, such as DIC and WAIC, and facilitates K-fold cross-validation without much programming effort. Familiar diagnostic plots and model fit exploration using the S3 methods such as summary, residuals and plot are included so that a beginner user confident in model fitting using the base R function lm can quickly learn to analyzing data by fitting a range of appropriate spatial and spatio-temporal models. This vignette illustrates the package using five built-in data sets. Three of these are on point reference data on air pollution and temperature at the deep ocean and the other two are areal unit data sets on Covid-19 mortality in England.

1 Introduction

Model-centric analysis of spatial and spatio-temporal data is essential in many applied areas of research such as atmospheric sciences, climatology, ecology, environmental health and oceanography. Such diversity in application areas is being serviced by the rich diversity of R contributed packages listed in the abstract and many others, see the CRAN Task Views: Handling and Analyzing Spatio-Temporal Data and Analysis of Spatial Data. Moreover, there are a number of packages and text books discussing handling of spatial and spatio-temporal data. For example, see the references Pebesma and Bivand (2005), Bivand, Pebesma, and Gomez-Rubio (2013), Pebesma (2012), Millo and Piras (2012), Banerjee, Carlin, and Gelfand (2015), Wikle, Zammit-Mangion, and Cressie (2019) and Sahu (2022). The diversity in packages, however, is also a source of challenge for an applied scientist who is also interested in exploring solutions offered by other models from rival packages. The challenge comes from the essential requirement to learn the package specific commands and setting up of the prior distributions that are to be used for the applied problem at hand.

The current package bmstdr sets out to help researchers in applied sciences model a large variety of spatial and spatio-temporal data using a multiplicity of packages but by using only three commands with different options. Point reference spatial data, where each observation comes with a single geo-coded location reference such as a latitude-longitude pair, can be analyzed by fitting several spatial and spatio-temporal models using R software packages such as spBayes (Finley, Banerjee, and Gelfand 2015), spTimer (Bakar and Sahu 2015), INLA (Rue, Martino, and Chopin 2009), rstan (Stan Development Team 2020), and spTDyn (Bakar, Kokic, and Jin 2016).
A particular package is chosen with the package= option to the bmstdr model fitting routines Bspatial for point reference spatial only data and Bsptime for spatio-temporal data. In each of these cases a Bayesian linear model, which can be fitted with the option package="none" provides a base line for model comparison purposes. For areal unit data modeling the bmstdr function Bcartime provides opportunities for model fitting using three packages: CARBayes (Lee 2021), CARBayesST (Lee, Rushworth, and Napier 2018) and INLA (Blangiardo and Cameletti 2015). Here also a base line Bayesian generalized linear model for independent data, fitted using CARBayes, is included for model comparison purposes.

Models fitted using bmstdr can be validated using the optional argument validrows, which can be a vector of row numbers of the model fitting data frame, to any of the three model fitting functions. The package then automatically sets aside the nominated data rows as specified by the validrows argument and use the remaining data rows for model fitting. Inclusion of this argument also automatically triggers calculation of four popular model validation statistics: root mean square error, mean absolute error, continuous ranked probability score (Gneiting and Raftery 2007) and coverage percentage. While performing validation the package also produces a scatter plot of predictions against observations with further options controlling the behavior of this plot.

The generality of the platforms INLA and rstan allows tremendous flexibility in modeling and validation. However, bespoke code must be written to implement each different model. The current version of bmstdr provides a limited number of models which can be fitted using INLA and rstan. For point reference spatial only data a marginal model, after integrating the spatial random effects, with a known exponential correlation function is implemented using rstan and for spatio-temporal point reference data a marginal model has also been implemented using this package. A marginal model has also been implemented in a separate bmstdr model fitting function called Bmoving_sptime which facilitates modeling of point reference temporal data from moving sensors such as Argo floats in the oceans, see Section 3.11. INLA based models use a discretized Gaussian Markov random field with penalized complexity prior distribution (Fuglstad et al. 2018).

For areal unit data modeling the bmstdr function Bcartime provides opportunities for selected model fitting using CARBayes (Lee 2021), and CARBayesST (Lee, Rushworth, and Napier 2018) CARBayesST(Lee, Rushworth, and Napier 2018) and also INLA as illustrated by Blangiardo and Cameletti (2015). Here also a base line Bayesian generalized linear model for independent data, fitted using CARBayes, is included for model comparison purposes. The INLA based models can fit the celebrated Besag, York, and Mollié (1991) model and the Leroux model (Leroux, Lei, and Breslow 2000).

Models fitted using bmstdr can be validated using the optional argument validrows, which can be a vector of row numbers of the model fitting data frame, to any of the three model fitting functions. The package then automatically sets aside the nominated data rows as specified by the validrows argument and use the remaining data rows for model fitting. Inclusion of this argument also automatically triggers calculation of four popular model validation statistics: root mean square error, mean absolute error, continuous ranked probability score (Gneiting and Raftery 2007) and coverage percentage. While performing validation, the package also produces a scatter plot of predictions against observations with further options controlling the behavior of this plot.

The remainder of this vignette is organized as follows. Section 2 illustrates point reference spatial data modeling with Gaussian error distribution. Section 3 discusses Gaussian models for point reference spatio-temporal data. Area data are modeled in Section 4 where Section 4.3 illustrates models for static areal unit data and Section 4.4 considers areal temporal data. Some summary remarks are provided in Section 5.

2 Point reference spatial data modeling

2.1 Illustration data set nyspatial

To illustrate point reference spatio-temporal data modeling we use the nyspatial data set included in the package. This data set has 28 rows and 9 columns containing average ground level ozone air pollution data from 28 sites in the state of New York. The averages are taken over the 62 days in July and August 2006. The full spatio-temporal data set from 28 sites for 62 days is used to illustrate spatio-temporal modeling, see Section 3.1. Figure 2.1 represents a map of the state of New York together with the 28 monitoring locations where the three sites 1, 5 and 10 have been identified.

25 fitted sites and 3 validation sites (numbered) in  New York

Figure 2.1: 25 fitted sites and 3 validation sites (numbered) in New York

For regression modeling purposes, the response variable is yo3 and the three important covariates are maximum temperature: xmaxtemp in degree Celsius, wind speed: xwdsp in nautical miles and percentage average relative humidity: xrh. This data set is included in the package and further information regarding this can be obtained from the help file ?nyspatial.

2.2 The Bspatial function for fitting spatial regression models

The bmstdr package includes the function Bspatial for fitting regression models to point referenced spatial data. The arguments to this function has been documented in the help file which can be viewed by issuing the R command ?Bspatial. The package manual also contains the full documentation. The discussion below highlights the main features of this model fitting function.

Besides the usual data and formula the argument scale.transform can take one of three possible values: NONE, SQRT and LOG. This argument defines the on the fly transformation for the response variable which appears on the left hand side of the formula.

Default values of the arguments prior.beta0, prior.M and prior.sigma2 defining the prior distributions for \(\mathbf{\beta}\) and \(1/\sigma^2_{\epsilon}\) are provided.

The options model="lm" and model="spat" are respectively used for fitting and analysis using the independent spatial regression model with exponential correlation function. If the latter regression model is to be fitted, the function requires three additional arguments, coordtype, coords and phi. The coords argument provides the coordinates of the data locations. The type of these coordinates, specified by the coordtype argument, taking one of three possible values: utm, lonlat and plain determines various aspects of distance calculation and hence model fitting. The default for this argument is utm when it is expected that the coordinates are supplied in units of meter. The coords argument provides the actual coordinate values and this argument can be supplied as a vector of size two identifying the two column numbers of the data frame to take as coordinates. Or this argument can be given as a matrix of number of sites by 2 providing the coordinates of all the data locations.

The parameter phi determines the rate of decay of the spatial correlation for the assumed exponential covariance function. The default value, if not provided, is taken to be 3 over the maximum distance between the data locations so that the effective range is the maximum distance.

The argument package chooses one package to fit the spatial model from among four possible choices. The default option none is used to fit the independent linear regression model and the also the spatial regression model without the nugget effect when the parameter phi is assumed to be known. The three other options are spBayes, stan and inla. Each of these options use the corresponding R packages for model fitting. The exact form of the models in each case is documented in Chapter 6 of the book Sahu (2022).

Calculation of model choice statistics is triggered by the option mchoice=T. In this case the DIC, WAIC and PMCC values are calculated.

An optional vector argument validrows providing the row numbers of the supplied data frame for model validation can also be given. The model choice statistics are calculated on the opted scale but model validations and their uncertainties are calculated on the original scale of the response for ease of interpretation. This strategy of a possible transformed modeling scale but predictions on the original scale is adopted throughout the package.

There are other arguments of Bspatial, e.g. verbose, which control various aspects of model fitting and return values. Some of these other arguments are only relevant for specifying prior distributions and performing specific tasks as we will see throughout the remainder of this section.

The return value of Bspatial is a list of class bmstdr providing parameter estimates, and if requested model choice statistics and validation predictions and statistics. The S3methods print, plot, summary, fitted, and residuals have been implemented for objects of the bmstdr class. Thus the user can give the commands such as summary(M1) and plot(M1) where M1 is the model fitted object .

2.3 Fitting independent error regression models

The bmstdr package allows us to fit the base linear regression model given by: \[\begin{equation} Y_i = \beta_1 x_{i1} + \ldots + \beta_p x_{ip} + \epsilon_i, i=1, \ldots, n \tag{2.1} \end{equation}\] where \(\beta_1, \ldots, \beta_p\) are unknown regression coefficients and \(\epsilon_i\) is the error term that we assume to follow the normal distribution with mean zero and variance \(\sigma^2_{\epsilon}\). The usual linear model assumes the errors \(\epsilon_i\) to be independent for \(i=1, \ldots, n\). With the suitable default assumptions regarding the prior distributions we can fit the above model (2.1) by using the following command:

2.4 Fitting linear models with spatial error distribution

The independent linear regression model (2.1) is now extended to have spatially colored covariance matrix \(\sigma^{2}_{\epsilon}H\) where \(H\) is a known correlation matrix of the error vector \(\mathbf{\epsilon}\), i.e. \(H_{ij}=\mbox{Cor}(\epsilon_i, \epsilon_j)\) for \(i, j=1, \ldots,n\), \[\begin{equation} {\bf Y} \sim N_n \left(X{\mathbf \beta}, \sigma^{2}_{\epsilon} H\right) \tag{2.2} \end{equation}\] Assuming the exponential correlation function, i.e., \(H_{ij} = \exp(-\phi d_{ij})\) where \(d_{ij}\) is the distance between locations \({\bf s}_i\) and \({\bf s}_j\) we can fit the model (2.2) by issuing the command:

We discuss the choice of the fixed value of the spatial decay parameter \(\phi=0.4\) in M2. We use cross-validation methods to find an optimal value for \(\phi\). We take a grid of values for \(\phi\) and calculate a cross-validation error statistics, e.g. root mean square-error (rmse), for each value of \(\phi\) in the grid. The optimal \(\phi\) is the one that minimizes the statistics.

To perform the grid search a simple R function, phichoice_sp is provided. The documentation of this function explains how to set the arguments. For example, the following commands work:

For the nyspatial data example 0.4 turns out to be the optimal value for \(\phi\).

2.5 Fitting spatial models with nugget effect

A general spatial model with nugget effect is written as: \[\begin{equation} Y({\bf s}_i) = {\bf x}'({\bf s}_i) \mathbf{\beta} + w({\bf s}_i) + \epsilon( {\bf s}_i) \tag{2.3} \end{equation}\] for all \(i=1, \ldots, n\). In the above equation, the pure error term \(\epsilon({\bf s}_i)\) is assumed to follow the independent zero mean normal distribution with variance \(\sigma^2_{\epsilon}\), called the nugget effect, for all \(i=1. \ldots, n\). The stochastic process \(w({\bf s})\) is assumed to follow a zero mean Gaussian Process with the exponential covariance function, see Sahu (2022) for more details.

The un-observed random variables \(w({\bf s}_i)\), \(i=1, \ldots, n\), also known as the spatial random effects can be integrated out to arrive at the marginal model \[\begin{align} {\bf Y} & \sim N\left(X{\mathbf \beta}, \sigma^2_{\epsilon} \, I + \sigma^2_w S_w \right), \tag{2.4} \end{align}\] where the matrix \(S_w\) is determined using the exponential correlation function. This marginal model is fitted using any of the three packages mentioned above. The code for this model fitting is very similar to the one for fitting M2 above; the only important change is in the package= argument as noted below.

Model fitting is very fast except for M4 with the stan package. The model run for M4 takes about 20 minutes on a fast personal computer. These run produces the values of various Bayesian model choice criteria shown in Table 2.2.

Table 2.1: Table 2.2: Model choice criteria for various models fitted to the nyspatial data set.
CriteriaM0M1M2M3M4M5
pdic2.074.994.985.175.314.17
pdicalt13.585.175.167.836.46   
dic169.20158.36158.06158.68158.75157.23
dicalt192.22158.72158.41163.99161.04   
pwaic11.825.204.934.884.934.73
pwaic22.526.325.916.775.96   
waic1168.95158.57157.51158.70157.92158.46
waic2170.35160.82159.47162.48159.99   
gof591.82327.98330.08323.56316.67334.03
penalty577.13351.52346.73396.63394.8639.17
pmcc1168.95679.50676.82720.18711.52373.19

Mathematical expressions for all the quantities in the above table are provided in Sahu (2022). Here M0 is the intercept only model for which the results are obtained using the following bmstdr command,

The implementation using inla does not calculate the alternative values of the DIC and WAIC.

2.6 Illustrating the model validation statistics

The model fitting function Bspatial also calculates the values of four validation statistics: - root mean square-error (rmse), - mean absolute error (mae), - continuous ranked probability score (crps) and - coverage (cvg) if an additional argument validrows containing the row numbers of the supplied data frame to be validated is provided.

Data from eight validation sites 8, 11, 12, 14, 18, 21, 24 and 28 are set aside and model fitting is performed using the data from the remaining 20 sites.

The bmstdr command for performing validation needs an additional argument validrows which are the row numbers of the supplied data frame which should be used for validation.

Table 2.4 presents the validation statistics for all five models. Coverage is 100% for all five models and the validation performances are comparable. Model M4 with \(\phi=0.4\) can be used as the best model if it is imperative that one must be chosen using the rmse criterion.

Table 2.3: Table 2.4: Model validation statistics for the five models fitted to the nyspatial data set.
CriteriaM1M2M3M4M5
rmse2.4472.4002.4282.4222.380
mae2.1352.0152.0432.0541.971
crps2.8912.8852.8912.0372.137

To illustrate \(K\)-fold cross-validation, the 28 observations in the nyspatial data set are randomly assigned to \(K=4\) groups of equal size.

Now the M2.v command is called four times with the validrows argument taking values s1, ... s4. Table 2.6 presents the 4-fold cross-validation statistics for M2 only. It shows a wide variability in performance with a low coverage of 57.14% for Fold 3.

Table 2.5: Table 2.6: 4-fold cross-validation statistics for model M2 fitted to the nyspatial data set.
CriteriaFold1Fold2Fold3Fold4
rmse2.4415.0055.8652.508
mae1.7893.5455.4622.145
crps2.0852.0771.2282.072
cvg100.00085.71457.143100.000

A validation plot is automatically drawn each time a validation is performed. Below, we include the validation plot for fold-3 only.

Prediction against observation plot with the prediction intervals included. The `in/out' symbol in the plot indicates whether or not a prediction interval includes the 45 degree line.

Figure 2.2: Prediction against observation plot with the prediction intervals included. The `in/out’ symbol in the plot indicates whether or not a prediction interval includes the 45 degree line.

In this particular instance four of the seven validation observations are over-predicted. The above figure shows low coverage and high rmse. However, these statistics are based on data from seven validation sites only and as a result these may have large variability explaining the differences in the \(K\)-fold validation results.

The above validation plot has been drawn using the bmstdr command obs_v_pred_plot. This validation plot may be drawn without the line segments, which is recommended when there are a large number of validation observations. The plot may also use the mean values of the predictions instead of the default median values. The documentation of the function explains how to do this. For example, having the fitted object M2.v3, we may issue the commands:

3 Point reference spatio-temporal data modeling

3.1 Illustration data set nysptime

To illustrate point reference spatio-temporal data modeling we use the nysptime data set included in the package. This is a spatio-temporal version of the data set nyspatial introduced in Section 2.1. This data set, taken from Sahu and Bakar (2012), has 1736 rows and 12 columns containing ground level ozone air pollution data from 28 sites in the state of New York for the 62 days in July and August 2006.

For regression modeling purposes, the response variable is y8hrmax and the three important covariates are maximum temperature: xmaxtemp in degree Celsius, wind speed: xwdsp in nautical miles and percentage average relative humidity: xrh.

3.2 The Bsptime function for fitting spatio-temporal models

In this section we extend the spatial model (2.3) to the following spatio-temporal model. \[\begin{equation} Y({\bf s}_i, t) = {\bf x}'({\bf s}_i, t) \mathbf{\beta} + w({\bf s}_i, t) + \epsilon( {\bf s}_i, t) \tag{3.1} \end{equation}\] for \(i=1, \ldots, n\) and \(t=1, \ldots, T.\) Different distribution specifications for the spatio-temporal random effects \(w({\bf s}_i, t)\) and the observational errors \(\epsilon({\bf s}_i, t)\) give rise to different models. Variations of these models have been described in Sahu (2022). The bmstdr function Bsptime has been developed to fit these models.

Similar to the Bspatial function, the Bsptime function takes a formula and a data argument. It is important to note that the Bsptime function always assumes that the data frame is first sorted by space and then time within each site in space. Note that missing covariate values are not permitted.

The arguments defining the scale, scale.transform, and the hyper parameters of the prior distribution for the regression coefficients \({\mathbf \beta}\) and the variance parameters are also similar to the corresponding ones in the spatial model fitting case with Bspatial. Other important arguments are described below.

The arguments coordtype, coords, and validrows are also similarly defined as before. However, note that when the separable model is fitted the validrows argument must include all the rows of time points for each site to be validated.

The package argument can take one of six values: spBayes, stan, inla, spTimer, sptDyn and none with none being the default. Fittings using each of these package options are illustrated in the sections below. Only a limited number of models, specified by the model argument, can be fitted with each of these six choices. The model argument is described below.

In case the package is none, the model can either be lm or seperable. The lm option is for an independent error regression model while the other option fits a separable spatio-temporal model without any nugget effect. The separable model fitting method cannot handle missing data. All missing data points in the response variable will be replaced by the grand mean of the available observations. When the package option is one of the five named packages the model argument is passed to the chosen package.

For fitting a separable model Bsptime requires specification of two decay parameters \(\phi_s\) and \(\phi_t\). If these are not specified then values are chosen which correspond to the effective ranges as the maximum distance in space and length in time.

There are numerous other package specific arguments that define the prior distributions and many important behavioral aspects of the selected package. Those are not described here. Instead the user is directed to the documentation ?Bsptime and also the vignettes of the individual packages.

With the default value of package="none" the independent error regression model M1 and the separable model M2 are fitted using the commands:

The fitted model objects M1 and M2 are of class bmstdr and these can be explored using the S3 methods print, plot, summary and residuals. To explore the model fitted object issue the command names(M2). Here we explore the residuals by issuing the command:

A multiple time series plot of residuals

Figure 3.1: A multiple time series plot of residuals

This command renders a multiple time series plot of the residuals. However, the same command a <- residuals(M1) will not draw the residual plot since the independent error regression model is not aware of the temporal structure of the data. In this case it is possible to modify the command to

to have the desired result, see ?residuals.bmstdr.

3.3 Exploring the separable model

The above command for fitting M2 does not specify the values of the spatial and temporal decay parameters \(\phi_s\) and \(\phi_t\). The adopted values of these parameters are printed by the command:

These values are approximately 0.005 and 0.048 which correspond to the spatial range (the value of distance by which spatial correlation dies down) of 591 kilometers and the temporal range of 62 days which are the maximum possible values for the spatial and temporal domains of the data.

Optimal values of \(\phi_s\) and \(\phi_t\) can be determined by performing a grid search as in the case spatial only model fitting with Bspatial. The bmstdr package contains a function called phichoice which does this grid search specifically for the nysptime data set using the eight validation sites noted earlier. The command is:

The optimal values in this case turns out to be \(\phi_s=0.005\) and \(\phi_t=0.05\) approximately.

We now explore model validation for the separable model using the three sites chosen previously.

The fitted model M2.1 can be passed to the plot function that may draw several plots depending on the contents of the model fitted object. For example, this always draws a residuals against fitted values plot. The plot command for objects of class bmstdr can take additional arguments such as segments = FALSE which will provide a plot of the predicted values against observations without the line segments.

3.4 Spatio-temporal model fitting with the spTimer package

The spTimer package Bakar and Sahu (2015) can be used to fit, predict and forecast using various spatio-temporal models. This package also offers a great deal of flexibility in data modeling since it can model segmented time series data and also mixture of discrete and continuous data such as precipitation. The bmstdr function Bsptime can implement these models by invoking the package="spTimer" option. For example:

As before, the fitted model object M3 can be explored using the print, summary, plot and residuals commands. The plot command will draw the MCMC trace and density plots for each model parameter. The output plots of these commands are omitted from this document for brevity. Instead, we show validation performance at three selected sites: 1, 5, and 10 as shown in the map.

We randomly select 31 time points for validation at the three selected sites and identify the validation rows by using the following commands.

The getvalidrows command is included in the databmstdr library. Now we use the spTimer package to fit and validate the model.

For ease of illustration we have chosen scale.transform = "NONE". More complicated additional coding will be required if a different scale is chosen.

We illustrate a plot of the observations, fitted values and the validation predictions and their uncertainty intervals as plotted in Figure 11.13 in the book Banerjee, Carlin, and Gelfand (2015). The databmstdr package includes the function fig11.13.plot to generate this plot. The arguments required for this function are prepared as follows. The spTimer package does not provide the 95% intervals for the fitted values nor does it provide the MCMC samples of the fitted values which can be used to construct the 95% intervals. Hence, in the code below we use a normal approximation to obtain the uncertainty intervals. Now we first organize the data for plotting.

Now we call the fig11.13.plot function to render the plot for each site.

Time series of observed and predicted values at three sites.

Figure 3.2: Time series of observed and predicted values at three sites.

This ability to fit and validate using user defined validation rows is an enhancement of the spTimer package since that only allows validation at all time points for any selected site. The original package does not allow selective predictions at a subset of time points.

3.4.1 Illustrating spatio-temporal predictions

The spTimer package includes a prediction method function that can be used to predict at a large number locations. It does these predictions at all time points of the modeling data hence the covariates used in the model must be available for all prediction locations and at all time points. We illustrate the predictions using the fitted model M3. We only show the average predicted pollution map over the 62 days. To do the site-wise averaging we use the sitemeans function:

The bmstdr package includes the data set gridnysptime which contains the prediction data for 100 locations within the state of New York. Here is the code-chunk to perform prediction at these 100 locations and then averaging:

The data frame b contains the location information and the prediction summaries at the 100 prediction sites. To draw the prediction map we also include the fitted values from the 28 data modeling sites. We extract the fitted values as follows:

Finally we combine the predictions and the fitted values by the command:

Now we can obtain the average pollution map by using the linear interpolation function interp in the akima library. Also we discard the interpolations outside the state of New York by using the function fnc.delete.map.XYZ included in the bmstdr package.

Predicted map of average ozone air pollution in New York

Figure 3.3: Predicted map of average ozone air pollution in New York

Similar methods are used to obtain a map of the standard deviations of the predictions which has been omitted for brevity.

3.5 Marginal model fitting with the rstan package

The option package="stan" can be used to fit the only available model, which is a marginalized GP model, like (2.4) independently at each time point. Details for this model are provided in Chapter 7 of the book Sahu (2022). In this implementation the spatial decay parameter \(\phi\) is sampled and its estimate is made available in the parameter estimates table.

We illustrate model fitting by comparing the stan fitted model with the three previously fitted models. The commands for fitting all four models are:

The above commands have been executed to produce Table 3.2 of model choice criteria for the four models M1 to M4.

Table 3.1: Table 3.2: Model choice criteria for the four spatio-temporal models M1 to M4.
CriteriaM1M2M3M4
pdic4.984.9878.6530.36
pdicalt4.944.95841.9631.22
dicorig3912.073214.553132.102695.11
dicalt3912.013214.504658.722696.83
pwaic14.8514.3948.539.05
pwaic24.8714.58132.9010.04
waic13911.952448.002603.862088.15
waic23911.992448.382772.602090.12
gof963.24286.08216.75328.74
penalty965.58240.38873.84361.95
pmcc1928.82526.471090.59690.69
Later we will investigate how these models perform by refitting the models by requesting to validate.

3.6 Autoregressive model fitting with spTimer and INLA

Temporal auto regressive (AR) models can be fitted using both the spTimer and INLA package. The command for fitting the AR model using the spTimer package requires the additional option model="AR":

To fit and validate with the AR model using the INLA package we issue the command:

The AR models M5 and M6 produces the following model choice results in Table 3.4.

Table 3.3: Table 3.4: Model choice and validation statistics for the two AR models M5 and M6.
Criteriagofpenaltypmcc
spTimer321.33607.31928.64
INLA737.5421.17758.70

Parameter estimates from the two models are shown in Table 3.6. The INLA implemented model autoregressive model M6 has been fitted without an intercept as this has been seen to be better than spTimer model M5. Other parameter estimates are somewhat comparable but there are large differences also, see e.g., the estimates of the autoregressive parameter \(\rho\) and the spatial decay parameter \(\phi\). These differences are expected as the two packages use different model parameterisations and prior distributions. The current package bmstdr facilitates this sort of model comparison without much additional programming effort.

Table 3.5: Table 3.6: Parameter estimates for the two AR models M5 (first four columns) and M6 (last four columns).
Criteriameansd2.5%97.5%meansd2.5%97.5%
(Intercept)1.4370.5400.3832.513                
xmaxtemp0.0910.0150.0600.1220.2410.0030.2350.247
xwdsp0.0310.023-0.0140.0780.0520.0120.0290.076
xrh-0.1910.060-0.304-0.074-0.0430.018-0.079-0.008
rho0.5120.0210.4710.5540.8980.0660.7390.984
sig2eps0.0140.0020.0100.0190.4430.0160.4110.474
sig2eta0.5700.0320.5110.6380.6130.3720.2021.607
phi0.0090.0010.0080.0100.3290.1320.1510.660

3.7 Spatio-temporal dynamic models using the spTDyn package

The spTDyn package can be used to fit dynamic models in both time and space. A spatial dynamic model allows the regression coefficient to vary in space. A temporal dynamic model allows the regression coefficient to have a dynamic equation in time. To invoke these dynamic models the regression variables are specified in the formula argument with the optional enclosures sp for spatially dynamic and tp for temporally dynamic. Here is an example where the model is specified to have spatially varying effects of maximum temperature and dynamic regression coefficients for wind speed.

The model fitting results can be examined by the S3 methods functions as before. Here we explore the spatially varying regression coefficients as follows:

Spatial effects of maximum temperature from model M7

Figure 3.4: Spatial effects of maximum temperature from model M7

Similarly, we provide a plot of the temporal effects parameter.

Temporal effects  of wind speed from model M7

Figure 3.5: Temporal effects of wind speed from model M7

3.8 Fitting dynamic models using spBayes

The package option package="spBayes" in the Bsptime function triggers model fitting by using a complex dynamic model due to Gelfand, Banerjee, and Gamerman (2005), see also Chapter 11 of the book Banerjee, Carlin, and Gelfand (2015). This model fitting is sensitive to the choice of the prior distributions and the options below leads to a reasonable model fit.

The dynamic model parameters are extracted using the code below.

These extracted variance and the range (\(3/\phi\)) parameters are plotted using the ggplot function. The details are omitted from this document. We also do not provide parameter plots for the regression parameters.