1

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 statistici 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)} )
}
LoganH
  • 31
  • 4
  • 1
    What do you want the function's return value to be when it `try` or `tryCatch` gets an error? – Thomas Jun 10 '13 at 22:05
  • @Thomas I had previously had it return an error message. Would having it return NA be appropriate? From what i understand, boot requires something to take the place of an error. – LoganH Jun 11 '13 at 00:28
  • Can you include some sample data the reproduces your error? – SchaunW Jun 11 '13 at 00:48
  • The errors occur from bootstrapping, there are no errors when simply running `constrOptim`. As `C1` is a covariance matrix it may contain NA's if the replicated data is just so. This is then passed into `constrOptim` and as i use values from C1 for the initial parameters it returns an error. Also, i have tried having `tryCatch` return NA's from `constrOptim` but boot does not accept this. – LoganH Jun 11 '13 at 01:24
  • Based on your updated question, it's unclear what you would prefer more than your current solution. Your use of tryCatch is correct, but if you don't like the return value, you could simply specify something else instead of a vector of `NA`s. – Thomas Jun 11 '13 at 06:37
  • @Thomas My preference would be to have `boot` use only the successful iterations and continue after errors. However, with a better understanding of `tryCatch`, that it must return a value, i'm not sure it's as useful. – LoganH Jun 11 '13 at 09:04

1 Answers1

1

This is not an answer with your specific code, but a more general demonstration of tryCatch for the situation you describe. If you want to simply remove entries that cause errors, have the function return nothing on error and then remove NULL values from the results:

testfun <- function(i) {
tryCatch({
d <- rbinom(1,1,.3) # generate an error 30% of the time
if(d==1)
    error("test stop")
else
    return(1:10) # return your actual values
}, 
error = function(err) {return()} # return NULL on error
    )
}

x <- sapply(1:20, FUN=testfun) # run demo 20 times
x <- x[-(which(sapply(x,is.null),arr.ind=TRUE))]
# when errors happen, x is shorter than 20

The final line removes NULL entries from the list (based on this: https://stackoverflow.com/a/3336726/2338862).

Community
  • 1
  • 1
Thomas
  • 43,637
  • 12
  • 109
  • 140