Search code examples
rloopsfor-loopgraphigraph

Loop to see the result of a variable with different parameter values


I need a loop that repeats the calculation of a variable, considering all resolution levels between 0.0 and 2.0. And then build a curve graph showing which value is most predominant, regardless of the resolution used.

    library(igraph)
    
    ###DADOSEXEMPLO##

valores <- c(1, 5, 3, 8, 2, 9, 3, 2, 3 ,2, 5, 3, 6, 5, 1, 5, 3, 4, 2, 2, 5, 6, 5, 7, 1, 2, 
             8, 12, 5, 1, 5, 3, 6, 5, 9, 3,3,4,5,7,8,2,7,8,4,3,1,4,4 )
matrixnet <- matrix(valores, 7, 7)
matcor <- cor(matrixnet, method = "spearman")
    cor_pequena <- 0.1
    matcor[] <- ifelse(matcor < cor_pequena, 0, matcor)
    matrixnetwork = graph.adjacency(matcor, mode = "undirected", 
                                    weighted = TRUE, 
                                    add.colnames = NULL, diag = FALSE)
    plot(matrixnetwork)

I want to calculate this variable, within the resolution parameter, from the value 0.0 to 2.0, considering each point with value 0.01 to 0.01

This is function, here I have resolution (where I need change value)

    louvain <- cluster_louvain(matrixnetwork, weights = NULL, resolution = 0.87)
orig_memb_louvain <- membership(louvain)     
mod_louvain <- modularity(matrixnetwork, orig_memb_louvain,weights = NULL, resolution = 0.87)

I try this formula, but it doesn't change resolution value

valores_louvain<- vector("list", length = 10)

for(i in seq.int(10)) {
  valores_louvain[[i]] <- cluster_louvain(matrixnetwork, weights = NULL, resolution = i + 0.01)
}

After I want build a graph, plotting the curve of values resulting from each change in the resolution parameter, to see the most prominent value.

PS: cluster_louvain returns 2 answers groups: 1, mod: 0.13


Solution

  • This?

    
    
    clv <- cluster_louvain(matrixnetwork, weights = NULL)
    plot(clv, matrixnetwork)
    
    # from 0 to 2 step = 0.01
    valores_louvain <- c()
    for(i in seq(from=0, to= 2, by=0.01)) {
      clv <- cluster_louvain(matrixnetwork, weights = NULL, resolution = i)
      valores_louvain[length(valores_louvain) + 1] <- tail(clv$modularity, 1)
    }
    
    dev.new(); plot(valores_louvain)