0

我试图将我的模拟输出存储在一个数组中。我写了以下代码:

nsim=50
res=array(0,c(nsim,20,20))
for(i in 1:nsim) {
    cat("simul=",i,"\n")
    simulated = NULL
    stik.simulated = NULL
    simulated = rpp(....)
    stik.simulated = STIKhat(....)
    # from stik.simulated we will get $khat and $Ktheo and 
    # the dimension of stik.simulated$Khat-stik.simulated$Ktheo is 20 x 20
    res[i,,] = stik.simulated$Khat - stik.simulated$Ktheo  
}

但是每当函数试图将输出存储在数组中时,我都会收到以下错误:

simul= 1 
Xrange is  20 40 
Yrange is  -20 20 
Doing quartic kernel
Error in res[, , i] = stik.simulated$Khat - stik.simulated$Ktheo : 
  subscript out of bounds

寻求你的帮助。谢谢。

4

1 回答 1

0

我认为您需要组织代码以避免此类错误。我假设您正在使用 package stpp

首先,您创建一个生成每次迭代矩阵的函数;尝试使用许多值测试您的函数。

stick_diff <- function(u,v){
  u <- seq(0,u,by=1)
  v <- seq(0,v,by=1)
  simulated <- rpp(...)                    ## call here rpp with right parameters
  stik <- STIKhat(xyt=simulated$xyt,
                     dist=u, times=v, ...)
  stik$Khat-stik$Ktheo                     ## !! here if u#v you will have recycling!
}

一旦你确定你的功能,你就称它为循环,具有正确的尺寸。

nsim <- 50
u    <- 20
res=array(0,c(nsim,u,u))
for(i in 1:nsim)
  res[i,,] <- stick_diff(i,u,u)
于 2013-01-21T21:29:06.827 回答