Adjust rank command for various methods in R

The code below uses spearman's coef between the rankings obtained by both methods. To generate the rankings, I use 4 commands for this and then specifically filter the columns I use to do the coef. of spearman. However, I would like to do this in an easier way than I did.

padr<-structure(list(Method1= c(0.343394182514031, 1, 0.860087696840587, 
0.860087696840587, 0.868085451239441, 0.698055447477473, 0.43737803420133, 
0.434970400304271, 0.434970400304271, 0.379233994071699), Method2 = c(1, 
0.232979733215734, 0.240392548713602, 0.240392548713602, 0.213384133751235, 
0.240137915565427, 0.321393780370283, 0.322481353908317, 0.322481353908317, 
0.352233249467427), Method3 = c(1, 0.214432400448801, 0.214809476505306, 
0.214809476505306, 0.16783443847331, 0.210797750473198, 0.293103343189013, 
0.293692283587016, 0.293692283587016, 0.281085590908947), Method4 = c(0, 
1, 0.875556823046433, 0.875556823046433, 0.891768819029077, 0.832271929255291, 
0.741168314099481, 0.740578512687553, 0.740578512687553, 0.819053554576837
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10"))

   padr$RankMethod1 <- (nrow(padr) + 1) - rank(-padr$`Method1`, ties.method = "last")
   padr$RankMethod2 <- (nrow(padr) + 1) - rank(padr$`Method2`, ties.method = "first")
   padr$RankMethod3 <- (nrow(padr) + 1) - rank(padr$`Method3`, ties.method = "first")
   padr$RankMethod4 <- (nrow(padr) + 1) - rank(-padr$`Method4`, ties.method = "last")
  
  padr<-padr[,5:8]

  padr %>%
    summarise(across(RankMethod1:RankMethod4,  ~cor.test(., RankMethod1, method = "spearman")$estimate))
  RankMethod1 RankMethod2 RankMethod3 RankMethod4
1           1   0.9272727   0.8060606   0.9272727

Is this closer to what you want?

padr<-structure(list(Method1= c(0.343394182514031, 1, 0.860087696840587, 
                                0.860087696840587, 0.868085451239441, 0.698055447477473, 0.43737803420133, 
                                0.434970400304271, 0.434970400304271, 0.379233994071699), 
                     Method2 = c(1, 
                                 0.232979733215734, 0.240392548713602, 0.240392548713602, 0.213384133751235, 
                                 0.240137915565427, 0.321393780370283, 0.322481353908317, 0.322481353908317, 
                                 0.352233249467427), 
                     Method3 = c(1, 0.214432400448801, 0.214809476505306, 
                                 0.214809476505306, 0.16783443847331, 0.210797750473198, 0.293103343189013, 
                                 0.293692283587016, 0.293692283587016, 0.281085590908947), 
                     Method4 = c(0, 
                                 1, 0.875556823046433, 0.875556823046433, 0.891768819029077, 0.832271929255291, 
                                 0.741168314099481, 0.740578512687553, 0.740578512687553, 0.819053554576837
                     )), 
                class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))

library(purrr)       
library(dplyr)

METHODS <- c("last","first","first","last")
COEF <- c(-1,1,1,-1)

RankFunc <- function(R,Meth,Coef){
  length(R) + 1 - rank(Coef * R, ties.method = Meth)
}

pmap_dfc(list(padr, METHODS, COEF), RankFunc) |> 
  summarise(across(Method1:Method4,  ~cor.test(., Method1, method = "spearman")$estimate))
#> # A tibble: 1 x 4
#>   Method1 Method2 Method3 Method4
#>     <dbl>   <dbl>   <dbl>   <dbl>
#> 1       1   0.927   0.806   0.927

Created on 2022-07-20 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.