andrewcparnell/Bchron

Bchronlogy crashes R when duplicates are involved

Opened this issue · 1 comments

GPawi commented

Hi Andrew,

hope you're doing well. I know you've been improving Bchron over the past few months (thanks for that), but I still found some strange behavior:
When dates are at the same depths, I use the artificialThickness argument. Then I get the following message: Some positionThicknesses are zero for identical positions. artificialThickness has been added so that the model can attempt to run. If the model still fails then increase the value of artificialThickness further. Great - but what happens is that the progress bar stops at 20 - 30 percent and then nothing.

Would it be possible to add an error message, if Bchronolgy fails? I cannot try to catch an error, because stopping in the progress bar does not produce an error. I always have to shut down R, because I cannot interrupt the kernel. If I had an error message, I could use try(), catch the error and then iteratively increase the artificialThickness.

Tthese are the values that I tested for artificalThickness:

Value Sucess
.001 Failed
.01 Failed
.1 Failed
.5 Failed
1 Failed
2 Succeeded

As always for reproducibility

This is my dataset:
PG1984.txt

This my code:

library('Bchron')
clength <- 120
core_selection <- csv.read("PG1984.txt")
Bchronology(ages = core_selection$ages,
                    ageSds = core_selection$ageSds,
                    calCurves = core_selection$calCurves,
                    positions = core_selection$position,
                    positionThickness = core_selection$thickness,
                    ids = core_selection$id,
                    artificialThickness = 0.5,
                    iterations = 15000, 
                    burn = 5000,       
                    thin = 1,        
                    allowOutside = TRUE,
                    predictPositions = seq(0,clength, by = 1))

And my sessionInfo():

Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    
system code page: 65001

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] Bchron_4.7.6

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8           tidyr_1.1.4          prettyunits_1.1.1    ps_1.6.0            
 [5] assertthat_0.2.1     utf8_1.2.2           ggforce_0.3.3        R6_2.5.1            
 [9] plyr_1.8.6           ggridges_0.5.3       R.devices_2.17.0     backports_1.4.1     
[13] stats4_4.1.2         ggplot2_3.3.5        pillar_1.6.4         rlang_0.4.12        
[17] car_3.0-12           callr_3.7.0          hamstr_0.6.1         R.utils_2.11.0      
[21] R.oo_1.24.0          checkmate_2.0.0      readr_2.1.1          stringr_1.4.0       
[25] loo_2.4.1            polyclip_1.10-0      munsell_0.5.0        broom_0.7.11        
[29] compiler_4.1.2       rstan_2.21.3         base64enc_0.1-3      pkgconfig_2.0.3     
[33] pkgbuild_1.3.1       tidyselect_1.1.1     tibble_3.1.6         gridExtra_2.3       
[37] codetools_0.2-18     matrixStats_0.61.0   fansi_0.5.0          crayon_1.4.2        
[41] dplyr_1.0.7          tzdb_0.2.0           ggpubr_0.4.0         MASS_7.3-54         
[45] R.methodsS3_1.8.1    grid_4.1.2           gtable_0.3.0         lifecycle_1.0.1     
[49] DBI_1.1.1            magrittr_2.0.1       StanHeaders_2.21.0-7 scales_1.1.1        
[53] RcppParallel_5.1.5   cli_3.1.0            stringi_1.7.6        carData_3.0-5       
[57] farver_2.1.0         ggsignif_0.6.3       ellipsis_0.3.2       generics_0.1.1      
[61] vctrs_0.3.8          tools_4.1.2          glue_1.5.0           tweenr_1.0.2        
[65] purrr_0.3.4          hms_1.1.1            processx_3.5.2       abind_1.4-5         
[69] parallel_4.1.2       inline_0.3.19        colorspace_2.0-2     rstatix_0.7.0

Thanks,
Gregor

I think you can also change positionEps. My iterations also got stuck and I believe it's because of the while loop in the code:

  getCurrPositions <- function(pos, posThick, posEps) {
    n <- length(pos)
    badPositions <- TRUE
    while (badPositions) {
      currPositions <- stats::runif(
        n,
        pos - 0.5 * posThick,
        pos + 0.5 * posThick
      )
      do <- order(currPositions)
      diffPosition <- diff(currPositions[do])
      if (all(diffPosition > posEps)) badPositions <- FALSE # can get stuck here
    }
    return(currPositions)
  }

It should have a break mechanism here otherwise it can become an infinite loop.