Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spearman's rho between ordered factors in R

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.

like image 729
Frikster Avatar asked Sep 04 '25 17:09

Frikster


1 Answers

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 
like image 63
josliber Avatar answered Sep 07 '25 19:09

josliber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!