2016-07-01 52 views
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)) 
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 
    } 
} 
+0

你可以用兩個'sapply'函數替換你的內部循環。這可能會有0.2的輕微性能增加。看一看,你可能不得不調換結果矩陣。 – lmo

+0

您的問題也可以並行完成。這裏有很好的參考。看看「foreach」。這是一個很好的資源http://www.r-bloggers.com/how-to-go-parallel-in-r-basics-tips/。 –

回答

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()確實爲矩陣。