所以我有一个运行的矩阵模型,当模型被告知停止时,我收到一条消息"错误:"
现在模型运行正常,它应该停止它等等.但这意味着我不能在循环中多次运行它,这是不理想的.所以我想知道是否有办法让这个错误消息停止发生,或绕过它.
下面的虚拟代码:
n.steps <- 100
x <- array(0,c(85,n.steps))
mnalph<-50
x[12,]<-2
x[24,]<-2
x[40,]<-3
x[50,]<-1
x[70,]<-2
x[85,] <- 1
k<-0
for (i in 2:n.steps){
k<-k+1
a<-sample(c(replicate(1000,
sample(c(1,0), prob=c(0.75, 0.25), size = 1))),1)
mat[85,85] <- a
currclass <- replacementclass <- round(rnorm(1, mean = mnalph, sd = 11))
if (a == 0) {
d <- sample(1:100, 1)
if (d > 60) {
x[85,k]<-0
stop()
} else {
while(a == 0) {
while (a == 0) {
if (x[currclass, k] > 1) {
x[currclass, k] <- x[currclass, k] - 1
a <- 1
x[85,k]<-a
} else {
nonzeroes<-which(x[0:84,k]>0)
distances<-abs(nonzeroes - currclass)
minlocs<-nonzeroes[which(distances==min(distances))]
replacementclass<-minlocs
if(x[replacementclass, k]>=1){
x[replacementclass, k]<- x[replacementclass, k] - 1}
else {
x[replacementclass, k]<- 0}
a <- 1
x[85,k]<-a
if (all(x[11:84, k] == 0)) {
stop()
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)