ggplot2 stat_compare_means和wilcox.test中的p值不同
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ggplot2 stat_compare_means和wilcox.test中的p值不同相关的知识,希望对你有一定的参考价值。
我尝试使用ggplot
函数将p值添加到我的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的结果
Wilcoxon签名等级测试的结果:
数据:通过测试$ c2测试$ c1 V = 0,p值= 0.0004883 备选假设:真正的位置偏移不等于0
如您所见,结果是ggplot中的p = 0.0025和基本wilcox.test函数的p = 0.0004883。你知道为什么会有所不同吗?哪个值是正确的?
PS:我试图用ToothGrowths做同样的事情。在那种情况下,stat_compare_means
和wilcox.test
的结果显示相同的结果:p = 0.004313。我不知道为什么它不适用于我的数据:/
在一种情况下,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)
以上是关于ggplot2 stat_compare_means和wilcox.test中的p值不同的主要内容,如果未能解决你的问题,请参考以下文章
R - stat_compare_means 从 Kruskal-Wallis 测试返回不同的值
ggpubr:更改 stat_compare_means Kruskal-Wallis p 值的字体大小
将自定义注释添加到 p 值标签到 ggpubr stat_compare_means()