SPATIAL ERROR COVARIANCE STRUCTURES FOR MULTILEVEL MODELS

Stata’s mixed command now supports spatial error covariance structures, letting you model correlation between errors as a function of distance computed from coordinate variables. This feature is a part of StataNow™.

Spatial data arise in many scientific and applied settings: soil samples across a floodplain, crop yields across field plots, or sensor measurements across a region. In these settings, observations that are close together tend to have more similar errors than observations that are far apart. Modeling that dependence can reduce bias and produce more accurate inference.

 

In a multilevel mixed-effects model, random effects capture systematic differences across groups (such as farms, sites, or soil types). Spatial dependence often remains within groups because nearby locations share unobserved conditions. Spatial error covariance structures let you represent that within-group dependence directly through a distance-based correlation function.

 

You specify a spatial structure inside the residuals() option, along with the coordinate variables used to compute distances via the coordinates() suboption.

 

FOUR NEW SPATIAL STRUCTURES

mixed adds four widely used spatial correlation kernels:

spexponential – spatial exponential: correlation decays exponentially with distance.

spgaussian – spatial Gaussian: correlation decays smoothly and is flat near the origin.

spspherical – spatial spherical: correlation becomes exactly 0 beyond a finite range.

sppower – spatial power: correlation follows a power law.

 

Each structure can be fit using Euclidean distances by default, or using alternative metrics (Manhattan or maximum) via the metric() suboption.

 

Real spatial measurements often include microscale variation and measurement error. A nugget effect captures this by allowing correlation at zero distance to be less than one. All four spatial structures support the nugget suboption, and Stata reports the nugget proportion eta, which summarizes how much of the total error variance is attributable to the nugget component.

 

These spatial error covariance structures are also useful for modeling intensive longitudinal data with repeated measurements taken at irregular time points.


MODELING SPATIALLY CORRELATED ERRORS IN A FIELD TRIAL

To illustrate spatial error covariance structures in mixed, we analyze data from a simulated agricultural field trial measuring wheat yield across multiple farms. The fictional study design includes 12 farms, with 60 plots measured per farm, totaling 720 observations. At each plot, wheat yield (yield) was recorded along with nitrogen fertilizer application (nitrogen) and rainfall (rainfall). The spatial coordinates of each plot within its farm are stored in variables x and y, measured in meters.

 

We want to quantify the effects of nitrogen and rainfall on yield while accounting for two sources of dependence: between-farm variation, captured by random intercepts, and within-farm spatial correlation, modeled through a spatial error covariance structure. Because yield measurements are subject to measurement error and because microscale spatial variation exists below the sampling resolution, we also allow for a nugget effect.

 

SPATIAL SPHERICAL COVARIANCE STRUCTURE WITH A NUGGET EFFECT

We fit a linear mixed-effects model in which the predictors nitrogen_c and rainfall_c are centered at their sample means. Spatial correlation is modeled using a spherical spatial (spspherical) error covariance structure with a nugget effect (via the nugget suboption). The spatial coordinates x and y define plot locations within each farm on a 300-meter-by-300-meter grid.

 

The estimated range parameter indicates that plots within roughly 109 meters exhibit spatial correlation, while plots farther apart are effectively uncorrelated. The nugget accounts for about 39% of the total error variance, suggesting substantial microscale variation and measurement error.

 

We store this model for further analysis:

. estimates store spher_nug

 

ASSESSING STABILITY USING ALTERNATIVE INITIAL VALUES

Spatial covariance parameters enter the likelihood nonlinearly, so it is often good practice to assess sensitivity to starting values. For the spherical structure, this can be done by varying the initial value of the range parameter using the range0() suboption within residuals(). Below, we specify four initial values (50, 100, 200, and 400) for the range parameter, and we store the corresponding fitted models as sph_50sph_100sph_200, and sph_400.

 

. local init_vals 50 100 200 400

. foreach i of local init_vals {
     quietly mixed yield nitrogen_c rainfall_c || farmid:,
     residuals(spspherical, coordinates(x y) range0(`i') nugget) reml
     estimates store sph_`i'
  }

© Copyright 1996–2026 StataCorp LLC. All rights reserved.

We summarize the results from the above four fitted models by using the etable command. We also report the log restricted-likelihood (via the mstat(ll) option).

 

All four models yield identical results, indicating that the fitted model is robust to the choice of initial range. In applications where different initial values lead to different solutions, one would typically retain the model with the highest log restricted-likelihood.

 

COMPARING ALTERNATIVE SPATIAL-ERROR STRUCTURES

To explore alternative specifications, we also fit two additional models: one using the same spherical structure without a nugget effect and one using a Gaussian spatial-error covariance structure.

 

. quietly mixed yield nitrogen_c rainfall_c || farmid:, residuals(spspherical, coordinates(x y)) reml nolog

. estimates store spher

 

. estimates store gauss

 

Compared with the mixed model with the spherical structure, the Gaussian structure replaces the finite-range parameter (range) with a scale parameter phi, which controls how quickly spatial correlation decays with distance. In this example, observations become approximately uncorrelated (correlation <= 0.05) at distances greater than about 3ϕ.

 

We compare the three candidate models using information criteria:

 

The spherical spatial model with a nugget effect provides the best fit for this dataset, as indicated by having the lowest AIC and BIC values.