wviechtb / metafor

A meta-analysis package for R
https://www.metafor-project.org
226 stars 52 forks source link

Support for penalized splines/GAMs #51

Open bwiernik opened 2 years ago

bwiernik commented 2 years ago

Classification:

Feature Request

Summary

I'm interested in being able to fit meta-analysis models with penalized splines, ala mgcv::gam(). For example, I have a meta-analytic dataset where we want to model change in effect sizes over time using thin plate splines, with Country random effects in both intercepts and the year splines.

Here is how I might fit such a model with brms by using mgcv's tensor product spline implementation.

library(brms)
m2 <- brm(
  bf(yi | se(sqrt(vi)) ~ t2(year2004, Country, bs = c("tp", "re")) + 
       (1  | sample_id)
  ),
  data = filter(dat_mnsd2, param == "mn"),
  backend = "cmdstanr",
  cores = parallel::detectCores(),
  threads = 2
)

Is there any possibility that these sorts of penalized splines might be supported in metafor?

wviechtb commented 2 years ago

I will have to find time to figure out how exactly thin plate splines are incorporated into the model.

An alternative might be to use restricted cubic splines, as illustrated here:

https://www.metafor-project.org/doku.php/tips:non_linear_meta_regression

The nice thing is that this just requires creating transformations of the original predictor variable, which can then be added to the model as additional predictors in the usual manner (and one could add random slopes for them as well). If this is how these thin plate splines work, then it would be the same principle.

bwiernik commented 2 years ago

The way the splines are implemented is with a design matrix and penalty matrix. The major benefit over restricted cubic splines is that I could include inner/outer-style random effects for the splines via a tensor product of splines for the year and the grouping factor.

wviechtb commented 2 years ago

I would have to really study the details, but I suspect that this will not currently be possible with the way things work in metafor.

wviechtb commented 2 years ago

I happened to have taken Gavin Simpson's excellent course on GAMs in the meantime. So, in principle, one can do the same thing as in the example linked to above with restricted cubic splines. I have expanded the example to illustrate this with a thin plate spline model. However, rma() or rma.mv() do not currently include the necessary functionality for imposing penalties on the model matrix. However, the way this is done in brm() and gamm4() is to add the smooths (on top of a linear term) as random effects and the variance component then controls the wiggliness. That might be possible with rma.mv().