创建一个树状图

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

Create a dendrogram

问题

我有这样的数据集

| 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        |

自我代表8名足球运动员,其他代表这8名足球运动员中的一个与哪些人交友。我想评估两名足球运动员的友谊圈有多相似,以及共同的朋友。然后我想做一个聚类。

到目前为止我做了这些,但是出现了错误消息

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)


但是它不起作用。

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

footballplayers  &lt;- read.csv(&quot;footballplayer.csv&quot;, header = TRUE)
footballplayers_e &lt;- graph_from_edgelist(as.matrix(footballplayers),directed = TRUE)
similarities &lt;- as.dist(1 - cor(footballplayers_e), upper = TRUE) 
footballplayersdend&lt;-hclust(similarities)
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函数。

library(tibble)

df <- tribble(~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)
with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE))

##> 8 x 8 sparse Matrix of class "ngCMatrix"
##>                     
##> [1,] . . | | | . . .
##> [2,] . . . . | | . .
##> [3,] | . . | | . . .
##> [4,] | . | . | . | |
##> [5,] | | | | . . . .
##> [6,] . | . . . . | |
##> [7,] . . . | . | . .
##> [8,] . . . | . | . .

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

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

创建一个树状图

英文:

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

library(tibble)

df &lt;- tribble(~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)
with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE))

##&gt; 8 x 8 sparse Matrix of class &quot;ngCMatrix&quot;
##&gt;                     
##&gt; [1,] . . | | | . . .
##&gt; [2,] . . . . | | . .
##&gt; [3,] | . . | | . . .
##&gt; [4,] | . | . | . | |
##&gt; [5,] | | | | . . . .
##&gt; [6,] . | . . . . | |
##&gt; [7,] . . . | . | . .
##&gt; [8,] . . . | . | . .

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

with(df, Matrix::sparseMatrix(SELF, OTHER, symmetric = TRUE)) |&gt;
    dist(&quot;binary&quot;) |&gt;
    hclust() |&gt;
    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:

确定