Error with CAR model in BRMS


I am trying to make a heirarchical model using a spatial autocorrelation term (CAR). I have been having an issue with actually running the model. I get the errors: “Error: Row names of ‘M’ for CAR terms do not match the names of the grouping levels.” when I run the code (below). Any insight on to how to fix this would be amazing. The row names match up with the data and data2 arguments.

Thank you so much,


dist.mat <- as.matrix(dist(cbind($Latitude,$Longitude))) 
W <- matrix(dist.mat, nrow = nrow(dist.mat), ncol = ncol(dist.mat))
W[W < 100] <- 1 #Distances over 100
W[W > 100] <- 0  #Distances under 100
rownames(W) <-$FISHNET_ID

barley.icar<-brm(mean_barle ~ Barley_Fertilizer +car(type="icar", M=W,gr = FISHNET_ID ),data =, data2 =list(W=distance),family="gaussian", warmup = 1000, iter = 5000,thin = 5,cores = 4, chains = 4, seed = 10,control = list(max_treedepth = 12))

OS: Linus

  • Operating System:
  • brms Version: 2.16

Hi! I think that you should run$FISHNET_ID =$FISHNET_ID instead of rownames(<$FISHNET_ID since the id(s) must be one of the variables in the data, not the rownames.

Unfotunatly get the same error…

Don’t know if you ever found a solution for this, but in your model should data2 = list(W = distance) be data2 = list(W = W) (W being what you called your matrix)?