0

I am trying to subset data within lapply for different columns to produce repeated single arm meta-analysis using metaprop command for many outcomes in 1 step as I have a giant number of outcomes.

Some outcomes have empty cells, so I need to properly subset data within lapply

Here is my data and code:

############################### Data sample  #####################################
data<- read.table(text="
studlab Number.of.patients  Procedure   Mortality   Post.op.stroke  
Lee2015 15  SPT 0   0
Wang2019    44  SPT     1
Nachira2018 12  SPT 1   1
Zhang2020   20  SPT 1   1
Lee2017 48  SPT 0   1
Lv2018  10  SPT 0   0
Weiping2018 28  SPT 0   0
Liu2021 60  SPM 0   
Gan2020 56  SPM     
Ye2021  105 SPM 1   1
Takeda2021  12  SPM     
Yin2020 22  SPM     0
Gan2020 28  SPM     
Egberts2019 4   SPM 1   1
Wang2019    80  SPM     0
Fujiwara2017    60  SPM 0   1
Mori2017    7   SPM 0   0
Parker2011  8   SPM 0   1
Zhu2021 19  SPM 0   1", header=T, sep="\t")

############################ Same data with dput command ####################
dput(data)
structure(list(studlab = c("Lee2015", "Wang2019", "Nachira2018", 
"Zhang2020", "Lee2017", "Lv2018", "Weiping2018", "Liu2021", "Gan2020", 
"Ye2021", "Takeda2021", "Yin2020", "Gan2020", "Egberts2019", 
"Wang2019", "Fujiwara2017", "Mori2017", "Parker2011", "Zhu2021"
), Number.of.patients = c(15L, 44L, 12L, 20L, 48L, 10L, 28L, 
60L, 56L, 105L, 12L, 22L, 28L, 4L, 80L, 60L, 7L, 8L, 19L), Procedure = c("SPT", 
"SPT", "SPT", "SPT", "SPT", "SPT", "SPT", "SPM", "SPM", "SPM", 
"SPM", "SPM", "SPM", "SPM", "SPM", "SPM", "SPM", "SPM", "SPM"
), Mortality = c(0L, NA, 1L, 1L, 0L, 0L, 0L, 0L, NA, 1L, NA, 
NA, NA, 1L, NA, 0L, 0L, 0L, 0L), Post.op.stroke = c(0L, 1L, 1L, 
1L, 1L, 0L, 0L, NA, NA, 1L, NA, 0L, NA, 1L, 0L, 1L, 0L, 1L, 1L
)), class = "data.frame", row.names = c(NA, -19L))

############################### Used code  #####################################
library(tidyr); library(ggplot2); library(meta); library(metafor)
lapply(names(data)[4:5], function(columntoplot){
 ## To subset based on the outcome column ## 
  df <-subset( data, data[[columntoplot ]] >=0 )  ## NOT WORKING PROPERLY
  
 mp<-metaprop(columntoplot,Number.of.patients,  data=df, studlab=studlab,  method = "Inverse",method.tau = "DL");mp
  
 mp2<- update (mp, byvar=Procedure);mp2
 pdf(filename = paste0(graphname, ".pdf"), width = 20, height = 20)
 forest (mp );   forest (mp2 )
  dev.off()
  })
#################################################################################

TylerH
  • 20,799
  • 66
  • 75
  • 101
Mohamed Rahouma
  • 1,084
  • 9
  • 20
  • 1
    Your sample is in an awkward format and in its current form gives a one-column `data.frame`. Can you please fix this and provide sample data with `dput`. – Maurits Evers Jul 24 '22 at 22:59
  • PS. There are more issues with your sample data. For example, row 2 (Wang 2019) has only four entries instead of five, row 9 (Gan 2020) has only three entries instead of five. Please provide sample data in a reproducible format. – Maurits Evers Jul 24 '22 at 23:07
  • @MauritsEvers Thx for your time and efforts. I edited my question based on your advice. row 9 (Gan 2020) has only three entries instead of five as this is a real sample of my data and I will need to subset it to include those with actual events even zero events but not NA. Appreciate your guidance. Upvoted. – Mohamed Rahouma Jul 25 '22 at 01:19

1 Answers1

1

There are a couple of issues with your code.

  1. In metaprop, the event and n arguments must be symbols (not character strings) referring to columns in data.
  2. As you correctly identified, the subset command is syntactically incorrect.
  3. The filename argument inside pdf is file = , not filename = .
  4. I strongly recommend using a linter to ensure consistent code formatting & indentation. Or at the very least, adopt & stick to a style guide to increase the readability of your code.

As to your question, the following seems to work.

# Variables of interest
vars <- names(data)[4:5]

# Reshape `data` from wide to long for the variables of interest
df <- data %>% pivot_longer(all_of(vars))

# Since the loop is not returning anything and only generates plots, 
# use `purrr::walk` to loop through all variables of interest.
vars %>%
    walk(function(var) {
        # Meta-analysis        
        mp <- metaprop(
            event = value, 
            n = Number.of.patients,
            studlab = studlab,
            data = df %>% filter(name == var, !is.na(value)),
            method = "Inverse",
            method.tau = "DL")
        mp2 <- update(mp, byvar = Procedure)
        
        #Plot
        pdf(file = paste0(var, ".pdf"), width = 20, height = 20)
        forest(mp)
        forest(mp2)
        dev.off()
    })
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • @Mauritz Evers Thx a lot for your precious input. I tried your code but I got 2 errors from my side (`Error in match.arg(method) : object 'value' not found` which was fixed by using `attach(df)` but I got another error `Error in match.arg(method) : 'arg' must be NULL or a character vector` although I checked using str(df). Any advice will be greatly appreciated. Upvoted. – Mohamed Rahouma Jul 25 '22 at 02:25
  • 1
    Hi @MohamedRahouma. [Don't use `attach`](https://stackoverflow.com/questions/10067680/why-is-it-not-advisable-to-use-attach-in-r-and-what-should-i-use-instead) (I'm not aware of any good use-case for `attach`). The code above works without an error on my side, so there is something else going on on your side. Please restart R from a clean environment. Then try running my code again. Also make sure that you have a recent enough version of R and packages. This is tested & verified on `R_4.1.2`, `metafor_3.4-0`, `metadat_1.2-0`, `tidyverse_1.3.1`. – Maurits Evers Jul 25 '22 at 02:30
  • @Mauritz Evers Thx a lot. It worked now after I used `tidyverse` and `purrr::walk`. Upvoted and accepted as answer. I would greatly appreciate if you can amend the code to help me getting the results of `mp2` i.e. subgroup analysis in a separate word file or in the same resulting pdf file. Thx again and nice to get in contact with an expert as you. – Mohamed Rahouma Jul 25 '22 at 02:41
  • Glad it worked Mohamed. I'm definitely no expert with meta-analyses ;-) Not sure what you mean by "getting the results of `mp2`". I simply copied what you had in your original code and assumed that was what you were after. – Maurits Evers Jul 25 '22 at 02:47
  • @Mauritz Evers Thx a lot. I will try to figure such additional request on my side. Appreciate all your efforts. – Mohamed Rahouma Jul 25 '22 at 14:33