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

Creating two-mode network from binary and character variables in data frame

SNA 人员:我正在尝试从 R 中的数据框创建双模式网络。我有一个组织列表,这些组织通过父组织中的共同成员资格连接。我在那个用二进制变量编码的组织中有成员资格。我已经通过以下代码(来自 )基于这些数据成功创建了社会矩阵和后续网络对象:

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

如何创建双模式网络,第一组边是组织在较大组织中的成员资格(由 link 变量表示),第二组边是个人在组织中的领导职位?

谢谢大家。

reprex package (v0.3.0)

于 2020-10-24 创建

有许多不同的方法可以完成您想要做的事情。我不知道有什么函数可以根据您拥有的数据神奇地创建 two-mode 网络,因此下面的解决方案涉及一些数据操作。我们首先创建一个带有节点的数据框,然后是另一个带有边的数据框。然后使用节点和边作为输入来创建一个 network 对象。代码是self-explanatory:

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来改变node/edge颜色、大小、形状等。

这里是一个使用包 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)

这是网络现在的样子:

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

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)

我们可以使用以下函数更改 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 页)上找到更多详细信息。