Hi everyone,
I'm trying to implement a geostatistical model using RTMB, so in other words, I'm rewriting sdmTMB models in RTMB. I'm encountering an issue when incorporating anisotropy: creating the precision matrix Q needed for anisotropy causes issues with advectors, in that there are no methods yet for assigning a RHS advector value into a LHS sparse matrix via indexing:
> mat <- Matrix::Matrix(0, nrow = 2 , ncol = 2)
> mat[1,1] <- advector(2)
Error in .local(x, i, j, ..., value) :
not-yet-implemented 'Matrix[<-' method
To highlight this issue in relation to geostatistical precision matrices, I've written a short-ish MWE that helps with understanding the barrier with implementing anisotropy in RTMB:
It seems to me there are a few ways of potentially resolving this issue:
1. Implement 'Matrix[<-' method for advectors.
2. Compile R_inla.hpp as R objects and use those Q SPDE functions instead of R ones.
3. Move away from using sparse matrices for the precision matrix to a simpler "base" matrix (`advector`). I explored using this but encountered other errors along the way, and I'm not sure if it would be a fruitful avenue.
4. Calling `RTMB::getValues` within `Q_spde_aniso()` to remove RHS advector and avoid matrix assignment issue, for example replacing line 72 of the MWE with:
G1_aniso[TV[i, 2] , TV[i, 1] ] <- G1_aniso[TV[i, 2] , TV[i, 1] ] + RTMB:::getValues(Gtmp[i, 1, 2])
and so on until line 80. However, this is likely to mess up with the tape and is probably not advisable, and also throws an error ("Not compatible with requested type: [type=language; target=double]") when `Q_spde_aniso()`returns the final object.
I'm unsure how to proceed from here, so any thoughts and suggestions are most welcome!
Thanks,
Seb