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_50, sph_100, sph_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.
- Read more about spatial covariance structures in [ME] mixed in the Stata Multilevel Mixed-Effects Reference Manual.
- Learn more about Stata’s multilevel mixed-effects features.