How to create a scatter plot of partial rank correlation
11 views (last 30 days)
Show older comments
Hello,
I have a question regarding a partial rank correlation.
I would like to use "partialcorr" function to find correlation between X and Y after controlling for Z and visualize it on a scatter plot. As far as I understood the calculation, I can plot it as below.
X = variable 1
Y = variable 2
Z = Confounder
mdl_Z_X = fitlm(Z,X);
mdl_Z_Y = fitlm(Z,Y);
plot(mdl_Z_X.Residuals.Raw,mdl_Z_Y.Residuals.Raw);
I calculated the correlation in both ways to make sure they match each other
R1 = partialcorr(X,Y,Z,"type","Pearson")
R2 = corr(mdl_Z_X.Residuals.Raw, mdl_Z_Y.Residuals.Raw, "type", "Pearson")
And they were matched (R1 = R2). However, When I switched it to "Spearman", the correlation coefficient did not match. What I mean is ...
R1 = partialcorr(X,Y,Z,"type","Spearman")
R2 = corr(mdl_Z_X.Residuals.Raw, mdl_Z_Y.Residuals.Raw, "type", "Spearman")
R1 and R2 were a different value. I also tried to convert X and Y variables into rank variables as if ...
X = [2.5 3.1 6.5 1.2]
Xrank = [2 3 4 1]
The conversion seemed working somewhat as the correlation coefficient became closer to the coefficient from the "partialcorr" function, but it still did not match. Since the algorithm of a built-in function is hidden by MATLAB, I would really appreciate if someone knows how I can create a scatter plot of partial rank correlation.
Thank you in advance.
1 Comment
俊荣 韩
on 25 Mar 2022
hello,
Thank you for your example shown here, and I got a lot of help. I tried your code and did a bit of improvement
"R1 and R2 were a different value. I also tried to convert X and Y variables into rank variables as if ...
X = [2.5 3.1 6.5 1.2]
Xrank = [2 3 4 1]"
The rank of X should be descended when conducting Spearman. It's ascending in your example.
And the R2 calculation with resduals of X and Y should use Pearson,
R2 = corr(mdl_Z_X.Residuals.Raw, mdl_Z_Y.Residuals.Raw, "type", "Pearson")
Then, the R1 and R2 were the same.
Answers (0)
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!