简体   繁体   中英

R pnorm function with sparse matrix

I would like to find pvalues of a large sparse matrix. All the elements in this matrix are standard normal z-scores. I want to use pnorm function, but I met a problem that pnorm does not support sparse matrix. Except for transforming sparse matrix to full matrix, is there any other more efficient way?

Any suggestions are appreciated!

If it is a sparse matrix, you can easily replace the 0 values with pnorm(0..). What remains is to calculate the non-zero values, which you can do. For example a sparse matrix:

data <- rnorm(1e5)
zero_index <- sample(1e5)[1:9e4]
data[zero_index] <- 0
mat <- matrix(data, ncol=100)
mat_sparse <- Matrix(mat, sparse=TRUE)

Create a matrix with pnorm for 0:

mat_pnorm <- matrix(pnorm(rep(0,length(mat_sparse))),ncol=ncol(mat_sparse))
nzData <- summary(mat_sparse)
mat_pnorm[as.matrix(nzData[,1:2])] <- pnorm(nzData$x)
all.equal(mat_pnorm,pnorm(mat))
[1] TRUE

You did not specify how you would like the p-values, but you can easily have it cast into a vector instead of a matrix which was used above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM