0

I have an existing blockdiagonal covariance matrix and I would like sort it in such a fashion that the block structure is easily visible (some functions like blockdiag do this automatically, but I inherited a process that doesn't). That is, right now my rows and columns are not in the order of the individual blocks.

I have 2 groups:

Group 1: 'a', 'b'

Group 2: 'c', 'd'

and what I would like is this:

  a  b  c  d
a 1  3  0  0
b 9 11  0  0
c 0  0  6  8
d 0  0 14 16
Tartaglia
  • 949
  • 14
  • 20
  • 4
    You provided the expected output but can you provide the input as well? – bretauv Mar 22 '23 at 17:52
  • I am actually not able to, because I don't know how to produce the input. I am getting the input for my actual matrix, but I am not sure how to recreate it with this small example. It would be bascially with say columns b and c, and rows b switched. – Tartaglia Mar 22 '23 at 18:29
  • 1
    [edit] your question then paste the output from `dput(x)` (or `dput(head(x))` if large) into a code block. Alternatives: `read.table(text='...')` or `data.frame(...)`. See https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info – r2evans Mar 22 '23 at 18:55

1 Answers1

0

Take a look at the seriation package.

Starting with a block diagonal matrix:

m <- diag(1:5) %x% matrix(1, 2, 2)
m
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    1    1    0    0    0    0    0    0    0     0
#>  [2,]    1    1    0    0    0    0    0    0    0     0
#>  [3,]    0    0    2    2    0    0    0    0    0     0
#>  [4,]    0    0    2    2    0    0    0    0    0     0
#>  [5,]    0    0    0    0    3    3    0    0    0     0
#>  [6,]    0    0    0    0    3    3    0    0    0     0
#>  [7,]    0    0    0    0    0    0    4    4    0     0
#>  [8,]    0    0    0    0    0    0    4    4    0     0
#>  [9,]    0    0    0    0    0    0    0    0    5     5
#> [10,]    0    0    0    0    0    0    0    0    5     5

Shuffle it:

mm <- m[sample(1:10), sample(1:10)]
mm
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    0    0    0    0    2    0    0    0    0     2
#>  [2,]    0    0    0    0    0    5    5    0    0     0
#>  [3,]    0    3    3    0    0    0    0    0    0     0
#>  [4,]    0    0    0    0    2    0    0    0    0     2
#>  [5,]    0    0    0    4    0    0    0    0    4     0
#>  [6,]    0    0    0    4    0    0    0    0    4     0
#>  [7,]    0    0    0    0    0    5    5    0    0     0
#>  [8,]    1    0    0    0    0    0    0    1    0     0
#>  [9,]    0    3    3    0    0    0    0    0    0     0
#> [10,]    1    0    0    0    0    0    0    1    0     0

Seriate the shuffled matrix:

ser <- seriation::seriate(mm)
mm[ser[[1]], ser[[2]]]
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    5    5    0    0    0    0    0    0    0     0
#>  [2,]    5    5    0    0    0    0    0    0    0     0
#>  [3,]    0    0    1    1    0    0    0    0    0     0
#>  [4,]    0    0    1    1    0    0    0    0    0     0
#>  [5,]    0    0    0    0    2    2    0    0    0     0
#>  [6,]    0    0    0    0    2    2    0    0    0     0
#>  [7,]    0    0    0    0    0    0    3    3    0     0
#>  [8,]    0    0    0    0    0    0    3    3    0     0
#>  [9,]    0    0    0    0    0    0    0    0    4     4
#> [10,]    0    0    0    0    0    0    0    0    4     4
jblood94
  • 10,340
  • 1
  • 10
  • 15