简体   繁体   English

显示Kruskal-Wallis的考试等级

[英]Show Kruskal-Wallis test ranks

I performed a kruskal wallis test on multi-treatment data where I compared five different methods. 我对多处理数据进行了kruskal wallis测试,其中比较了五种不同的方法。

A friend showed me the calculation in spss and the results included the mean ranks of each method. 一位朋友向我展示了spss中的计算结果,结果包括每种方法的平均等级。

In R, I only get the chi2 and df value and p-value when applying kruskal.test to my data set. 在R中,将kruskal.test应用于数据集时,我只会获得chi2df value以及p-value those values are equal to the ones in spss but I do not get any ranks. 这些值等于spss中的值,但我没有任何排名。

How can I print out the ranks of the computation? 如何打印出计算等级? My code looks like this: 我的代码如下所示:

 comparison <- kruskal.test(all,V3,p.adj="bon",group=FALSE, main="over")

If I print comparison I get the following: 如果我打印比较,则会得到以下结果:

Kruskal-Wallis rank sum test
data:  all
Kruskal-Wallis chi-squared = 131.4412, df = 4, p-value < 2.2e-16

But I would like to get something like this additional output from spss: 但我想从spss获得类似此额外输出的内容:

Type    H   Middle Rank
1,00    57  121.11
2,00    57  148.32
3,00    57  217.49
4,00    57  53.75
5,00    57  174.33
total   285 

How do I get this done in r? 我如何在R中完成这项工作?

The table you want you have to compute yourself unfortunately. 不幸的是,您想要的表必须自己计算。 Luckely I have made a function for you: 幸运的是,我为您提供了一个功能:

#create some random data
ozone <- airquality$Ozone
names(ozone) <- airquality$Month


spssOutput <- function(vector) {
  # This function takes your data as one long
  # vector and ranks it. After that it computes 
  # the mean rank of each group. The groupes
  # need to be given as names to the vector.
  # the function returns a data frame with
  # the results in SPSS style.

  ma <- matrix(, ncol=3, nrow= 0)
  r  <- rank(vector, na.last = NA)
  to <- 0
  for(n in unique(names(r))){
    # compute the rank mean for group n
    g  <- r[names(r) == n]
    gt <- length(g)
    rm <- sum(g)/gt
    to <- to + gt
    ma <- rbind(ma, c(n, gt, rm))
  }
  colnames(ma) <- c("Type","H","Middle Rank")
  ma <- rbind(ma, c("total", to, ""))
  as.data.frame(ma)
}

# calculate everything
out <- spssOutput(ozone)
print(out, row.names= FALSE)
kruskal.test(Ozone ~ Month, data = airquality) 

This gives you the following output: 这将为您提供以下输出:

Type    H      Middle Rank
 5     26 36.6923076923077
 6      9 48.7222222222222
 7     26 77.9038461538462
 8     26 75.2307692307692
 9     29 48.6896551724138
total 116                 

Kruskal-Wallis rank sum test

data:  Ozone by Month
Kruskal-Wallis chi-squared = 29.2666, df = 4, p-value = 6.901e-06

You haven't shared your data so you have to figure out yourself how this would work for your data set. 您尚未共享数据,因此您必须弄清楚自己如何处理您的数据集。

I had an assignment where I had to do this. 我有一个必须要做的任务。 Make a data frame where one column is the combined values you're ranking, one column is the categories each value belongs to, and the final column is the ranking of each value. 创建一个数据框,其中一栏是您要排序的组合值,一栏是每个值所属的类别,最后一栏是每个值的排名。 The function rank() is the one you need for the actual ranking. 函数rank()是实际排名所需要的函数。 The code looks like this: 代码如下:

low <- c(0.56, 0.57, 0.58, 0.62, 0.64, 0.65, 0.67, 0.68, 0.74, 0.78, 0.85, 0.86)
medium <- c(0.70, 0.74, 0.75, 0.76, 0.78, 0.79, 0.80, 0.82, 0.83, 0.86)
high <- c(0.65, 0.73, 0.74, 0.76, 0.81,0.82, 0.85, 0.86, 0.88, 0.90)

data.value <- c(low, medium, high)
data.category <- c(rep("low", length(low)), rep("medium", length(medium)), rep("high", length(high)) )
data.rank <- rank(data.value)
data <- data.frame(data.value, data.category, data.rank)
data
         data.value data.category data.rank
1        0.56           low       1.0
2        0.57           low       2.0
3        0.58           low       3.0
4        0.62           low       4.0
5        0.64           low       5.0
6        0.65           low       6.5
7        0.67           low       8.0
8        0.68           low       9.0
9        0.74           low      13.0
10       0.78           low      18.5
11       0.85           low      26.5
12       0.86           low      29.0
13       0.70        medium      10.0
14       0.74        medium      13.0
15       0.75        medium      15.0
16       0.76        medium      16.5
17       0.78        medium      18.5
18       0.79        medium      20.0
19       0.80        medium      21.0
20       0.82        medium      23.5
21       0.83        medium      25.0
22       0.86        medium      29.0
23       0.65          high       6.5
24       0.73          high      11.0
25       0.74          high      13.0
26       0.76          high      16.5
27       0.81          high      22.0
28       0.82          high      23.5
29       0.85          high      26.5
30       0.86          high      29.0
31       0.88          high      31.0
32       0.90          high      32.0

This will give you a table that looks like this. 这将为您提供一个看起来像这样的表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM