R 中的基本 SNA? - 如何加载网络数据

发布于 2024-08-14 17:25:54 字数 1078 浏览 6 评论 0原文

几年前,我使用 UCINET 进行一些社交网络分析。这些天我想再次使用 SNA - 但这次我更喜欢统一的分析框架 - 对我来说就是 R。

我已经查看了 sna 和 statnet 文档,但有点不知所措。

我想做的:首先:加载直接从网络调查(通常很有价值)中提取的二分/关联矩阵。将此矩阵转换为两个邻接矩阵(逐个隶属关系和逐个案例)。它也可以是一个有方向的、有价值的案例矩阵。

第二:加载顶点属性的文件(也来自例如网络调查数据)。

第三:然后根据某些中心性度​​量绘制具有例如顶点大小的图,通过某些顶点属性进行着色和标记,仅绘制值超过特定阈值的边。

这是一个迷你关联矩阵:

data <- structure(list(this = c(0, 1, 0, 1, 1, 2, 0, 1, 3), 
 that = c(1, 1, 3, 0, 0, 0, 2, 1, 0), 
 phat = c(0, 0, 2, 1, 0, 0, 1, 2, 0)), 
 .Names = c("this", "that", "phat"), 
 row.names = c("a", "b", "c", "d", "e", "f", "g", "h", "i"), 
 class = "data.frame")

带有一些属性数据:

att <-structure(list(sex = structure(c(1L, 1L, 2L, 2L, 1L, 2L, 1L, 
1L, 1L), .Label = c("F", "M"), class = "factor"), agegr = c(1L, 
1L, 3L, 1L, 3L, 1L, 1L, 3L, 1L), place = structure(c(1L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 1L), .Label = c("Lower", "Upper"), 
class = "factor")), .Names  = c("sex", 
"agegr", "place"), row.names = c(NA, -9L), class = "data.frame")

ps 也许 SNA 是这篇文章的一个很好的标签?我只是没有必要的善意:-)

A few years back I used UCINET for some social network analysis. Theese days I'd like to use SNA again - but this time I prefer a unified analysis framework - which for me is R.

I have looked at the sna and statnet documentation but am a bit overwhelmed.

What I'd like to do: First: Load an bipartite/incidence matrix pulled directly from e.g. a websurvey (often valued). Convert this matrix to two adjacency matrix' (affiliatoin by affiliation and cases by cases). It could also be a directed, valued cases by cases matrix.

Second: Load a file (also from e.g. websurvey data) of vertice attributes.

Third: Then plot the graph with e.g. vertice size according to some centrality measure, colored and labeled by some vertice attributes, with only edges with value over a certain threshold being drawn.

This is a mini incidence matrix:

data <- structure(list(this = c(0, 1, 0, 1, 1, 2, 0, 1, 3), 
 that = c(1, 1, 3, 0, 0, 0, 2, 1, 0), 
 phat = c(0, 0, 2, 1, 0, 0, 1, 2, 0)), 
 .Names = c("this", "that", "phat"), 
 row.names = c("a", "b", "c", "d", "e", "f", "g", "h", "i"), 
 class = "data.frame")

with som attribute data:

att <-structure(list(sex = structure(c(1L, 1L, 2L, 2L, 1L, 2L, 1L, 
1L, 1L), .Label = c("F", "M"), class = "factor"), agegr = c(1L, 
1L, 3L, 1L, 3L, 1L, 1L, 3L, 1L), place = structure(c(1L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 1L), .Label = c("Lower", "Upper"), 
class = "factor")), .Names  = c("sex", 
"agegr", "place"), row.names = c(NA, -9L), class = "data.frame")

p.s. maybe SNA would be a good tag for this post? I just don't have the nescassary SO goodwill :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃酥萝莉 2024-08-21 17:25:54

这是一个很好的问题,为进一步探索 R 中的 SNA 提供了一些机会。我更熟悉 igraph 包,所以我将使用该库中的函数来回答您的问题。

您问题的第一部分有一个相当简单的解决方案:

# Convert data frame to graph using incidence matrix
G<-graph.incidence(as.matrix(data),weighted=TRUE,directed=FALSE)
summary(G)
# Vertices: 12 
# Edges: 30 
# Directed: TRUE 
# No graph attributes.
# Vertex attributes: type, name.
# Edge attributes: weight.

这会返回一个图形对象,该对象具有来自关联矩阵的无向边和加权边。要从二分图生成从属关系图,您有两种选择。快速而简单的方法是这样的:

proj<-bipartite.projection(G)

这将返回一个列表,其中每个投影索引为 $proj1 和 proj2,不幸的是这些项目不包含执行此操作时通常需要的边权重。要做到这一点,最好的解决方案是自己简单地执行矩阵乘法。

# Create the matrices, and set diagonals to zero
M<-as.matrix(data)
affil.matrix<-M%*%t(M)
diag(affil.matrix)<-0
cases.matrix<-t(M)%*%M
diag(cases.matrix)<-0
# Create graph objects from matrices
affil.graph<-graph.incidence(affil.matrix,weighted=TRUE)
cases.graph<-graph.incidence(cases.matrix,weighted=TRUE)

使用属性数据生成绘图有点棘手,需要更多编码,但我建议查看一些 igraph 示例,甚至我自己的一些< /a> 因为有很多东西可以帮助您入门。祝你好运!

This is a good question, and provides some opportunity for further exploration of SNA in R. I am more familiar with the igraph package, so I will answer your question using the the functions in that library.

The first part of your question has a fairly straightforward solution:

# Convert data frame to graph using incidence matrix
G<-graph.incidence(as.matrix(data),weighted=TRUE,directed=FALSE)
summary(G)
# Vertices: 12 
# Edges: 30 
# Directed: TRUE 
# No graph attributes.
# Vertex attributes: type, name.
# Edge attributes: weight.

This returns a graph object with undirected and weighted edges from the incidence matrix. To generate the affiliations graphs from the bipartite graph you have two options. The quick and easy one is this:

proj<-bipartite.projection(G)

This will return a list with each projection indexed as $proj1 and proj2, the unfortunate thing is that these projects do not contain the edge weights that you would normally want when performing this manipulation. To do this the best solution is to simply perform the matrix multiplication yourself.

# Create the matrices, and set diagonals to zero
M<-as.matrix(data)
affil.matrix<-M%*%t(M)
diag(affil.matrix)<-0
cases.matrix<-t(M)%*%M
diag(cases.matrix)<-0
# Create graph objects from matrices
affil.graph<-graph.incidence(affil.matrix,weighted=TRUE)
cases.graph<-graph.incidence(cases.matrix,weighted=TRUE)

Generating the plots with the attribute data is a bit trickier and requires more coding, but I recommend looking through some of the igraph examples, or even some of my own as there is plenty there to get you started. Good luck!

错々过的事 2024-08-21 17:25:54

这并不能直接回答你的问题,但我强烈建议观看 Drew Conway 关于 R 中的 SNA 的演示 如果您还没有看过的话。

This doesn't directly answer your question, but I strongly recommend watching Drew Conway's presentation on SNA in R if you haven't already seen it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文