library(data.table)
library(coin)
## Loading required package: survival
library(exactRankTests)
##  Package 'exactRankTests' is no longer under development.
##  Please consider using package 'coin' instead.
## 
## Attaching package: 'exactRankTests'
## The following objects are masked from 'package:coin':
## 
##     dperm, pperm, qperm, rperm
anxiety <- fread("treatment_anxiety.csv", 
                 select = c(1, 4:6), 
                 col.names = c("id", "period", "personal", "situation"))
anxiety <- anxiety[, period := factor(period)]
anxiety <- anxiety[, index := factor(rep(seq_len(.N/2), 2))]

wilcox test

Попробуем базовые функции для парного теста Вилкоксона. Результаты схожи, ориентировочно p-value = 0.29.

wilcox.test(x = anxiety[period == 'before', personal], 
             y = anxiety[period == 'after', personal],
             paired = T, exact = T)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  anxiety[period == "before", personal] and anxiety[period == "after", personal]
## V = 121.5, p-value = 0.2928
## alternative hypothesis: true location shift is not equal to 0
wilcox.exact(x = anxiety[period == 'before', personal], 
             y = anxiety[period == 'after', personal],
             paired = T, exact = T)
## 
##  Exact Wilcoxon signed rank test
## 
## data:  anxiety[period == "before", personal] and anxiety[period == "after", personal]
## V = 121.5, p-value = 0.2987
## alternative hypothesis: true mu is not equal to 0

wilcoxsign_test

Пробуем функцию wilcoxsign_test() из пакета coin. Согласно справке по функции, переменных задаются следующим образом: personal ~ period. Описание из справки: a formula of the form y ~ x | block where y is a numeric variable, x is a factor with two (sign_test and wilcoxsign_test). Таким образом мы получаем p-value = 2.842e-14.

wx1 <- wilcoxsign_test(personal ~ period, 
                anxiety, distribution = "exact", zero.method = c("Wilcoxon"))
wx1
## 
##  Exact Wilcoxon Signed-Rank Test
## 
## data:  y by x (pos, neg) 
##   stratified by block
## Z = 5.9073, p-value = 2.842e-14
## alternative hypothesis: true mu is not equal to 0

Однако если задавать переменные по-другому, когда в x и y подставляются значения измерения до и после, а не фактор группировки, то результаты мы получаем идентичные базовым тестам.

wx2 <- wilcoxsign_test(anxiety[period == 'before', personal] ~ anxiety[period == 'after', personal], 
                anxiety, distribution = "exact", zero.method = c("Wilcoxon"), paired = T)
wx2
## 
##  Exact Wilcoxon Signed-Rank Test
## 
## data:  y by x (pos, neg) 
##   stratified by block
## Z = 1.0723, p-value = 0.2987
## alternative hypothesis: true mu is not equal to 0