'R Mann-Whitney-U test output like in SPSS

I want to run Mann-Whitney-U test. But R's wilcox.test(x~y, conf.int=TRUE) does not give such statistics as N, Mean Rank, Sum of Ranks, Z-value for both factors. I need R to give as much information as SPSS does (see here)

I'm wondering whether I didn't select some options, or if there is a good package I could install?

Thanks!



Solution 1:[1]

In R, you need to calculate the various outputs of SPSS separately. For example, using dplyr::summarise:

library(dplyr)
mt_filt <- mtcars %>%
  filter(cyl > 4) %>%
  mutate(rank_mpg = rank(mpg))
mt_filt %>%
  group_by(cyl) %>%
  summarise(n = n(),
            mean_rank_mpg = mean(rank_mpg),
            sum_rank_mpg = sum(rank_mpg))

# # A tibble: 2 × 4
#       cyl     n mean_rank_mpg sum_rank_mpg
#     <dbl> <int>         <dbl>        <dbl>
#   1     6     7          17.4          122
#   2     8    14          7.82          110

# Number in first group
n1 <- sum(as.integer(factor(mt_filt$cyl)) == 1)

wilcox.test(mpg ~ cyl, mt_filt) %>%
  with(data_frame(U = statistic, 
            W = statistic + n1 * (n1 + 1) / 2,
            Z = qnorm(p.value / 2),
            p = p.value))
# # A tibble: 1 × 4
#       U     W         Z           p
#   <dbl> <dbl>     <dbl>       <dbl>
# 1  93.5 121.5 -3.286879 0.001013045

Edit 2020-07-15

Thanks to @Paul for pointing out that the ranks need to be generated prior to grouping.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1