首页 > 解决方案 > ggplot2 stat_compare_means 和 wilcox.test 中的不同 p 值

问题描述

我尝试将 p 值添加到我ggplot的使用stat_compare_means函数中。但是,我在 ggplot 中得到的 p 值与基本 wilcox.test 的结果不同。

我在这两种情况下都使用了配对测试,并且还在 ggplot 中使用了 wilcoxon 测试。

我试图搜索我的问题,但找不到确切的答案。我更新了 R (v. 3.5.2)、R-Studio (v. 1.1.463) 和所有软件包。在下面我添加了几行代码和一个例子。我是 R 和统计的新手,所以如果我以新手的方式提问,请原谅我。

library("ggplot2")  
library("ggpubr")


c1 <- c( 798.3686, 2560.9974,  688.3051,  669.8265, 2750.6638, 1136.3535,  
         1335.5696, 2347.2777, 1149.1940,  901.6880, 1569.0731 ,3915.6719,  
         3972.0250 ,5517.5016, 4616.6393, 3232.0120, 4020.9727, 2249.4150,  
         2226.4108, 2582.3705, 1653.4801, 3162.2784, 3199.1923, 4792.6118)  
c2 <- c(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1)  

test <-data.frame(c2,c1)  

test$c2 <- as.factor(test$c2)  

ggplot(test, aes(x=c2, y=c1)) +  
  stat_compare_means(paired = TRUE)  

wilcox.test( test$c1~ test$c2, paired= TRUE)  

ggplot 中 stat_compare_means 的结果 ggplot 中 stat_compare_means 的结果

Wilcoxon 符号秩检验的结果:

数据:test$c1 by test$c2
V = 0, p-value = 0.0004883 备择
假设:真实位置偏移不等于 0

如您所见,结果是 ggplot 中的 p = 0.0025 和基本 wilcox.test 函数的 p = 0.0004883。你知道为什么不一样吗?哪个值是正确的?

PS:我尝试对 ToothGrowths 做同样的事情。在这种情况下,结果stat_compare_meanswilcox.test显示相同的结果:p = 0.004313。我不知道为什么它不适用于我的数据:/

标签: rggplot2p-value

解决方案


在一种情况下,p 值是精确的,而在另一种情况下,它是正态近似值。

wilcox.test( test$c1~ test$c2, paired = TRUE, exact = TRUE)
# Wilcoxon signed rank test
# 
# data:  test$c1 by test$c2
# V = 0, p-value = 0.0004883
# alternative hypothesis: true location shift is not equal to 0

wilcox.test( test$c1~ test$c2, paired = TRUE, exact = FALSE)
# Wilcoxon signed rank test with continuity correction
# 
# data:  test$c1 by test$c2
# V = 0, p-value = 0.002526
# alternative hypothesis: true location shift is not equal to 0

根据help(wilcox.test),如果样本包含的值少于 50 个(如您的情况),则会计算精确的 p 值(除非您另有指定)。

stat_compare_means有一个method.args论点,但它似乎没有exact = TRUE正确传递规范。相反,您可以先按照您想要的方式准确计算 p 值,然后将其添加到图中:

exact_pvalue <-
  wilcox.test( test$c1~ test$c2, paired = TRUE, exact = TRUE) %>%
  # Format the test output as a tibble
  broom::tidy() %>%
  # Format the p-value
  mutate(pval_fmt = format.pval(p.value, digits = 2)) %>%
  # Specify position in (c1, c2) coordinates
  mutate(c1 = 5518, c2 = 0)
exact_pvalue
# A tibble: 1 x 7
#  statistic  p.value method                    alternative pval_fmt    c1    c2
#      <dbl>    <dbl> <chr>                     <chr>       <chr>    <dbl> <dbl>
#1         0 0.000488 Wilcoxon signed rank test two.sided   0.00049   5518     0

ggplot(test, aes(x=c2, y=c1)) +
  geom_text(aes(label = glue::glue("Wilcoxon p = {pval_fmt}")), 
            data = exact_pvalue)

您可以推广这种方法以同时执行多个测试并在最后创建一个多面图。需要大量使用 tidyverse 魔法。

library("tidyverse")

test2 <-
  # Fake data with two subsets to run to test on (in this case the p-value
  # will be the same because the subsets actually contain the same data).
  bind_rows(test, test, .id = "subset") %>%
  # Group by subset and nest the data columns. This creates a "list of
  # tibbles" column called "data".
  group_by(subset) %>%
  nest() %>%
  # Use `purrr::map` to perform the test on each group.
  mutate(wilcox = map(data, ~ wilcox.test(.x$c1 ~ .x$c2,
                                          paired = TRUE, exact = TRUE))) %>%
  # And again `purrr::map` to tidy the test results.
  # Now we have two list columns, one with the data and the other with 
  # the test results
  mutate(wilcox = map(wilcox, broom::tidy))
test2
# A tibble: 2 x 3
# subset data              wilcox
# <chr>  <list>            <list>
#   1 1      <tibble [24 x 2]> <tibble [1 x 4]>
#   2 2      <tibble [24 x 2]> <tibble [1 x 4]>

test2 %>%
  unnest(data) %>%
  ggplot(aes(c1, c2)) +
  # Plot the raw data
  geom_point() +
  # Add the p-value
  geom_text(data = test2 %>% unnest(wilcox),
            # Specify the aestetic mapping so that the p-value is
            # plotted in the top right corner of each plot.
            aes(x = Inf, y = Inf, label = format.pval(p.value, digits = 2)),
            inherit.aes = FALSE, hjust = "inward", vjust = "inward") +
  # Do this for each subset in its own subplot.
  facet_wrap(~ subset)

推荐阅读