3
votes

I have two ordered factors and simply want to find Spearman's rho between them.

However:

> cor(dat$UEMS.2,dat$SCIM23_SubScore1.2,use="pairwise.complete.obs",method="spearman")

Error in cor(dat$UEMS.2, dat$SCIM23_SubScore1.2, use = "pairwise.complete.obs",  : 
  'x' must be numeric

And just as a sanity check:

> class(dat$UEMS.2)
[1] "ordered" "factor" 
> class(dat$SCIM23_SubScore1.2)
[1] "ordered" "factor" 

How do I find spearman's rho for ordered factors using R?

I did find the following: Calculate correlation - cor() - for only a subset of columns

Which raises the same issue: R's cor() function only accepts numerical data. This doesn't seem right to me, because spearman's rho should be able to handle ordinal variables. Ordered factors are ordinal variables.

Thanx in advance.

1

1 Answers

4
votes

You can use the pspearman package to handle ordinal variables:

a <- factor(c(1, 2, 3, 4, 4, 4, 3, 4, 2, 2, 1), ordered=TRUE)
b <- factor(c(1, 4, 2, 2, 4, 1, 1, 4, 4, 3, 3), ordered=TRUE)
library(pspearman)
spearman.test(a, b)
#      Rsquare            F          df1          df2       pvalue            n 
#  0.001015235  0.009146396  1.000000000  9.000000000  0.925904654 11.000000000