I am trying to catch a failure of a function within a loop in R. I want to return NA
and move on to continue to execute the loop.
The function glm.nb
fails at j=1328, i=1, and thereafter fills all the MLEb_d0
and MLEb_d1
slots with missing values.
I would like to catch the error, returning for both MLEb_d0
and MLEb_d1
, and move on to
j=1329 correctly:
# Now get MLE parametric bootstrap solutions, matching the TMM
#-----------------------------------------------------------------
require(foreign)
require(ggplot2)
require(MASS)
MLEb_d0 <- matrix(nrow=2000, ncol=500)
MLEb_d1 <- matrix(nrow=2000, ncol=500)
for (i in 1:2){
yy <- as.matrix(sample[[i]])
for(j in 1:2000){
yyy1 <- as.vector(yy[j,])
yv <- cbind(yyy1,TMM, design_tdDESeq);
yv <- as.data.frame(yv)
m <- summary(m1 <- glm.nb(yyy1 ~ -1 + timed0 + timed1
+ p1 + p2 + p3 + p4 + p5 + p6
+ p7 + p8 + p9 + p10 + p11 + p12
+ p13 + p14 + p15, data = yv, offset(TMM)))
MLEb_d0[j,i] <- m$coefficients[1,4]
MLEb_d1[j,i] <- m$coefficients[2,4]
}
}