denise
denise

Reputation: 1

Which post-hoc test to run with Wilcoxon signed rank sum test in R?

I have ran a Wilcoxon signed rank sum test on my paired data in R. my code is:

plot.wilcox <- wilcox.test(score ~ time, data = data, paired = TRUE)

Which post hoc test to use that would accompany the Wilcoxon signed rank sum test?

I plugged in:

plot.wilcox.bonferroni <- data %>% 
  wilcox.test(score ~ Category_time, paired = TRUE, p.adjust.method = "bonferroni") 
plot.wilcox.bonferroni

Obviously, an error comes up. I also don't know if Bonferroni adjustment is the proper post hoc test. Should I be conducting a Dunn test, not sure if Dunn test if for paired data. Can someone help?

Upvotes: 0

Views: 765

Answers (1)

r2evans
r2evans

Reputation: 160687

I'm going to guess at your error: the %>%-pipe is placing the data as the first unnamed argument in the wilcox.test. There are two ways to call that function, described in ?wilcox.test:

     ## Default S3 method:
     wilcox.test(x, y = NULL,
                 alternative = c("two.sided", "less", "greater"),
                 mu = 0, paired = FALSE, exact = NULL, correct = TRUE,
                 conf.int = FALSE, conf.level = 0.95,
                 tol.root = 1e-4, digits.rank = Inf, ...)
     
     ## S3 method for class 'formula'
     wilcox.test(formula, data, subset, na.action, ...)

In general, %>%-piping allows you the option to specify which argument gets the data by using the special . symbol. When that is not included, it goes in the first unname argument.

So this is what I think you are seeing (using airquality, taken from examples in the doc for that function):

library(dplyr) # or library(magrittr)
airquality %>%
  wilcox.test(Ozone ~ Month, subset = Month %in% c(5, 8))
# Error in wilcox.test.default(., Ozone ~ Month, subset = Month %in% c(5,  : 
#   'x' must be numeric

This is because the function takes as its first unnamed argument with x (numeric) or formula, a formula (aptly-named).

For this function, you really only have one option to use %>% to feed the data: provide the . symbol to the data= argument, by-name.

airquality %>%
  wilcox.test(Ozone ~ Month, data = ., subset = Month %in% c(5, 8))
# Warning in wilcox.test.default(x = DATA[[1L]], y = DATA[[2L]], ...) :
#   cannot compute exact p-value with ties
#   Wilcoxon rank sum test with continuity correction
# data:  Ozone by Month
# W = 127.5, p-value = 0.0001208
# alternative hypothesis: true location shift is not equal to 0

Since we're using data=., you can choose to put it almost anywhere, though if you move Ozone ~ Month, you should likely name it as well, with formula = Ozone ~ Month.


For you, I suspect then that this will work:

plot.wilcox.bonferroni <- data %>% 
  wilcox.test(score ~ Category_time, data = .,
              paired = TRUE, p.adjust.method = "bonferroni")

Upvotes: 1

Related Questions