I'm using boot
to bootstrap an optimization function in order to estimate standard errors. Unfortunately, on rare occasions the optimization function returns an error which stops the boot
function. The error's are not critical to estimation and i would like to skip that iteration and continue to the next.
I have tried to find a solution with try
and tryCatch
but haven't been able to use either correctly. When wrapping the optimization function within statistic
i have managed to skip the errors. However, this results in the number of estimations within boot
being less than the initial number of iterations and returning an error.
A basic example of my code is below
Any help is appreciated, Thanks
bootfun = function(bootdata, i, d, C1) {
C1 = cov (bootdata[i])
ans = constrOptim(...) #This function returns an error
return(ans$par [d])
}
bootres = boot(bootdata, statistic = bootfun, 500)
EDIT: I have managed to find an acceptable solution to my problem. However, if a function gives errors often this may not be acceptable as each error replaces a bootstrap replication with NA.
bootfun = function(bootdata, i, d, C1) {
C1 = cov(bootresid[i])
tryCatch({
ans = constrOptim(...)
return(ans$par[1:18] [d]) },
error=function(err) {rep(NA,18)} )
}