简体   繁体   中英

Show Kruskal-Wallis test ranks

I performed a kruskal wallis test on multi-treatment data where I compared five different methods.

A friend showed me the calculation in spss and the results included the mean ranks of each method.

In R, I only get the chi2 and df value and p-value when applying kruskal.test to my data set. those values are equal to the ones in spss but I do not get any ranks.

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:

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?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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