来自多个 TPM 的中心性计算

Centrality calculations from multiple TPMs

需要大脑。我不知道这是否可以用 igraph 解决。基本上:

a.) 根据我的数据,我想为每个 ID 创建 TPM(完成请参见示例代码)

b.) 我想为每个 TPM 创建一个有向图

c.) 计算特定节点的中间性(在我的例子中是 1 和 5)

d.) return 在一个单独的文件中基于 id 所需节点的中间性

如何为超过 1000 个 TPM 的大型数据集执行此操作?

有些相似topic

期望的输出:

数据结构:

示例代码:

Transition matrix creation:


lapply(seq_len(nrow(stack)),
       function(i) {
         tmp <- trans.matrix(as.matrix(stack[i, 2:6]))
         write.csv(tmp, file =  paste0(i, ".csv"), quote = FALSE)
       })

每个 id 的结果 TPM,每个 df 代表每个 id 的 TPM

df1<-structure(list(X1 = c(1, 2, 3, 4), `2` = c(1, 0, 0, 0), `3` = c(0, 
1, 0, 0), `4` = c(0, 0, 1, 0), `5` = c(0, 0, 0, 1)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L), spec = structure(list(
    cols = list(X1 = structure(list(), class = c("collector_double", 
    "collector")), `2` = structure(list(), class = c("collector_double", 
    "collector")), `3` = structure(list(), class = c("collector_double", 
    "collector")), `4` = structure(list(), class = c("collector_double", 
    "collector")), `5` = 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(0, 7, 8, 9), `6` = c(0, 1, 0, 0), `7` = c(0, 
0, 1, 0), `8` = c(0, 0, 0, 1), `9` = c(1, 0, 0, 0)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L), spec = structure(list(
    cols = list(X1 = structure(list(), class = c("collector_double", 
    "collector")), `6` = structure(list(), class = c("collector_double", 
    "collector")), `7` = structure(list(), class = c("collector_double", 
    "collector")), `8` = structure(list(), class = c("collector_double", 
    "collector")), `9` = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

df3<-structure(list(X1 = c(10, 14, 22, 23), `14` = c(0, 0, 0, 1), 
    `22` = c(1, 0, 0, 0), `23` = c(0, 0, 1, 0), `25` = c(0, 1, 
    0, 0)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L), spec = structure(list(cols = list(
    X1 = structure(list(), class = c("collector_double", "collector"
    )), `14` = structure(list(), class = c("collector_double", 
    "collector")), `22` = structure(list(), class = c("collector_double", 
    "collector")), `23` = structure(list(), class = c("collector_double", 
    "collector")), `25` = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1L), class = "col_spec"))

df4<-structure(list(X1 = c(1, 2, 13), `1` = c(0, 0.5, 1), `2` = c(1, 
0, 0), `13` = c(0, 0.5, 0)), class = c("spec_tbl_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -3L), spec = structure(list(
    cols = list(X1 = structure(list(), class = c("collector_double", 
    "collector")), `1` = structure(list(), class = c("collector_double", 
    "collector")), `2` = structure(list(), class = c("collector_double", 
    "collector")), `13` = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

df5<--structure(list(X1 = c(1, 2), `1` = c(0, 0.333333333333333), `2` = c(1, 
0.333333333333333), `5` = c(0, 0.333333333333333)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L), spec = structure(list(
    cols = list(X1 = structure(list(), class = c("collector_double", 
    "collector")), `1` = structure(list(), class = c("collector_double", 
    "collector")), `2` = structure(list(), class = c("collector_double", 
    "collector")), `5` = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))



Sample data:

    stack<-structure(list(X1 = c(1, 2, 3, 4, 5), a = c(1, 0, 10, 2, 2), 
        b = c(2, 9, 22, 13, 2), c = c(3, 8, 23, 1, 1), d = c(4, 7, 
        14, 2, 2), e = c(5, 6, 25, 1, 5)), class = c("spec_tbl_df", 
    "tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L), spec = structure(list(
        cols = list(X1 = structure(list(), class = c("collector_double", 
        "collector")), a = structure(list(), class = c("collector_double", 
        "collector")), b = structure(list(), class = c("collector_double", 
        "collector")), c = structure(list(), class = c("collector_double", 
        "collector")), d = structure(list(), class = c("collector_double", 
        "collector")), e = structure(list(), class = c("collector_double", 
        "collector"))), default = structure(list(), class = c("collector_guess", 
        "collector")), skip = 1L), class = "col_spec"))

示例数据

一个可能的igraph选项

# interested vertices in all graphs
v <- c("1", "5")
data.frame(
    t(
        list2DF(
            lapply(
                # get all `df`s in the global environment and save in a list
                mget(ls(pattern = "^df\d+")),
                function(x) {
                    # row-column indices for non-zero values
                    inds <- data.frame(which(as.matrix(x[-1]) != 0, arr.ind = TRUE))
                    # replace values in `inds` by row or col names
                    df <- transform(
                        inds,
                        row = x$X1[row],
                        col = names(x[-1])[col]
                    )
                    # create graph object
                    g <- graph_from_data_frame(df)
                    # if the interested vertex shows up in the graph, then we calculate its betweenness centrality; otherwise, return NA
                    sapply(v, function(z) {
                        if (z %in% names(V(g))) {
                            betweenness(g, z, normalized = TRUE)
                        } else {
                            NA
                        }
                    })
                }
            )
        )
    ),
    check.names = FALSE
)

给予

      1  2
df1 0.0  0
df2  NA NA
df3  NA NA
df4 0.5 NA
df5 0.0  0