Non-linear regression with spatial random effects


I am a Greta novice, looking for advice on how to use the package to run a non-linear regression model with spatial random effects. I have had some success running my desired model in STAN using ‘brm()’, but am hoping Greta offers a more time effective alternative. In STAN the model took the form:

brm(y_var ~ x_var + I(x_var^2) + sar(Distmat), family = 'gaussian', data = data)

With sar() offering a spatial autoregressive variance structure based on the distance matrix Distmat. For implementing this model using Greta, I am however stumped with how to translate this random effect component, and how to then use this error component in defining the distribution of my dependent variable?

I have included a section of code below, to help clarify my query. Briefly, the ‘data’ is a small sample of a large dataset comparing global patterns in two variables (for brevity referred to here as x and y). Each xy data point is associated with a GPS coordinate (Lat & Lon) from which it is possible to generate a distance matrix, which I would like to be able to build into my model under the assumption that data points clustered closer together are likely to possess similar values for both x and y. From my limited experience with Greta, I believe that I should use this distance matrix to parameterise the variance component of the model (sigma in the code below)? However, this approach results in a matrix array which is then incompatible with the scalar and vector structures of my remaining model parameters, and dependent variable distribution. Is this an issue that can be solved by implementing my spatial random effects using the package?

Any advice would be most appreciated. Thanks in advance!

My Code:

#load required packages

# Load dataset (represents a small sample of much larger dataset)
data <- data.frame(Lon = c(3121951.802,-7201570.19918,-13843810.1918,-698458.161,
                   Lat = c(5387011.84712,-4523614.1588,-1278790.1201,5084071.812,
                   x_var = c(2.319, 2.091, 2.221, 2.095,
                             2.150, 2.133, 2.189, 2.291,
                             2.308, 2.076, 2.067, 2.157,
                             2.103, 2.181, 2.151, 2.377,
                             2.148, 2.377, 2.278, 2.349),
                   y_var = c(-5.336, -2.931, -2.417, -1.705,
                             -4.435, -2.561, -3.298, -4.443,
                             -3.611, -4.371, -3.396, -3.053,
                             -2.567, -2.210, -2.756, -4.717,
                             -3.141, -4.837, -3.045, -3.068))

# Generate spatial distance matrix
coords <- suppressWarnings(sp::spTransform(SpatialPoints(data[,c('Lon','Lat')], sp::CRS('+proj=moll')), sp::CRS('+proj=longlat'))) # this line of code involves the translation from Mollweide to WGS184 projection.
DistMat <- 1/geodist(coords@coords, measure = 'geodesic') # invert distance matrix - values closer to 1 denote closer proximity. 
DistMat[sapply(DistMat, is.infinite)] <- 1 # reassign matrix diagonal.

# set up greta variables
x <- as_data(data$x_var)
y <- as_data(data$y_var)

# Model parameters (no priors)
int <- variable()
beta1 <- variable() 
beta2 <- variable() # polynomial slope function

# Spatial random effects. 
sigma <- ? # I am pressuming that this would require some sort of Gaussian decay distribution utilising the distance matrix above to outline the neighbouring context of the xy variables e.g. exp((-DistMat^2)/(2*(Lengthscale^2)))
distribution(y) <- ?

# setup non-linear model (a relatively simple polynomial model)
mod <- int + (x * beta1) + ((x^2) * beta2)

# implement greta model
model_greta <- model(int, beta1, beta2, sigma)
draws_greta <- mcmc(model_greta, warmup = 1000,  n_samples = 1000)