Search code examples
rcorrelationordinal

correlation of two ordinal variables in R


I want to test the spearman correlation of two ordinal variables.

x=c(1,2,3)
y=c(4,3,6)
x=ordered(x)
y=ordered(y)
cor(x,y,methods="spearman")

I always get "Error in cor(x, y) : 'x' must be numeric"

what's the correct way to do this?


Solution

  • Two methods:

    1. use as.numeric.

      x=c(1,2,3)
      y=c(4,3,6)
      x=ordered(x)
      y=ordered(y)
      cor(as.numeric(x), as.numeric(y), method="spearman")
      [1] 0.5
      

    Note that this is not treating x and y simply as continuous numbers. It is treating them as ranks.

    as.numeric(y)
    [1] 2 1 3
    

    This method will allow you to ignore NA values.

    x=c(1,2,3, NA)
    y=c(4,3,6, 7)
    x=ordered(x)
    y=ordered(y)
    cor(as.numeric(x), as.numeric(y), 
        method="spearman", use="pairwise.complete.obs")
    [1] 0.5
    
    1. You can use the package pspearman which will handle the ordered factor.

      x=c(1,2,3)
      y=c(4,3,6)
      x=ordered(x)
      y=ordered(y)
      
      library(pspearman)
      spearman.test(x,y)
      
      
      Spearman's rank correlation rho
      
      data:  x and y
      S = 2, p-value = 1
      alternative hypothesis: true rho is not equal to 0
      sample estimates:
      rho 
      0.5 
      

    Or if you want to reduce some of the output, you could use:

    spearman.test(x,y)$estimate
    rho 
    0.5