1
votes

I've got a correlation matrix (say 3x3) and I'd like to extract the pairwise correlations and put them into a vector. That is, I'd like to go from the correlation matrix to:

corVec = c(rho_12, rho_13, rho_23)

I'd like to be able to do this for correlation matrices of any dimension.

The reason I'm doing this is because I'd like to construct a multivariate (elliptical) copula using the copula package with a random correlation matrix.

Thanks!

2
Welcome to the site. Since this is about how to do something in R, I marked it for movement to StackOverflow - Peter Flom

2 Answers

3
votes

If the correlation matrix is rho then you can extract the pairwise correlations with:

rho[upper.tri(rho)]
0
votes

Suppose you have a data.frame df1 with 3 columns.
rho=cor(df1) would make a 3x3 matrix.
To make a pairwise correlation "list" (data.frame):

require(reshape2)
rho[!upper.tri(rho)]=NA
rho=na.omit(melt(rho,value.name = 'cor')) 
rho=rho[order(-rho$cor),]