【问题标题】:Different results for wilcox.test when using data.frame or vectors as input使用 data.frame 或向量作为输入时 wilcox.test 的不同结果
【发布时间】:2017-04-12 19:58:11
【问题描述】:

所以我遇到了一个奇怪的差异,这取决于我如何使用 R 中的 wilcox.test() 函数分析相同的数据。在此示例中,我正在比较两组中的值。我可以将它们作为两个单独的向量提供给 wilcox.test 函数,或者我可以给函数一个 data.frame 并使用一个公式来指定我想要进行的比较。奇怪的是,我最终得到了不同的测试统计值 (W),这取决于我使用的输入法。我在下面包含了一个示例(R v3.3.1):

#Prepare test data
wt_exp = c(0.59, 0.56, 0.45, 0.59, 0.54, 0.13, 0.25, 0.10, 0.15)
kd_exp = c(0.27, 0.27, 0.33, 0.25, 0.22, 0.2, 0.16, 0.2, 0.36, 0.58, 0.51)
test.data_frame = 
    data.frame(Expression = c(wt_exp, kd_exp),
               Genotype = rep(c("WT", "KD"),
                              times=c(length(wt_exp), length(kd_exp))))

#Wilcox test using two input vectors
wilcox.test(wt_exp, kd_exp)

# Result:
# Wilcoxon rank sum test with continuity correction
# 
# data:  wt_exp and kd_exp
# W = 55.5, p-value = 0.6756
# alternative hypothesis: true location shift is not equal to 0
# 
# Warning message:
#     In wilcox.test.default(wt_exp, kd_exp) :
#     cannot compute exact p-value with ties

#Wilcox test using data.frame and formula
wilcox.test(Expression ~ Genotype, data=test.data_frame)

# Result:
# Wilcoxon rank sum test with continuity correction
# 
# data:  Expression by Genotype
# W = 43.5, p-value = 0.6756
# alternative hypothesis: true location shift is not equal to 0
# 
# Warning message:
#     In wilcox.test.default(x = c(0.27, 0.27, 0.33, 0.25, 0.22, 0.2,  :
#                                      cannot compute exact p-value with ties

虽然我意识到在这种情况下 p 值是相同的,但我将进行数千次这样的测试,并且我想确定造成这种情况的原因,所以我不需要保留抽查结果。想法?

【问题讨论】:

    标签: r


    【解决方案1】:

    如果您有两个样本xy,则此测试的测试统计量本质上是x 值的秩和。因此,对于测试统计,哪一组观察是x 和哪一组是y 会有所不同。比较

    wilcox.test(wt_exp, kd_exp)
    wilcox.test(kd_exp, wt_exp)
    

    请注意,您从后者获得的值对应于您使用公式语法时获得的值。

    请注意,这些都得到相同的 p 值,因为在生成 p 值之前,统计数据已按样本大小进行了归一化。

    【讨论】:

    • 该死,我以为我检查了参数顺序。感谢您清除它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2019-08-26
    • 2018-09-25
    相关资源
    最近更新 更多