📜  R 编程中的 Wilcoxon 符号秩检验

📅  最后修改于: 2022-05-13 01:54:52.559000             🧑  作者: Mango

R 编程中的 Wilcoxon 符号秩检验

Wilcoxon 符号秩检验是一种非参数统计假设检验,用于比较两个相关样本、匹配样本或对单个样本的重复测量,以估计它们的总体均值秩是否不同,例如,它是配对差异检验。当两个样本的均值之间的差异分布不能假设为是正态分布的。 Wilcoxon 符号秩检验是一种非参数检验,可用于确定是否从具有相同分布的总体中选择了两个相关样本。

R中的Wilcoxon符号秩检验

这个测试可以分为两个部分:

  • 单样本 Wilcoxon 符号秩检验
  • 配对样本 Wilcoxon 检验

单样本 Wilcoxon 符号秩检验

当数据不能被假定为正态分布时,单样本 Wilcoxon 符号秩检验是单样本 t 检验的非参数替代方案。它用于确定样本的中位数是否等于已知的标准值,即理论值。在 R 语言中,可以非常轻松地执行此测试。

R中的实现

为了执行单样本 Wilcoxon 检验,R 提供了一个函数wilcox.test() ,可以按如下方式使用:

示例:在这里,让我们使用一个包含 10 只兔子体重的示例数据集。让我们知道兔子的体重中位数是否不同于25g?

R
# R program to illustrate
# one-sample Wilcoxon signed-rank test
 
# The data set
set.seed(1234)
myData = data.frame(
name = paste0(rep("R_", 10), 1:10),
weight = round(rnorm(10, 30, 2), 1)
)
 
# Print the data
print(myData)
 
# One-sample wilcoxon test
result = wilcox.test(myData$weight, mu = 25)
 
# Printing the results
print(result)


R
# R program to illustrate
# one-sample Wilcoxon signed-rank test
 
# The data set
set.seed(1234)
myData = data.frame(
name = paste0(rep("R_", 10), 1:10),
weight = round(rnorm(10, 30, 2), 1)
)
 
# One-sample wilcoxon test
wilcox.test(myData$weight, mu = 25,
            alternative = "less")
 
# Printing the results
print(result)


R
# R program to illustrate
# one-sample Wilcoxon sign-rank test
 
# The data set
set.seed(1234)
myData = data.frame(
name = paste0(rep("R_", 10), 1:10),
weight = round(rnorm(10, 30, 2), 1)
)
 
# One-sample wilcoxon test
wilcox.test(myData$weight, mu = 25,
            alternative = "greater")
 
# Printing the results
print(result)


R
# R program to illustrate
# Paired Samples Wilcoxon Test
 
# The data set
# Weight of the rabbit before treatment
before <-c(190.1, 190.9, 172.7, 213, 231.4,
        196.9, 172.2, 285.5, 225.2, 113.7)
 
# Weight of the rabbit after treatment
after <-c(392.9, 313.2, 345.1, 393, 434,
        227.9, 422, 383.9, 392.3, 352.2)
 
# Create a data frame
myData <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
 
# Print all data
print(myData)
 
# Paired Samples Wilcoxon Test
result = wilcox.test(before, after, paired = TRUE)
 
# Printing the results
print(result)


R
# R program to illustrate
# Paired Samples Wilcoxon Test
 
# The data set
# Weight of the rabbit before treatment
before <-c(190.1, 190.9, 172.7, 213, 231.4,
        196.9, 172.2, 285.5, 225.2, 113.7)
 
# Weight of the rabbit after treatment
after <-c(392.9, 313.2, 345.1, 393, 434,
        227.9, 422, 383.9, 392.3, 352.2)
 
# Create a data frame
myData <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
 
# Paired Samples Wilcoxon Test
result = wilcox.test(weight ~ group,
                    data = myData,
                    paired = TRUE,
                    alternative = "less")
 
# Printing the results
print(result)


R
# R program to illustrate
# Paired Samples Wilcoxon Test
 
# The data set
# Weight of the rabbit before treatment
before <-c(190.1, 190.9, 172.7, 213, 231.4,
        196.9, 172.2, 285.5, 225.2, 113.7)
 
# Weight of the rabbit after treatment
after <-c(392.9, 313.2, 345.1, 393, 434,
        227.9, 422, 383.9, 392.3, 352.2)
 
# Create a data frame
myData <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
 
# Paired Samples Wilcoxon Test
result = wilcox.test(weight ~ group,
                    data = myData,
                    paired = TRUE,
                    alternative = "greater")
 
# Printing the results
print(result)


输出:

name weight
1   R_1   27.6
2   R_2   30.6
3   R_3   32.2
4   R_4   25.3
5   R_5   30.9
6   R_6   31.0
7   R_7   28.9
8   R_8   28.9
9   R_9   28.9
10 R_10   28.2

    Wilcoxon signed rank test with continuity correction

data:  myData$weight
V = 55, p-value = 0.005793
alternative hypothesis: true location is not equal to 25

在上述输出中,检验的 p 值为 0.005793,小于显着性水平 alpha = 0.05。因此我们可以拒绝原假设并得出结论,兔子的平均体重与 25g 显着不同,p 值 = 0.005793。

如果要测试兔子的体重中位数是否小于25g(单尾测试),那么代码将是:

R

# R program to illustrate
# one-sample Wilcoxon signed-rank test
 
# The data set
set.seed(1234)
myData = data.frame(
name = paste0(rep("R_", 10), 1:10),
weight = round(rnorm(10, 30, 2), 1)
)
 
# One-sample wilcoxon test
wilcox.test(myData$weight, mu = 25,
            alternative = "less")
 
# Printing the results
print(result)

输出:

Wilcoxon signed rank test with continuity correction

data:  myData$weight
V = 55, p-value = 0.9979
alternative hypothesis: true location is less than 25

或者,如果要测试兔子的体重中位数是否大于 25g(单尾测试),那么代码将是:

R

# R program to illustrate
# one-sample Wilcoxon sign-rank test
 
# The data set
set.seed(1234)
myData = data.frame(
name = paste0(rep("R_", 10), 1:10),
weight = round(rnorm(10, 30, 2), 1)
)
 
# One-sample wilcoxon test
wilcox.test(myData$weight, mu = 25,
            alternative = "greater")
 
# Printing the results
print(result)

输出:

Wilcoxon signed rank test with continuity correction

data:  myData$weight
V = 55, p-value = 0.002897
alternative hypothesis: true location is greater than 25

R中的配对样本Wilcoxon检验

配对样本 Wilcoxon 检验是配对 t 检验的非参数替代方法,用于比较配对数据。当数据不是正态分布时使用它。

R中的实现

为了执行配对样本 Wilcoxon 检验,R 提供了一个函数wilcox.test() ,可以按如下方式使用:

示例:在这里,我们使用一个示例数据集,其中包含 10 只兔子在治疗前后的体重。我们想知道,治疗前后体重中位数是否有显着差异?

R

# R program to illustrate
# Paired Samples Wilcoxon Test
 
# The data set
# Weight of the rabbit before treatment
before <-c(190.1, 190.9, 172.7, 213, 231.4,
        196.9, 172.2, 285.5, 225.2, 113.7)
 
# Weight of the rabbit after treatment
after <-c(392.9, 313.2, 345.1, 393, 434,
        227.9, 422, 383.9, 392.3, 352.2)
 
# Create a data frame
myData <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
 
# Print all data
print(myData)
 
# Paired Samples Wilcoxon Test
result = wilcox.test(before, after, paired = TRUE)
 
# Printing the results
print(result)

输出:

group weight
1  before  190.1
2  before  190.9
3  before  172.7
4  before  213.0
5  before  231.4
6  before  196.9
7  before  172.2
8  before  285.5
9  before  225.2
10 before  113.7
11  after  392.9
12  after  313.2
13  after  345.1
14  after  393.0
15  after  434.0
16  after  227.9
17  after  422.0
18  after  383.9
19  after  392.3
20  after  352.2

    Wilcoxon signed rank test

data:  before and after
V = 0, p-value = 0.001953
alternative hypothesis: true location shift is not equal to 0

在上述输出中,检验的 p 值为 0.001953,小于显着性水平 alpha = 0.05。我们可以得出结论,治疗前小鼠的中位体重与治疗后的中位体重显着不同,p 值 = 0.001953。

如果要测试治疗前的体重中位数是否小于治疗后的体重中位数,则代码将是:

R

# R program to illustrate
# Paired Samples Wilcoxon Test
 
# The data set
# Weight of the rabbit before treatment
before <-c(190.1, 190.9, 172.7, 213, 231.4,
        196.9, 172.2, 285.5, 225.2, 113.7)
 
# Weight of the rabbit after treatment
after <-c(392.9, 313.2, 345.1, 393, 434,
        227.9, 422, 383.9, 392.3, 352.2)
 
# Create a data frame
myData <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
 
# Paired Samples Wilcoxon Test
result = wilcox.test(weight ~ group,
                    data = myData,
                    paired = TRUE,
                    alternative = "less")
 
# Printing the results
print(result)

输出:

Wilcoxon signed rank test

data:  weight by group
V = 55, p-value = 1
alternative hypothesis: true location shift is less than 0

或者,如果要测试治疗前的体重中位数是否大于治疗后的体重中位数,则代码将是:

R

# R program to illustrate
# Paired Samples Wilcoxon Test
 
# The data set
# Weight of the rabbit before treatment
before <-c(190.1, 190.9, 172.7, 213, 231.4,
        196.9, 172.2, 285.5, 225.2, 113.7)
 
# Weight of the rabbit after treatment
after <-c(392.9, 313.2, 345.1, 393, 434,
        227.9, 422, 383.9, 392.3, 352.2)
 
# Create a data frame
myData <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
 
# Paired Samples Wilcoxon Test
result = wilcox.test(weight ~ group,
                    data = myData,
                    paired = TRUE,
                    alternative = "greater")
 
# Printing the results
print(result)

输出:

Wilcoxon signed rank test

data:  weight by group
V = 55, p-value = 0.0009766
alternative hypothesis: true location shift is greater than 0