0

我正在研究政府机构如何随时间变化。计划是使用 ndtv 包来可视化更改。但是,我遇到了问题,不知道我做错了什么!我怀疑我的数据格式不正确。

我有一个节点列表,其中列出了顶点 ID、代理名称、节点起始和节点终点:

nodelist <- read.csv("https://github.com/aterhorst/data/raw/master/edgelist.csv", header=T, stringsAsFactors = F)

和一个边缘列表,显示边缘的起点和终点:

edgelist <- read.csv("https://github.com/aterhorst/data/raw/master/edgelist.csv", header=T, stringsAsFactors = F)

我可以很容易地创建一个网络对象:

library(tidyverse)
library(sna)
nw <- network(edgelist %>% select(head, tail),
              vertex_attr = nodelist %>% select(vertex.id, agency),
              vertex.attrnames = c("vertex.id", "agency"),
              directed = F)
print(nw)
plot(nw)

现在我想做一个动态网络对象:

library(tsna)
library(ndtv)
dn <- networkDynamic(nw, 
                     edge.spells = edgelist,
                     vertex.spells = nodelist)

然后车轮脱落:

Error in networkDynamic(nw, edge.spells = edgelist, vertex.spells = nodelist) : 
  vertex.spells requires the vertex.id column to be numeric

我尝试将起始、终点从整数更改为数字 - 没有区别。我尝试了不同的方法来创建动态网络对象,例如

dn <- networkDynamic(edge.spells = edgelist)

这会产生此错误:

Initializing base.net of size 2020 imputed from maximum vertex id in edge records
Error in activate.edges(base.net, onset = edge.data[, 1], terminus = edge.data[,  : 
  Onset times must precede terminus times in activate.edges.

和:

dn <- networkDynamic(edge.spells = edgelist, vertex.spells = nodelist)

给我这个错误:

Error in networkDynamic(edge.spells = edgelist, vertex.spells = nodelist) : 
  vertex.spells requires the vertex.id column to be numeric 

我玩弄了 edge.spell 的顺序,这产生了积极的结果:

 dn <- networkDynamic(net, 
                      edge.spells = edgelist %>% select(onset, terminus, tail, head))

Edge activity in base.net was ignored
Created net.obs.period to describe network
 Network observation period info:
  Number of observation spells: 1 
  Maximal time range observed: 2014 until 2020 
  Temporal mode: continuous 
  Time unit: unknown 
  Suggested time increment: NA 

都好。现在当我添加 vertex.spell 我得到这个错误:

dynamicNet <- networkDynamic(net, 
                         edge.spells = edgelist %>% select(onset, terminus, tail, head), 
                         vertex.spells = nodelist %>% select(onset, terminus, vertex.id) %>% 
                         mutate(vertex.id = as.numeric(vertex.id)))

Error in networkDynamic(net, edge.spells = edgelist %>% select(onset,  : 
  vertex.spells requires the vertex.id column to be numeric

这很奇怪,因为我明确指出 vertex.id 是数字!显然,我在某个地方填满了格式,但在哪里并不明显。任何帮助,将不胜感激。

4

1 回答 1

2

在这种情况下,我认为您不需要首先创建静态网络(应该可以)。主要问题似乎只是列表中的列排序。帮助文件?networkDynamic 表明它需要特定的edge.spellsand顺序vertex.spells

edge.spells“...假定为 [onset,terminus,tail vertex.id, head vertex.id]....”

vertex.spells“...假定为 [onset,terminus,vertex.id]”

您的数据如下所示:

> head(nodelist)
  vertex.id                                  agency onset terminus
1         1                             AAF Company  2014     2020
2         2              Aboriginal Hostels Limited  2014     2020
3         3         Administrative Appeals Tribunal  2014     2020
4         4 Aged Care Quality and Safety Commission  2014     2020
5         5                   Airservices Australia  2014     2020
6         6  Albury-Wodonga Development Corporation  2014     2020
> head(edgelist)
  head tail onset terminus
1   10    3  2014     2020
2   10   11  2014     2020
3   10   12  2014     2020
4   10   18  2014     2020
5   10   22  2014     2020
6   10   23  2014     2020

因此,如果您的数据的 cols 被重新排序..

nd <-networkDynamic(edge.spells = edgelist[,c(3,4,2,1)],
  vertex.spells=nodelist[,c(3,4,1)])

Initializing base.net of size 217 imputed from maximum vertex id in edge records
Created net.obs.period to describe network
 Network observation period info:
  Number of observation spells: 1 
  Maximal time range observed: 2014 until 2020 
  Temporal mode: continuous 
  Time unit: unknown 
  Suggested time increment: NA 

这会创建网络对象和动态。由于看起来您nodelist每个顶点只有一行,因此可以带入标签

network.vertex.names(nd)<-nodelist$agency
于 2019-05-14T06:09:54.067 回答