Skip to content

Commit

Permalink
Added an error for botstrap when Sample.Label not unique across strata
Browse files Browse the repository at this point in the history
See issue #157
  • Loading branch information
LHMarshall committed Dec 20, 2023
1 parent ce0c77d commit b8d19d9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Description: A simple way of fitting detection functions to distance sampling
Horvitz-Thompson-like estimator) if survey area information is provided. See
Miller et al. (2019) <doi:10.18637/jss.v089.i01> for more information on
methods and <https://examples.distancesampling.org/> for example analyses.
Version: 1.0.8.9001
Version: 1.0.8.9002
URL: https://github.com/DistanceDevelopment/Distance/
BugReports: https://github.com/DistanceDevelopment/Distance/issues
Language: en-GB
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Distance 1.0.9
* Fixed bug which produced NA's when stratum names came after 'Total' in the alphabet. (Issue #158)
* Added additional documentation explaining the adjustment term options when covariates are in the model. (Issue #156)
* Fixed dht bootstrap to work when distbegin and distend are supplied but not distance. (Issue #147)
* Added a warning for the dht bootstrap when Sample.Label values are not unique across all strata. (Issue #157)

Distance 1.0.8
----------------------
Expand Down
5 changes: 5 additions & 0 deletions R/bootdht_resample_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ bootdht_resample_data <- function(bootdat, our_resamples,

# get all samples per stratum
samps_per_strata <- unique(bf[, c(stratum_label, sample_label)])
# Check that the sampler names are unique across strata
# The number of unique sampler names should be the same as the rows in samps_per_strata
if(sample_label %in% our_resamples && !nrow(samps_per_strata) == length(unique(samps_per_strata[[sample_label]]))){
stop("Cannot bootstrap on samplers within strata as sampler ID values are not unique across strata. Please ensure all Sample.Label values are unique.", call. = FALSE)
}
samps_per_strata <- by(bf[,c(stratum_label, sample_label)],
bf[[stratum_label]],
function(x) unique(x[[sample_label]]))
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test_bootdht.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,20 @@ test_that("Issue #158 is fixed (stratum names > 'Total' bug)", {
set.seed(225)
bootout <- bootdht(mod1, flatfile=minke, nboot=3)
expect_true(nrow(bootout) > 0)
})


# generate some data to test on
dat <- data.frame(object = 1:60, Sample.Label = rep(1:10,6),
Area = 100, Effort = 1000)
dat$Region.Label <- c(rep("StrataA", 30), rep("StrataB", 30))
dat$distance <- abs(rnorm(nrow(dat), 0, 25))
dat$size <- rpois(nrow(dat), 20)
dat$ref.object <- dat$object

test_that("Error raised when sampler ID not unique", {

set.seed(123)
expect_error(bootdht_resample_data(dat, c("Sample.Label")),
"Cannot bootstrap on samplers within strata as sampler ID values are not unique across strata. Please ensure all Sample.Label values are unique.")
})

0 comments on commit b8d19d9

Please sign in to comment.