【问题标题】:How to perform statistical test using dplyr grouping and then make tibble with broom如何使用 dplyr 分组进行统计测试,然后用扫帚制作 tibble
【发布时间】:2019-01-21 02:27:17
【问题描述】:

我有以下数据框:

library(tidyverse)

dat <- structure(list(charge.Group3 = c(0.167, 0.167, 0.1, 0.067, 0.033, 
0.033, 0.067, 0.133, 0.2, 0.067, 0.133, 0.114, 0.167, 0.033, 
0.1, 0.033, 0.133, 0.267, 0.133, 0.233, 0.1, 0.167, 0.067, 0.133, 
0.1, 0.133, 0.1, 0.133, 0.1, 0.067, 0.167, 0), hydrophobicity.Group3 = c(0.267, 
0.467, 0.067, 0.167, 0.267, 0.1, 0.367, 0.233, 0.367, 0.233, 
0.133, 0.205, 0.333, 0.267, 0.267, 0.067, 0.133, 0.3, 0.233, 
0.267, 0.5, 0.333, 0.2, 0.5, 0.5, 0.4, 0.033, 0.3, 0.233, 0.5, 
0.233, 0.033), class = c("Negative", "Negative", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Negative", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Negative", 
"Positive", "Negative", "Negative", "Negative", "Negative", "Negative", 
"Negative", "Negative", "Negative", "Negative", "Negative", "Positive", 
"Positive", "Positive", "Negative", "Positive", "Negative")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -32L))

dat
#> # A tibble: 32 x 3
#>    charge.Group3 hydrophobicity.Group3 class   
#>            <dbl>                 <dbl> <chr>   
#>  1         0.167                 0.267 Negative
#>  2         0.167                 0.467 Negative
#>  3         0.1                   0.067 Positive
#>  4         0.067                 0.167 Positive
#>  5         0.033                 0.267 Positive
#>  6         0.033                 0.1   Positive
#>  7         0.067                 0.367 Positive
#>  8         0.133                 0.233 Negative
#>  9         0.2                   0.367 Positive
#> 10         0.067                 0.233 Positive
#> # ... with 22 more rows

我想对每个功能做些什么:charge.Group3 和 hydrophobicity.Group3,在 Negative 和 positive 类之间执行 wilcox.test。最后将 p 值作为数据框或小标题:

features                      pvalue
charge.Group3                 0.1088  
hydrophobicity.Group3         0.03895
# I do by hand.

请注意,实际上有两个以上的功能。 我怎样才能做到这一点?

【问题讨论】:

    标签: r tidyverse broom


    【解决方案1】:

    如果您只需要测试的 p 值,则实际上不需要使用 broom。

    library(tidyverse)
    
    
    dat %>% 
      gather(group, value, -class) %>%    # reshape data            
      nest(-group) %>%                    # for each group nest data
      mutate(pval = map_dbl(data, ~wilcox.test(value ~ class, data = .)$p.value)) %>%  # get p value for wilcoxon test
      select(-data)                       # remove data column
    
    
    # # A tibble: 2 x 2
    #   group                   pval
    #   <chr>                  <dbl>
    # 1 charge.Group3         0.109 
    # 2 hydrophobicity.Group3 0.0390        
    

    假设class 是唯一的额外变量,那么无论您有多少列,首先重塑都可以让您应用此过程。

    或者你甚至可以避免map @Moody_Mudskipper 建议使用

    dat %>% 
      gather(group, value, -class) %>% 
      group_by(group) %>% 
      summarize(results = wilcox.test(value ~ class)$p.value)
    

    如果你真的想参与broom 那么你可以这样做

    library(broom)
    
    dat %>% 
       gather(group, value, -class) %>%  
       nest(-group) %>%                  
       mutate(results = map(data, ~tidy(wilcox.test(value ~ class, data = .)))) %>%
       select(-data) %>%
       unnest(results)
    
    # # A tibble: 2 x 5
    # group                 statistic p.value method                                            alternative
    #   <chr>                     <dbl>   <dbl> <chr>                                             <chr>      
    # 1 charge.Group3              170.  0.109  Wilcoxon rank sum test with continuity correction two.sided  
    # 2 hydrophobicity.Group3      183   0.0390 Wilcoxon rank sum test with continuity correction two.sided 
    

    返回更多列,但您可以根据需要保留 p 值。

    【讨论】:

    • 我认为你可以通过跳过嵌套让它变得非常好和惯用,在这里分组就足够了:dat %&gt;% gather(group, value, -class) %&gt;% group_by(group) %&gt;% summarize(pval = wilcox.test(value ~ class)$p.value)(无论如何都赞成)
    • 确实如此。这就是我的想法,但由于某种原因,今天我使用了summarize(results = wilcox.test(value ~ class, data = .)$p.value),但由于data = . 的事情,它不起作用! :( 谢谢提醒。
    【解决方案2】:

    这是一种使用 dplyr::summarize_at 和 tidyr::gather 的方法:

    library(tidyverse)
    dat %>%
      summarize_at(c("charge.Group3","hydrophobicity.Group3"),
                   ~wilcox.test(.x ~ .y)$p.value, .$class) %>%
      gather(features, pvalue)
    
    # # A tibble: 2 x 2
    #                features pvalue
    #                   <chr>  <dbl>
    # 1         charge.Group3  0.109
    # 2 hydrophobicity.Group3  0.039
    

    总结除class之外的所有变量:

    dat %>%
      summarize_at(vars(-class),
                   ~wilcox.test(.x ~ .y)$p.value,
                   .$class) %>%
      gather(features,pvalue)
    

    【讨论】:

    • 谢谢。但是我怎样才能概括你的代码。因为超过 2 个功能。我无法在 summarised_at 对它们进行硬编码
    • 是的。除了课堂之外的一切。
    • 改为公式表示法,因为它更紧凑(受@AntoniosK 启发)
    猜你喜欢
    • 2016-10-09
    • 2020-07-18
    • 2015-05-12
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    相关资源
    最近更新 更多