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 に答える