4

Do you know how to transform a matrix to a so-called double centering matrix in R? Such that sum(col) and sum(row) of the transformed matrix are all zero vector. Thanks.

Azizah
  • 53
  • 1
  • 3
  • 2
    Do you have any example of this being done somewhere else? Any link for a tutorial or something? – Rodrigo Apr 26 '17 at 15:55
  • I want to transform the matrix A into a double centering matrix | 1 4 7 | A = | 2 5 8 | | 3 6 9 | – Azizah Apr 29 '17 at 14:19
  • I was expecting a more clear explanation. – Rodrigo Apr 29 '17 at 21:05
  • I want to make a two way clustering in R. I have a matrix consisting of compounds (in row) and proteins (in column). Before I make a two dendograms in two way clustering, I need to make a double centering from that matrix. So, that whay I asked about double centering matrix. – Azizah May 03 '17 at 04:01
  • I think that if you include a link to an on-line example, or provide a [reproducible example](http://stackoverflow.com/q/5963269/1086511), your question will be much better received. – Rodrigo May 03 '17 at 12:13

1 Answers1

8

Double-centering a matrix M is done with the following algorithm:

  1. Generate two matrices R and C with the same size as M. R and C contain the row-wise and column-wise means respectively:
    | mean(M[1,1:3])  mean(M[1,1:3])  mean(M[1,1:3]) |
R = | mean(M[2,1:3])  mean(M[2,1:3])  mean(M[2,1:3]) |
    | mean(M[3,1:3])  mean(M[3,1:3])  mean(M[3,1:3]) | 

and

    | mean(M[1:3,1])  mean(M[1:3,2])  mean(M[1:3,3]) |
C = | mean(M[1:3,1])  mean(M[1:3,2])  mean(M[1:3,3]) |
    | mean(M[1:3,1])  mean(M[1:3,2])  mean(M[1:3,3]) |
  1. Subtract them to M and add the grand mean: M - C - R + grand_mean(M).

Here is a code performing this:

# example data
M = matrix(runif(9), nrow=3, ncol=3)

# compute the row-wise and column-wise mean matrices
R = M*0 + rowMeans(M)  # or `do.call(cbind, rep(list(rowMeans(tst)), 3))`
C = t(M*0 + colMeans(M))  # or `do.call(rbind, rep(list(colMeans(tst)), 3))`

# substract them and add the grand mean
M_double_centered = M - R - C + mean(M[])

You can check that this gives the right answer by computing rowMeans(M_double_centered) and colMeans(M_double_centered).

Community
  • 1
  • 1
Jealie
  • 6,157
  • 2
  • 33
  • 36