类似于 SPSS 中的 R Mann-Whitney-U 测试输出

R Mann-Whitney-U test output like in SPSS

我想运行 Mann-Whitney-U 测试。但是R的wilcox.test(x~y, conf.int=TRUE)没有给出NMean Rank、Sum of Ranks、Z-value等两个因素的统计数据。我需要 R 提供与 SPSS 一样多的信息 (see here)

我想知道是不是我没有select一些选项,或者是否有我可以安装的好包?

谢谢!

在R中,需要分别计算SPSS的各种输出。例如,使用 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

编辑 2020-07-15

感谢@Paul 指出需要在分组之前生成排名。