Extracting pairwise correlations from correlation matrix - r

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!

If the correlation matrix is rho then you can extract the pairwise correlations with:
rho[upper.tri(rho)]

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),]

Related

Is PCA possible in 2x2 matrix taken from 3x3 population covariance matrix

Population matrix is given as below:
May I know if I want only first two Principal Components, PC1 and PC2, can I make the matrix as 2x2 matrix below to perform calculation of eigen vectors?

use correlation matrix in robust PCA functions R

I want to perform robust principal component analysis (PCA) on the correlation matrix. Namely, rrcov::PcaHubert.
I know that if I give to the function cor=TRUE, rrcov:CovMcd calculates the robust covariance and correlation matrix. How can I force the PCA to use the correlation matrix instead of the covariance matrix?
Thanks!

Generate random matrix given a correlation value to an input matrix

Given an input matrix and a correlation Rho, I want to generate a random matrix that is correlated to the input matrix with a correlation value of Rho.
I can create random matrices through rnorm, but I'm not sure how to force this new matrix to be correlated to the original input matrix.
I looked through some other posts such as this but couldn't find what I was looking for. For example, this post Generating random correlation matrix with given average correlation looks to calculate a random matrix, but correlated to itself, not an input matrix.

Can I use a covariance matrix to specify the correlation structure in the nlme function gls?

I wish to use the function gls in the R package nlme to analyse a set of nested spatial samples, in which many samples overlap in at least some spatial coordinates. I want to account for non-independence in the response variable (the thing I'm measuring in each spatial sample) using either a corStruct or pdMat object, but I'm confused about how to do this.
I have generated a covariance matrix that should encode all the information about non-independence between spatial samples. Each row/column is a distinct spatial sample, the diagonal contains the total number of sampling units captured by each spatial sample, and the off-diagonal elements contain counts of sampling units shared between spatial samples.
I think I should use the nlme function gls while specifying a correlation structure, possibly using a corSymm or pdMat object. But I've only seen examples where the correlation structure in gls is specified via a formula. How can I use the covariance matrix that I've created?
I discovered that you can pass the nlme function gls a positive-definite correlation matrix by using the general correlation structure provided by corSymm.
# convert your variance covariance matrix into a correlation matrix
CM <- cov2cor(vcv_matrix)
# if your correlation matrix contains zeros, as mine did, you need to convert it to a positive-definite matrix that substitutes very small numbers for those zeros
CM <- nearPD(CM)$mat
# convert into a corStruct object using general correlation structure provided by corSymm
C <- corSymm(CM[lower.tri(CM)], fixed = T)
# correlation structure can now be included in a gls model
gls(y ~ x, correlation = C, method = "ML")

Create an artificial correlation matrix

I want to do some testing of a program but I would like to have a really big matrix
Is there any tool that can generate an artificial correlation matrix?
Pick n random n-dimensional vectors of numbers from -1 to 1. Use the dot product of any 2 vectors is their correlation. Use that fact to make a random n x n correlation matrix.
Is this really a correlation matrix? Make each dimension into an independent standard normal distribution. The coefficients of each vector then describes a random variable. Those random variables have the specified correlations. So yes, this is actually going to be a correlation matrix.
There is a repository of sample matrix data for use in comparing algos available at the Matrix Market - free despite the name.

Resources