Data transformation avoiding nested loops in R Data transformation avoiding nested loops in R r r

Data transformation avoiding nested loops in R


You can use this in these types of calculations without needing even a single loop. Rewrite your equation, and then you get :

Xtrans[i,j] = X[i,j] / ( sum( X[i, ] ) * sqrt( sum( X[ ,j] ) ) )

To get a matrix representing the term - sum( X[i, ] ) * sqrt( sum( X[ ,j] ) ) - you use the function outer() or %o% like this:

rowSums(X) %o% sqrt(colSums(X))

Or, for the column transformation :

sqrt(rowSums(X)) %o% colSums(X)

The only thing you need to do, is divide your original matrix by this one, eg for the col transformation :

TEST <- matrix(               c(15366,2079,411,366,23223,2667,699,819,                 31632,2724,717,1473,49938,3111,1062,11964),                 nrow=4,ncol=4,byrow=T)> TEST / (sqrt(rowSums(TEST)) %o% colSums(TEST))             [,1]        [,2]        [,3]         [,4][1,] 0.0009473414 0.001455559 0.001053892 0.0001854284[2,] 0.0011674098 0.001522501 0.001461474 0.0003383284[3,] 0.0013770523 0.001346668 0.001298230 0.0005269580[4,] 0.0016167998 0.001143812 0.001430074 0.0031831055

In approximately the same way you can calculate the row transformation.

Doing the hand calculations, I can confirm that my solution is correct, provided I understood your index notation correctly (meaning that i stands for rows and j for columns). The numbers you expect are not the ones you say you expect. To show you :

> ( TEST[1,2] / sum(TEST[,2]) ) / sqrt(sum(TEST[1,]))[1] 0.001455559

The chi-square normalization you talk about, can actually be found in the function decostand of the vegan package. Mind you that by default, the method adjusts by multiplying by the square root of the matrix total. This makes sense in a correspondence analysis.

If you don't want to use this correction, then you can get eg the column transformation also as follows :

> require(vegan)> decostand(TEST,method="chi.square",MARGIN=2)/sqrt(sum(TEST))             [,1]         [,2]        [,3]        [,4][1,] 0.0009473414 0.0011674098 0.001377052 0.001616800[2,] 0.0014555588 0.0015225011 0.001346668 0.001143812[3,] 0.0010538924 0.0014614736 0.001298230 0.001430074[4,] 0.0001854284 0.0003383284 0.000526958 0.003183106attr(,"decostand")[1] "chi.square"