Name correlation matrix

I have a matrix of about 1000 lines of X 500, I am trying to establish a correlation matrix for these variables with names, not numbers, so the result should look like this:

variable1 variable2 variable3 variable4 ... mrv1 mrv2 mrv3 mrv4 ... smrv1 smrv2 smrv3 smrv4 ... . . . . . . . . . . . . 

where mrv1 = Large associated variable with variable1, smrv1 = second most related variable, etc.

I made a correlation matrix, but using a for loop and a very complex command (perhaps the worst command of all time, but it really works!). I look forward to setting this with the proper command, here is the command I'm using now.

 mydata <- read.csv("location", header=TRUE, sep=",") lgn <- length(mydata) crm <- cor(mydata) k <- crm[,1] K <- data.frame(rev(sort(k))) A <- data.frame(rownames(K)) for (x in 2:lgn){ k <- crm[,x] K <- data.frame(rev(sort(k))) B <- data.frame(rownames(K)) A <- cbind(A,B) } 

Any ideas for a simpler, more reliable team?

Thanks,

+7
source share
2 answers

This example works for what you want?

 W <- rnorm( 10 ) X <- rnorm( 10 ) Y <- rnorm( 10 ) Z <- rnorm( 10 ) df <- round( cor( cbind( W , X , Y , Z ) ) , 2 ) df # WXYZ # W 1.00 -0.50 -0.36 -0.27 # X -0.50 1.00 -0.42 -0.02 # Y -0.36 -0.42 1.00 0.17 # Z -0.27 -0.02 0.17 1.00 apply( df , 2 , FUN = function(x){ j <- rev(order(x)); y <- names(x)[j] } ) # WXYZ # [1,] "W" "X" "Y" "Z" # [2,] "Z" "Z" "Z" "Y" # [3,] "Y" "Y" "W" "X" # [4,] "X" "W" "X" "W" #And use abs() if you don't care about the direction of the correlation (negative or postive) just the magnitude apply( df , 2 , FUN = function(x){ j <- rev(order( abs(x) )); y <- names(x)[j] } ) # WXYZ # [1,] "W" "X" "Y" "Z" # [2,] "X" "W" "X" "W" # [3,] "Y" "Y" "W" "Y" # [4,] "Z" "Z" "Z" "X" 
+6
source

To visualize relationships in a correlation matrix, you might consider conducting cluster analysis. Use one minus of the correlation matrix as a distance matrix (or perhaps one minus the absolute value of the correlation matrix), then pass it to a function like agnes or another cluster function. The order and schedules with this can be informative.

+1
source

All Articles