Home > Enterprise >  Compare rankings using the Spearman Rho coefficient
Compare rankings using the Spearman Rho coefficient

Time:07-04

I would like to compare the ranking results of Methods 2, 3 and 4 with Method 1. In this regard, I would like to use the Spearman Rho coefficient. I would also like to know if a significant relationship between the results of methods 2, 3 and 4 with method 1. So how to do this?

database<-structure(list( 
Method1 = c(1L, 10L, 7L, 8L, 9L, 6L, 5L, 3L, 4L, 2L), Method2 = c(1L, 
8L, 6L, 7L, 10L, 9L, 4L, 2L, 5L, 3L), Method3 = c(1L, 
10L, 7L, 8L, 9L, 6L, 4L, 2L, 3L, 5L), Method4 = c(1L, 
9L, 6L, 7L, 10L, 8L, 5L, 3L, 2L, 4L)), class = "data.frame", row.names = c(NA, 
10L))

> database
   Method1 Method2 Method3 Method4
1        1       1       1       1
2       10       8      10       9
3        7       6       7       6
4        8       7       8       7
5        9      10       9      10
6        6       9       6       8
7        5       4       4       5
8        3       2       2       3
9        4       5       3       2
10       2       3       5       4

CodePudding user response:

Using summarise with across

library(dplyr)
database %>%
  summarise(across(Method2:Method4,  ~cor.test(., Method1, method = "spearman")$estimate))
    Method2   Method3   Method4
1 0.8787879 0.9272727 0.9030303

CodePudding user response:

Maybe you want something like this where you extract the estimate of the cor.test using the "spearman" method:

library(dplyr)
database %>% 
  summarise_each(funs(cor.test(., database$Method1, method = "spearman")$estimate))

Output:

  Method1   Method2   Method3   Method4
1       1 0.8787879 0.9272727 0.9030303

For p-value:

library(dplyr)
database %>% 
  summarise_each(funs(cor.test(., database$Method1, method = "spearman")$p.value))

Output:

  Method1     Method2      Method3     Method4
1       0 0.001977059 0.0001301624 0.000880225

CodePudding user response:

In an sapply loop over the remaining columns .

sapply(c('Method2', 'Method3', 'Method4'), \(x) 
       cor.test(database[, 1], database[, x], method='spearman')[c('estimate', 'p.value')])
#          Method2     Method3      Method4    
# estimate 0.8787879   0.9272727    0.9030303  
# p.value  0.001977059 0.0001301624 0.000880225
  •  Tags:  
  • r
  • Related