如何使用foreach包将普通循环转换为并行循环?
我尝试从vrtest包中转换此代码
CvMexpb <- matrix(0,nrow=B,ncol=2)
for(k in 1:B)
{
eb <- e * Mammen2(n)
eb <- eb - mean(eb)
tem <- 0
for( j in 1:(n-1) )
{
aux2 <- 1/((j*pi)*(j*pi))
aux2 <- aux2/(n-j+1)
tem <- tem+aux2* t(eb[(1+j):n]) %*% weiexp[1:(n-j),1:(n-j)] %*% eb[(1+j):n]
}
CvMexpb[k,] <- cbind(tem/v > CvMexp,tem/v)
}
pboot <- mean(CvMexpb[,1])
Critboot <- quantile(CvMexpb[,2],c(0.9,0.95,0.99))
return(pboot)
进入
CvMexpb <- foreach(k = 1:B, .combine='cbind') %:%
{
eb <- e * Mammen2(n)
eb <- eb - mean(eb)
tem <- 0
foreach(j = 1:(n-1), .combine='c') %dopar%
{
aux2 <- 1/((j*pi)*(j*pi))
aux2 <- aux2/(n-j+1)
tem <- tem+aux2* t(eb[(1+j):n]) %*% weiexp[1:(n-j),1:(n-j)] %*% eb[(1+j):n]
}
CvMexpb[k,] <- cbind(tem/v > CvMexp,tem/v)
}
pboot <- mean(CvMexpb[,1])
Critboot <- quantile(CvMexpb[,2],c(0.9,0.95,0.99))
return(pboot)
但返回错误
foreach(k = 1:B, .combine = "cbind") %:% { : "%:%" 中的错误传递了一个非法的右操作数
原始完整代码是
compweexp <-
function(inf)
{
n <- length(inf)
weiexp <- matrix(1,nrow=n,ncol=n)
for(i in 1:n)
{
for(j in (i+1):n)
{
if(j > n) break
aux1 <- (inf[i]-inf[j]) %*% t(inf[i]-inf[j])
weiexp[i,j] <- exp(-0.5*aux1)
weiexp[j,i] <- weiexp[i,j]
}
}
return(weiexp)
}
Mammen <-
function(n)
{
p <- (sqrt(5)+1)/(2*sqrt(5))
zmat <- rep(1,n)*(-(sqrt(5)-1)/2);
u <- runif(n,0,1)
zmat[u > p] <- (sqrt(5)+1)/2
return(zmat)
}
Gen.Spec <-
function(y,B=300)
{
set.seed(12345)
n<- length(y)
e <- y - mean(y)
v <- var(e)
y1 <- y[1:(n-1)]
weiexp <- compweexp(y1)
CvMexp <- 0
for(j in 1:(n-1)) {
aux2 <- 1/((j*pi)^2)
aux2 <- aux2/(n-j+1)
CvMexp <- CvMexp+ aux2* t(e[(1+j):n]) %*% weiexp[1:(n-j),1:(n-j)] %*% e[(1+j):n]
}
CvMexp <- CvMexp/v
CvMexpb <- matrix(0,nrow=B,ncol=2)
for(k in 1:B)
{
eb <- e * Mammen(n)
eb <- eb - mean(eb)
tem <- 0
for( j in 1:(n-1) ){
aux2 <- 1/((j*pi)^2)
aux2 <- aux2/(n-j+1)
tem <- tem+aux2* t(eb[(1+j):n]) %*% weiexp[1:(n-j),1:(n-j)] %*% eb[(1+j):n]
}
CvMexpb[k,] <- cbind(tem/v > CvMexp,tem/v)
}
pboot <- mean(CvMexpb[,1])
Critboot <- quantile(CvMexpb[,2],c(0.9,0.95,0.99))
return(pboot)
}