使用R和igraph
,我有两个已合并的图表,然后运行constraint()
。约束适用于g1
,但当我添加g2
时,constraint()
会为NA
添加的新顶点和这些新顶点旁边的顶点返回g2
。
以下是复制我的问题的示例代码。我从二分图的投影中获得g1
,因为这反映了我的数据处理。
我的问题可能是两个图表联合的问题,但无论哪种方式我都尝试(g3 <- g1 + g2
或g3 <- graph.union(g1, g2)
),约束计算会带来NA
s。
set.seed(42)
g <- sample_bipartite(100, 10, type = c("gnp"), p=.03, directed = FALSE)
gproj <- bipartite_projection(g, types=NULL, multiplicity = TRUE)
g1 <- gproj[[1]]
V(g1)$name <- 1:vcount(g1) #this gets their actual vertex id to show as label
V(g1)$name
components <- decompose.graph(g1)
largest <- which.max(sapply(components, vcount))
largest #this tells me which component is largest
lc <- components[[largest]]
lc
plot(lc)
cg1 <- constraint(lc)
cg1 #constraint for all connected vertices is calculated
#Create g2, some vertices in g1, some are new
rel <- data.frame( rel1 =
c(95), rel2 =
c(2000), stringsAsFactors = F) #create edgelist of g2, one vertex in lc, one vertex new
g2 <- graph.data.frame(rel, directed=FALSE)
#combine graphs and calculate constraint on combined graph
#g1 is used instead of lc because relationships in g2 may connect previously isolated vertices/components
g3 <- g1 + g2
components1 <- decompose.graph(g3)
largest1 <- which.max(sapply(components1, vcount))
largest1 #this tells me the first component is largest
lc1 <- components1[[largest1]]
plot(lc1)
cg3 <- constraint(lc1)
cg3 #now constraint vertices close to 2000 is 'NA'
有关进一步的信息,其他igraph测量,如特征向量中心,学位和bonpower不会遇到这个问题。
答案 0 :(得分:0)
通过一些额外的调查,我找到了一个解决方案。问题是g1
有权重,而g2
没有。删除权重后,使用此代码(并使用g1a
向下行)constraint
计算所有顶点。
g1a <- remove.edge.attribute(g1, "weight")
甚至比为这个玩具示例工作更好,这个解决方案适用于我的完整,更大的数据集。