Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correlation() and cor_test() handle factors differently when using method = spearman #236

Open
axrhart opened this issue May 20, 2022 · 2 comments

Comments

@axrhart
Copy link

axrhart commented May 20, 2022

Dear authors,

a student pointed out that the following uses of correlation() and cor_test() return different results.

set.seed(1337)

data.frame(
  a = sample(1:5, 10, replace = T),
  b = factor(sample(c("s", "m", "l"), 10, , replace = T), 
             levels = c("s", "m", "l"), 
             ordered = T)
) -> df

correlation::correlation(df, method = "spearman")

# # Correlation Matrix (spearman-method)
# 
# Parameter1 | Parameter2 |   rho |         95% CI |      S |     p
# -----------------------------------------------------------------
#   a          |        b.s | -0.49 | [-0.86,  0.22] | 245.72 | 0.668
# a          |        b.m | -0.11 | [-0.70,  0.57] | 182.97 | 0.765
# a          |        b.l |  0.51 | [-0.20,  0.87] |  81.12 | 0.668
# b.s        |        b.m | -0.41 | [-0.83,  0.32] | 232.36 | 0.725
# b.s        |        b.l | -0.41 | [-0.83,  0.32] | 232.36 | 0.725
# b.m        |        b.l | -0.67 | [-0.92, -0.04] | 275.00 | 0.212
# 
# p-value adjustment method: Holm (1979)
# Observations: 10
# Warning message:
#   It seems like there is not enough continuous variables in your data. Maybe you want to include the factors? 
#   We're setting `include_factors=TRUE` for you. 

correlation::cor_test(df, "a", "b", method = "spearman")

# Parameter1 | Parameter2 |  rho |        95% CI |     S |     p
# --------------------------------------------------------------
# a          |          b | 0.59 | [-0.08, 0.89] | 67.64 | 0.073
# 
# Observations: 10

I wondered whether this behavior is intentional?
If yes, which one would you consider the correct approach to compute rho between an ordered factor and a numeric variable?

Best,
Alexander

@bwiernik
Copy link
Contributor

The proper method for ordered factors (but not unordered factors) should probably be the cor_test behavior of converting the ordered levels to integers (i.e., ranks).

@axrhart
Copy link
Author

axrhart commented May 23, 2022

Thank you. Then we will stick to using cor_test() in our materials. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants