[英]storing output of simulation in an array (R)
i was trying to store my simulation output inside an array. 我试图将模拟输出存储在数组中。 I have written the following code: 我写了以下代码:
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
}
But whenever the function is trying to store the output inside an array, I get the following error: 但是每当函数尝试将输出存储在数组中时,都会出现以下错误:
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
seeking your help. 寻求您的帮助。 Thanks. 谢谢。
I think you need to organize your code to avoid such errors. 我认为您需要组织代码以避免此类错误。 I assume you are using package stpp
. 我假设您正在使用软件包stpp
。
First You create, a function which generate the matrix of each iteration; 首先,创建一个函数,该函数生成每次迭代的矩阵; Try to test your function with many values. 尝试用许多值测试您的功能。
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!
}
Once you are sure of your function you call it the loop, with right dimensions. 一旦确定了函数,就可以将其称为具有正确尺寸的循环。
nsim <- 50
u <- 20
res=array(0,c(nsim,u,u))
for(i in 1:nsim)
res[i,,] <- stick_diff(i,u,u)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.