创建一个树状图

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

Create a dendrogram

问题

  1. 我有这样的数据集
  2. | SELF | OTHER |
  3. | -------- | -------- |
  4. | 1 | 3 |
  5. | 1 | 4 |
  6. | 1 | 5 |
  7. | 2 | 6 |
  8. | 2 | 1 |
  9. | 2 | 5 |
  10. | 3 | 5 |
  11. | 3 | 4 |
  12. | 3 | 2 |
  13. | 4 | 8 |
  14. | 4 | 7 |
  15. | 4 | 5 |
  16. | 5 | 1 |
  17. | 5 | 2 |
  18. | 5 | 3 |
  19. | 6 | 7 |
  20. | 6 | 8 |
  21. | 6 | 5 |
  22. | 7 | 5 |
  23. | 7 | 2 |
  24. | 7 | 3 |
  25. | 8 | 6 |
  26. | 8 | 6 |
  27. | 8 | 6 |
  28. 自我代表8名足球运动员,其他代表这8名足球运动员中的一个与哪些人交友。我想评估两名足球运动员的友谊圈有多相似,以及共同的朋友。然后我想做一个聚类。
  29. 到目前为止我做了这些,但是出现了错误消息

footballplayers <- read.csv("footballplayer.csv", header = TRUE)
footballplayers_e <- graph_from_edgelist(as.matrix(footballplayers),directed = TRUE)
similarities <- as.dist(1 - cor(footballplayers_e), upper = TRUE)
footballplayersdend<-hclust(similarities)
plot(footballplayersdend)

  1. 但是它不起作用。
  2. Error in FUN(X[[i]], ...) : as.sociomatrix.sna input must be an adjacency matrix/array, network, or list.
英文:

I have a dataset like this

SELF OTHER
1 3
1 4
1 5
2 6
2 1
2 5
3 5
3 4
3 2
4 8
4 7
4 5
5 1
5 2
5 3
6 7
6 8
6 5
7 5
7 2
7 3
8 6
8 6
8 6

The self represent the 8 football players and the other represents with which of this 8 football players one is befriended. I one to evaluate how similar two football players have a friendship circle, also share same people as friends. And then I would like to do a clustering

This is what I did so far, but got error message

  1. footballplayers &lt;- read.csv(&quot;footballplayer.csv&quot;, header = TRUE)
  2. footballplayers_e &lt;- graph_from_edgelist(as.matrix(footballplayers),directed = TRUE)
  3. similarities &lt;- as.dist(1 - cor(footballplayers_e), upper = TRUE)
  4. footballplayersdend&lt;-hclust(similarities)
  5. plot(footballplayersdend)

But it doesn't work.

Error in FUN(X[[i]], ...) : as.sociomatrix.sna input must be an adjacency matrix/array, network, or list.

答案1

得分: 0

从边缘列表获取邻接矩阵的简单方法是使用Matrix::sparseMatrix函数。

  1. library(tibble)
  2. df <- tribble(~SELF, ~OTHER,
  3. 1, 3,
  4. 1, 4,
  5. 1, 5,
  6. 2, 6,
  7. 2, 1,
  8. 2, 5,
  9. 3, 5,
  10. 3, 4,
  11. 3, 2,
  12. 4, 8,
  13. 4, 7,
  14. 4, 5,
  15. 5, 1,
  16. 5, 2,
  17. 5, 3,
  18. 6, 7,
  19. 6, 8,
  20. 6, 5,
  21. 7, 5,
  22. 7, 2,
  23. 7, 3,
  24. 8, 6,
  25. 8, 6,
  26. 8, 6)
  27. with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE))
  28. ##> 8 x 8 sparse Matrix of class "ngCMatrix"
  29. ##>
  30. ##> [1,] . . | | | . . .
  31. ##> [2,] . . . . | | . .
  32. ##> [3,] | . . | | . . .
  33. ##> [4,] | . | . | . | |
  34. ##> [5,] | | | | . . . .
  35. ##> [6,] . | . . . . | |
  36. ##> [7,] . . . | . | . .
  37. ##> [8,] . . . | . | . .

然后我们可以应用 disthclust,例如:

  1. with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE)) |>
  2. dist("binary") |>
  3. hclust() |>
  4. plot()

创建一个树状图

英文:

An easy way to get an adjacency matrix from an edge list is by using Matrix::sparseMatrix

  1. library(tibble)
  2. df &lt;- tribble(~SELF, ~OTHER,
  3. 1, 3,
  4. 1, 4,
  5. 1, 5,
  6. 2, 6,
  7. 2, 1,
  8. 2, 5,
  9. 3, 5,
  10. 3, 4,
  11. 3, 2,
  12. 4, 8,
  13. 4, 7,
  14. 4, 5,
  15. 5, 1,
  16. 5, 2,
  17. 5, 3,
  18. 6, 7,
  19. 6, 8,
  20. 6, 5,
  21. 7, 5,
  22. 7, 2,
  23. 7, 3,
  24. 8, 6,
  25. 8, 6,
  26. 8, 6)
  27. with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE))
  28. ##&gt; 8 x 8 sparse Matrix of class &quot;ngCMatrix&quot;
  29. ##&gt;
  30. ##&gt; [1,] . . | | | . . .
  31. ##&gt; [2,] . . . . | | . .
  32. ##&gt; [3,] | . . | | . . .
  33. ##&gt; [4,] | . | . | . | |
  34. ##&gt; [5,] | | | | . . . .
  35. ##&gt; [6,] . | . . . . | |
  36. ##&gt; [7,] . . . | . | . .
  37. ##&gt; [8,] . . . | . | . .

Then we can apply dist and hclust e.g.:

  1. with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE)) |&gt;
  2. dist(&quot;binary&quot;) |&gt;
  3. hclust() |&gt;
  4. plot()

创建一个树状图

huangapple
  • 本文由 发表于 2023年2月16日 14:42:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468669.html
匿名

发表评论

匿名网友

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

确定