I am planning to set up a spatial conditional autoregressive (CAR) term in brms. The problem is that I cannot manage to construct an adjacency matrix of locations because my dataset is very large (700K). Do you know a way to construct that matrix with such a large dataset?

Could you expand a bit about the locations in your data? Are they discrete areal units (e.g., districts, counties) or something more continuous like latitude longitude? Usually adjacency matrices are developed for areal units, and I’d be surprised if there are 700K of those.

Continuous data, longitude latitude! I actually have a map with a grid, where every cell has a different value of something I measured through projections.

If that’s a regular raster grid, I have some stuff here that might work for you: geostan/R/raster-analysis.R at main · ConnorDonegan/geostan · GitHub

especially this `dims_to_W_elements`

function, it returns a sparse matrix

`#' given row and column dimensions, returns elements of row-standardized W matrix for regular (rectangular) tessellation with Rook adjacency dims_to_W_elements <- function(row = 100, col = 100) { ...`

You could also just use the `prep_car_data2`

function as in

```
library(geostan)
row = number_of_rows
col = number_of_columns
car_list <- prep_car_data2(row = row, col = col)
adjacency <- car_list$C
```

Then convert it to a binary (sparse) matrix if needed.

There’s a simple illustration here using geostan for raster regression: Raster regression • geostan