【问题标题】:Creating transition matrices in R using "table" function [closed]使用“表”函数在 R 中创建转换矩阵 [关闭]
【发布时间】:2021-07-13 09:12:03
【问题描述】:

我有一个包含 4 列的数据框: 周期,ID,当前周期的类别,下一个周期的类别。

period <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
ID <- c('a12', 'a123', 'a1234', 'a12345', 'b12', 'b123', 'b1234', 'b12345', 'c12', 'c123')
category_t0 <- c(1, 2, 3, 4, 1, 1, 1, 2, 3, 5, 1, 3, 2, 5, 1, 1, 2, 3, 4, 1)
category_t1 <- c(1, 3, 2, 5, 1, 1, 2, 3, 4, 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA')
df <- data.frame(period, ID, category_t0, category_t1)

我需要为每个时期创建单独的转换矩阵。我试图使用“表格”功能,但无法弄清楚如何在某个条件下使用某个时间段。以下代码考虑了数据框中的所有观察结果,而我只需要采用“period == 1”的值。

trans_1to2 = table(df$category_t0, df$category_t1, useNA = c("ifany"))
matrix_1to2 = trans_1to2 / rowSums(trans_1to2)
print(matrix_1to2)

有人可以分享这方面的想法吗? 此外,真实数据有 50 个周期,因此我需要创建 50 个不同的转换矩阵。最好的方法是什么(例如使用循环)?

谢谢!

【问题讨论】:

  • 如果您可以提供一些小的虚拟数据而不是图像,这将很有帮助,因为这样可以确保我们完全理解问题并且还可以提供有效的答案

标签: r matrix


【解决方案1】:

您可以在[ 中使用df$period==1df 进行子集化,以获取第1 期的表格。

trans_1to2 = table(df[df$period==1, 3:4])
trans_1to2 / rowSums(trans_1to2)
#           category_t1
#category_t0    1    2    3    4    5   NA
#          1 0.75 0.25 0.00 0.00 0.00 0.00
#          2 0.00 0.00 1.00 0.00 0.00 0.00
#          3 0.00 0.50 0.00 0.50 0.00 0.00
#          4 0.00 0.00 0.00 0.00 1.00 0.00
#          5 0.00 0.00 0.00 0.00 0.00 1.00

要使其适用于多个时期,只需将时期也添加到table

trans_1to2 <- table(df[c(3,4,1)])
matrix_1to2 <- proportions(trans_1to2, c(1,3))

matrix_1to2[,,1] #Get first period
#           category_t1
#category_t0    1    2    3    4    5   NA
#          1 0.75 0.25 0.00 0.00 0.00 0.00
#          2 0.00 0.00 1.00 0.00 0.00 0.00
#          3 0.00 0.50 0.00 0.50 0.00 0.00
#          4 0.00 0.00 0.00 0.00 1.00 0.00
#          5 0.00 0.00 0.00 0.00 0.00 1.00

matrix_1to2[,,2] #Get second period
#           category_t1
#category_t0 1 2 3 4 5 NA
#          1 0 0 0 0 0  1
#          2 0 0 0 0 0  1
#          3 0 0 0 0 0  1
#          4 0 0 0 0 0  1
#          5 0 0 0 0 0  1

【讨论】:

    【解决方案2】:
    period <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
    ID <- c('a12', 'a123', 'a1234', 'a12345', 'b12', 'b123', 'b1234', 'b12345', 'c12', 'c123')
    category_t0 <- c(1, 2, 3, 4, 1, 1, 1, 2, 3, 5, 1, 3, 2, 5, 1, 1, 2, 3, 4, 1)
    category_t1 <- c(1, 3, 2, 5, 1, 1, 2, 3, 4, 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA')
    df <- data.frame(period, ID, category_t0, category_t1)
    
    library(tidyverse)
    df %>% 
      mutate(across(starts_with("category_"), as.factor)) %>% 
      group_split(period) %>% 
      purrr::set_names(nm = str_c("period_", unique(period))) %>% 
      map(~table(.x$category_t0, .x$category_t1) %>% prop.table(margin = 1))
    #> $period_1
    #>    
    #>        1    2    3    4    5   NA
    #>   1 0.75 0.25 0.00 0.00 0.00 0.00
    #>   2 0.00 0.00 1.00 0.00 0.00 0.00
    #>   3 0.00 0.50 0.00 0.50 0.00 0.00
    #>   4 0.00 0.00 0.00 0.00 1.00 0.00
    #>   5 0.00 0.00 0.00 0.00 0.00 1.00
    #> 
    #> $period_2
    #>    
    #>     1 2 3 4 5 NA
    #>   1 0 0 0 0 0  1
    #>   2 0 0 0 0 0  1
    #>   3 0 0 0 0 0  1
    #>   4 0 0 0 0 0  1
    #>   5 0 0 0 0 0  1
    

    reprex package (v2.0.0) 于 2021-07-13 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多