Skip to content

Commit

Permalink
Informative error when all parboot samples fail, fixes #270
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkellner committed Mar 3, 2024
1 parent 8c06592 commit 5b2b00e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/boot.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ setMethod("parboot", "unmarkedFit", function(object, statistic=SSE, nsim=10,
if(length(t0) == 1) t.star <- matrix(t.star, ncol=1)

failed <- apply(t.star, 1, function(x) any(is.na(x)))
if(all(failed)){
stop("Model fitting failed in all sims.", call.=FALSE)
}
if(sum(failed) > 0){
warning(paste0("Model fitting failed in ",sum(failed), " sims."), call.=FALSE)
t.star <- t.star[!failed,,drop=FALSE]
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test_parboot.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ test_that("parboot handles failing model fits", {
set.seed(123)
expect_warning(pb <- parboot(fm, nsim=20, statistic=fail_func))
expect_equal(nrow(pb@t.star), 13)

# Error message when all parboot samples are bad

# force error only when running function on new simulated datasets,
# but not for original dataset
fail_func <- function(x){
if(round(x@AIC, 5) == 23.29768){
return(0)
} else {
stop("fail")
}
}

set.seed(123)
expect_error(pb2 <- parboot(fm, nsim=20, statistic=fail_func))
})

test_that("parboot handles failing model fits in parallel", {
Expand Down

0 comments on commit 5b2b00e

Please sign in to comment.