替换双循环以提高速度

时间:2016-07-01 22:12:44

标签: r performance optimization

我正在估计条件边际密度并在新观察时对其进行评估。然后我将估计输入到数组中。这段代码很慢,我无法显着加快速度。任何帮助深表感谢。这是一个可重复的小例子:

library(sm)

y <- rep(1:6, 30)
K <- length(unique(y))
X <- matrix(rnorm(180 * 1000), nrow=180)
newx <- matrix(rnorm(20 * 1000), nrow=20)

f.estimates <- array(dim=c(dim(newx)[1], dim(X)[2], K - 1))
g.estimates <- array(dim=c(dim(newx)[1], dim(X)[2], K - 1))
for(k in 1:(K - 1)) {
  for(j in 1:dim(X)[2]) {
    f.estimates[, j, k] <- sm.density(X[y <= k, j], 
                              eval.points=newx[, j], 
                              display="none")$estimate
    g.estimates[, j, k] <- sm.density(X[y > k, j], 
                              eval.points=newx[, j],
                              display="none")$estimate
  }
}

1 个答案:

答案 0 :(得分:0)

设定:

library(sm)

y <- rep(1:6, 30)
K <- length(unique(y))
X <- matrix(rnorm(180 * 1000), nrow=180)
newx <- matrix(rnorm(20 * 1000), nrow=20)

f.estimates <- array(dim=c(dim(newx)[1], dim(X)[2], K - 1))
g.estimates <- array(dim=c(dim(newx)[1], dim(X)[2], K - 1))

使用plyr

library(plyr)
cond <- expand.grid(k=1:(K-1), j=1:dim(X)[2]) #conditions, to avoid multiple **ply loops

f.estimates <- aaply(cond, 1, function(c) sm.density(X[y <= c[,1], c[,2]], 
                                                 eval.points=newx[, c[,2]], 
                                                 display="none")$estimate)
f.estimates <- aperm(f.estimates, c(3,2,1))

g.estimates <- aaply(cond, 1, function(c) sm.density(X[y > c[,1], c[,2]], 
                                                 eval.points=newx[, c[,2]], 
                                                 display="none")$estimate)
g.estimates <- aperm(g.estimates, c(3,2,1))

使用aperm()转置数组维度的顺序,如t()对矩阵的作用。