首页 > 解决方案 > 从数据帧中的二进制和字符变量创建双模网络

问题描述

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 包(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 包(v0.3.0)于 2020 年 10 月 24 日创建

标签: rigraphsnastatnet

解决方案


有很多不同的方法可以做你想做的事情。我不知道有什么功能可以根据您拥有的数据神奇地创建双模网络,因此下面的解决方案涉及一些数据操作。我们首先创建一个带有节点的数据框,然后创建另一个带有边的数据框。然后使用节点和边作为输入来创建一个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来更改节点/边缘的颜色、大小、形状等。

这是一个使用 package 的例子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)

这是网络现在的样子:

在此处输入图像描述

现在让我们$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)

在此处输入图像描述

我们可以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"

您可以在此 iGraph 手册(第 10-11 页)中找到更多详细信息。


推荐阅读