Incorporating Spatial Weights Matrix

Is there any way to incorporate a spatial weights (based on geographic coordinates) matrix into my regression modeling in BRMS? I’d like to test my results for spatial autocorrelation, then, if necessary, explicitly model location in my model…

Cheers, Kellen

check out cor_sar and cor_car

1 Like

Thanks this is intuitive if I’m not mistaken.

cor_sar: I take a spatial weights object and simply select lag or error, then, plug the object in as a parameter in my brm function for the autocor parameter? Ditto for cor_car?

Is there another way to conceptualize the difference between the conditional (CAR) vs. simultaneous (SAR) autoregressive structure? I notice CAR permits grouping of spatial features…

Researching this independently I’m seeing it’s not a BRMS question per se as much as a spatial/conceptual model type question…sorry

Hi Paul, I get the following error when I add my spatial weights matrix to test for autocorrelation:

SAR models are not implemented for family ‘bernoulli’.

spat.log <- brm(status ~ mo(size.catNUM),
data=df.tri, family=bernoulli(),
autocor = cor_lagsar(W),
prior = c(set_prior(“normal(0,1)”, class = “b”)))

I didn’t realize you were using a bernoulli model. In this case, just go for cor_car.

Hello Paul,
Recently I am using brm package to fit spatially correlated data.
Here is the formula and model: <- bf(yield ~ 1 + cnitro + cnitro.sq  + (1+cnitro+cnitro.sq | gridId) + s(x,y),
               sigma ~ (1|gridId))
mod.brm <- brm(, data = mydata,   autocor = cor_car(W),
                 family = gaussian())

where in the formula, I used fixed-term and correlated random term, and x and y are coordinates of the data, and in the model I put autocorrelation matrix W using cor_car.

However, the problem is when I try to fit the model or predict for a new data set, the function predict(mod.brm) itself works okay, but predict(mod.brm, newdata = newdata) returns the following error:

Error: Without a grouping factor, CAR models cannot handle newdata.
In addition: Warning message:
Using CAR terms without a grouping factor is deprecated. Please use argument ‘gr’ even if each observation represents its own location.

It is appreciated if you could help me on this issue.
Thank you.

The error message tells you that you need to specify the gr argument of cor_car, so that new data can be related to the original data, which is required for predictions of car models.

1 Like

Hi Paul,

Thank you for replying my message.
I tried to use gr argument in the model, but it still does not work. <- bf(yield ~ 1 + cnitro + cnitro.sq  + (1+cnitro+cnitro.sq | gr(gridId)) + s(x,y),
           sigma ~ (1| gr(gridId)))

same error.
If I put gr in cor_car, it says Error in cor_car(W, gr = gridId) : unused argument.

Thank you in advance for your time.

I am sorry. I overlooked that you use an old brms version with the old way of specifying autocor structures. You need to specify the grouping variable in the formula argument of cor_car as described in the documentation.

Hi Paul, thank you for the prompt response. I have updated the brms to version 2.12.8, and I missed that

the row names of W have to match the levels of the grouping factor

which is described in the cor_car document. I also missed the formula in cor_car function. Then I used
cor_car(W,formula = ~ 1| gridId)
then the model works well.

Thank you very much for your help. I hope this post could help other users who might have similar problems.

1 Like