Skip to content
Prev 82090 / 398528 Next

Correlation matrix from a vector of pairwise correlations

Serguei Kaniovski <kaniovsk <at> wifo.ac.at> writes:
Not absolutely sure what you want to do, but guessing a little
bit (should your list above include corr(1,4) and corr(2,4) as well?)
If you have

corrs <- c(c12=0.1,c13=0.2,c14=0.3,c23=0.4,c24=0.5,c34=0.6)
## names unnecessary but included for clarity
m = diag(4)   ## 4x4 diagonal matrix
m[lower.tri(m)] = corrs  ## fill in lower triangle

## two ways to make it symmetric:
m = t(m)                 ## flip around matrix
m[lower.tri(m)] = corrs  ## fill in lower triangle

## OR
 m[row(m)>col(m)] = t(m)[row(m)<col(m)]

don't know which is more efficient, but either should work ---
if that's what you were trying for

 Ben Bolker