How to save all the information obtained from a loop?

Hi community
I am writing this issue in connection with a former query. where a loop has been run:

for(i in 1:length(bacteria_names)){
  ma_model <- rma.mv(yi, vi, data = final_csv, subset = (Bacteria == bacteria_names[i]))
  re_table <- coef(summary(ma_model))
  df[i, -1] <- re_table
}
  • I am seeing that the re_tableonly stores result for a single loop, not for all the loops. How can I save all the results?

  • Another query is, as per the tutorial of metafor the summary(ma_model) command should show heterogeneity of the data like: tau^2, I^2, Q value, etc. I am getting only the Q-value information and that too for only one loop. How can I get all of them for all the loops?
    Here's what I get when I use summary(ma_model):

> summary(ma_model)

Multivariate Meta-Analysis Model (k = 6; method: REML)

  logLik  Deviance       AIC       BIC      AICc 
 -2.1902    4.3804    6.3804    5.9898    7.7137   

Variance Components: none

Test for Heterogeneity:
Q(df = 5) = 9.4612, p-val = 0.0920

Model Results:

estimate      se     zval    pval    ci.lb   ci.ub 
 -0.0855  0.0950  -0.8998  0.3682  -0.2718  0.1007    

---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Thanks you very much in advance.
DC7

Hello!

There is an easy way to do it. You simply need re_table as a list. So in my example below my_list would work similar to your re_table where we assign each then. Have a look at my_list after the loop

my_list <- list()

for(i in 1:10){
  my_list[[i]] <- sample(1:10,3)
}

my_list

Thanks. But, it gives me the same result. Still i am getting result for only a single loop.

DId you try the example I had? You will see that all results get saved.

yes, I have seen that works in your example. But not in my case. I don't know why.

Can you share a proper reprex? FAQ: How to do a minimal reproducible example ( reprex ) for beginners - #2 by andresrcs

1 Like

I am not sure reprex of which portion do you want. Actually it is a long script and lots of data are there.
Secondly, the problem lies in the ma_model. I want all the ma_model value for all the loops. It gives result for only a single loop which is

> ma_model

Multivariate Meta-Analysis Model (k = 6; method: REML)

Variance Components: none

Test for Heterogeneity:
Q(df = 5) = 9.4612, p-val = 0.0920

Model Results:

estimate      se     zval    pval    ci.lb   ci.ub 
 -0.0855  0.0950  -0.8998  0.3682  -0.2718  0.1007    

---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Thanks

A reprex is meant to represent your problem as a minimal reproducible example. The code I provides shows how you would be able to assign repeatedly to an object and be able to look at all the values. Without having your code as a more simple example I cannot see how you're trying to incorporate it and can't really advise. If you're getting the same result then re_table is probably not a list and probably not assigning it to lists as re_table[[i]]

2 Likes

Hi thanks for your time. Is it possible for you to use the data frame in the referred query? I am also using the same code that has been used in the referred query.

(I am referring you to that question because it is really difficult to create a reprex of my data. As, I am working with a very complex data)

@GreyMerchant Sir/madam- please copy and paste the following table save as all_es.csv in the desktop and use that with the following code:

study year test n_controls n_patients age_controls age_patients mean_controls mean_patients sd_controls sd_patients yi vi
Caraquez 1998 n-back 12 15 39.1 23.4 1.01 15.4 31.1 0.9 -0.67601411442671 0.158462871905632
Caraquez 1998 n-back 12 15 39.1 23.4 1.01 54 2.1 7 -9.47933975490664 1.81403485535099
Franzen 2012 n-back 65 56 67.1 66.9 10 47 91.1 5 -0.549607753881664 0.03448997594063
Franzen 2012 n-back 65 56 67.1 66.9 50 67 4.1 2 -5.11910608650243 0.141527903385833
Franzen 2012 n-back 65 56 67.1 66.9 50 67 6.1 2 -3.61295800630745 0.087181698553085
Lieu 2012 digit_span 102 99 21.1 23.4 24 10 21.5 5 0.887578672128736 0.021864622961224
Lieu 2012 digit_span 102 99 21.1 23.4 12 20 1.5 5 -2.17285011427459 0.031649403358949
Lieu 2012 digit_span 102 99 21.1 23.4 12 20 1.9 9 -1.23386292200698 0.023692040401736
McMullin 1990 n-back 10 10 35.6 25.1 1.02 111 0.29 0.29 -363.179151876099 3297.67740893607
McMullin 1990 n-back 10 10 35.6 25.1 1.02 131 0.5 0.29 -304.549916872949 2318.966296683
McMullin 1990 n-back 10 10 35.6 25.1 1.02 1 2.5 0.29 0.01076236528614 0.200002895712664
Meyer 1992 digit_span 75 80 22.2 54.5 56 61 0.9 6.8 -1.00999884488257 0.029123970967734
Meyer 1992 digit_span 75 80 22.2 54.5 56 61 0.9 6.8 -1.00999884488257 0.029123970967734

Code:

library(RCurl)
library(bitops)
library(metafor)
library(Formula)
library(dplyr)
library(ggforestplot)

all_csv <- read.csv("~/Desktop/all_es.csv")
Bacteria <- unique(all_csv$Bacteria)






single_bacteria <- all_csv %>% count(Bacteria, sort = TRUE) %>% 
  filter(n == 1) %>% select(Bacteria) %>% .$Bacteria
single_bacteria
[1] "Adlercreutzia"   "Asaccharobacter" "Atopobium"

# Keep only those bacteria that have >1 studies without having to type each bacteria out
final_csv <- all_csv[which(!all_csv$Bacteria %in% single_bacteria), ]

# Our usual code, except now all_csv is replaced with final_csv
bacteria_names <- unique(final_csv$Bacteria)

df <- data.frame(matrix(NA, ncol = 7, nrow = length(bacteria_names)))
df[, 1] <- bacteria_names
names(df) <- c("Bacteria", "estimate", "se", "zval", "pval", "ci.lb", "ci.ub")
df

ma_model <- list()

for(i in 1:length(bacteria_names)){
  ma_model[[i]] <- rma.mv(yi, vi, data = final_csv, subset = (Bacteria == bacteria_names[i]))
  re_table <- coef(summary(ma_model))
  df[i, -1] <- re_table
}

ma_model

I think it will help you to work with my problem.

Thanks,
DC7

Seems like no one is helping you because copy/pasting a table like that doesn't just work, and requires some effort to get into R (I tried).

Also, your code already doesn't work on the second line (after the libraries). The Bacteria column/data is no where to be found.

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

1 Like

You could use a list structure to save the output using the for loop indices. You could also try using a matrix. For a large dataset I would suggest defining the size of the matrix. Increasing matrix size dynamically is time consuming and your program will take a long time to run.
Try a small dataset first. Get that to work, then scale up. Starting with the full data set will waste your time making mistakes that should have been solved with a small mockup. If you can get the program to work with five variables then you understand it well enough to scale up.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.