braverock / PerformanceAnalytics

209 stars 105 forks source link

Bug in chart.Correlation() #105

Open Payamdel opened 6 years ago

Payamdel commented 6 years ago

Dear developer, Thanks for creating such a great package! I would like to report that there is a bug in chart.Correlation() in that selecting either "pearson" or "spearman" as method in the function results in the same chart (output). In other words, the Correlation coefficients are calculated on the basis of "pearson" even if the user choose "spearman" as method in the function!

 data(managers)
 chart.Correlation(managers[,1:8], histogram=TRUE, pch="+",method = "pearson")
 chart.Correlation(managers[,1:8], histogram=TRUE, pch="+",method = "spearman")

Hope you can address this issue soon!

rfbrondum commented 5 years ago

I am experiencing the same problem. Pearson correlation seems to be hardcoded into the panel.cor subfunction in chart.correlation.

jshousephd commented 5 years ago

This is still true as of today.
function (R, histogram = TRUE, method = c("pearson", "kendall", "spearman"), ...) { x = checkData(R, method = "matrix") if (missing(method)) method = method[1] panel.cor <- function(x, y, digits = 2, prefix = "", use = "pairwise.complete.obs", method = "pearson", cex.cor, ...) {

jonaslenz commented 5 years ago

It's still true for version 1.5.2.

The trace()-function can be used for a workaround - specify desired correlation method "spearman" or "kendall" where default method is defined ("XXXXXXX" in this code):

trace(PerformanceAnalytics::chart.Correlation, quote( panel.cor <- function(x, y, digits = 2, prefix = "", use = "pairwise.complete.obs", method = "XXXXXXX", cex.cor, ...) { usr <- par("usr") on.exit(par(usr)) par(usr = c(0, 1, 0, 1)) r <- cor(x, y, use = use, method = method) txt <- format(c(r, 0.123456789), digits = digits)[1] txt <- paste(prefix, txt, sep = "") if (missing(cex.cor)) cex <- 0.8/strwidth(txt) test <- cor.test(as.numeric(x), as.numeric(y), method = method) Signif <- symnum(test$p.value, corr = FALSE, na = FALSE, cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1), symbols = c("***", "**", "*", ".", " ")) text(0.5, 0.5, txt, cex = cex * (abs(r) + 0.3)/1.3) text(0.8, 0.8, Signif, cex = cex, col = 2) } ), at=5)

This can be undone by untrace(PerformanceAnalytics::chart.Correlation)

The problem might be, that pairs function does not pass the method parameter to panel.cor()