paul-buerkner / brms

brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
https://paul-buerkner.github.io/brms/
GNU General Public License v2.0
1.25k stars 177 forks source link

Define new SPDE smooth class #1186

Closed lionel68 closed 5 months ago

lionel68 commented 3 years ago

Hello Paul,

As mentioned in this thread https://discourse.mc-stan.org/t/spde-models-in-brms/17346 I think that it would be possible to fit spatial effects in brms using SPDE via smooths s() using code developed from Miller et al https://link.springer.com/article/10.1007/s13253-019-00377-z.

The idea would be to allow for a new type of smooths:

y ~ s(lon, lat, bs = "spde", xt = mesh)

With the penalty matrix (Zs** in Stancode generated by brms) that would be derived internally like:

mgcv::PredictMat(mgcv::smooth.construct(sm_term, data), data = data)

I am happy to get my hand dirty trying to implement this if you think this would make sense and would fit to brms.

paul-buerkner commented 3 years ago

Is the spde spline basis already implemented in mgcv? If yes, what stops it from currently working with brms already?

lionel68 commented 3 years ago

I am not sure what is meant by implemented in mgcv, it is not in the package itself but in their paper Miller et al provide a smooth.construct function to fit the spde spline using mgcv. For instance, after loading the smooth.construct.spde.smooth.spec function in the workspace, this works in mgcv:

mod <- gam(y ~ s(lon, lat, bs = "spde", xt = mesh), data)

But not in brms:

mod_brms <- brm(y ~ s(lon, lat, bs = "spde", xt = mesh), data)

Error in smooth2random.mgcv.smooth(sm, names(data), type = 2) : 
  Can not convert this smooth class to a random effect
paul-buerkner commented 3 years ago

Ok. So what is missing is the smooth2random method (not sure about the exact name for this right now) to change the parameterization to a "random effects" parameterization. This would make the method work with the gamm function of mgcv as well as with brms. I don't know if this spline basis emits such a parameterization. Is something like this discussed in the paper or their R code?

lionel68 commented 3 years ago

Hmm I am not sure if the spline basis returned by the smooth constructor functions in the article correspond to a "random effect" parametrization. I cannot find reference to random effect parametrization of the smooths in the article and in the code. But when I try on some simulated and some toy example it looks like using naively

mgcv::PredictMat(mgcv::smooth.construct(sm_term, data), data = data)

as the spline basis (the Zs matrix), works pretty fine. The math is however a bit beyond me but because the Miller article says that SPDE = smooth and because we have smooth = random effect, I thought that SPDE = random effect could work.

paul-buerkner commented 3 years ago

The latter may or may not be true. I cannot tell unfortunately. You would have to ask the authors of this paper for details on the parameterization and if and how it would work with random effects parameterization in gamm.

paul-buerkner commented 5 months ago

I will close this issue for now to clean up the issue tracker a bit since I don't this this has high priority.