Tutorials › Generalized Linear Mixed Models (GLMM) › Building Spatial Covariance Structures from Locations and Adjacency

Spatial and Temporal Correlation · Tutorial 586 of 698

Building Spatial Covariance Structures from Locations and Adjacency

You will construct distance-based covariance and adjacency-based precision matrices, check their indexing, and determine whether they define valid spatial Gaussian structures.

Advanced 11 min read

What You'll Learn

  • Translate coordinates and adjacency lists into consistently indexed matrices.
  • Construct a distance-based spatial covariance matrix and its precision matrix.
  • Build a proper CAR precision matrix from a graph and check positive definiteness.
  • Verify matrix ordering under a permutation of spatial units.
  • Diagnose invalid structures, including singular intrinsic precisions and unsupported isolated nodes.

From Spatial Information to a Matrix

A spatial model may be specified through coordinates, pairwise distances, or a graph of neighboring regions. To use that information, it must be converted into a matrix whose rows and columns refer to the spatial units in a fixed order. A correct formula with mismatched labels can produce a matrix for the wrong model; a correctly indexed matrix that is not positive definite may fail to define the intended Gaussian distribution.

This tutorial focuses on that construction and verification step. The earlier material on spatial covariance, CAR models, and BYM decomposition provides the relevant model families. Here, the emphasis is on assembling their matrices and checking them. Let \(m\) denote the number of locations or regions, indexed \(1,\ldots,m\), and let \(u=(u_1,\ldots,u_m)^\mathsf{T}\) be a vector of spatial effects.

Definition: A covariance matrix \(K\) for \(u\) has entry \(K_{ij}=\operatorname{Cov}(u_i,u_j)\). A precision matrix \(Q\) is the inverse covariance matrix, \(Q=K^{-1}\), when that inverse exists. A covariance matrix must be symmetric and positive semidefinite; a nonsingular covariance matrix and its precision matrix are positive definite. The row and column order in either matrix must match the order of the components of \(u\).

A spatial covariance matrix is often built by first forming a correlation matrix \(R\) from distances, then multiplying by a variance parameter. For an adjacency-based model, one usually constructs \(Q\) directly from a graph. These are different routes to a spatial structure: the inverse of a covariance matrix is not generally obtained by applying the covariance formula entry by entry to a graph.

Constructing a Distance-Based Covariance Matrix

Suppose locations have coordinates \(s_i\), and let \(d_{ij}\) be the distance between locations \(i\) and \(j\), calculated using the chosen coordinate system and distance metric. A common exponential correlation specification is \(R_{ij}=\exp(-d_{ij}/\phi)\), where \(\phi>0\) is a range parameter. With marginal variance \(\sigma^2>0\), the covariance is \(K=\sigma^2R\). The diagonal has \(d_{ii}=0\), so \(K_{ii}=\sigma^2\).

The chosen distance and covariance family matter. Coordinates in degrees of longitude and latitude do not automatically represent Euclidean distances in a meaningful physical unit. A distance-based formula also must be one that yields a valid positive semidefinite matrix for the locations and distances in use. A familiar-looking decreasing function of distance is not, by itself, proof of validity.

Worked Example: Exponential Covariance Along Three Locations

Consider three hypothetical locations on a line at coordinates \(s_1=0\), \(s_2=1\), and \(s_3=2\) kilometres. Use the exponential correlation \(R_{ij}=\exp(-d_{ij}/\phi)\), with \(\phi=1/\log(2)\) kilometres, and marginal variance \(\sigma^2=4\) square units. The distances are \(d_{12}=1\), \(d_{23}=1\), and \(d_{13}=2\) kilometres. These distinct locations and a positive range parameter satisfy the conditions for this standard exponential covariance on Euclidean space.

Since \(\exp(-1/\phi)=1/2\), the correlations at distances 1 and 2 are \(1/2\) and \(1/4\), respectively. Using the order \(1,2,3\), the correlation and covariance matrices are

$$ R= \begin{pmatrix} 1 & 1/2 & 1/4\\ 1/2 & 1 & 1/2\\ 1/4 & 1/2 & 1 \end{pmatrix}, \qquad K=4R= \begin{pmatrix} 4 & 2 & 1\\ 2 & 4 & 2\\ 1 & 2 & 4 \end{pmatrix}. $$

For this small matrix, positive definiteness can be checked using the leading principal minors of \(R\). They are \(1\), \(1-(1/2)^2=3/4\), and

$$ \det(R)=1+2(1/2)(1/2)(1/4)-(1/2)^2-(1/2)^2-(1/4)^2 =1+\frac18-\frac14-\frac14-\frac1{16} =\frac9{16}>0. $$

All leading principal minors are positive, so \(R\) is positive definite. Multiplication by the positive scalar \(4\) preserves positive definiteness, so \(K\) is a valid, nonsingular covariance matrix. Its diagonal entries are \(4\) square units, as required by the specified marginal variance.

When a measurement-level independent component is included, its variance can be added to the diagonal: \(K_{\text{total}}=\sigma^2R+\tau^2I\), where \(\tau^2\geq 0\) and \(I\) is the identity matrix. If \(R\) is positive semidefinite and \(\sigma^2>0\), adding \(\tau^2 I\) with \(\tau^2>0\) makes the result positive definite, because for every nonzero vector \(a\),

$$ a^\mathsf{T}K_{\text{total}}a =\sigma^2a^\mathsf{T}Ra+\tau^2a^\mathsf{T}a \geq \tau^2a^\mathsf{T}a>0. $$

This can resolve singularity, for example when locations are duplicated, but it changes the covariance model by adding independent variation. It should not be added merely to conceal a construction or indexing error.

Building a Precision Matrix From Adjacency

For areal data, spatial information is often represented by an adjacency matrix \(W\). In a common binary convention, \(W_{ij}=1\) if distinct regions \(i\) and \(j\) are neighbors and \(W_{ij}=0\) otherwise; \(W_{ii}=0\). For an undirected graph, \(W\) is symmetric. The neighbor count is \(d_i=\sum_j W_{ij}\), and \(D\) is the diagonal matrix with entries \(D_{ii}=d_i\).

One proper CAR precision construction is \(Q=\tau(D-\rho W)\), where \(\tau>0\) and \(\rho\) is chosen so that \(D-\rho W\) is positive definite. For a graph with all \(d_i>0\), this condition can be checked through the eigenvalues of \(D^{-1/2}WD^{-1/2}\): every eigenvalue \(\lambda_k\) must satisfy \(1-\rho\lambda_k>0\). For a small matrix, direct positive-definiteness checks are also useful. If \(Q\) is positive definite, it defines covariance \(K=Q^{-1}\); \(Q\) itself is not the covariance matrix.

Worked Example: Proper CAR Precision for a Three-Region Path

Take three hypothetical regions arranged as a path: region 1 neighbors region 2, region 2 neighbors regions 1 and 3, and region 3 neighbors region 2. Order the regions as \(1,2,3\). Then

$$ W= \begin{pmatrix} 0&1&0\\ 1&0&1\\ 0&1&0 \end{pmatrix}, \qquad D= \begin{pmatrix} 1&0&0\\ 0&2&0\\ 0&0&1 \end{pmatrix}. $$

Choose \(\tau=1\) and \(\rho=1/2\). The proposed precision is

$$ Q=D-\frac12W= \begin{pmatrix} 1&-1/2&0\\ -1/2&2&-1/2\\ 0&-1/2&1 \end{pmatrix}. $$

The graph is undirected, so \(W\), \(D\), and \(Q\) are symmetric. The leading principal minors of \(Q\) are \(1\), \(2-(1/2)^2=7/4\), and

$$ \det(Q)=1\{2(1)-(-1/2)^2\} -(-1/2)\{(-1/2)(1)-0\} =\frac74-\frac14=\frac32>0. $$

Thus all leading principal minors are positive and \(Q\) is positive definite. Its inverse, the covariance matrix for this construction, is

$$ K=Q^{-1}= \begin{pmatrix} 7/6&1/3&1/6\\ 1/3&2/3&1/3\\ 1/6&1/3&7/6 \end{pmatrix}. $$

For instance, the first row of \(QK\) is \((7/6-1/6,\;1/3-1/3,\;1/6-1/6)=(1,0,0)\). The second row is \((-7/12+2/3-1/12,\;-1/6+4/3-1/6,\;-1/12+2/3-7/12)=(0,1,0)\); the third row is \((0,0,1)\) by symmetry with the first. Hence \(QK=I\), consistent with the stated inverse. The covariance entries are not just the adjacency entries: they result from inverting the precision matrix.

An intrinsic CAR precision uses \(D-W\), corresponding to \(\rho=1\) in this expression. For a connected graph, it is singular: a constant vector lies in its null space. It therefore does not define an ordinary nonsingular covariance by inversion. This construction is useful under its appropriate constraints, but it must not be reported as an invertible precision matrix without accounting for the null space. An isolated region has \(d_i=0\); the usual expression based on \(D^{-1/2}\) is then undefined, and that region requires explicit model handling rather than silent inclusion in the same check.

Indexing and Reordering

Matrix indices are labels, not merely positions on a page. If a data vector is ordered as \((u_1,u_2,u_3)^\mathsf{T}\), then row 2 and column 3 must refer to locations 2 and 3. If locations are reordered, the same permutation must be applied to both rows and columns of every covariance or precision matrix. Let \(P\) be a permutation matrix. The reordered quantities are \(u'=Pu\), \(K'=PKP^\mathsf{T}\), and \(Q'=PQP^\mathsf{T}\). Since \(P^\mathsf{T}P=I\), if \(QK=I\), then \(Q'K'=P Q P^\mathsf{T}P K P^\mathsf{T}=I\).

Worked Example: Verifying a Reordered Covariance and Precision Pair

Return to the distance-based example with \(\sigma^2=4\), and reorder the locations as \(3,1,2\). The new covariance is obtained by reordering both matrix dimensions, not by changing the underlying distances:

$$ K'= \begin{pmatrix} 4&1&2\\ 1&4&2\\ 2&2&4 \end{pmatrix}. $$

From the earlier covariance, its precision is

$$ Q'=K'^{-1}= \begin{pmatrix} 1/3&0&-1/6\\ 0&1/3&-1/6\\ -1/6&-1/6&5/12 \end{pmatrix}. $$

The first row of \(Q'K'\) is \((4/3-1/3,\;1/3-1/3,\;2/3-2/3)=(1,0,0)\). The second row is \((1/3-1/3,\;4/3-1/3,\;2/3-2/3)=(0,1,0)\). The third row is \((-2/3-1/6+5/6,\;-1/6-2/3+5/6,\;-1/3-1/3+5/3)=(0,0,1)\). Therefore \(Q'K'=I\). This check confirms that the reordering preserved the inverse relation.

The new first row and column refer to location 3, the second to location 1, and the third to location 2. Applying the permutation to only one matrix, or to only rows but not columns, would not represent the same spatial structure.

Checks, Alternatives, and Failure Modes

For a small construction, use checks that target different possible errors. Confirm symmetry; confirm that the diagonal matches the stated variances for a covariance matrix; and check positive definiteness if a nonsingular Gaussian covariance or precision is required. For a proposed inverse pair, multiply them and inspect whether the product is the identity. Also compare labels and order against the coordinate table or adjacency list. These checks answer different questions: a symmetric matrix can still be indefinite, and a positive-definite matrix can still be indexed incorrectly.

  • Distances or graph edges are misaligned. A row may use the coordinate for one region while its label refers to another. Keep one explicit ordered list of identifiers and use it to build every matrix.
  • The graph is unintentionally directed. If neighborhood is meant to be undirected, verify \(W=W^\mathsf{T}\). A one-sided edge can make a proposed precision nonsymmetric.
  • A precision is mistaken for a covariance. The entries of \(Q\) describe conditional dependence in the precision parameterization; marginal covariances are entries of \(Q^{-1}\) when \(Q\) is positive definite.
  • A singular intrinsic precision is inverted as though it were nonsingular. A zero determinant or a zero eigenvalue signals that an ordinary inverse does not exist. Constraints or a proper model are needed for a different, well-defined construction.
  • A plausible decay rule is assumed valid without checking its scope. Validity depends on the covariance family and the locations or distance structure, not only on whether correlations decrease with distance.

Distance-based covariance and adjacency-based precision structures are alternatives with different assumptions. A distance model can encode separation between point locations and may distinguish near from far pairs even when no explicit graph is available. An adjacency model encodes the chosen neighbor relation and is natural for areal units; it need not reflect physical distance between non-neighboring regions. Neither construction automatically captures every feature of a spatial process. The results also depend on coordinate choices, graph definitions, and parameter values. In large applications, numerical factorizations and eigenvalue or Cholesky checks are more practical than hand calculation, but they do not remove the need to verify labels, model assumptions, and singularity.

Key takeaway: Construct the matrix in a declared location order, state whether it is a covariance or precision matrix, and check symmetry and positive definiteness under the model's conditions. If the order changes, apply the same row-and-column permutation to the entire matrix.

Check Your Understanding

Answer each question, then compare with the brief solutions below.

  1. For three locations ordered \(1,2,3\), what covariance matrix follows from marginal variance \(2\) and correlations \(0.6\) between adjacent locations and \(0.36\) between locations 1 and 3?
  2. In the three-region path example, why does the symmetry of \(Q\) not alone establish that it is a valid proper precision matrix?
  3. If a covariance matrix is reordered by a permutation, what must happen to its rows and columns? State the corresponding transformation for a precision matrix.
  4. What is the obstacle to treating \(D-W\) for a connected graph as an ordinary invertible precision matrix?

Solutions. (1) Multiply the correlation matrix by 2: \(K=2\begin{pmatrix}1&0.6&0.36\\0.6&1&0.6\\0.36&0.6&1\end{pmatrix}\). Its entries are in variance units. (2) Symmetry does not imply positive definiteness; the leading principal minors must also be positive. In the worked path example they are \(1,7/4,3/2\), which establishes positive definiteness. (3) Apply the same permutation to both dimensions: \(K'=PKP^\mathsf{T}\). A precision matrix transforms as \(Q'=PQP^\mathsf{T}\), preserving the inverse relation. (4) For a connected graph, \(D-W\) has a constant vector in its null space, so its determinant is zero and its ordinary inverse does not exist.

Clinical Biostats