The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
The variogram() function plots the variogram for the spatial residual variance component for ASReml models. The variogram can be used for checking the assumptions of the model by revealing unexplained spatial patterns. The function only works on model objects from ASReml, and the model has to have a two-dimensional spatial residual variance component. We will go into more detail below about how to use the variogram for model fitting, and then show a practical example of this approach using the functions from biometryassist.
The variogram can be used in situations where we expect two-dimensional spatial correlation, and we want to find an appropriate model for this correlation. For instance, in an agricultural field trial, we expect some correlation between the observed properties of adjacent plants in the field. We will refer to the two dimensions as “rows” and “columns” in this vignette, but they could equivalently have other names, such as “ranges” or “bays”.
There are multiple ways to account for spatial effects in a linear mixed model, including:
Assuming that the residuals follow an autoregressive process.
Adding random effects for the rows and/or columns, essentially assuming there is a different underlying mean on each row/column.
Adding a trend term across one of the dimensions, for instance a linear trend or a cubic spline.
But it is difficult to know where to start with including spatial effects in the model. Gilmour et al. (1997) recommend starting with a model assuming a separable two-dimensional AR1 process across the rows and columns. We can then compare the sample variogram to the theoretical variogram for a two-dimesional AR1 process, and use the differences to infer what changes are required to appropriately model the spatial pattern in the data.
The theoretical variogram for a two-dimensional AR1 process increases from 0 at 0 row and column displacement to a plateau. How many lags it takes to reach this plateau depends on the strength of the correlation in each dimension. For instance, the following plot shows the theoretical variogram of a two-dimensional autoregressive process with 0.6 row correlation and 0.4 column correlation.
However, for the sample variogram for observed data, we will not observe something exactly like the theoretical variogram. Instead, even assuming the residuals follow a two-dimensional AR1 process, we will observe the variogram increasing to a plateau and then oscillating around it.
After hypothesised spatial effects are included in the model, we inspect the sample variogram again to determine the extent to which the patterns we observed have disappeared. Model fitting using the variogram is an iterative process, where the variogram is interpreted at each step. Other metrics can also be used to determine the extent to which the model has been improved at each step, such as the results of log-likelihood ratio tests or the Akaike’s Information Criterion (AIC) values for the different models.
Unfortunately, the process of determining what spatial effects are present based on the variogram is somewhat subjective. Section 3 below shows the process of interpreting sample variograms with a real dataset as an example.
It is important to note that we need to have enough rows or columns to fit an autoregressive process. A general rule of thumb is that there need to be at least 5 levels in the dimension values For instance, if there are less than 5 columns but 5 or more rows, we would fit an AR1 process only in the row dimension, and assume there is no correlation in the column dimension. In ASReml syntax, we would write this as ar1(row):id(column).
In the case where we are only fitting an AR1 process along one dimension, we do not expect to see exactly an AR1 process in the other dimension, but we could still look for the presence of trend or random effects in that dimension.
To demonstrate the process outlined in the previous section, we will go through an example of using the variogram to find an appropriate spatial model for a dataset. The example is from Gilmour et al. (1997), where process of using the variogram for model-fitting is extensively discussed, and uses the Slate Hall wheat trial dataset which is publicly available in the agridat package.
# Load necessary packages
library(tidyverse)
library(asreml)
library(biometryassist)
# Load the data from the agridat package
data <- agridat::gilmour.slatehall
# Data formatting
data <- data |>
mutate(row = factor(row), col = factor(col)) |>
arrange(row, col)While our focus is on the use of the variogram, it is often a good idea to visualise the data before fitting models. Spatial patterns can sometimes be detected visually in the exploratory plots.
The heat_map() function from biometryassist can be used to generate a heat map of the data. As seen below, in the case of the Slate Hall data, the heat map reveals an increasing trend in yield across the rows.
data |> heat_map(value = yield, x_axis = row, y_axis = col)We can also use the summary_graph() function to generate side-by-side boxplots showing the distribution of yield for each row and column. The side-by-side boxplot of yield by row supports the presence of a trend across the rows.
summary_graph(data = data, response = "yield", exp_var = "row")summary_graph(data = data, response = "yield", exp_var = "col")As recommended by Gilmour et al. (1997), we first fit an initial linear mixed model using ASReml with an AR1:AR1 residual structure over the rows and columns. This allows us to calculate the sample variogram, which is plotted by the variogram function. We also include the genotype as a fixed effect, and rep as a random effect in the model.
m1 <- asreml(yield ~ gen,
random = ~rep,
residual = ~ar1(row):ar1(col),
data = data)We can now calculate and display the sample variogram of the first model by running the variogram function from biometryassist using the fitted ASReml model object as the input.
variogram(m1)The sample variogram of our initial model does not fit the theoretical variogram of an AR1:AR1 process. The clearest deviation from this pattern is the increasing variance across row lags, which suggests the presence of a trend across the rows. This is in line with our initial impression from the exploratory plots.
To make our model better fit the data, we will add a linear row effect, re-fit the model, and observe how the change impacts the sample variogram. If a linear trend does not accurately capture the trend, then we will still observe an increasing tendency across the rows in the variogram of the new model. In that case, we would need to explore other trend terms such as cubic splines.
m2 <- asreml(yield ~ gen + lin(row),
random = ~rep,
residual = ~ar1(row):ar1(col),
data = data)## ASReml Version 4.2 30/11/2026 19:56:14
## LogLik Sigma2 DF wall
## 1 -742.1896 37392.58 124 19:56:14
## 2 -732.6288 34187.13 124 19:56:14
## 3 -725.6024 35433.58 124 19:56:14
## 4 -723.3242 39758.19 124 19:56:14
## 5 -722.8578 43918.28 124 19:56:14
## 6 -722.8311 45103.99 124 19:56:14
## 7 -722.8293 45402.24 124 19:56:14
variogram(m2)For the second model, we no longer see an increasing pattern in variogram across the row lag, which suggests that the trend has been captured well enough by the linear row effect.
We can also check the significance of this effect with the Wald test, where we find the linear row term has a p-value of around 0.0117, supporting its significance.
wald(m2, denDF = 'default')$Wald## ASReml Version 4.2 30/11/2026 19:56:15
## LogLik Sigma2 DF wall
## 1 -722.8292 45477.53 124 19:56:15
## 2 -722.8292 45483.67 124 19:56:15
## 3 -722.8292 45498.58 124 19:56:15
##
## Wald tests for fixed effects.
## Response: yield
##
## Df denDF F.inc Pr
## (Intercept) 1 7.3 3793.0 0.0000000
## gen 24 83.1 22.6 0.0000000
## lin(row) 1 9.5 9.7 0.0116671
However, there is still deviation from an AR1:AR1 process in the variogram for the second model. Instead of a random distribution at each row lag, we see ridges where the variance is higher or lower across different column lags at the same row lag. This suggests the presence of random row effects.
There are also traces of a similar pattern for the columns, but the pattern in the rows is clearer so we will investigate it first by fitting a third model with a random row term, in addition to the linear row term already included.
m3 <- asreml(yield ~ gen + lin(row),
random = ~rep + row,
residual = ~ar1(row):ar1(col),
data = data)## ASReml Version 4.2 30/11/2026 19:56:15
## LogLik Sigma2 DF wall
## 1 -732.1524 30074.69 124 19:56:15
## 2 -726.0563 27937.51 124 19:56:15
## 3 -721.1006 27164.63 124 19:56:15 ( 1 restrained)
## 4 -719.0413 27084.45 124 19:56:15 ( 1 restrained)
## 5 -718.5305 26184.63 124 19:56:15 ( 1 restrained)
## 6 -718.4968 25864.68 124 19:56:15 ( 1 restrained)
## 7 -718.4965 25831.41 124 19:56:15 ( 1 restrained)
variogram(m3)We no longer see the identified patterns in the row lags, suggesting the random row term has improved the model. We can also check the significance of the random row term using the log-likelihood ratio test.
logl_test(m3, rand.terms = c("rep", "row"))## Term LogLRT.pvalue
## 1 rep 1.000
## 2 row 0.002
The p-value of 0.002 suggests that the random row term is clearly significant.
The variogram of the third model is close to an AR1:AR1 process. However, we can still see some signs of a ridge pattern on the column lags. For instance, the variance at a column lag of 1 is notably higher than at a column lag of 2, across most row lags. This pattern suggests the possibility of a random column term, so we will add this to the model and fit model 4.
m4 <- asreml(yield ~ gen + lin(row),
random = ~rep + row + col,
residual = ~ar1(row):ar1(col),
data = data)## ASReml Version 4.2 30/11/2026 19:56:15
## LogLik Sigma2 DF wall
## 1 -731.8597 28440.58 124 19:56:15
## 2 -724.4951 25840.73 124 19:56:15
## 3 -719.0729 25067.17 124 19:56:15
## 4 -716.8311 25084.41 124 19:56:15 ( 1 restrained)
## 5 -716.2114 24318.66 124 19:56:15 ( 1 restrained)
## 6 -716.1670 23981.65 124 19:56:15 ( 1 restrained)
## 7 -716.1663 23946.58 124 19:56:15 ( 1 restrained)
variogram(m4)The variogram now looks very close to what we would expect for an AR1:AR1 process. The semivariance initially increases from zero displacement along both axes, and then oscillates around a fairly constant level.
We can also check the significance of the row and column random terms together with the log-likelihood ratio test.
logl_test(m4, rand.terms = c("rep", "row", "col"))## Term LogLRT.pvalue
## 1 rep 1.000
## 2 row 0.010
## 3 col 0.031
The log-likelihood ratio test suggests that both our row and column random effects are clearly significant. The column effect is not as significant as the row effect, which makes sense given it was not as clear on the variogram.
We can also check the fixed effects, to make sure that the linear row term is still significant with the additional random effects in the model.
wald(m4, denDF = 'default')$Wald## ASReml Version 4.2 30/11/2026 19:56:16
## LogLik Sigma2 DF wall
## 1 -716.1663 23945.45 124 19:56:16
## 2 -716.1663 23945.44 124 19:56:16
## 3 -716.1663 23945.43 124 19:56:16
##
## Wald tests for fixed effects.
## Response: yield
##
## Df denDF F.inc Pr
## (Intercept) 1 17.0 4009.0 0.0000000
## gen 24 77.4 21.4 0.0000000
## lin(row) 1 13.9 10.5 0.0059781
Another way to assess whether the additional random terms improved the model is using the AIC, which is included in the summary() of an ASReml model, for instance:
summary(m2)$aic## [1] 1453.659
## attr(,"parameters")
## [1] 4
Note that ASReml fits models by residual maximum likelihood (REML), and REML log-likelihoods, and hence AIC values, are only comparable between models with the same fixed effects. Model 1 is therefore not included in the comparison below, because it does not have the lin(row) fixed effect. The evidence for adding that term comes from the Wald test above instead.
| Model | Spatial Terms | AIC |
|---|---|---|
| 2 | lin(row) | 1453.659 |
| 3 | lin(row), row | 1446.993 |
| 4 | lin(row), row, col | 1444.333 |
The AIC for models 2 to 4 is displayed in ?@tbl-aic. We can see that the AIC has decreased at each step where additional random terms were included, indicating a better fitting model.
While we have not included this above, in order to be concise, the standard assumptions of linear regression should also be checked at each stage of model fitting. We can use the resplot() function from biometryassist to check the QQ-plot of the residuals and residuals vs fitted plot to assess the assumptions of normality, linearity and homoscedasticity.
Below, we generate the residual plots for our initial model and our final model.
# Assumption plots for initial model
resplot(m1)# Assumption plots for final model
resplot(m4)In this case, the assumptions are sufficiently satisfied for both the initial and final models.
In situations where there is a two-dimensional spatial variance structure fitted separately across a third dimension (for instance, using dsum in ASReml), the variogram function will produce a separate variogram for every level of this dimension. The default behaviour of the function is to print each variogram as a separate plot, but up to six can be combined on a single plot with the argument onepage = TRUE.
This feature is particularly useful in field trials where the response was measured across the same number of rows and columns at different sites, dates or soil depths. In such cases, it is possible that there are different spatial trends on different row-column grids, which need to be observed separately.
For example, in the multi-environment trial of corn from (Besag and Higdon 1999), sourced from the agridat package, yield was measured at six sites, each in a different county in North Carolina, each with 18 rows and 11 columns. With a model fitting a separate AR1:AR1 process at each site, we can visualise the variogram for each site as follows.
# Load the data
besag.df <- agridat::besag.met
besag.df <- besag.df |>
mutate(across(c(row, col), factor)) |>
arrange(county, col, row)
# Fit the model
besag.asr <- asreml(
fixed = yield ~ county,
random = ~ gen:county,
residual = ~ dsum( ~ ar1(col):ar1(row)|county),
trace = FALSE,
data = besag.df
)
# Plot the variograms
variogram(besag.asr, onepage = TRUE)## [[1]]
Note that you will need a display window large enough to see all six variograms clearly, such as by making the plots pane in RStudio a full-screen window.
The colour scheme of the variogram plot can be changed from the default using the palette argument. The potential options are:
"viridis""colo(u)r blind" (both equivalent to "viridis")"magma""inferno""plasma""cividis""rocket""mako""turbo""Spectral"For example, we can use the colour blind friendly viridis colour scheme to display the variogram for the final model for to the Slate Hall wheat trial data from Section 3 above.
variogram(m4, palette = "viridis")These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.