Serguei Kaniovski
2005-Dec-03 12:42 UTC
[R] Correlation matrix from a vector of pairwise correlations
I've a vector of pairwise correlations in the order low-index element precedes the high-index element, say: corr(1,2)=0.1, corr(1,3)=0.2, corr(2,3)=0.3, corr(3,4)=0.4 How can I construct the corresponding correlation matrix? I tried using the "combn"-function in "combinat" package: library(combinat) combn(c(0.1,0.2,0.3,0.4),2) , but to no avail... Thank you for your help, Serguei Kaniovski -- ___________________________________________________________________ ??sterreichisches Institut f??r Wirtschaftsforschung (WIFO) Name: Serguei Kaniovski Postadresse: Postfach 91 Tel.: +43-1-7982601-231 A-1103 Wien Fax : +43-1-7989386 Standort: Arsenal Objekt 20 Mail: Serguei.Kaniovski at wifo.ac.at A-1030 Wien http://www.wifo.ac.at/
Ben Bolker
2005-Dec-04 02:12 UTC
[R] Correlation matrix from a vector of pairwise correlations
Serguei Kaniovski <kaniovsk <at> wifo.ac.at> writes:> > I've a vector of pairwise correlations in the order low-index element > precedes the high-index element, say: > > corr(1,2)=0.1, corr(1,3)=0.2, corr(2,3)=0.3, corr(3,4)=0.4 > > How can I construct the corresponding correlation matrix?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