matrix - Compare only pairwise comparisons BETWEEN two matrices using rcorr() in R -


hopefully post isn't duplicate, i've spent quite time searching , haven't been able find answer.

i have 2 matrices trying make pairwise correlations between, this.

matrix 1        gene1     gene2     gene3 id1    12        32        43 id2    94        34        95 id3    90        54        23 id4    43        76        65  matrix2        te1       te2       te3     id1    94        90        82 id2    23        46        94 id3    23        49        39 id4    39        34        46 

i'm able table of r^2 values using base function cor(), makes comparisons between matrices, in pairwise way. result looks this:

     gene1        gene2        gene3 te1  0.98         0.48         0.45 te2  0.77         0.46         0.76 te3  0.45         0.56         0.76 

which great! problem need p values too, can cut matrix down have p values less cutoff (because real matrices [30,800] , [30,1000] , need way cut down data intelligible).

the rcorr() package great @ this, produces matrix r of correlations, matrix p of p values, , vector of number of observations. but, have not been able figure out way compare between matrices- compares within matrices. result looking this:

       gene1   gene2   gene3   te1   te2   te3 gene1  1.0     0.5     0.5     0.5   0.3   0.9 gene2          1.0     0.4     0.7   0.7   0.5 gene3                  1.0     0.8   0.8   0.5 te1                            1.0   0.8   0.2 te2                                  1.0   0.7 te3                                        1.0 

this made data, illustrates point. produces twice data need, , slows down calculations produces correlation graphs nonsense, visually.

so question this: there way compare between, not within matrices using hmisc package function rcorr()?

i've tried cor.test on lists of matrices:

cor.test(c(matrix1),c(matrix2), method="pearson") 

but error 'x' , 'y' must have same length.

next have figure out how subset matrices based on list of high correlation , low p value, see there's helpful answers here need scrutinize.

there easier way it, 1 option cor.test every column pair

tmp <- with(expand.grid(seq(ncol(matrix1)), seq(ncol(matrix2))),             mapply(function(i, j) cor.test(matrix1[, i], matrix2[, j]),                    var1, var2)) 

and extract elements test objects

matrix(unlist(tmp['estimate', ]), nrow=ncol(matrix1),        dimnames=list(colnames(matrix1), colnames(matrix2))) #             te1        te2          te3 #gene1 -0.8757869 -0.4755768 -0.008312574 #gene2 -0.3567850 -0.7585136 -0.834883959 #gene3 -0.2723512 -0.3764091  0.546779587  matrix(unlist(tmp['p.value', ]), nrow=ncol(matrix1),        dimnames=list(colnames(matrix1), colnames(matrix2))) #             te1       te2        te3 #gene1 0.05156122 0.4181472 0.98941622 #gene2 0.55555798 0.1371765 0.07851595 #gene3 0.65756758 0.5323119 0.34025894 

you can check it's correct comparing output of cor(matrix1, matrix2) matrix of estimates, matrices should equal.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -