简体   繁体   English

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

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

I want to construct a networkDynamic object in R from transaction data where each line represents a contribution by a person to a document. 我想从交易数据构造R中的networkDynamic对象,其中每一行代表一个人对文档的贡献。 Multiple contributions should be represented as an increase in edge weight instead of creating multiple edges. 应将多个贡献表示为边缘权重的增加,而不是创建多个边缘。

The following code snippets should be reproducible in RStudio easily to see the problem. 以下代码段应该可以很容易地在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)

Up to now, everything works as expected (if you skip the activate.edge.attribute block below and jump directly to the last block, you will see in the animation that edges are activated at times 1,2,3,10,12.) However, when using the activate.edge.attribute function intuitively in the same way as the activate.edges function, for the first edge the weight attribute is only initialized at 3 with a value of 100 . 到目前为止,一切都按预期工作(如果您跳过下面的activate.edge.attribute块并直接跳到最后一个块,您将在动画中看到边缘在1,2,3,10,12时间被激活。 )但是,当以与activate.edge.attribute函数相同的方式直观地使用activate.edges函数时,对于第一个边缘,权重属性仅初始化为3 ,其值为100 The earlier two weight values are dropped. 前两个权重值将被删除。

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

I could iterate over the transaction data frame, but I suppose this will not scale too well: 我可以遍历事务数据帧,但是我想这不会很好地扩展:

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"]])
})

This last block renders the animation... 最后一块渲染动画...

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()

What is the correct and efficient way to set the weight attribute at multiple different timestamps? at多个不同的时间戳上设置权重属性的正确有效方法是什么?

Partially for efficiency reasons, the attribute activate code cannot activate multiple spells per vertex/edge. 部分出于效率的考虑,属性激活代码无法为每个顶点/边缘激活多个咒语。 As the documentation says: 如文档所述:

... 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. ...可以使用一个函数调用来激活多个顶点上具有不同活动时间的多个顶点上的多个值,但是无法通过一次调用在单个顶点上多次激活多个值。

I would recommend the following syntax for creating your network with the networkDynamic() constructor function, which has the option to import attributes at the same time. 我建议使用以下语法通过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

So we can see that the first edge now has the expected 3 values for 'weight'. 因此,我们可以看到第一个边缘现在具有“权重”的预期3个值。 Note the networkDynamic() has to do similar looping to appropriately attach the dynamic attributes, but at least it is all under the hood. 请注意, networkDynamic()必须执行类似的循环以适当地附加动态属性,但至少它全在幕后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R中使用MDSJ渲染引擎来渲染networkDynamic网络? - How can I render a networkDynamic network using the MDSJ rendering engine in R? 在R包igraph中,如何通过将边缘属性除以出节点的节点属性来规范边缘属性? - In R package igraph, how can I normalize an edge attribute by dividing the edge attribute by a node attribute of the out-node? 构建网络时边和顶点属性映射不正确Dynamic object - Edge and vertex attributes not mapping properly when constructing networkDynamic object 在R中,如何根据多个属性分数从igraph对象生成子图? - In R, how can I generate a subgraph from a igraph object based on multiple attribute scores? 使用 R 中的 statnet 将边缘属性添加到网络对象 - Adding edge attribute to network object with statnet in R 如何在R中有效地将分层网络的表转换为边缘列表 - How to transform a table of an hierarchical network to an edge list efficiently in R 如何激活来自 R 的“.js”脚本? - How can I activate a '.js' script from R? 如何有效地使用Java的R预测模型? - How can I efficiently use an R prediction model from Java? 如何有效地请求 R 中的一部分栅格堆栈 - How can I efficiently request part of a rasterstack in R 如何在R中添加小数倍? - How can I add fractional times in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM