Skip to contents

Introduction

The STDGLM package provides a framework for fitting spatio-temporal dynamic generalized linear models. These models are useful for analyzing data that varies over both space and time, allowing for the incorporation of spatial and temporal dependencies in the modeling process. The package provides functions for fitting these models, as well as tools for visualizing and interpreting the results.

Installation

You can install the package from GitHub using the following command:

if (!requireNamespace("devtools", quietly = TRUE)) {
  install.packages("devtools")
  require(devtools)
}
devtools::install_github("czaccard/STDGLM")

Run the following command to load the package:

Quick Usage Example

data(ApuliaAQ)
p = length(unique(ApuliaAQ$AirQualityStation)) # 51
t = length(unique(ApuliaAQ$time))              # 365

# distance matrix
W = as.matrix(dist(cbind(ApuliaAQ$Longitude[1:p], ApuliaAQ$Latitude[1:p])))

# response variable: temperature
y = matrix(ApuliaAQ$CL_t2m, p, t)
# covariates (intercept + altitude)
X = array(1, dim = c(p, t, 2))
X[,,2] = matrix(ApuliaAQ$Altitude, p, t)

mod <- stdglm(y=y, X=X, W=W)

Detailed Explanation on supported STDGLMs

Let pp denote the number of spatial units (either georeferenced locations or areal units) where data is collected, and let TT denote the number of time points. As for the current version of the package (0.0.0.9000), only Gaussian outcomes are supported. Specifically only dynamic linear models (DLMs) with the following observation equation can be handled: yit=𝐱it′𝛃it+𝐳it′𝛄+Ο΅it,Ο΅it∼N(0,σϡ2) y_{it} = \boldsymbol{x}_{it}' \boldsymbol{\beta}_{it} + \boldsymbol{z}_{it}' \boldsymbol{\gamma} + \epsilon_{it}, \quad \epsilon_{it} \sim N(0, \sigma_\epsilon^2) where:

  • yity_{it} is the response variable at spatial unit i=1,…,pi=1, \dots, p and time t=1,…,Tt=1, \dots, T,

  • 𝐱it=(x1,it,…,xJ,it)β€²\boldsymbol{x}_{it} = (x_{1,it}, \dots, x_{J,it})' is a JJ-dimensional (Jβ‰₯1J\ge 1) vector of covariates at spatial unit ii at time tt (an intercept may or may not be included here),

  • 𝛃it=(Ξ²1,it,…,Ξ²J,it)β€²\boldsymbol{\beta}_{it} = (\beta_{1,it}, \dots, \beta_{J,it})' is the state vector at time tt at spatial unit ii,

  • 𝐳it\boldsymbol{z}_{it} is a qq-dimensional vector of covariates whose effects are constant (an intercept may or may not be included here),

  • 𝛄\boldsymbol{\gamma} is a vector of non-varying coefficients,

  • Ο΅it\epsilon_{it} is the observation error at time tt at spatial unit ii.

The evolution of the state vector is described by the following state equation, which accounts for spatial correlations in the state vector: 𝛃j,t=𝐅j,t𝛃j,tβˆ’1+π›ˆj,t,π›ˆj,t∼Np(0,𝚺η,j),j=1,…,J \boldsymbol{\beta}_{j, t} = \boldsymbol{F}_{j,t} \boldsymbol{\beta}_{j, t-1} + \boldsymbol{\eta}_{j,t}, \quad \boldsymbol{\eta}_{j,t} \sim N_p(0, \boldsymbol{\Sigma}_{\eta, j}), \quad j=1, \dots, J

where:

  • 𝛃j,t=(Ξ²j,1t,…,Ξ²j,pt)β€²\boldsymbol{\beta}_{j,t} = (\beta_{j,1t}, \dots, \beta_{j,pt})' is the state vector related to the jj-th covariate xj,t=(xj,1t,…,xj,pt)β€²x_{j,t} = (x_{j,1t}, \dots, x_{j,pt})', at time tt for all spatial units,

  • 𝐅j,t=Ο•j(𝖳)𝐈p\boldsymbol{F}_{j,t} = \phi_j^{(\mathsf{T})} \boldsymbol{I}_p is a transition matrix,

  • 𝚺η,j\boldsymbol{\Sigma}_{\eta,j} is the covariance matrix of the state evolution error π›ˆj,t\boldsymbol{\eta}_{j,t}.

The transition matrix 𝐅j,t,j=1,…,J,\boldsymbol{F}_{j,t}, j=1, \dots, J, is assumed to be a scalar multiple of the identity matrix. The parameter Ο•j(𝖳)\phi_j^{(\mathsf{T})} controls the temporal autocorrelation of the state vector.

The state evolution covariance matrix 𝚺η,j\boldsymbol{\Sigma}_{\eta,j} can be structured to reflect spatial relationships, e.g.Β by assuming an exponential covariance function if the data are point-referenced: (𝚺η,j)iβ„“=ρj,2exp(βˆ’diℓρj,2),diβ„“=βˆ₯𝐬iβˆ’π¬β„“βˆ₯ (\boldsymbol{\Sigma}_{\eta,j})_{i\ell} = \rho_{j,2} \exp\left(-\frac{d_{i\ell}}{\rho_{j,2}}\right), \quad d_{i\ell} = \| \boldsymbol{s}_i - \boldsymbol{s}_\ell \| where ρj,1\rho_{j,1} is the partial sill, ρj,2\rho_{j,2} is the range parameter, and $ d_{i}$ is the Euclidean distance between locations 𝐬i\boldsymbol{s}_i and 𝐬ℓ\boldsymbol{s}_\ell. At the moment, this is the only supported covariance structure for point-referenced data.

In this case, the evolution error π›ˆj,t\boldsymbol{\eta}_{j,t} is assumed to be a zero-mean Gaussian process with exponential covariance matrix parameteterized by ρj,1\rho_{j,1} and ρj,2\rho_{j,2}, which we will denote as π›ˆj,t∼GP(0,ρj,1,ρj,2;exp)\boldsymbol{\eta}_{j,t} \sim GP(0, \rho_{j,1}, \rho_{j,2}; exp).

If the data are areal, a proper conditional autoregressive (PCAR) covariance structure is assumed: 𝚺η,j=ρj,1(𝐃wβˆ’Οj,2𝐖)βˆ’1 \boldsymbol{\Sigma}_{\eta,j} = \rho_{j,1} \left( \boldsymbol{D}_w - \rho_{j,2} \boldsymbol{W} \right)^{-1} where 𝐖\boldsymbol{W} is a binary adjacency matrix, 𝐃w\boldsymbol{D}_w is a diagonal matrix with row sums of 𝐖\boldsymbol{W} on the diagonal, and ρj,1\rho_{j,1} and ρj,2\rho_{j,2} are the conditional variance and autocorrelation parameters, respectively.

In this case, the evolution error π›ˆj,t\boldsymbol{\eta}_{j,t} follows a zero-mean PCAR process, and we will denote this as π›ˆj,t∼PCAR(0,ρj,1,ρj,2)\boldsymbol{\eta}_{j,t} \sim PCAR(0, \rho_{j,1}, \rho_{j,2}).

ANOVA Decomposition of the State Vector

The function stdglm allows for the decomposition of the state vector into components that can be interpreted as contributions from different sources of variability. Dropping the subscript jj for the sake of simplicity, the state vector Ξ²it\beta_{it} is decomposed as follows: Ξ²it=Ξ²Β―+Ξ²i(𝖲)+Ξ²t(𝖳)+Ξ²it(𝖲𝖳) \beta_{it} = \overline{\beta} + \beta_{i}^{(\mathsf{S})} + \beta_{t}^{(\mathsf{T})} + \beta_{it}^{(\mathsf{ST})} where:

  • Ξ²Β―\overline{\beta} is the overall mean effect,

  • Ξ²i(𝖲)\beta_{i}^{(\mathsf{S})} is the spatial effect at spatial unit ii,

  • Ξ²t(𝖳)\beta_{t}^{(\mathsf{T})} is the temporal effect at time tt,

  • Ξ²it(𝖲𝖳)\beta_{it}^{(\mathsf{ST})} is the interaction effect between space and time at spatial unit ii and time tt.

Bayesian Hierarchical Structure

The Bayesian model is as follows, for t=1,…,Tt=1, \dots, T and j=1,…,Jj=1, \dots, J: yit∼N(𝐱it′𝛃it+𝐳it′𝛄,σϡ2)𝛃j,t=𝟏pΞ²Β―j+𝛃j(𝖲)+𝟏pΞ²j,t(𝖳)+𝛃j,t(𝖲𝖳)𝛃𝐣(𝖲)=(Ξ²j,1(𝖲),…,Ξ²j,p(𝖲))β€²βˆΌGP(0,ρj,1(𝖲),ρj,2(𝖲);exp) or PCAR(0,ρj,1(𝖲),ρj,2(𝖲))Ξ²j,t(𝖳)∼N(Ο•j(𝖳)Ξ²j,tβˆ’1(𝖳),VΞ²,j(𝖳))𝛃j,t(𝖲𝖳)=(Ξ²j,1t(𝖲𝖳),…,Ξ²j,pt(𝖲𝖳))β€²βˆΌGP(Ο•j(𝖲𝖳)𝛃j,tβˆ’1(𝖲𝖳),ρj,1(𝖲𝖳),ρj,2(𝖲𝖳);exp) or PCAR(Ο•j(𝖲𝖳)𝛃j,tβˆ’1(𝖲𝖳),ρj,1(𝖲𝖳),ρj,2(𝖲𝖳))\begin{align*} y_{it} &\sim N(\boldsymbol{x}_{it}' \boldsymbol{\beta}_{it} + \boldsymbol{z}_{it}' \boldsymbol{\gamma}, \sigma_\epsilon^2) \\ \boldsymbol{\beta}_{j,t} &= \boldsymbol{1}_p \overline{\beta}_j + \boldsymbol{\beta}_j^{(\mathsf{S})} + \boldsymbol{1}_p \beta_{j,t}^{(\mathsf{T})} + \boldsymbol{\beta}_{j,t}^{(\mathsf{ST})} \\ \boldsymbol{\beta_j}^{(\mathsf{S})} &= (\beta_{j,1}^{(\mathsf{S})}, \dots, \beta_{j,p}^{(\mathsf{S})})' \sim GP(0, \rho_{j,1}^{(\mathsf{S})}, \rho_{j,2}^{(\mathsf{S})}; exp) \mbox{ or } PCAR(0, \rho_{j,1}^{(\mathsf{S})}, \rho_{j,2}^{(\mathsf{S})}) \\ \beta_{j,t}^{(\mathsf{T})} &\sim N(\phi_j^{(\mathsf{T})} \beta_{j,t-1}^{(\mathsf{T})}, V_{\beta,j}^{(\mathsf{T})}) \\ \boldsymbol{\beta}_{j,t}^{(\mathsf{ST})} &= (\beta_{j,1t}^{(\mathsf{ST})}, \dots, \beta_{j,pt}^{(\mathsf{ST})})' \sim GP(\phi_j^{(\mathsf{ST})} \boldsymbol{\beta}_{j,t-1}^{(\mathsf{ST})}, \rho_{j,1}^{(\mathsf{ST})}, \rho_{j,2}^{(\mathsf{ST})}; exp) \mbox{ or } PCAR(\phi_j^{(\mathsf{ST})} \boldsymbol{\beta}_{j,t-1}^{(\mathsf{ST})}, \rho_{j,1}^{(\mathsf{ST})}, \rho_{j,2}^{(\mathsf{ST})}) \end{align*}

The model is completed with the following priors (again, dropping the subscript jj, since these are common across jj): β¯∼N(0,VΞ³)Ξ²0(𝖳)∼N(0,VΞ²0)𝛃0(𝖲𝖳)∼Np(0,VΞ²0𝐈p)π›„βˆΌNq(0,VΞ³)σϡ2∼IG(aΟ΅,bΟ΅)ρ1(𝖲)∼IG(0.01,0.01)ρ2(𝖲)∼U(aρ,bρ)ρ1(𝖲𝖳)∼IG(0.01,0.01)ρ2(𝖲𝖳)∼U(aρ,bρ)Ο•(𝖳)∼TN(βˆ’1,1)(0,1)Ο•(𝖲𝖳)∼TN(βˆ’1,1)(0,1)VΞ²(𝖳)∼IG(a(𝖳),b(𝖳))\begin{align*} \overline{\beta} &\sim N(0, V_\gamma) \\ \beta_{0}^{(\mathsf{T})} &\sim N(0, V_{\beta_0}) \\ \boldsymbol{\beta}_{0}^{(\mathsf{ST})} &\sim N_p(0, V_{\beta_0} \boldsymbol{I}_p) \\ \boldsymbol{\gamma} &\sim N_q(0, V_\gamma) \\ \sigma_\epsilon^2 &\sim IG(a_\epsilon, b_\epsilon) \\ \rho_1^{(\mathsf{S})} &\sim IG(0.01, 0.01) \\ \rho_2^{(\mathsf{S})} &\sim U(a_\rho, b_\rho) \\ \rho_1^{(\mathsf{ST})} &\sim IG(0.01, 0.01) \\ \rho_2^{(\mathsf{ST})} &\sim U(a_\rho, b_\rho) \\ \phi^{(\mathsf{T})} &\sim TN_{(-1, 1)}(0, 1) \\ \phi^{(\mathsf{ST})} &\sim TN_{(-1, 1)}(0, 1) \\ V_\beta^{(\mathsf{T})} &\sim IG(a^{(\mathsf{T})}, b^{(\mathsf{T})}) \end{align*} where TN(q,r)(ΞΌ,Οƒ2)TN_{(q, r)}(\mu, \sigma^2) denotes a normal distribution with mean ΞΌ\mu and variance Οƒ2\sigma^2 truncated to the interval (q,r)(q, r). The hyperparameters aρa_\rho and bρb_\rho depend on the type of spatial data. If the data are point-referenced, they are set based on the minimum and maximum distances between points, respectively. If the data are areal, aρ=0.1a_\rho= 0.1 and bρ→1b_\rho \rightarrow 1.

Note that the spacetime-varying coefficients are assumed independent a priori across j=1,…,Jj=1,\dots,J.

Efficient Inference and Identifiability

To build an efficient sampler, the algorithm proposed by Chan and Jeliazkov (2009) is used in conjuction with sparse matrix techniques.

To make the model identifiable, some constraints are imposed on the varying parameters at each MCMC iteration:

  • Set βˆ‘t=1TΞ²t(𝖳)=0\sum_{t=1}^{T} \beta_{t}^{(\mathsf{T})} = 0.

  • Set βˆ‘i=1pΞ²i(𝖲)=0\sum_{i=1}^{p} \beta_{i}^{(\mathsf{S})} = 0.

  • Set βˆ‘i=1pΞ²i,t(𝖲𝖳)=0\sum_{i=1}^{p} \beta_{i,t}^{(\mathsf{ST})} = 0 for each t=1,…,Tt=1,\dots,T.

  • Set βˆ‘t=1TΞ²i,t(𝖲𝖳)=0\sum_{t=1}^{T} \beta_{i,t}^{(\mathsf{ST})} = 0 for each i=1,…,pi=1,\dots,p.

References

Chan, Joshua CC, and Ivan Jeliazkov. 2009. β€œEfficient Simulation and Integrated Likelihood Estimation in State Space Models.” International Journal of Mathematical Modelling and Numerical Optimisation 1 (1-2): 101–20.