Skip to content

Commit db8f960

Browse files
committed
Fix crash in analyse_baSAR() if n.MCMC is equal to the thinning interval.
Now we don't let the thinning interval exceed n.MCMC / 2, so that we are certain to get at least two posterior samples: with that we don't crash anymore and JAGS doesn't print out ugly errors.
1 parent 2d12e99 commit db8f960

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

NEWS.Rmd

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ above, the function now only accepts CSV files as input (#237, fixed in #270).
4949
* Add support for `recordType` passed to `get_RLum` in the additional arguments.
5050
* Option `plot.single` has been renamed to `plot_singlePanels` (#351, fixed
5151
in #408).
52+
* The function crashed if the number of MCMC iterations was set equal to the
53+
thinning interval; on the other hand, we saw errors reported from JAGS if the
54+
number of MCMC iterations was not at least double the thinning interval. To
55+
address this, we slightly retouched our automatic setting of the thinning
56+
interval (which was problematic only if the number of MCMC iterations was
57+
extremely low); on the other hand, if a user sets a thinning interval that
58+
is too high, we now reset it to a lower value and raise a warning (#407,
59+
fixed in #409).
5260

5361
### `analyse_FadingMeasurement()`
5462
* The function now checks for the version of the BIN-file that originated the

R/analyse_baSAR.R

+16-25
Original file line numberDiff line numberDiff line change
@@ -484,21 +484,30 @@ analyse_baSAR <- function(
484484
thin <- n.MCMC/1e+05 * 250
485485

486486
}else{
487-
thin <- 10
488-
487+
thin <- min(10, n.MCMC / 2)
489488
}
490489
} else{
490+
.validate_positive_scalar(method_control[["thin"]], int = TRUE,
491+
name = "'thin' in 'method.control'")
491492
method_control[["thin"]]
492493
}
493494

495+
## jags reports ugly errors if thin exceeds n.MCMC / 2, as that
496+
## would correspond to producing just one posterior sample, see #407
497+
if (!is.null(method_control[["thin"]]) && thin > n.MCMC / 2) {
498+
thin <- n.MCMC / 2
499+
.throw_warning("'thin = ", method_control[["thin"]],
500+
"' is too high for 'n.MCMC = ", n.MCMC,
501+
"', reset to ", thin)
502+
}
503+
494504
##variable.names
495505
variable.names <- if (is.null(method_control[["variable.names"]])) {
496506
c('central_D', 'sigma_D', 'D', 'Q', 'a', 'b', 'c', 'g')
497507
} else{
498508
method_control[["variable.names"]]
499509
}
500510

501-
502511
#check whether this makes sense at all, just a direty and quick test
503512
stopifnot(lower_centralD >= 0)
504513

@@ -532,7 +541,6 @@ analyse_baSAR <- function(
532541

533542
for (i in 1:Nb_aliquots) {
534543
Limited_cycles[i] <- length(data.Dose[, i]) - length(which(is.na(data.Dose[, i])))
535-
536544
}
537545
}
538546

@@ -668,8 +676,8 @@ analyse_baSAR <- function(
668676
cat("\n[analyse_baSAR()] ---- baSAR-model ---- \n")
669677
cat("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
670678
cat("[analyse_baSAR()] Bayesian analysis in progress ...\n")
671-
message(".. >> bounds set to: lower_centralD =", lower_centralD,
672-
"| upper_centralD =", upper_centralD)
679+
message(".. >> bounds set to: lower_centralD = ", lower_centralD,
680+
" | upper_centralD = ", upper_centralD)
673681
}
674682

675683
Nb_Iterations <- n.MCMC
@@ -717,7 +725,6 @@ analyse_baSAR <- function(
717725
thin = thin
718726
)
719727

720-
721728
pt_zero <- 0
722729
nb_decal <- 2
723730
pt_zero <- Nb_aliquots
@@ -733,7 +740,6 @@ analyse_baSAR <- function(
733740
rm(temp.vector)
734741
}else{
735742
gm <- NULL
736-
737743
}
738744

739745
##quantiles
@@ -774,12 +780,12 @@ analyse_baSAR <- function(
774780
)
775781
)
776782
)
777-
778783
}
779784
##END
780785
##////////////////////////////////////////////////////////////////////////////////////////////////
781786

782-
# Integrity tests -----------------------------------------------------------------------------
787+
788+
## Integrity checks -------------------------------------------------------
783789

784790
.require_suggested_package("rjags")
785791
.require_suggested_package("coda")
@@ -1782,7 +1788,6 @@ analyse_baSAR <- function(
17821788
##LxTx SD values
17831789
OUTPUT_results[comptage, (9 + 2*max_cycles):(8 + 2*max_cycles + llong)] <-
17841790
as.numeric(Disc_Grain.list[[k]][[disc_selected]][[grain_selected]][[4]])
1785-
17861791
}
17871792
}
17881793
}
@@ -1917,7 +1922,6 @@ analyse_baSAR <- function(
19171922

19181923
if(!is(source_doserate, "list")){
19191924
source_doserate <- list(source_doserate)
1920-
19211925
}
19221926
}
19231927

@@ -1979,7 +1983,6 @@ analyse_baSAR <- function(
19791983

19801984
}else{
19811985
cat("\t\t\t\tmean\tsd\tHPD\n")
1982-
19831986
}
19841987

19851988

@@ -2002,7 +2005,6 @@ analyse_baSAR <- function(
20022005
cat("* mean of the central dose is the geometric mean\n")
20032006
}
20042007
cat("** 68 % level | *** 95 % level\n")
2005-
20062008
}
20072009

20082010

@@ -2036,7 +2038,6 @@ analyse_baSAR <- function(
20362038

20372039
}else{
20382040
try(plot(results[[2]]))
2039-
20402041
}
20412042

20422043

@@ -2137,7 +2138,6 @@ analyse_baSAR <- function(
21372138
pos = 3,
21382139
col = col[2],
21392140
cex = 0.9 * par()$cex)
2140-
21412141
}
21422142
##update counter
21432143
i <- i + 15
@@ -2194,7 +2194,6 @@ analyse_baSAR <- function(
21942194

21952195
}else{
21962196
legend_pos <- "topleft"
2197-
21982197
}
21992198

22002199
##set plot area
@@ -2227,7 +2226,6 @@ analyse_baSAR <- function(
22272226
add = TRUE,
22282227
col = rgb(0, 0, 0, .1)
22292228
)
2230-
22312229
}
22322230
}else{
22332231
message("[analyse_baSAR()] Error: Wrong 'variable.names' ",
@@ -2291,7 +2289,6 @@ analyse_baSAR <- function(
22912289
bg = "grey",
22922290
legend = "measured dose points"
22932291
)
2294-
22952292
}
22962293
##remove object, it might be rather big
22972294
rm(plot_matrix)
@@ -2367,7 +2364,6 @@ analyse_baSAR <- function(
23672364

23682365
}else{
23692366
legend_pos <- "topleft"
2370-
23712367
}
23722368

23732369
legend(
@@ -2377,13 +2373,9 @@ analyse_baSAR <- function(
23772373
col = c("black", col[3], col[2]),
23782374
bty = "n",
23792375
cex = par()$cex * 0.8
2380-
23812376
)
2382-
23832377
}
2384-
23852378
}
2386-
23872379
}
23882380

23892381
# Return --------------------------------------------------------------------------------------
@@ -2398,5 +2390,4 @@ analyse_baSAR <- function(
23982390
),
23992391
info = list(call = sys.call())
24002392
))
2401-
24022393
}

tests/testthat/test_analyse_baSAR.R

+17
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,20 @@ test_that("Full check of analyse_baSAR function", {
324324
expect_message(expect_null(analyse_baSAR(object = results2)),
325325
"Error: Number of aliquots < 3, NULL returned")
326326
})
327+
328+
test_that("regression tests", {
329+
skip_on_cran()
330+
331+
## issue 407
332+
SW({
333+
expect_warning(expect_s4_class(
334+
analyse_baSAR(CWOSL.sub, verbose = FALSE, plot = FALSE,
335+
source_doserate = c(0.04, 0.001),
336+
signal.integral = c(1:2),
337+
background.integral = c(80:100),
338+
method_control = list(n.chains = 1, thin = 60),
339+
n.MCMC = 60),
340+
"RLum.Results"),
341+
"'thin = 60' is too high for 'n.MCMC = 60', reset to 30")
342+
})
343+
})

0 commit comments

Comments
 (0)