Sparse adjacency matrix for CAR?

Hi,

I want to use brms to fit a model on data from spatial polygons. I want to use CAR to account for autocorrelation in the response. Unfortunately my adjacency matrix is too large to fit into memory, that is I can compute it but brms returns the error (with traceback) when I fit the model.

cannot allocate vector of size 20.8 Gb

  1. └─brms::brm(…)
  2. └─brms:::validate_data2(…)
  3. └─brms:::validate_car_matrix(get_from_data2(M, data2))
    
  4.   └─Matrix::Matrix(M, sparse = TRUE)
    
  5.     └─methods::as(data, if (sparse) "CsparseMatrix" else "unpackedMatrix")
    
  6.       └─Matrix (local) asMethod(object)
    
  7.         └─Matrix:::.m2sparse.checking(from, ".", "C")
    
  8.           ├─base::isSymmetric(from, ...)
    
  9.           ├─base::isSymmetric(from, ...)
    
  10.           └─base::isSymmetric.matrix(from, ...)
    
  11.             ├─base::all.equal(object, t(object), tolerance = tol, ...)
    
  12.             └─base::all.equal.numeric(object, t(object), tolerance = tol, ...)
    
  13.               └─base::as.vector(current)
    

Is there a way to use a sparse matrix representation for the adjacency matrix in brms? From the traceback it seems like brms already transforms the matrix into a sparse one but then the object should be smaller.

Thanks!
Jonathan

I’m not familiar with brms, but it could be helpful to share some code. More specifically, is your matrix simply something you set up and pass as an argument? I am assuming it is not, but otherwise you could make it as sparse as you need it.

In Stan, for instance, you could just use the diagonal of a matrix as a vector, and set up a diagonal matrix from it whenever you need it.

Without more information, I’m afraid I cannot be more helpful.