ggplot2 stat_compare_means和wilcox.test

时间:2019-02-23 13:14:13

标签: r ggplot2 p-value

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

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

我试图搜索我的问题,但找不到确切答案。 我更新了R(3.5.2版),R-Studio(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的结果 Result of the stat_compare_means within the ggplot

Wilcoxon签名等级测试的结果:

  

数据:由test $ c2测试$ c1
  V = 0,p值= 0.0004883
  替代假设:真实的位置偏移不等于0

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

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

1 个答案:

答案 0 :(得分:2)

在一种情况下,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)

您可以推广这种方法以同时执行多个测试,并在最后创建多面图。需要更广泛地使用整形魔术。

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)
相关问题