Error with CAR model in BRMS

Hello,

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,

Justin

dist.mat <- as.matrix(dist(cbind(barley.final$Latitude, barley.final$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) <- barley.final$FISHNET_ID
rownames(barley.final@data)<-barley.final$FISHNET_ID

barley.icar<-brm(mean_barle ~ Barley_Fertilizer +car(type="icar", M=W,gr = FISHNET_ID ),data = barley.final@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 barley.final@data$FISHNET_ID = barley.final$FISHNET_ID instead of rownames(barley.final@data)<-barley.final$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)?