Spearman Rho – How to Calculate Correlation by Spearman Method for Each Quantile of Quantile Regression in R

quantile regressionspearman-rho

I'm trying to calculate the Spearman's correlation coefficient for each quantile of my quantile regression (to later calculate the R² of each quantile), but the package I know of (QCSIS) uses Pearson's correlation. So my idea was to try to separate the quantiles and use the cor function to manually find the correlations.

To check if the values ​​are correct, I thought of using the QCSIS package function as a way to check the results. I calculated Pearson's correlation using the package in question and then I did the same by separating the data and using the cor function, but the values ​​were different at the end.
Could anyone tell me why?

Example code:
cor1 and cor2 are different.

library(tidyverse)

# Method 1
cars_quant <- cars %>% 
  mutate(quantile = ntile(speed, 9)) %>% 
  group_by(quantile) %>%
  summarize(correlation = cor(x = speed, y = dist,
                              method = "pearson")) 

cor1 <- cars_quant$correlation

# Method 2
library(QCSIS)
qs <- 1:9/10 
cor2 <- qc(y = cars$dist, x = cars$speed, tau = qs)$rho

# print both
cor1
cor2

Output:

> cor1
[1]  0.41440951 -0.58283514  0.72063396 -0.36642744 -0.18308346  0.36548170 -0.39123040  0.05675958 -0.21557348

> cor2
[1] 0.5169284 0.5862724 0.6091363 0.7041367 0.7156306 0.6763418 0.6239933 0.5484484 0.4791043

Best Answer

The methodology QCSIS comes from Robust model-free feature screening via quantile correlation. It uses the quantile correlation (QC) proposed by Li et al., given by $$ \text{qcor}_\tau(Y,Z) = \frac{\text{qcov}_\tau(Y,Z)}{\sqrt{\text{var}\{\psi_\tau(Y-Q_{\tau,Y}) \}\text{var}(Z)}}=\frac{\text{E}\{\psi_\tau(Y-Q_{\tau,Y})(Z-\text{E}(Z)\}}{\sqrt{(\tau-\tau^2)\text{var}(Z)}}, $$ where $\psi_\tau(u) = \tau - I(u < 0)$. It's not Pearson's correlation.