For loops to make leave-one-out analysis with netmeta

Viewed 28

I'm doing a network metanalysis of 29 studies using the netmeta package with R and I now have to do the leave-one-out analysis. I was thus wondering whether there is a way to use for loops to gain the results of a such method in order not to do it by manually excluding one trial at a time.

I came up with this:

 for (i in 1:29){
  NMA_DB_L<-NMA_DB[-i,]
  yi_All_cause<-summary(escalc(ai= NMA_DB_L$All_Cause_d_C, bi=NMA_DB_L$PTS_All_Cause_d_C - NMA_DB_L$All_Cause_d_C,
                               ci= NMA_DB_L$All_Cause_d_I, di= NMA_DB_L$PTS_All_Cause_d_I - NMA_DB_L$All_Cause_d_I, 
                               measure = "RR"))[,"yi"]
  
 sei_All_cause<-summary(escalc(ai= NMA_DB_L$All_Cause_d_C, bi=NMA_DB_L$PTS_All_Cause_d_C - NMA_DB_L$All_Cause_d_C,
                               ci= NMA_DB_L$All_Cause_d_I, di= NMA_DB_L$PTS_All_Cause_d_I - NMA_DB_L$All_Cause_d_I, 
                               measure = "RR"))[,"sei"]
  
  netmeta(TE=yi_All_cause, seTE =  sei_All_cause, treat1 = NMA_DB_L$Arm_1, treat2 = NMA_DB_L$INT, sm="RR",
          studlab = NMA_DB_L$Study, reference.group = "Standard_DAPT")
}

and it seems to work properly, but I cannot find a way to save the results of each analysis without one of the trials.

Does anyone have an idea of how to do so?

2 Answers

Why not save the outputs of netmeta function into a list?

# Create list of length 29
net_results <- vector('list', 29)

for (i in 1:29) {
    NMA_DB_L<-NMA_DB[-i,]

    ...

    net <- netmeta(TE=yi_All_cause, seTE =  sei_All_cause,
            treat1 = NMA_DB_L$Arm_1, treat2 = NMA_DB_L$INT, sm="RR",
            studlab = NMA_DB_L$Study, reference.group = "Standard_DAPT")
    net_results[[i]] <- net
}

You can then access results of the specific run with net_results[[1]] etc.

R lists can in general contain any type of element which makes it a suitable structure for this type of problems.

Consider also lapply (to avoid bookkeeping of initializing a list and assign in for loop by index). Also, use a defined method and avoid rerunning summary + escalc just to retrieve attributes. Run it once and extract attributes as needed.

# DEFINED METHOD TO RUN CALCULATIONS
# AVOID DRY (I.E., DON'T REPEAT YOURSELF)
run_trials <- function(i) {
  NMA_DB_L <- NMA_DB[-i,]

  results <- summary(escalc(
    ai = NMA_DB_L$All_Cause_d_C, 
    bi = NMA_DB_L$PTS_All_Cause_d_C - NMA_DB_L$All_Cause_d_C,
    ci = NMA_DB_L$All_Cause_d_I, 
    di = NMA_DB_L$PTS_All_Cause_d_I - NMA_DB_L$All_Cause_d_I, 
    measure = "RR"
  ))

  yi_All_cause <- results[,"yi"]
  sei_All_cause <- results[,"sei"]
  
  netmeta(
      TE = yi_All_cause, 
      seTE = sei_All_cause, 
      treat1 = NMA_DB_L$Arm_1, 
      treat2 = NMA_DB_L$INT, sm="RR",
      studlab = NMA_DB_L$Study, 
      reference.group = "Standard_DAPT"
  )
}

# BUILD LIST OF RESULTS
netmeta_results <- lapply(1:29, run_trials)
Related