多个转移矩阵的介数中心性计算
Betweenness centrality calculation for multiple transition matrices
我对如何解决这个问题有点迷茫。
例如,我有 2 个转移概率矩阵,我想将其转换为 2 个图形并保存 2 个节点的介数中心性:3110 和 110。我想将结果保存在 2 个单独的 csv 文件中。这可以使用 igraph 完成吗?
我正在处理大型数据集。如何使用转换矩阵读入 R n
个 csv 文件 - 具体文件的位置 b.) 从每个转换矩阵创建一个图形; c.) 计算特定节点的介数中心性
示例数据
df1<-structure(list(X1 = c(110, 111, 210, 300, 310, 390, 1110, 3110,
3240, 3290, 3310, 3727, 3811, 3819, 3830, 5190, 7241, 7330, 8210,
9000, 9130, 9380, 9390, 9960), `110` = c(0.939393939, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
`111` = c(0.03030303, 0, 0, 0, 0, 0, 0, 0, 0, 0.333333333,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `210` = c(0, 0,
0.571428571, 0, 0, 0, 0.025, 0.5, 0, 0, 0, 0, 0, 0.25, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), `300` = c(0, 0, 0.142857143,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0), `310` = c(0, 0, 0, 0, 0.666666667, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0), `390` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.111111111,
0, 0, 0, 0, 0), `1110` = c(0, 0, 0.142857143, 0, 0, 0, 0.925,
0, 0, 0, 0, 0, 0, 0, 0, 0.333333333, 0, 0, 0, 0, 0, 0.1,
0, 0), `3110` = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0.333333333,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `3240` = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0.1, 0, 0), `3290` = c(0.03030303, 0, 0, 0, 0, 0, 0, 0, 0,
0.333333333, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), `3310` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.666666667, 0, 0, 0.25, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), `3727` = c(0, 0, 0.142857143, 0,
0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0), `3811` = c(0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0.5,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `3819` = c(0, 0, 0, 0,
0.166666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0.1, 0, 0), `3830` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0.5, 0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `5190` = c(0,
0, 0, 0, 0, 0, 0.025, 0, 0, 0, 0, 0, 0, 0, 0, 0.666666667,
0, 0, 0, 0, 0, 0, 0, 0), `7241` = c(0, 0, 0, 0, 0.166666667,
0, 0, 0, 0, 0.333333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0), `7330` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0), `8210` = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0.777777778,
0, 0, 0, 0, 1), `9000` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0.111111111, 0, 0, 0, 0, 0), `9130` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0.833333333, 0, 1, 0), `9380` = c(0, 0, 0, 0, 0, 0, 0.025,
0, 0, 0, 0, 0, 0, 0.25, 0, 0, 0, 0, 0, 0, 0.166666667, 0.7,
0, 0), `9390` = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `9960` = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -24L), spec = structure(list(cols = list(
X1 = structure(list(), class = c("collector_double", "collector"
)), `110` = structure(list(), class = c("collector_double",
"collector")), `111` = structure(list(), class = c("collector_double",
"collector")), `210` = structure(list(), class = c("collector_double",
"collector")), `300` = structure(list(), class = c("collector_double",
"collector")), `310` = structure(list(), class = c("collector_double",
"collector")), `390` = structure(list(), class = c("collector_double",
"collector")), `1110` = structure(list(), class = c("collector_double",
"collector")), `3110` = structure(list(), class = c("collector_double",
"collector")), `3240` = structure(list(), class = c("collector_double",
"collector")), `3290` = structure(list(), class = c("collector_double",
"collector")), `3310` = structure(list(), class = c("collector_double",
"collector")), `3727` = structure(list(), class = c("collector_double",
"collector")), `3811` = structure(list(), class = c("collector_double",
"collector")), `3819` = structure(list(), class = c("collector_double",
"collector")), `3830` = structure(list(), class = c("collector_double",
"collector")), `5190` = structure(list(), class = c("collector_double",
"collector")), `7241` = structure(list(), class = c("collector_double",
"collector")), `7330` = structure(list(), class = c("collector_double",
"collector")), `8210` = structure(list(), class = c("collector_double",
"collector")), `9000` = structure(list(), class = c("collector_double",
"collector")), `9130` = structure(list(), class = c("collector_double",
"collector")), `9380` = structure(list(), class = c("collector_double",
"collector")), `9390` = structure(list(), class = c("collector_double",
"collector")), `9960` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
df2<-structure(list(X1 = c(110, 210, 310, 1110, 3110, 5310, 8210,
9120, 9390), `110` = c(0.970588235, 0, 0, 0, 0, 0, 0.125, 0,
0), `210` = c(0, 0.5, 0, 0, 0.666666667, 0, 0, 0, 0), `310` = c(0.029411765,
0.25, 0.6, 0, 0, 0, 0, 0, 0), `1110` = c(0, 0, 0, 0.981481481,
0, 0, 0, 0.25, 0), `3110` = c(0, 0, 0, 0, 0.333333333, 0.25,
0, 0, 0.037037037), `5310` = c(0, 0, 0, 0, 0, 0.75, 0, 0, 0.037037037
), `8210` = c(0, 0, 0.2, 0, 0, 0, 0.875, 0, 0), `9120` = c(0,
0.25, 0, 0, 0, 0, 0, 0.75, 0), `9390` = c(0, 0, 0.2, 0.018518519,
0, 0, 0, 0, 0.925925926)), class = c("spec_tbl_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -9L), spec = structure(list(
cols = list(X1 = structure(list(), class = c("collector_double",
"collector")), `110` = structure(list(), class = c("collector_double",
"collector")), `210` = structure(list(), class = c("collector_double",
"collector")), `310` = structure(list(), class = c("collector_double",
"collector")), `1110` = structure(list(), class = c("collector_double",
"collector")), `3110` = structure(list(), class = c("collector_double",
"collector")), `5310` = structure(list(), class = c("collector_double",
"collector")), `8210` = structure(list(), class = c("collector_double",
"collector")), `9120` = structure(list(), class = c("collector_double",
"collector")), `9390` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
你在找这个吗?
> g1 <- graph_from_adjacency_matrix(as.matrix(df1[-1]), weighted = TRUE)
> g2 <- graph_from_adjacency_matrix(as.matrix(df2[-1]), weighted = TRUE)
> v <- c("3110", "110")
> betweenness(g1, v)
3110 110
64 22
> betweenness(g2, v)
3110 110
31 7
更新
如果你想保存到单独的文件中,也许你可以试试
v <- c("3110", "110")
g <- list(g1,g2)
for (i in seq_along(g)) {
write.csv(between(g[[i]],v),file = paste0("g_",i,".csv"))
}
我对如何解决这个问题有点迷茫。
例如,我有 2 个转移概率矩阵,我想将其转换为 2 个图形并保存 2 个节点的介数中心性:3110 和 110。我想将结果保存在 2 个单独的 csv 文件中。这可以使用 igraph 完成吗?
我正在处理大型数据集。如何使用转换矩阵读入 R n
个 csv 文件 - 具体文件的位置 b.) 从每个转换矩阵创建一个图形; c.) 计算特定节点的介数中心性
示例数据
df1<-structure(list(X1 = c(110, 111, 210, 300, 310, 390, 1110, 3110,
3240, 3290, 3310, 3727, 3811, 3819, 3830, 5190, 7241, 7330, 8210,
9000, 9130, 9380, 9390, 9960), `110` = c(0.939393939, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
`111` = c(0.03030303, 0, 0, 0, 0, 0, 0, 0, 0, 0.333333333,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `210` = c(0, 0,
0.571428571, 0, 0, 0, 0.025, 0.5, 0, 0, 0, 0, 0, 0.25, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), `300` = c(0, 0, 0.142857143,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0), `310` = c(0, 0, 0, 0, 0.666666667, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0), `390` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.111111111,
0, 0, 0, 0, 0), `1110` = c(0, 0, 0.142857143, 0, 0, 0, 0.925,
0, 0, 0, 0, 0, 0, 0, 0, 0.333333333, 0, 0, 0, 0, 0, 0.1,
0, 0), `3110` = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0.333333333,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `3240` = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0.1, 0, 0), `3290` = c(0.03030303, 0, 0, 0, 0, 0, 0, 0, 0,
0.333333333, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), `3310` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.666666667, 0, 0, 0.25, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), `3727` = c(0, 0, 0.142857143, 0,
0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0), `3811` = c(0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0.5,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `3819` = c(0, 0, 0, 0,
0.166666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0.1, 0, 0), `3830` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0.5, 0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `5190` = c(0,
0, 0, 0, 0, 0, 0.025, 0, 0, 0, 0, 0, 0, 0, 0, 0.666666667,
0, 0, 0, 0, 0, 0, 0, 0), `7241` = c(0, 0, 0, 0, 0.166666667,
0, 0, 0, 0, 0.333333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0), `7330` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0), `8210` = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0.777777778,
0, 0, 0, 0, 1), `9000` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0.111111111, 0, 0, 0, 0, 0), `9130` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0.833333333, 0, 1, 0), `9380` = c(0, 0, 0, 0, 0, 0, 0.025,
0, 0, 0, 0, 0, 0, 0.25, 0, 0, 0, 0, 0, 0, 0.166666667, 0.7,
0, 0), `9390` = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `9960` = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -24L), spec = structure(list(cols = list(
X1 = structure(list(), class = c("collector_double", "collector"
)), `110` = structure(list(), class = c("collector_double",
"collector")), `111` = structure(list(), class = c("collector_double",
"collector")), `210` = structure(list(), class = c("collector_double",
"collector")), `300` = structure(list(), class = c("collector_double",
"collector")), `310` = structure(list(), class = c("collector_double",
"collector")), `390` = structure(list(), class = c("collector_double",
"collector")), `1110` = structure(list(), class = c("collector_double",
"collector")), `3110` = structure(list(), class = c("collector_double",
"collector")), `3240` = structure(list(), class = c("collector_double",
"collector")), `3290` = structure(list(), class = c("collector_double",
"collector")), `3310` = structure(list(), class = c("collector_double",
"collector")), `3727` = structure(list(), class = c("collector_double",
"collector")), `3811` = structure(list(), class = c("collector_double",
"collector")), `3819` = structure(list(), class = c("collector_double",
"collector")), `3830` = structure(list(), class = c("collector_double",
"collector")), `5190` = structure(list(), class = c("collector_double",
"collector")), `7241` = structure(list(), class = c("collector_double",
"collector")), `7330` = structure(list(), class = c("collector_double",
"collector")), `8210` = structure(list(), class = c("collector_double",
"collector")), `9000` = structure(list(), class = c("collector_double",
"collector")), `9130` = structure(list(), class = c("collector_double",
"collector")), `9380` = structure(list(), class = c("collector_double",
"collector")), `9390` = structure(list(), class = c("collector_double",
"collector")), `9960` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
df2<-structure(list(X1 = c(110, 210, 310, 1110, 3110, 5310, 8210,
9120, 9390), `110` = c(0.970588235, 0, 0, 0, 0, 0, 0.125, 0,
0), `210` = c(0, 0.5, 0, 0, 0.666666667, 0, 0, 0, 0), `310` = c(0.029411765,
0.25, 0.6, 0, 0, 0, 0, 0, 0), `1110` = c(0, 0, 0, 0.981481481,
0, 0, 0, 0.25, 0), `3110` = c(0, 0, 0, 0, 0.333333333, 0.25,
0, 0, 0.037037037), `5310` = c(0, 0, 0, 0, 0, 0.75, 0, 0, 0.037037037
), `8210` = c(0, 0, 0.2, 0, 0, 0, 0.875, 0, 0), `9120` = c(0,
0.25, 0, 0, 0, 0, 0, 0.75, 0), `9390` = c(0, 0, 0.2, 0.018518519,
0, 0, 0, 0, 0.925925926)), class = c("spec_tbl_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -9L), spec = structure(list(
cols = list(X1 = structure(list(), class = c("collector_double",
"collector")), `110` = structure(list(), class = c("collector_double",
"collector")), `210` = structure(list(), class = c("collector_double",
"collector")), `310` = structure(list(), class = c("collector_double",
"collector")), `1110` = structure(list(), class = c("collector_double",
"collector")), `3110` = structure(list(), class = c("collector_double",
"collector")), `5310` = structure(list(), class = c("collector_double",
"collector")), `8210` = structure(list(), class = c("collector_double",
"collector")), `9120` = structure(list(), class = c("collector_double",
"collector")), `9390` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
你在找这个吗?
> g1 <- graph_from_adjacency_matrix(as.matrix(df1[-1]), weighted = TRUE)
> g2 <- graph_from_adjacency_matrix(as.matrix(df2[-1]), weighted = TRUE)
> v <- c("3110", "110")
> betweenness(g1, v)
3110 110
64 22
> betweenness(g2, v)
3110 110
31 7
更新
如果你想保存到单独的文件中,也许你可以试试
v <- c("3110", "110")
g <- list(g1,g2)
for (i in seq_along(g)) {
write.csv(between(g[[i]],v),file = paste0("g_",i,".csv"))
}