I have the following codes to run.
library(fExtremes)
library(maxLik)
theta0 <- c(0.1, 1)
theta1 <- c(0.1, 2)
set.seed(20150518)
X <- matrix(0, nrow=1000, ncol=1000)
for(i in 1:1000){
X[i, ] <- c(rgpd(900, xi=-theta0[1], beta=theta0[2]),
rgpd(100, xi=-theta1[1], beta=theta1[2]))
}
library(maxLik)
loglik <- function(param){
shape <- param[1]
scale <- param[2]
sum(dgpd(x, xi=-shape, beta=scale, log=TRUE))
}
scale.mle <- rep(0, 1000)
for(i in 1:1000){
x <- X[i, ]
scale.mle[i] <- as.numeric(maxLik(logLik=loglik, start=c(theta0[1], theta0[2]), fixed=1)$estimate[2])
}
However, for certain runs I get the following error message:
Error in maxNRCompute(fn = logLikAttr, fnOrig = fn, gradOrig = grad, hessOrig = hess, :
NA in the initial gradient
And what is worse is that once this error occurs, the entire for
loop is stopped even if later data input can be used. How can I ask R
to skip such problematic case and move on to the next. In order words, how do I continue to finish all the 1000 runs without stopping. Thanks.
Update: Thanks for pointing out the other question. However, I still do not get it. Could anyone illustrate the usage of tryCatch
with my example, please?