Search code examples
rdplyrmetafor

Using dplyr to run rma() on multiple subsets


I want to run a subgroup meta-analysis within metafor package. The simplest way to do it is:

model.s.1 <- rma(yi=ES, vi=Va, data=dataset, method="DL", subset=S=="S_Level1")
model.s.2 <- rma(yi=ES, vi=Va, data=dataset, method="DL", subset=S=="S_Level2")
...
model.s.n <- rma(yi=ES, vi=Va, data=dataset, method="DL", subset=S=="S_Leveln")

However, it's very confusing to do it by hand if a factor for subgroups has multiple levels. I tried to use dplyr to solve this and extract simply coefficients for all subgroups:

Dataset %>%
   mutate(S=as.factor(S)) %>%
   group_by(S) %>%
   summarize(Coeff=coef.rma(rma(yi=ES, vi=Va, method="DL", data=.)))

But the result looked like this:

   S          Coeff
   <fct>       <dbl>
 1 hmdb        0.114
 2 HMDB0000123 0.114
 3 HMDB0000148 0.114
 4 HMDB0000158 0.114
 5 HMDB0000159 0.114
 6 HMDB0000161 0.114
 7 HMDB0000162 0.114
 8 HMDB0000167 0.114
 9 HMDB0000168 0.114
10 HMDB0000172 0.114
# ... with 14 more rows

It seems that the rma function omits the group_by and calculates the pooled effect for the whole dataset each time. What might be the cause? Are there any alternatives for such approach?


Solution

  • We may do a group_split and then loop through the list elements with map

    library(tidyverse)
    Dataset %>%
       group_split(S= factor(S)) %>%
       map_dfr(~ .x %>% 
                summarise(S = first(S), Coeff=coef.rma(rma(yi=ES, 
                              vi=Va, method="DL", data=.))))