Please I need help partitioning a symmetric or any matrix in R, say my variables are grouped say the default data in R called longley,
data()
L.df<-longley
cor(L.df)
I need a code which will help me partition this correlation matrix as
So that I can be able to take the average of it partition allowing me to assume equal correlation as the interpolation of each group!
So I can have something like
As my structured correlation matrix.
PS. I obtained the structured manually
Would love to be able to partition it at any Column or row of choice.
Note: The partition assumes variables 1&2 are in group 1, Variables 3,4 & 5 are in group 2 And variables 6&7 are in group 3
You can get the correlation matrix, set the diagonal to
NA
, iterate through the groups and rewrite their entries with their NA-omitted means, then write 1 into the diagonal:To change the groups, you need to supply them as a list of column indices. For example, if you wanted two groups with columns 1:3 and 4:7, you could do:
Created on 2022-04-11 by the reprex package (v2.0.1)