Why Model a Spatial Expression Field?
Expression measurements from nearby tissue locations can be related even after accounting for measured covariates and the count-generating process. A spatial field represents this remaining location-dependent variation. A Gaussian process (GP) is one way to model it: instead of assigning unrelated effects to each spot, it specifies how strongly effects at two coordinates are expected to covary as a function of their separation.
This tutorial focuses on that covariance structure and its consequences. In a count regression, the GP is usually a latent effect on a linear predictor, not a claim that raw counts are normally distributed. The earlier material on count likelihoods and spatial random effects provides the relevant regression context; here the central questions are how to define the field, choose a kernel, and understand spatial borrowing.
A common specification sets the residual field mean to zero, \(m(s)=0\), and gives it a covariance depending only on coordinate differences. Such a field is stationary in the second-order sense: its variance is constant and its covariance depends on separation rather than absolute position. If the covariance depends only on Euclidean distance \(d=\lVert s-s'\rVert\), it is also isotropic: direction does not matter. These are modeling assumptions, not properties guaranteed by tissue biology.
Common Kernels and the Meaning of Length Scale
A useful starting point is to write a kernel as \(k(s,s')=\sigma_f^2 r(d)\). Here \(\sigma_f^2\) is the marginal variance of the latent field, and \(r(d)\) is a correlation function with \(r(0)=1\). The length scale \(\ell\) controls how quickly correlation declines with distance. Its interpretation depends on the kernel and on the units used for the coordinates.
Squared-exponential (SE): \(k_{\mathrm{SE}}(s,s')=\sigma_f^2\exp\left(-\frac{d^2}{2\ell^2}\right)\). This kernel produces very smooth, infinitely differentiable sample paths.
Exponential: \(k_{\mathrm{Exp}}(s,s')=\sigma_f^2\exp\left(-\frac{d}{\ell}\right)\). This is a Matérn kernel with smoothness parameter \(\nu=1/2\); its sample paths are less smooth than those from the SE kernel.
Matérn: a family with a separate smoothness parameter \(\nu\). For example, \(\nu=3/2\) gives \(k(d)=\sigma_f^2(1+\sqrt{3}d/\ell)\exp(-\sqrt{3}d/\ell)\). Larger \(\nu\) gives smoother fields; the SE kernel is the limiting case as \(\nu\) tends to infinity.
A larger \(\ell\) makes correlations decay more slowly, so observations farther apart can contribute more to prediction at a target location. This is spatial borrowing: information at one location informs the latent field at another through their covariance. A larger \(\ell\) does not increase the marginal variance \(\sigma_f^2\); it changes the dependence pattern. Conversely, a very short length scale gives weak borrowing between distinct locations. Coordinates should therefore be expressed in meaningful and consistent units, since changing their scale changes the numerical value of \(\ell\).
Worked Example: Comparing Kernel Correlations
Suppose tissue coordinates are measured in millimeters and the latent field has marginal variance \(\sigma_f^2=2.25\) expression-predictor units squared. Compare correlations at distance \(d=0.5\) mm for an SE kernel with \(\ell=0.5\) mm and an exponential kernel with the same length scale. Assume the respective kernels are valid for the locations and that the stated length scale is fixed.
For the SE kernel, the correlation is covariance divided by marginal variance:
Thus the covariance is \(2.25\exp(-0.5)\approx 1.3647\) expression-predictor units squared. For the exponential kernel,
so its covariance is \(2.25\exp(-1)\approx 0.8277\) expression-predictor units squared. At this separation, the SE kernel assigns greater correlation and covariance. This comparison reflects both the chosen kernel and the distance relative to \(\ell\); it does not establish that either kernel is appropriate for a particular tissue.
For the SE kernel at \(d=1\) mm, the correlation is \(\exp(-2)\approx 0.1353\), and the covariance is \(2.25\exp(-2)\approx 0.3045\) expression-predictor units squared. The covariance calculation uses the unrounded exponential value before rounding the result.
Conditioning and Spatial Prediction
To connect a GP prior to data, first consider a Gaussian observation model for a field. Let the observed values be \(z_i=u(s_i)+\epsilon_i\), where the errors are independent of the field and of one another, with \(\epsilon_i\sim N(0,\tau^2)\). The quantity \(\tau^2\) is observation-noise variance; it is distinct from the latent field variance \(\sigma_f^2\). In applications with counts, this Gaussian calculation is a transparent illustration of conditioning, while the observation likelihood may instead be a count likelihood.
Let \(K\) be the covariance matrix of the field at observed locations, let \(K_{*X}\) be the vector of covariances between the target field value \(u(s_*)\) and those observed field values, and let \(K_{**}=k(s_*,s_*)\). With a known mean function, write \(\mathbf{z}-\mathbf{m}\) for the observed residuals from that mean. Then the conditional distribution at the target is normal, with mean and variance as follows.
The inverse is well-defined when \(K+\tau^2I\) is positive definite, as it is for a positive semidefinite \(K\) and \(\tau^2>0\). For multiple target locations, the same result gives a conditional multivariate normal distribution. The predictive mean is a covariance-weighted combination of observed residuals, while the conditional variance measures remaining uncertainty about the latent field. If predicting a future noisy observation rather than the latent value, add its independent noise variance \(\tau^2\) to the predictive variance.
Why the Prediction Formula Holds
For clarity, take the zero-mean case. The observation vector has covariance \(K+\tau^2I\), and its covariance with \(u(s_*)\) is \(K_{X*}\). Define \(a=(K+\tau^2I)^{-1}K_{X*}\) and the residual \(R=u(s_*)-a^\mathsf{T}\mathbf{z}\). Directly,
The pair \((\mathbf{z},R)\) is jointly Gaussian because it is a linear transformation of jointly Gaussian variables. For jointly Gaussian variables, zero covariance implies independence. Thus conditioning on \(\mathbf{z}\) does not change the distribution of \(R\). The conditional mean of \(u(s_*)=a^\mathsf{T}\mathbf{z}+R\) is \(a^\mathsf{T}\mathbf{z}\), which is the stated prediction formula. Its conditional variance is the variance of \(R\):
This argument also explains why the formula is not a generic rule for non-Gaussian observations: the independence step depends on joint Gaussianity. For count data, posterior prediction generally requires conditioning under the count likelihood rather than simply applying this Gaussian-observation formula.
Worked Example: Predicting a Latent Field From One Noisy Measurement
Suppose \(u(s)\) has zero mean and an SE covariance with \(\sigma_f^2=1\) and \(\ell=1\) mm. There is one Gaussian observation \(z=1\) at \(s_1\), with noise variance \(\tau^2=0.25\). Predict the latent value at a location \(s_*\) exactly 1 mm away. Assume the model and its parameters are fixed and that the observation error is independent of the field.
The prior covariance between the target and observed field values is \(k(s_*,s_1)=\exp(-1^2/2)\approx 0.6065\). The observed variance, including noise, is \(1+0.25=1.25\). Therefore,
The conditional variance is
The observed positive residual pulls the predicted field above its prior mean of zero, but the prediction is smaller than the observed residual because it is one length scale away and the measurement is noisy. The remaining uncertainty is substantial: one noisy measurement does not determine the field at the target.
Using a GP With a Count Expression Model
For a gene measured at spot \(i\), a common structure is to put the spatial field on the log mean of a count distribution. For example, with a Poisson observation model,
Here \(Y_i\) is the count, \(L_i\) is an exposure or library-size factor, \(x_i\) contains measured covariates, and \(u(s_i)\) is the GP effect. Conditional on the field and covariates, the counts are independent under this model; dependence across spots is induced by the shared spatial field. The likelihood choice and mean structure must suit the data, and a GP does not by itself correct misspecified count variation or confounding.
Worked Example: Interpreting a Log-Scale Field on the Count Scale
At one hypothetical spot, suppose the exposure-adjusted baseline mean is 3 counts, a fixed covariate contributes 0.1 to the log mean, and the latent field has posterior distribution \(u(s)\mid\text{data}\sim N(0,0.04)\). Under the log-link Poisson mean model, calculate the posterior mean rate after accounting for uncertainty in the field. This calculation assumes the stated normal posterior and treats the baseline and covariate contribution as fixed.
For a normal variable \(U\sim N(\mu,v)\), the moment-generating function gives \(E\{\exp(U)\}=\exp(\mu+v/2)\). Therefore the mean count rate, averaged over the uncertain field, is
Simply substituting the field mean \(0\) would give \(3\exp(0.1)\approx 3.316\) counts and would omit the contribution of field uncertainty. The calculation illustrates a general point: exponentiating a Gaussian latent effect and then averaging is not the same as exponentiating its mean.
Diagnostics, Alternatives, and Limitations
A kernel is a substantive description of spatial dependence. Check whether the fitted field captures residual spatial structure, for example by inspecting residual spatial patterns or comparing empirical and model-implied covariance behavior at relevant separations. Strong remaining structure, systematic directional patterns, or abrupt changes across tissue regions can indicate that a stationary isotropic kernel is inadequate. Also inspect sensitivity to coordinate units, kernel choice, and plausible length scales. A length scale far beyond the tissue extent may imply nearly uniform borrowing; a scale far below typical spot spacing may imply little useful borrowing.
A GP can be compared with other spatial-effect approaches. A GP defines dependence through a covariance function of coordinates and can naturally predict at locations not in the observed set. Graph-based random effects instead use a specified neighborhood structure, which may better reflect irregular adjacency but ties dependence to that graph. Independent spot effects do not borrow information across locations. These approaches encode different assumptions; none is automatically superior. The following distinctions are useful when choosing a model.
| Approach | Dependence is specified through | Practical implication |
|---|---|---|
| Gaussian process | A coordinate-based covariance kernel | Can predict at new coordinates; results depend on kernel and length scale |
| Graph-based spatial effect | Adjacency or neighborhood relationships | Uses spot connectivity; behavior depends on how the graph is constructed |
| Independent spot effects | No cross-location dependence | No spatial borrowing; serves as a contrast when dependence is not assumed |
A standard GP also has computational costs: dense covariance matrices require substantial storage and matrix operations as the number of locations grows. Further, a single stationary isotropic length scale may be too restrictive for layered, anisotropic, or sharply bounded tissue. Spatial trends can be confounded with measured covariates, and the model cannot identify a biological mechanism from smooth spatial association alone. Inference about kernel parameters adds uncertainty beyond the fixed-parameter predictive calculations shown above.
Common Mistakes
- Equating the length scale with the field variance. The length scale controls the rate of covariance decay; \(\sigma_f^2\) sets marginal field variance.
- Calling a longer length scale “more variable.” It means dependence persists over greater distances, not that the field necessarily has a larger pointwise variance.
- Treating the latent field as the observed count. In a count regression the field typically changes the log mean; the count likelihood still describes observation variability.
- Adding observation noise to latent uncertainty indiscriminately. The latent predictive variance excludes future measurement noise; predicting a future noisy observation includes it.
- Assuming smoothness is evidence of biological continuity. Smoothness is partly imposed by the selected kernel and should be checked against tissue structure and residual patterns.
Check Your Understanding
Answer each question, then compare with the solutions below.
- For an SE kernel with fixed \(\sigma_f^2\) and distance \(d>0\), what happens to the correlation as \(\ell\) increases?
- At a target location, why does the predictive variance for a future noisy measurement exceed the latent predictive variance under independent observation noise?
- If a log-linked count model has baseline rate 3, fixed log-scale contribution 0.1, and latent effect \(U\sim N(0,0.04)\), what is the mean rate after averaging over \(U\)?
- Give one reason an isotropic kernel could be unsuitable for an oriented tissue structure.
Solutions. (1) The correlation \(r(d)=\exp\{-d^2/(2\ell^2)\}\) increases with \(\ell\), because the exponent becomes less negative. (2) The future observation includes independent noise, so its variance is the latent predictive variance plus \(\tau^2\). (3) The rate is \(3\exp(0.1+0.04/2)=3\exp(0.12)\approx 3.382\) counts. (4) Dependence may differ by direction in an oriented tissue layer, whereas an isotropic kernel assigns equal covariance to pairs at the same distance regardless of direction.