0

Using R and the Metafor package, I'd like to know how I can run many random effect models on all combinations of all of my moderators. I know I can pre-select any combination of moderators to include manually, though I'm unsure how to loop over all of my moderators so that I can run the rma function as many times as there are moderator combinations (I assume with a loop).

Example of rma function with all 7 moderators

mod <- rma(smd, se, mods=~ A + B + C + D + E + F + G, data=dat)

I can't manually keep running the function over and over adjusting moderator combinations as that would be far too time consuming. Is there such a way to automate this process? My moderators are columns (variables) in a 'moderator' data-frame.

I assume I can loop over my mod variables changing them on each iteration, though I'm not sure how best to implement this. I have tried using a for loop to iterate over the moderators, updating the rma function accordingly but the function doesn't seem accept this. Here is a basic example of what I tried, this is just to see if I can insert one moderator at a time into the function:

for (i in colnames(mods1)){
  rma(smd, sei=se, mods=~ mods1[i], data=dat)
}

>> invalid type (list) for variable 'mods1[i]'

Any help is appreciated.

Beatdown
  • 187
  • 2
  • 7
  • 20
  • This can be easily done using the glmulti package. See here: https://www.metafor-project.org/doku.php/tips:model_selection_with_glmulti_and_mumin – Wolfgang Aug 12 '22 at 17:14
  • Yes, it can. I really like that package but it doesn't give the I^2 statistic, which is what I'm interested in. – Beatdown Aug 12 '22 at 20:41
  • 1
    Every model fitted is saved. So you can extract the I^2 statistic for every fitted model. – Wolfgang Aug 13 '22 at 14:33
  • hmm okay, I will look into that. I was told it did not calculate the I^2 at all, how strange. Thanks – Beatdown Aug 13 '22 at 17:27
  • Can you tell me how to extract the I^2. I've fitted my models but I don't see it in any of the outputs. My models include moderators, I'm not sure if that changes things – Beatdown Aug 13 '22 at 17:38
  • In the example I linked to ``sapply(res@objects, function(x) x$I2)`` gives you I^2 for every model. – Wolfgang Aug 13 '22 at 20:35
  • I have tried that and the result of every fitted model is "NULL" – Beatdown Aug 13 '22 at 21:06
  • Okay I think my issue might have to do with the fact that I have to use gmulti with "x <- do.call("glmulti", list(.." unlike what is shown on the metafor page. See here: "https://stackoverflow.com/questions/24699673/error-object-not-found-when-calling-glmulti-within-a-function" – Beatdown Aug 13 '22 at 21:33
  • Okay, I fixed it so that I'm no longer using "do.call" etc. Everything is now working as desired, thanks for your help! – Beatdown Aug 13 '22 at 22:53
  • Do you know if glmulti can provide the adjusted I^2, to account for number of moderators? – Beatdown Aug 17 '22 at 09:50
  • 1
    (1) Just to clarify a misunderstanding: glmulti doesn't know anything about meta-analysis, I^2, and so on. It provides a way for easily fitting many models by swapping in/put predictors. `res@objects` contains all the models that were fitted. Those model objects contain whatever is stored by default inside such model objects. If those are `rma()` models, then they contain an element called `I2` for the I^2 values. (2) For a description of what I^2 means in a meta-regression model, see: https://wviechtb.github.io/metafor/reference/print.rma.html#details-1 – Wolfgang Aug 17 '22 at 14:27
  • Ah, this makes a lot of sense. Thank you. – Beatdown Aug 17 '22 at 14:55

0 Answers0