0

I have run several (17) meta-analyses (identified by specific names) and I need to extract the models' outputs into one single table, as well as add a column with the name of each name. I have done it manually, but I was wondering if I could build a loop to do so.

I'm attaching the first three of the 17 analyses, the "names" being "cent", "dist", and "sqrs"

#meta-analyses
res_cent<-rma.mv(yi, vi, mods = ~ factor(drug)-1, random = list(~ 1 | publication_id,~ 1 | strain_def), 
             data = SR_meta,subset=(SR_meta$measure=="cent"))
res_dist<-rma.mv(yi, vi, mods = ~ factor(drug)-1, random = list(~ 1 | publication_id,~ 1 | strain_def), 
             data = SR_meta,subset=(SR_meta$measure=="dist"))
res_sqrs<-rma.mv(yi, vi, mods = ~ factor(drug)-1, random = list(~ 1 | publication_id,~ 1 | strain_def), 
             data = SR_meta,subset=(SR_meta$measure=="sqrs"))

#Creating list for model output - cent
list_cent<-coef(summary(res_cent))
list_cent<-setNames(cbind(rownames(list_cent), list_cent, row.names = NULL), 
         c("Drug", "Estimate", "se","zval","p-value","CI_l","CI_u"))

df_cent <- list_cent[ -c(3,4) ]
df_cent$Drug<-gsub("factor*","",df_cent$Drug)
df_cent$Drug<-gsub("drug*","",df_cent$Drug)
df_cent$Drug<-gsub("[[:punct:]]","",df_cent$Drug)

n_cent<-plyr::count(cent_sum2, vars = "drug")
names(n_cent)[names(n_cent) == "freq"] <- "n_cent"

df_cent<-cbind(df_cent,n_cent[2])
##same thing can be repeated for the other two measures "dist", and "sqrs". 

The output is a data frame that contains the name of the drugs used as factors in the meta-analyses, their estimated effect sizes, p-values, confidence intervals, and how many measures we have per factor (n). I want to compile all of these outputs in a table, (at the end of the code called "matrix_ps") and add a column with the name of the measures. I have done all the steps manually (below) but it looks extremely inefficient. Is there a way to create a loop to do this, in which the all the names of the measures are changed an then outcome is appended?

Something like measures<-c("cent","dist","sqrs") for(i in measures) - not sure how to continue?

matrix_cent<-data.frame(df_cent$Drug,list_cent$`p-value`,df_cent$n_cent,df_cent$Estimate,df_cent$CI_l,df_cent$CI_u)
matrix_dist<-data.frame(df_dist$Drug,list_dist$`p-value`,df_dist$n_dist,df_dist$Estimate,df_dist$CI_l,df_dist$CI_u)
matrix_sqrs<-data.frame(df_sqrs$Drug,list_sqrs$`p-value`,df_sqrs$n_sqrs,df_sqrs$Estimate,df_sqrs$CI_l,df_sqrs$CI_u)

matrix_cent$measure<-"cent"
matrix_dist$measure<-"dist"
matrix_sqrs$measure<-"sqrs"

matrix_cent<-matrix_cent%>% rename(drug=df_cent.Drug,measure=measure,p=list_cent..p.value.,n=df_cent.n_cent,estimate=df_cent.Estimate,ci_low=df_cent.CI_l,ci_up=df_cent.CI_u)
matrix_dist<-matrix_dist%>% rename(drug=df_dist.Drug,measure=measure,p=list_dist..p.value.,n=df_dist.n_dist,estimate=df_dist.Estimate,ci_low=df_dist.CI_l,ci_up=df_dist.CI_u)
matrix_sqrs<-matrix_sqrs%>% rename(drug=df_sqrs.Drug,measure=measure,p=list_sqrs..p.value.,n=df_sqrs.n_sqrs,estimate=df_sqrs.Estimate,ci_low=df_sqrs.CI_l,ci_up=df_sqrs.CI_u)

matrix_ps<-rbind(matrix_cent,matrix_dist,matrix_rear,matrix_sqrs,matrix_toa,matrix_eca,matrix_eoa,matrix_trans,matrix_dark,matrix_light,matrix_stps,matrix_rrs,matrix_time,matrix_toc,matrix_cross,matrix_hd,matrix_lat)
mari_itabe
  • 31
  • 2
  • Please submit a [Minimal Replicable Example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Vincent Jan 18 '21 at 13:25

1 Answers1

0

We don't have your data but you can put all your code in a function :

get_result <- function(x, y) {

  list_cent<-coef(summary(x))
  list_cent<-setNames(cbind(rownames(list_cent), list_cent, row.names = NULL), 
                      c("Drug", "Estimate", "se","zval","p-value","CI_l","CI_u"))
  
  df_cent <- list_cent[ -c(3,4) ]
  df_cent$Drug<-gsub("factor*","",df_cent$Drug)
  df_cent$Drug<-gsub("drug*","",df_cent$Drug)
  df_cent$Drug<-gsub("[[:punct:]]","",df_cent$Drug)
  
  n_cent<-plyr::count(cent_sum2, vars = "drug")
  names(n_cent)[names(n_cent) == "freq"] <- y
  
  df_cent<-cbind(df_cent,n_cent[2])
  return(df_cent)
}

Now assuming all your analyses follow the pattern 'res_' you can do :

library(purrr)

list_models <- mget(ls(pattern = 'res_'))
result <- imap(list_models, get_result) %>% reduce(inner_join)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213