Search code examples
rfor-loopmatrixigraph

Make automatic analyse using for to 100x


I have 100 matrix, I need do same analyse for all.

###DADOSEXEMPLO##
vetor <- c(1, 5, 3, 8, 2, 9, 3, 2:15, 1, 5, 3, 6, 5, 9, 3 )
matrixnet(vetor, 7, 7)
matrixnet<-matrix(vetor, 7, 7)
matcor <- cor(matrixnet, method = "spearman")

 matrixnetwork = graph.adjacency(matcor, mode="undirected", weighted 
 = TRUE, 
 add.colnames=NULL, diag=FALSE)
plot(matrixnetwork)
matrix

Here I utilize boot with permute in igraph pacote, I don't know if it is correct!! Help

###BOOT
N <- 7L
set.seed(2023)
R <- 100L
BIPERMUT<- vector("list", length = R)
for(i in seq.int(R)) {
  indices <- sample(N, replace = TRUE)
  BIPERMUT[[i]] <- permute(matrixnetwork, 
 sample(vcount(matrixnetwork)))  
##ANALYSE
degree(BIPERMUT[[1]], normalized = FALSE, loops = FALSE)

I try, but this not run the analyse 100x, this resample 100x:

for(i in seq.int(R)) {
indices <- sample(N, replace = TRUE)
 boot_degree= degree(g_boot_list[[i]], normalized = FALSE, loops = 
  FALSE)

I try this form too for boot in matrix:

 ######Matrix permutation#####
 N <- 7L
 set.seed(2023)
 R <- 100L
 BIPERMUT<- vector("list", length = R)
 for(i in seq.int(R)) {
   indices <- sample(N, replace = TRUE)
   BIPERMUT[[i]] <- permute(matrixnetwork, 
 sample(vcount(matrixnetwork)))  
 }
 plot (BIPERMUT[[1]])

I try too, for permutation matrix, here I utilise only sample:

 N <- 7L
 set.seed(2023)
 R <- 100L
 BIboot2 <- vector("list", length = R)
 for(i in seq.int(R)) {
  indices <- sample(N, replace = TRUE)
  BIboot2[[i]] <- matrixnetwork
  }

plot (BIboot2[[1]])
degree(BIboot2[[1]], normalized = FALSE, loops = FALSE)

Help me find the best solution for permutation in 100x to matrixnetwork ...

I try

 boot_degree <- lapply(AIPERMUT, degree, normalized = FALSE, loops = 
 FALSE)
 degree_cool <- do.call("rbind", boot_degree)

but I need him to understand that a variable is allocated to different columns in each row, and he can always pull the same variable to a specific column enter image description here


Solution

  • Create the results vector boot_degree before the for loop.

    boot_degree <- vector("list", R)
    for(i in seq.int(R)) {
      boot_degree[[i]] <- degree(g_boot_list[[i]], normalized = FALSE, loops = FALSE)
    }
    

    Another way is to lapply function degree to each member of g_boot_list.

    boot_degree2 <- lapply(g_boot_list, degree, normalized = FALSE, loops = FALSE)
    
    identical(boot_degree, boot_degree2)  # TRUE