迭代一个for循环,遍历一个包含igraph对象的列表,以比较不同的聚类算法。

huangapple go评论54阅读模式
英文:

iterating a for loop over a list of igraph objects to compare different clustering algorithms

问题

以下是您要翻译的部分:

"我在想是否有人可以帮我解决这个for循环的问题。或者建议另一种获取我想要的方式(我知道代码有点混乱,但我对如何使它更加优雅感到困惑)。

问题:我有一个包含一些网络对象的列表;我想比较一些用于这些网络的社区检测算法。而且我希望一次性获取到这些信息(不像这里的玩具示例那样,我实际的列表中有许多网络)。以下的玩具示例让我接近我想要的信息,但它没有在最终输出中列出网络的名称。为了在最终的数据框中也包括网络的名称,我需要做些什么?

library(graph)
library(igraphdata)
library(igraph)

# 创建一些网络
data(karate)
g2 <- erdos.renyi.game(50, .2)

# 把网络放入一个列表中
list_of_networks <- list(karate, g2)

# 获取所有的社区检测算法
clusters <- grep("^cluster_", ls("package:igraph"), value=TRUE)[-1]

# 去掉我不想要的算法
clusters <- clusters[!grepl("spinglass|fluid|leiden|walktrap|infomap|prop|optimal", clusters)]

# for循环部分

# 列表中有两个网络
x <- 1:2

# 一个用于for循环结果的容器
container <- vector("list", length = 2)

# 嵌套的for循环
for (cluster in clusters) {
 
 for (i in x) {
  
  result <- t(c(
   
   modularity(do.call(cluster, list(list_of_networks[[i]])), 
   length(do.call(cluster, list(list_of_networks[[i]])),
   cluster
  ))
  
  container[[i]] <- rbind(result, container[[i]])
 }
}
 
container %>%
 map(~ as_tibble(.)) %>%
 bind_rows() %>%
 rename(modularity = 1,
        num_communities = 2,
        algorithm = 3)

"

英文:

I’m wondering if someone could help me with this for loop. Or suggest another way of getting at what I want (I know the code is a bit of a nightmare, but I’m stumped on how to make it, well, more elegant).

The problem: I have some network objects in a list; I would like to compare some community detection algorithms for those networks. And I would like to get at that information in one go (unlike the toy example here the real list I have has many networks in it). The following toy example gets me close to the information I want, but it does not list the networks in the final output. What do I need to do in order to also include the name of the networks in the final data frame?

library(graph)
library(igraphdata)
library(igraph)

# create a couple of networks
data(karate)
g2 &lt;- erdos.renyi.game(50, .2)

# stick the networks in a list
list_of_networks &lt;- list(karate, g2)

# get all the community detection algorithms 
clusters &lt;- grep(&quot;^cluster_&quot;, ls(&quot;package:igraph&quot;), value=TRUE)[-1] 

# get rid of the algorithms I don&#39;t want
clusters &lt;- clusters[!grepl(&quot;spinglass|fluid|leiden|walktrap|infomap|prop|optimal&quot;, clusters)]

# for loop stuff

# there are two networks in the network list
x &lt;- 1:2

# a container for my for loop results
container &lt;- vector(&quot;list&quot;, length = 2)

# nested for loop
for (cluster in clusters) {
 
 for (i in x) {
  
  result &lt;- t(c(
   
   modularity(do.call(cluster, list(list_of_networks[[i]]))), 
   length(do.call(cluster, list(list_of_networks[[i]]))),
   cluster
   
  ))
  
  container[[i]] &lt;- rbind(result, container[[i]])
 }
}

container %&gt;%
 map(~ as_tibble(.)) %&gt;%
 bind_rows() %&gt;% 
 rename(modularity = 1,
        num_communities = 2,
        algorithm = 3)

答案1

得分: 0

You could use map_df's .id argument:

library(tidyverse)

container %>%
  map_df(~ as_tibble(.) %>%
           rename(modularity = 1,
                  num_communities = 2,
                  algorithm = 3), 
         .id = "name")

This returns

# A tibble: 6 × 4
  name  modularity        num_communities algorithm            
  <chr> <chr>             <chr>           <chr>                
1 1     0.443854125672307 4               cluster_louvain      
2 1     0.4366391184573   5               cluster_leading_eigen
3 1     0.434521466989    3               cluster_fast_greedy  
4 2     0.224687153369057 5               cluster_louvain      
5 2     0.197245699823462 4               cluster_leading_eigen
6 2     0.193293130653502 4               cluster_fast_greedy  

so your networks are named 1 and 2.

To get better information, we add a line to your code:

container <- vector("list", length = 2)
# Add names here
names(container) <- unlist(lapply(list_of_networks, \(x) x$name))
# or
# names(container) <- sapply(list_of_networks, \(x) x$name)

Now

container %>%
  map_df(~ as_tibble(.) %>%
           rename(modularity = 1,
                  num_communities = 2,
                  algorithm = 3), 
         .id = "name")

returns

# A tibble: 6 × 4
  name                          modularity        num_communities algorithm            
  <chr>                         <chr>             <chr>           <chr>                
1 Zachary's karate club network 0.444903581267218 4               cluster_louvain      
2 Zachary's karate club network 0.4366391184573   5               cluster_leading_eigen
3 Zachary's karate club network 0.434521466989    3               cluster_fast_greedy  
4 Erdos-Renyi (gnp) graph       0.210112108800672 4               cluster_louvain      
5 Erdos-Renyi (gnp) graph       0.188379542113001 2               cluster_leading_eigen
6 Erdos-Renyi (gnp) graph       0.21151675068263  3               cluster_fast_greedy  
英文:

You could use map_df's .id argument:

library(tidyverse)

container %&gt;% 
  map_df(~ as_tibble(.) %&gt;% 
           rename(modularity = 1,
                  num_communities = 2,
                  algorithm = 3), 
         .id = &quot;name&quot;)

This returns

# A tibble: 6 &#215; 4
  name  modularity        num_communities algorithm            
  &lt;chr&gt; &lt;chr&gt;             &lt;chr&gt;           &lt;chr&gt;                
1 1     0.443854125672307 4               cluster_louvain      
2 1     0.4366391184573   5               cluster_leading_eigen
3 1     0.434521466989    3               cluster_fast_greedy  
4 2     0.224687153369057 5               cluster_louvain      
5 2     0.197245699823462 4               cluster_leading_eigen
6 2     0.193293130653502 4               cluster_fast_greedy  

so your networks are named 1 and 2.

To get better information, we add a line to your code

container &lt;- vector(&quot;list&quot;, length = 2)
# Add names here
names(container) &lt;- unlist(lapply(list_of_networks, \(x) x$name))
# or
# names(container) &lt;- sapply(list_of_networks, \(x) x$name)

Now

container %&gt;% 
  map_df(~ as_tibble(.) %&gt;% 
           rename(modularity = 1,
                  num_communities = 2,
                  algorithm = 3), 
         .id = &quot;name&quot;)

returns

# A tibble: 6 &#215; 4
  name                          modularity        num_communities algorithm            
  &lt;chr&gt;                         &lt;chr&gt;             &lt;chr&gt;           &lt;chr&gt;                
1 Zachary&#39;s karate club network 0.444903581267218 4               cluster_louvain      
2 Zachary&#39;s karate club network 0.4366391184573   5               cluster_leading_eigen
3 Zachary&#39;s karate club network 0.434521466989    3               cluster_fast_greedy  
4 Erdos-Renyi (gnp) graph       0.210112108800672 4               cluster_louvain      
5 Erdos-Renyi (gnp) graph       0.188379542113001 2               cluster_leading_eigen
6 Erdos-Renyi (gnp) graph       0.21151675068263  3               cluster_fast_greedy  

huangapple
  • 本文由 发表于 2023年4月11日 01:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定