I use a cross-card R package , which itself relies on some other R packages (survival, nbpMatching, MASS), and this in turn imports a wide range of additional dependencies. The crossmatch package implements a statistical test on a (potentially) large matrix, which I need to calculate very often (within the framework of the MCMC algorithm). I wrote the following shell, which calculates some preprocessing steps before calculating the actual test (which is crossmatchtest()
on the last line):
crossmatchdata <- function(dat) {
z = dat[,1]
X = subset(dat, select = -1)
n <- dim(X)[1]
k <- dim(X)[2]
for (j in 1:k) {
X[, j] <- rank(X[, j])
}
cv <- cov(X)
vuntied <- var(1:n)
rat <- sqrt(vuntied / diag(cv))
cv <- diag(rat) %*% cv %*% diag(rat)
out <- matrix(NA, n, n)
icov <- ginv(cv)
for (i in 1:n) {
out[i, ] <- mahalanobis(X, X[i, ], icov, inverted = TRUE)
}
dis <- out
return(crossmatchtest(z, dis))
}
I noticed that if the matrix is quite small, only one processor will be used in this test:
library(MASS)
library(crossmatch)
source("theCodeFromAbove.R")
m = cbind(c(rep(0, 100), rep(1, 100)))
m = cbind(m, (matrix(runif(100), ncol=10, nrow=20, byrow=T)))
while(TRUE) { crossmatchdata(m) }
htop
. , , R , ( , ):
m = cbind(c(rep(0, 1000), rep(1, 1000)))
m = cbind(m, (matrix(runif(100000), ncol=1000, nrow=2000, byrow=T)))
while(TRUE) { crossmatchdata(m) }
, , R. options(mc.cores = 4)
.
- , ? , ?