### Name: cor ### Title: Correlation, Variance and Covariance (Matrices) ### Aliases: var cov cor cov2cor ### Keywords: univar multivariate array ### ** Examples var(1:10)# 9.166667 var(1:5,1:5)# 2.5 ## Two simple vectors cor(1:10,2:11)# == 1 ## Correlation Matrix of Multivariate sample: (Cl <- cor(longley)) ## Graphical Correlation Matrix: symnum(Cl) # highly correlated ## Spearman's rho and Kendall's tau symnum(clS <- cor(longley, method = "spearman")) symnum(clK <- cor(longley, method = "kendall")) ## How much do they differ? i <- lower.tri(Cl) cor(cbind(P = Cl[i], S = clS[i], K = clK[i])) ## cov2cor() scales a covariance matrix by its diagonal ## to become the correlation matrix. cov2cor # see the function definition {and learn ..} stopifnot(all.equal(Cl, cov2cor(cov(longley))), all.equal(cor(longley, method="kendall"), cov2cor(cov(longley, method="kendall")))) ##--- Missing value treatment: C1 <- cov(swiss) range(eigen(C1, only.values=TRUE)$values) # 6.19 1921 swM <- swiss swM[1,2] <- swM[7,3] <- swM[25,5] <- NA # create 3 "missing" try(cov(swM)) # Error: missing obs... C2 <- cov(swM, use = "complete") range(eigen(C2, only.values=TRUE)$values) # 6.46 1930 C3 <- cov(swM, use = "pairwise") range(eigen(C3, only.values=TRUE)$values) # 6.19 1938 symnum(cor(swM, method = "kendall", use = "complete")) ## Kendall's tau doesn't change much: symnum(cor(swiss, method = "kendall"))