【问题标题】:Row-wise Fisher Exact Test, grouped by samples in R按 R 中的样本分组的逐行 Fisher 精确检验
【发布时间】:2021-05-18 20:33:06
【问题描述】:

考虑以下数据框:

df1
#   bacteria           sample     Number_x          Number_y    
#1        A           HM_001          100                30
#2        B           HM_001           50                60
#3        C           HM_001          300                10
#4        D        A2_HM_001          400                20
#5        E        A2_HM_001           22                11
#6        F           HM_002           23                35
#7        G           HM_002          120                46
#8        H           HM_003           50                51
# … with 1,342 more rows

按样本分组,我希望对每种细菌进​​行逐行双边 Fisher 精确检验。 (例如 HM_001 如下所示)。

HM_001 Number_x Number_y
A 100 30
Others (B and C in this case) 350 70
HM_001 Number_x Number_y
B 50 60
Others (A and C in this case) 400 40

等等,本质上是为数据帧中的 1350 行中的每一行生成一个 p 值。

以下是我的尝试:

Fisher_result <- df1 %>%   
  group_by(sample) %>% 
  row_wise_fisher_test(as.matrix(df1[,c(3,4)]), p.adjust.method = "BH")

但是没有用,输出如下错误信息:

Error in row_wise_fisher_test(., as.matrix(df1[, c(3, 4)]),  : 
  A cross-tabulation with two columns required

任何指针将不胜感激!

【问题讨论】:

    标签: r dplyr rstatix


    【解决方案1】:

    您可以group_by 每个sample 并将row_wise_fisher_test 应用于每个组,并使用unnest 将它们放在不同的列中。

    library(dplyr)
    library(tidyr)
    library(rstatix)
    
    df1 %>%
      group_by(sample) %>%
      summarise(data = list(row_wise_fisher_test(as.matrix(select(cur_data(), 
                            starts_with('Number'))), p.adjust.method = "BH"))) %>%
      unnest_wider(data) %>%
      unnest(c(group:p.adj.signif)) -> Fisher_result
    
    Fisher_result
    
    # sample    group     n        p    p.adj p.adj.signif
    #  <chr>     <chr> <int>    <dbl>    <dbl> <chr>       
    #1 A2_HM_001 1       453 1.73e- 6 1.73e- 6 ****        
    #2 A2_HM_001 2       453 1.73e- 6 1.73e- 6 ****        
    #3 HM_001    1       550 1.18e- 1 1.18e- 1 ns          
    #4 HM_001    2       550 9.31e-24 1.40e-23 ****        
    #5 HM_001    3       550 1.57e-26 4.71e-26 ****        
    #6 HM_002    1       224 1.44e- 5 1.44e- 5 ****        
    #7 HM_002    2       224 1.44e- 5 1.44e- 5 ****        
    #8 HM_003    1       101 1.00e+ 0 1.00e+ 0 ns         
    

    数据

    df1 <- structure(list(bacteria = c("A", "B", "C", "D", "E", "F", "G", 
    "H"), sample = c("HM_001", "HM_001", "HM_001", "A2_HM_001", "A2_HM_001", 
    "HM_002", "HM_002", "HM_003"), Number_x = c(100L, 50L, 300L, 
    400L, 22L, 23L, 120L, 50L), Number_y = c(30L, 60L, 10L, 20L, 
    11L, 35L, 46L, 51L)), class = "data.frame", row.names = c(NA, -8L))
    

    【讨论】:

    • Error: Problem with summarise()` 输入data。 x 需要两列的交叉表 ℹ 输入 datalist(...)。 ℹ 第1组出现错误:sample = "HM_001".`这个错误提示
    • @JujutsuR 您能否提供一个可重现的示例,我可以使用它重现您遇到的错误?
    • 我的数据框和我发布的问题一模一样;只是没有 Number_x 和 Number_y,我有最后两列分别名为 Reads_Community_T 和 Reads Community_N。
    • df1 %&gt;% group_by(sample) %&gt;% summarise(data = list(row_wise_fisher_test(as.matrix(select(cur_data(), starts_with('Reads_Community_T'))), p.adjust.method = "BH"))) %&gt;% unnest_wider(data) %&gt;% unnest(c(group:p.adj.signif)) -&gt; Fisher_result Fisher_result
    • @JujutsuR 你应该使用 starts_with('Reads_Community') 而不是 starts_with('Reads_Community_T') 。我还包括了我使用的数据,它对我有用,没有任何错误,如图所示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2014-01-26
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多