Sheldon
Sheldon

Reputation: 315

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?

Upvotes: 4

Views: 8754

Answers (2)

G5W
G5W

Reputation: 37641

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 

Upvotes: 7

zacdav
zacdav

Reputation: 4671

You had a few issues:

  1. using ordered will create a factor, you can use rank
  2. You had a typo, it should be method=... not methods=...

--

x=c(1,2,3)
y=c(4,3,6)
x=rank(x)
y=rank(y)
cor(x,y,method="spearman")

Upvotes: 0

Related Questions