R apply function on symmetrical matrix

196 views Asked by At

I'd like to utilise one of the apply set of functions to do some calculations.

First off, I have two matrices, mat1 and mat2:

mat1:

    a   b   c
1   NA  NA  NA
2   1   1   1
3   1   1   NA
4   NA  1   NA

mat2:

       a    b    c
a    1.0   0.2  0.3
b   -0.7   1.0  0.8 
c   -0.1  -0.3  1.0   

mat2 is calculated using mat1 using a function which is irrelevant here, and essentially I'd like to apply a weighting function to mat1 which penalizes the results from mat2 when there is less data (and therefore less accurate).

So to achieve this, I want to, for some coordinate x,y in mat2, calculate the pairwise completeness of two columns of mat1.

For example: mat2["a","b"] or mat2["b","a"] (should be same) would become the original values * (the complete rows of mat1 of a and b/total rows of mat1 of a and b).

So really the question is how can I apply a function to a matrix that loops every column for every column (double loop) and store this in a weight matrix to multiply against another matrix?

I can already compare two rows using rollapply from zoo package like so:

rowSums(rollapply(is.na(t(mat1)), 2, function(x) !any(x))) 

I get:

[1] 2 1

As in, comparing a and b, 2 rows are complete and comparing b and c, 1 row is complete. So how can I compare a to b, a to c and b to c?

Thanks.

1

There are 1 answers

0
Matthew Lundberg On BEST ANSWER

I was looking at your question again, and it appears that you want a matrix X with the same dimensions of mat2, where X[i,j] is given by the number of complete cases in mat1[,c(i,j)]. Then mat2 will be multiplied by X.

The number of complete cases is given by sum(complete.cases(mat1[,c(i,j)])). I want to use this in outer which requires a vectorized function, so this is passed through Vectorize:

outer(seq(nrow(mat2)), seq(ncol(mat2)),
       Vectorize(function(x,y) sum(complete.cases(mat1[,c(x,y)])))
)
##      [,1] [,2] [,3]
## [1,]    2    2    1
## [2,]    2    3    1
## [3,]    1    1    1

This is your desired symmetric matrix.