【发布时间】:2017-07-05 22:20:24
【问题描述】:
设置 出于模拟的目的,我生成了一个 B=2000 元素列表,每个元素都是排列过程的输出,在该排列过程中,我首先排列 @ 的行987654323@ 矩阵和对于每一列,我计算了第一和第二个 100 行之间的 Kolmogorov-Smirnov 检验统计量(您可以将前 100 行视为来自一个组和后 100 行作为另一个组的数据)。
问题此过程需要很长时间(大约 30-40 分钟)来生成列表。有更快的方法吗?以后我想把B 增加到更大的值。
代码
B=2000
n.row=200; n.col=8000
#Generate sample data
samp.dat = matrix(rnorm(n.row*n.col),nrow=n.row)
perm.KS.list = NULL
for (b in 1:B){
#permute the rows
perm.dat.tmp = samp.dat[sample(nrow(samp.dat)),]
#Compute the permutation-based test statistics
perm.KS.list[[b]]= apply(perm.dat.tmp,2,function(y) ks.test.stat(y[1:100],y[101:200]))
}
#Modified KS-test function (from base package)
ks.test.stat <- function(x,y){
x <- x[!is.na(x)]
n <- length(x)
y <- y[!is.na(y)]
n.x <- as.double(n)
n.y <- length(y)
w <- c(x, y)
z <- cumsum(ifelse(order(w) <= n.x, 1/n.x, -1/n.y))
z <- z[c(which(diff(sort(w)) != 0), n.x + n.y)] #exclude ties
STATISTIC <- max(abs(z))
return(STATISTIC)
}
【问题讨论】:
标签: r performance list permutation