R10=] return 与 Kruskal-Wallis 检验的不同值

R - stat_compare_means return differnt value from Kruskal-Wallis test

我想使用包 ggpubr.

中的 R 函数 stat_compare_means 将 Kruskal-Wallis 检验的 p 值绘制到我的 ggplot

但是,绘制的值与我简单地 运行 函数的值不同:

kruskal.test(value ~ type, data = Profile_melt)

我绘制 p 值的代码是:

ggplot(Profile_melt, aes(type, value)) + 
  geom_boxplot(aes(fill = factor(type), alpha = 0.5), 
               outlier.shape = NA, show.legend = FALSE) +
  geom_jitter(width = 0.2, size = 2, show.legend = FALSE,
              aes(colour = factor(type)), alpha = 0.5) +
  theme_bw() +
  facet_grid(Case ~ Marker, scales = 'free') +
  stat_compare_means(comparison = list(c("Real", "Binomial")),method = 'kruskal.test')+
  background_grid(major = 'y', minor = "none") + # add thin horizontal lines 
  xlab('Category') +
  ylab('Cell counts (Frequencies)')+
  theme(axis.text = element_text(size = 15), 
        axis.title = element_text(size = 20), 
        legend.text = element_text(size = 38),
        legend.title = element_text(size = 30), 
        strip.background = element_rect(colour="black", fill="white"),
        strip.text = element_text(margin = margin(10, 10, 10, 10), size = 25)) +
  panel_border()

这是我的数据sample data

有很多代码行可能与问题无关。或许,您的问题可能是:

为什么

kruskal.test(value ~ type, data = Profile_melt)

#Kruskal-Wallis chi-squared = 4.9673, df = 1, p-value = 0.02583

产生与

不同的 p 值
ggboxplot(Profile_melt, x="type", y = "value") + 
  stat_compare_means(comparison = list(c("Real", "Binomial")), method = 'kruskal.test')

# p-value = 0.49

您可以通过查看原始代码来找出原因。 ggpubr 的开发者可能会更好地解释这一点,如果这是一个问题,也许会在那里修复它。要获得正确且一致的 p 值,请删除 comparison = list(c("Real", "Binomial")):

ggboxplot(Profile_melt, x="type", y = "value") + 
  stat_compare_means(method = 'kruskal.test')

编辑

ggboxplot(Profile_melt, x="type", y = "value") + 
  stat_compare_means(comparison = list(c("Real", "Binomial")))

使用您的其他代码,图形如下所示:

来自 ggpubr 的

stat_compare_means 调用 compare_means 默认情况下使用 wilcox.test。因此,正如@ZhiqiangWang 指出的那样,如果您删除方法或比较,它会变为默认值,这与您首先获得的 p 值相似,因为 2 个样本的 wilcoxon 和 kruskal 非常相似:

kruskal.test(value ~ type, data = Profile_melt)
#Kruskal-Wallis chi-squared = 4.9673, df = 1, p-value = 0.02583
wilcox.test(value ~ type, data = Profile_melt)
#W = 1034939, p-value = 0.02583

现在,对于您拥有的数据,您很可能需要每个单独案例和标记的 p 值,而不是使用 kruskal.test(value ~ type, data = Profile_melt) 进行的泛比较。为所有方面打印相同的 p 值没有意义。

我们首先检查我们需要的 p 值:

compare_means(value ~ type, Profile_melt, group.by = c("Case","Marker"),
method="kruskal")
# A tibble: 30 x 8
   Case    Marker .y.            p   p.adj p.format p.signif method        
   <fct>   <fct>  <chr>      <dbl>   <dbl> <chr>    <chr>    <chr>         
 1 Case 1A CD3    value 0.000470   0.0085  0.00047  ***      Kruskal-Wallis
 2 Case 1A CD4    value 0.00000915 0.00022 9.2e-06  ****     Kruskal-Wallis
 3 Case 1A CD8    value 0.00695    0.09    0.00695  **       Kruskal-Wallis
 4 Case 1A CD20   value 0.707      1       0.70724  ns       Kruskal-Wallis
 5 Case 1A FoxP3  value 0.00102    0.014   0.00102  **       Kruskal-Wallis
 6 Case 1B CD3    value 0.0000415  0.00091 4.1e-05  ****     Kruskal-Wallis

类似于:

Profile_melt %>% 
group_by(Case,Marker) %>% 
summarize(k_p=kruskal.test(value ~ type)$p.value)

# A tibble: 30 x 3
# Groups:   Case [6]
   Case    Marker        k_p
   <fct>   <fct>       <dbl>
 1 Case 1A CD3    0.000470  
 2 Case 1A CD4    0.00000915
 3 Case 1A CD8    0.00695   
 4 Case 1A CD20   0.707     
 5 Case 1A FoxP3  0.00102   

而且我们可以绘图,使用 ggpubr 包中的 ggboxplot 一定更容易:

p = ggboxplot(Profile_melt,x="type",y="value",add="jitter",
facet.by=c("Case","Marker"),scales="free_y",ggtheme=theme_pubclean())

p+stat_compare_means(
aes(label =paste("p=",scientific(as.numeric(..p.format..)))),
method="kruskal",size=2)