如何在 R 中的 networkDynamic 对象中多次有效地激活边缘属性?

How can I efficiently activate an edge attribute at multiple times in a networkDynamic object in R?

我想根据事务数据在 R 中构造一个 networkDynamic 对象,其中每一行代表一个人对文档的贡献。多重贡献应表示为边权重的增加,而不是创建多个边。

下面的代码片段应该可以在 RStudio 中重现,很容易看出问题所在。

if (!require("pacman")) install.packages("pacman"); library("pacman")
pacman::p_load(network, networkDynamic, ndtv, lubridate)

stTransac <- "
'person', 'document', 'weight', 'instantId'
'A',      'a1',       '3',      '1'
'A',      'a1',       '15',     '2'
'A',      'a1',       '100',    '3'
'B',      'a1',       '20',     '10'
'C',      'a1',       '30',     '12'
"
dfTransac <- read.csv(text = stTransac, sep = "," , quote = '\'' , strip.white = TRUE, stringsAsFactors = FALSE)

net <- network.initialize(0, directed = TRUE, bipartite = 3)

add.vertices.networkDynamic(net, 3, vertex.pid = c("A","B","C"))
add.vertices.networkDynamic(net, 1, vertex.pid = "a1")

net %v% "vertex.names" <- c(c("A","B","C"), "a1")
set.network.attribute(net,'vertex.pid','vertex.names')
set.network.attribute(net,'edge.pid','edge.names')

add.edges.networkDynamic(net,
                         tail = get.vertex.id(net, c("A","B","C")),
                         head = get.vertex.id(net, "a1"),
                         edge.pid = paste0(c("A","B","C"), "->a1"))

activate.edges(net,
               e = get.edge.id(net, paste0(dfTransac[["person"]], "->a1")),
               at = dfTransac$instantId)

到目前为止,一切正常(如果您跳过下面的 activate.edge.attribute 块并直接跳到最后一个块,您将在动画中看到边缘在时间 1,2,3 被激活,10,12.) 但是,当以与 activate.edges 函数相同的方式直观地使用 activate.edge.attribute 函数时,对于第一条边,权重属性仅在 3 处初始化为一个值100。前面的两个权重值被丢弃。

activate.edge.attribute(net,
                        prefix = "weight",
                        value = dfTransac$weight,
                        e = get.edge.id(net, paste0(dfTransac[["person"]], "->a1")),
                        at = dfTransac$instantId)

我可以遍历交易数据框,但我想这不会很好地扩展:

by(dfTransac, 1:nrow(dfTransac), function(row) {
    net <<- activate.edge.attribute(net,
               prefix = "weight",
               value = row[["weight"]],
               e = get.edge.id(net, paste0(row[["person"]], "->", row[["document"]])),
               at = row[["instantId"]])
})

最后一个块渲染动画...

reconcile.vertex.activity(net = net, mode = "encompass.edges", edge.active.default = FALSE)

compute.animation(net, slice.par = list(start = 1, end = 13, interval = 1, aggregate.dur = 1, rule = "any"))
render.animation(net)
ani.replay()

设置权重属性at多个不同时间戳的正确有效方法是什么?

部分出于效率原因,属性激活码不能在每个 vertex/edge 激活多个法术。正如文档所说:

... it is possible to use one function call to activate multiple values on multiple vertices with a different activity time on each vertex, but it is not possible to activate multiple values at multiple times on a single vertex with one call.

我建议使用以下语法使用 networkDynamic() 构造函数创建网络,它可以选择同时导入属性。

# re-arrange the data.frame column order to an edge.spell format, 
# duplicating the time to use for onset and terminus
input<-dfTransac[,c(4,4,1,2,3)]

# convert the ids to numeric
ids<-unique(c(dfTransac$person,dfTransac$document))
input[,3]<-match(input[,3],ids)
input[,4]<-match(input[,4],ids)
input
  instantId instantId.1 person document weight
1         1           1      1        4      3
2         2           2      1        4     15
3         3           3      1        4    100
4        10          10      2        4     20
5        12          12      3        4     30

# initialize a base network with the appropriate characteristics
net<-network.initialize(length(ids),bipartite=3)
# copy in the vertex names
network.vertex.names(net)<-ids

# use the networkDynamic constructor, telling it to create dynamic attributes
netDyn <- networkDynamic(net,edge.spells = input,
+                          create.TEAs = TRUE,edge.TEA.names = 'weight')

Activated TEA edge attributes:  weightCreated net.obs.period to describe network
 Network observation period info:
  Number of observation spells: 1 
  Maximal time range observed: 1 until 12 
  Temporal mode: continuous 
  Time unit: unknown 
  Suggested time increment: NA 

# print out the attributes structure for edge 1 to double check
get.edge.attribute(netDyn,'weight.active',unlist=FALSE)[[1]]
[[1]]
[[1]][[1]]
[1] 3

[[1]][[2]]
[1] 15

[[1]][[3]]
[1] 100


[[2]]
     [,1] [,2]
[1,]    1    1
[2,]    2    2
[3,]    3    3

因此我们可以看到第一个边现在具有 'weight' 的预期 3 个值。请注意 networkDynamic() 必须执行类似的循环以适当地附加动态属性,但至少它是在幕后。