简体   繁体   中英

Create covariance matrix from correlation table with both positively and negatively correlated values

I have a correlation table with both positively and negatively correlated values:

cor.table <- matrix(rep(c(-0.1, 0.1), each=1250),50,50)
diag(cor.table) <- 1

I would like to create a covariance matrix from this using:

cov.mat<-cor2cov(cor.table,c(rep(20,50)))

However I get the following error:

Error in cor2cov(cor.table, c(rep(100, 50))) : 
  The object 'cor.mat' should be either a symmetric or a triangular matrix

How can I create a symmetric correlation matrix where values are either positively (0.1) or negatively (-0.1) correlated?

set.seed(1)
cor.table <- matrix(sample(c(0.1,-0.1),50^2,replace=TRUE),50,50)

> isSymmetric(cor.table)
[1] FALSE

ind <- lower.tri(cor.table)
cor.table[ind] <- t(cor.table)[ind]
diag(cor.table) <- 1

> isSymmetric(cor.table)
[1] TRUE

Your problem was you did not create a symmetric matrix. It should work now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM