\[ r_{XY}=\frac{Cov(X,Y)}{\sigma_X \sigma_Y} \]
\[ r_{XY}=\frac{\sum_{i=1}^n (x_i-\bar{x})(y_i-\bar{y})}{\sqrt{\sum_{i=1}^n(x_i-\bar{x})^2}\sqrt{\sum_{i=1}^n(y_i-\bar{y})^2}} \]
\[ \rho = r(rank(X), rank(Y)) \]
\[ \tau=\frac{n_c-n_d}{n_c+n_d}=\frac{n_c-n_d}{\binom{n}{2}}=\frac{1}{n(n-1)}\sum_{i\neq j}sgn(y_i-y_j)sgn(x_i-x_j) \]
\[ \Rightarrow \frac{1+\tau}{2}=\frac{n_c}{n_c+n_d}=\text{percentage of concordant pairs} \]
corPlot functioncorPlot = function(dt, group){
# Customize the lower panel: correlation efficients
correlation.panel = function(x, y){
usr = par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
# 3 types of correlation
rp = round(cor(x, y, method = "pearson", use = "complete.obs"),
digits = 2)
text(0.5, 0.75, paste("Pearson's r = ", rp))
rs = round(cor(x, y, method = "spearman", use = "complete.obs"),
digits = 2)
text(0.5, 0.5, bquote("Spearman's "~rho~" = "~.(rs)))
rk = round(cor(x, y, method = "kendall", use = "complete.obs"),
digits = 2)
text(0.5, 0.25, bquote("Kendall's "~tau~" = "~.(rk)))
}
# Customize upper panel: scatter plots
scatterplot.panel = function(x, y){
points(x, y, pch = 16,
col = group)
}
pairs(dt,
lower.panel = correlation.panel,
upper.panel = scatterplot.panel)
}
iriscorPlot(iris[1:4], iris$Species)