曼恩·惠特尼·泰西(Mann-Whitney tesy)错误:小组因素2水平?

时间:2018-07-18 09:30:19

标签: r

我不断收到以下错误:

y <- as.factor(data$Pos.44)
x <- as.factor(data$Neg.44)
wilcox.test( x ~ y,  
                 alternative='two.sided', 
                 paired=FALSE, var.equal = TRUE, 
               conf.level = 0.95)
  

wilcox.test.formula(x〜y,备选=“ two.side”,成对= FALSE,时出错:分组因子必须恰好具有2个水平

我尝试将数据转换为数字形式的因子,并告诉r标头为TRUE。有任何想法吗??

以下是一些数据。

Neg 44      Pos 44
13.8228455  7.126694925
13.8228455  8.402648457
13.8228455  8.402648457
6.85288518  12.27621627
9.232427575 
11.31157309 
195.5120218 
82.61317544 
9.751455813 
4.490663867 
7.732335943 
3.72520288  
17.51041865 
8.56912795  
17.59120857 
12.3571618  

2 个答案:

答案 0 :(得分:0)

您的格式错误。 Mann-Whitney检验的输入需要一列带有值,另一列包含两个级别因子。您尝试将两个转换为因子的数值向量提供给它。请参见下面的示例:

x=c(rnorm(50,0,1),rnorm(50,1,1))
y=rep(c('A','B'),c(50,50))
wilcox.test(x~y, alternative='two.sided',  paired=FALSE, var.equal = TRUE,  conf.level = 0.95) 

答案 1 :(得分:0)

您可以尝试

library(tidyverse)
d %>% 
  gather(k,v) %>% 
  with(.,wilcox.test(v ~ k, data=., exact =F))

Wilcoxon rank sum test with continuity correction

data:  v by k
W = 45, p-value = 0.2367
alternative hypothesis: true location shift is not equal to 0

或使用箱线图可视化数据

library(ggbeeswarm)
library(ggsignif)
d %>% 
  gather(k,v) %>% 
  ggplot(aes(k,v)) + 
   geom_boxplot() +
   geom_beeswarm() + 
   ggsignif::geom_signif(comparisons = list(c("Neg44", "Pos44")))

enter image description here

您的数据

d <- read.table(text="Neg44 Pos44
13.8228455  7.126694925
13.8228455  8.402648457
13.8228455  8.402648457
6.85288518  12.27621627
9.232427575 
11.31157309 
195.5120218 
82.61317544 
9.751455813 
4.490663867 
7.732335943 
3.72520288  
17.51041865 
8.56912795  
17.59120857 
12.3571618",header=T,  fill=T)