根据数据帧中的二进制和字符变量创建双模式网络

问题描述

SNA成员:我正在尝试从R中的数据帧创建一个双模式网络。我有一个组织列表,这些列表通过上级组织中的常见成员身份进行连接。我在该组织中具有用二进制变量编码的成员身份。我已经通过以下代码(来自Create adjacency matrix based on binary variable)基于这些数据成功创建了一个社会矩阵和后续的网络对象:

library(statnet)
org <- c("A","B","C","D","E","F","G","H","I","J")
link <- c(1,1,1)
person <- c("Mary","Michael","Mary","Jane","Jimmy","Johnny","Becky","Bobby","Becky")

df <- data.frame(org,link,person)

socmat1 <- tcrossprod(df$link)
rownames(socmat1) <- df$org
colnames(socmat1) <- df$org
diag(socmat1) <- 0
socmat1
#>   A B C D E F G H I J
#> A 0 0 0 0 1 1 0 0 1 1
#> B 0 0 0 0 0 0 0 0 0 0
#> C 0 0 0 0 0 0 0 0 0 0
#> D 0 0 0 0 0 0 0 0 0 0
#> E 1 0 0 0 0 1 0 0 1 1
#> F 1 0 0 0 1 0 0 0 1 1
#> G 0 0 0 0 0 0 0 0 0 0
#> H 0 0 0 0 0 0 0 0 0 0
#> I 1 0 0 0 1 1 0 0 0 1
#> J 1 0 0 0 1 1 0 0 1 0

testnet <- as.network(x = socmat1,directed = FALSE,loops = FALSE,matrix.type = "adjacency"
)
testnet
#>  Network attributes:
#>   vertices = 10 
#>   directed = FALSE 
#>   hyper = FALSE 
#>   loops = FALSE 
#>   multiple = FALSE 
#>   bipartite = FALSE 
#>   total edges= 10 
#>     missing edges= 0 
#>     non-missing edges= 10 
#> 
#>  Vertex attribute names: 
#>     vertex.names 
#> 
#> No edge attributes

reprex package(v0.3.0)于2020-10-24创建

但是,我显然无法使用tcrossprod()来实现与由组织联系的个人的相同结果,反之亦然,如以下代码所示:

socmat2 <- tcrossprod(df$org)
#> Error in df$org: object of type 'closure' is not subsettable
rownames(socmat2) <- df$person
#> Error in df$person: object of type 'closure' is not subsettable
colnames(socmat2) <- df$person
#> Error in df$person: object of type 'closure' is not subsettable
diag(socmat2) <- 0
#> Error in diag(socmat2) <- 0: object 'socmat2' not found
socmat2
#> Error in eval(expr,envir,enclos): object 'socmat2' not found

如何创建一种双模式网络,第一组边缘是组织在较大组织中的成员身份(由链接变量表示),第二组边缘是个人在组织中的领导地位?

谢谢。

reprex package(v0.3.0)于2020-10-24创建

解决方法

有许多不同的方法可以完成您想做的事情。我不知道有任何功能可以根据您拥有的数据神奇地创建双模网络,因此下面的解决方案涉及一些数据操作。我们首先使用节点创建一个数据框,然后使用边缘创建另一个数据框。然后,使用节点和边作为输入来创建network对象。该代码是不言自明的:

library(tidyverse)
library(network)

# Let's create a 'nodes' data frame
my_nodes <- as.data.frame(rbind(
  cbind(nodename = org,type = "Organization"),cbind(unique(person),"People"),cbind("Parent","Parent org")))

# Let's add an ID column to the nodes data frame
my_nodes <- rowid_to_column(my_nodes,"ID")

# Let's create a data frame with al possible edges 
# (i.e.,connecting organizations to people and organizations to the parent organization)
my_edges <- data.frame(rbind(
  cbind(ColA = org,ColB = person,type = "Set 1"),cbind(org,link,"Set 2")))

my_edges <- subset(my_edges,ColB != 0) 
my_edges$ColB[my_edges$ColB == 1] <- "Parent"

# Let's set up the network object using edges and nodes
my_network <- network(my_edges,vertex.attr = my_nodes,matrix.type = "edgelist",ignore.eval = FALSE)

请注意,我们创建了列type来对节点和边进行分类。可视化网络时,我们可以使用type来更改节点/边缘的颜色,大小,形状等。

以下是使用软件包igraph的示例。首先,我们将network对象转换为igraph对象。

library(igraph)
library(intergraph)

my_netgraph <- asIgraph(my_network)

可以使用V(my_netgraph)$attribute_name来评估节点的属性。例如,让我们看一下我们先前定义的网络中的节点type

> V(my_netgraph)$type
[1] "Organization" "Organization" "Organization" "Organization" "Organization" "Organization"
[7] "Organization" "Organization" "Organization" "Organization" "People"       "People"      
[13] "People"       "People"       "People"       "People"       "People"       "Parent org"

现在让我们根据type为这些节点着色。为此,我们将创建一个新属性$color。每个$color应该对应一个不同的$type

V(my_netgraph)[V(my_netgraph)$type == "People"]$color <- "green"
V(my_netgraph)[V(my_netgraph)$type == "Organization"]$color <- "red"
V(my_netgraph)[V(my_netgraph)$type == "Parent org"]$color <- "yellow"

plot(my_netgraph)

现在的网络是这样的:

enter image description here

现在让我们根据属性$shape更改节点的$type

V(my_netgraph)[V(my_netgraph)$type == "People"]$shape <- "circle"
V(my_netgraph)[V(my_netgraph)$type == "Organization"]$shape <- "square"
V(my_netgraph)[V(my_netgraph)$type == "Parent org"]$shape <- "rectangle" 

plot(my_netgraph)

enter image description here

我们可以使用以下功能更改igraph对象的其他属性:

E(my_netgraph) # changes he edges of the "net" object
V(my_netgraph) # changes the vertices of the "net" object
E(my_netgraph)$type # changes edge attribute "type"
V(my_netgraph)$media # changes the vertex attribute "media"

您可以在this iGraph manual(第10-11页)上找到更多详细信息。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...