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

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

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

问题

以下是您要翻译的部分:

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

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

  1. library(graph)
  2. library(igraphdata)
  3. library(igraph)
  4. # 创建一些网络
  5. data(karate)
  6. g2 <- erdos.renyi.game(50, .2)
  7. # 把网络放入一个列表中
  8. list_of_networks <- list(karate, g2)
  9. # 获取所有的社区检测算法
  10. clusters <- grep("^cluster_", ls("package:igraph"), value=TRUE)[-1]
  11. # 去掉我不想要的算法
  12. clusters <- clusters[!grepl("spinglass|fluid|leiden|walktrap|infomap|prop|optimal", clusters)]
  13. # for循环部分
  14. # 列表中有两个网络
  15. x <- 1:2
  16. # 一个用于for循环结果的容器
  17. container <- vector("list", length = 2)
  18. # 嵌套的for循环
  19. for (cluster in clusters) {
  20. for (i in x) {
  21. result <- t(c(
  22. modularity(do.call(cluster, list(list_of_networks[[i]])),
  23. length(do.call(cluster, list(list_of_networks[[i]])),
  24. cluster
  25. ))
  26. container[[i]] <- rbind(result, container[[i]])
  27. }
  28. }
  29. container %>%
  30. map(~ as_tibble(.)) %>%
  31. bind_rows() %>%
  32. rename(modularity = 1,
  33. num_communities = 2,
  34. 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?

  1. library(graph)
  2. library(igraphdata)
  3. library(igraph)
  4. # create a couple of networks
  5. data(karate)
  6. g2 &lt;- erdos.renyi.game(50, .2)
  7. # stick the networks in a list
  8. list_of_networks &lt;- list(karate, g2)
  9. # get all the community detection algorithms
  10. clusters &lt;- grep(&quot;^cluster_&quot;, ls(&quot;package:igraph&quot;), value=TRUE)[-1]
  11. # get rid of the algorithms I don&#39;t want
  12. clusters &lt;- clusters[!grepl(&quot;spinglass|fluid|leiden|walktrap|infomap|prop|optimal&quot;, clusters)]
  13. # for loop stuff
  14. # there are two networks in the network list
  15. x &lt;- 1:2
  16. # a container for my for loop results
  17. container &lt;- vector(&quot;list&quot;, length = 2)
  18. # nested for loop
  19. for (cluster in clusters) {
  20. for (i in x) {
  21. result &lt;- t(c(
  22. modularity(do.call(cluster, list(list_of_networks[[i]]))),
  23. length(do.call(cluster, list(list_of_networks[[i]]))),
  24. cluster
  25. ))
  26. container[[i]] &lt;- rbind(result, container[[i]])
  27. }
  28. }
  29. container %&gt;%
  30. map(~ as_tibble(.)) %&gt;%
  31. bind_rows() %&gt;%
  32. rename(modularity = 1,
  33. num_communities = 2,
  34. algorithm = 3)

答案1

得分: 0

You could use map_df's .id argument:

  1. library(tidyverse)
  2. container %>%
  3. map_df(~ as_tibble(.) %>%
  4. rename(modularity = 1,
  5. num_communities = 2,
  6. algorithm = 3),
  7. .id = "name")

This returns

  1. # A tibble: 6 × 4
  2. name modularity num_communities algorithm
  3. <chr> <chr> <chr> <chr>
  4. 1 1 0.443854125672307 4 cluster_louvain
  5. 2 1 0.4366391184573 5 cluster_leading_eigen
  6. 3 1 0.434521466989 3 cluster_fast_greedy
  7. 4 2 0.224687153369057 5 cluster_louvain
  8. 5 2 0.197245699823462 4 cluster_leading_eigen
  9. 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:

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

Now

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

returns

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

You could use map_df's .id argument:

  1. library(tidyverse)
  2. container %&gt;%
  3. map_df(~ as_tibble(.) %&gt;%
  4. rename(modularity = 1,
  5. num_communities = 2,
  6. algorithm = 3),
  7. .id = &quot;name&quot;)

This returns

  1. # A tibble: 6 &#215; 4
  2. name modularity num_communities algorithm
  3. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  4. 1 1 0.443854125672307 4 cluster_louvain
  5. 2 1 0.4366391184573 5 cluster_leading_eigen
  6. 3 1 0.434521466989 3 cluster_fast_greedy
  7. 4 2 0.224687153369057 5 cluster_louvain
  8. 5 2 0.197245699823462 4 cluster_leading_eigen
  9. 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

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

Now

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

returns

  1. # A tibble: 6 &#215; 4
  2. name modularity num_communities algorithm
  3. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  4. 1 Zachary&#39;s karate club network 0.444903581267218 4 cluster_louvain
  5. 2 Zachary&#39;s karate club network 0.4366391184573 5 cluster_leading_eigen
  6. 3 Zachary&#39;s karate club network 0.434521466989 3 cluster_fast_greedy
  7. 4 Erdos-Renyi (gnp) graph 0.210112108800672 4 cluster_louvain
  8. 5 Erdos-Renyi (gnp) graph 0.188379542113001 2 cluster_leading_eigen
  9. 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:

确定