【问题标题】:add results of chi square test to each row将卡方检验的结果添加到每一行
【发布时间】:2017-05-07 09:46:34
【问题描述】:

我有兴趣在每一行上生成一个卡方值(X 平方和 p 值)并将测试结果附加到单独的列中。我拥有的数据是每行的一个基因和两个不同组的突变或正常(野生型)计数。这是示例数据集的设置:

Genes<-c("GENE_A", "GENE_B","GENE_C")
Group1_Mut<-c(20,10,5)
Group1_WT<-c(40,50,55)
Group2_Mut<-c(10, 30, 10)
Group2_WT<-c(80, 60, 80)
main<-data.frame(Genes,Group1_Mut,Group1_WT,Group2_Mut,Group2_WT)

当我尝试将第一行作为矩阵传递给卡方检验时,出现以下错误:

chisq.test(矩阵(main[1,2:5], nrow=2, 2,2)) sum(x) 中的错误:参数的“类型”(列表)无效

有什么想法可以为 2x2 表创建一个函数来遍历列表并为主表中的每个基因附加 X 平方和 p 值吗?

注意:我确实在 SF 中看到了另一个示例:chi square test for each row in data frame

但它不太适合我在这里尝试应用的内容。

【问题讨论】:

    标签: r


    【解决方案1】:

    这里的奇怪之处在于您没有给matrix 一个向量,而是给它一个数据框。

    main[1,2:5]
      Group1_Mut Group1_WT Group2_Mut Group2_WT
    1         20        40         10        80
    

    由于矩阵中的每个元素都必须具有相同的类型,因此您的矩阵元素最终都是列表。

    m <- matrix(main[1,2:5], nrow=2, byrow = TRUE)
    
    class(m)
    "matrix"
    typeof(m)
    "list"
    
    class(m[1, 1])
    "list"
    

    在调用matrix之前,您需要unlist您的数据框元素

    chisq.test(matrix(unlist(main[1, 2:5], nrow = 2, byrow = TRUE)))
    

    这会产生你想要的。

    【讨论】:

    • 比我快 33 秒 ... ;-)
    • 可能是因为我在一些细节上懈怠了。 :)
    【解决方案2】:

    要了解错误尝试通信的原因,请将您的数据与 chisq.test 预期的数据类型进行比较:

    dput(matrix(main[1,2:5,drop=T], nrow=2, 2,2))
    # structure(list(20, 10, 40, 80), .Dim = c(2L, 2L))
    dput(matrix(1:4, nrow=2, 2,2))
    # structure(c(1L, 3L, 2L, 4L), .Dim = c(2L, 2L))
    

    一种补救方法是强制您将数据放入numeric 向量中:

    res <- chisq.test(matrix(as.numeric(main[1,2:5]), nrow=2, 2,2))
    res
    #   Pearson's Chi-squared test with Yates' continuity correction
    # data:  matrix(as.numeric(main[1, 2:5]), nrow = 2, 2, 2)
    # X-squared = 9.7656, df = 1, p-value = 0.001778
    

    现在,如果您想将结果添加到每一行,您首先需要选择“哪些结果”。也就是说,结果实际上有点美化了,内部有几个花絮:

    str(unclass(res))
    # List of 9
    #  $ statistic: Named num 9.77
    #   ..- attr(*, "names")= chr "X-squared"
    #  $ parameter: Named int 1
    #   ..- attr(*, "names")= chr "df"
    #  $ p.value  : num 0.00178
    #  $ method   : chr "Pearson's Chi-squared test with Yates' continuity correction"
    #  $ data.name: chr "matrix(as.numeric(main[1, 2:5]), nrow = 2, 2, 2)"
    #  $ observed : num [1:2, 1:2] 20 10 40 80
    #  $ expected : num [1:2, 1:2] 12 18 48 72
    #  $ residuals: num [1:2, 1:2] 2.309 -1.886 -1.155 0.943
    #  $ stdres   : num [1:2, 1:2] 3.33 -3.33 -3.33 3.33
    

    如果您想将(例如)测试统计数据包含在一个数字中,您可以这样做:

    chisq.statistic <- sapply(seq_len(nrow(main)), function(row) {
      chisq.test(matrix(as.numeric(main[row,2:5]), nrow=2, 2,2))$statistic
    })
    main$chisq.statistic <- chisq.statistic
    main
    #    Genes Group1_Mut Group1_WT Group2_Mut Group2_WT chisq.statistic
    # 1 GENE_A         20        40         10        80      9.76562500
    # 2 GENE_B         10        50         30        60      4.29687500
    # 3 GENE_C          5        55         10        80      0.07716049
    

    请注意,dplyrdata.table 之类的工具可能会促进这一点。例如:

    library(dplyr)
    main %>%
      rowwise() %>%
      mutate(
        chisq.statistic = chisq.test(matrix(c(Group1_Mut, Group1_WT, Group2_Mut, Group2_WT), nrow = 2))$statistic
      )
    # Source: local data frame [3 x 6]
    # Groups: <by row>
    # # A tibble: 3 × 6
    #    Genes Group1_Mut Group1_WT Group2_Mut Group2_WT chisq.statistic
    #   <fctr>      <dbl>     <dbl>      <dbl>     <dbl>           <dbl>
    # 1 GENE_A         20        40         10        80      9.76562500
    # 2 GENE_B         10        50         30        60      4.29687500
    # 3 GENE_C          5        55         10        80      0.07716049
    

    此示例显示了您可能希望在使用的任何方法中加入的一件事:显式命名列。也就是说,“2:5”可能会根据您的输入矩阵而改变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      相关资源
      最近更新 更多