jsocolar / flocker

flexible occupancy estimation in R
Other
26 stars 2 forks source link

Error compiling Stan version of rep-varying model on Windows 10 #103

Closed jdyen closed 5 months ago

jdyen commented 5 months ago

I ran into an error compiling a rep-varying model on my Windows computer. I have recreated it using the example from the flock help file (modified to test the rep-constant version of the model as well).

The rep-constant model samples fine but the rep-varying model throws an error (example below) on Windows 10 with R 4.3.3 and rstan 2.32.6 calling Stan 2.32.2. Was also getting this same error on R 4.3.0 with Stan 2.26.? before I re-installed everything. This is my main (work) computer and I use Stan, rstan, and brms regularly without issue. I'd hoped to try cmdstanr as well but cannot get the cmdstan toolchain working on this computer.

Both the rep-varying and rep-constant models work on my macbook running OSX (macOS Big Sur 11.7.9) with R 4.3.1 and rstan 2.32.6 calling Stan 2.32.2. Also works on the macbook with cmdstanr 0.6.1 calling cmdstan 2.32.2.

The error suggests installing the V8 package. This was already installed and I've updated it and tried again without success.

I appreciate the Stan compiler errors are quite cryptic. Happy to dig further, or happy to leave this and use my macbook for this particular project.

Example

library(flocker)

# rep constant model (works)
sfd <- simulate_flocker_data()
fd <- make_flocker_data(
  sfd$obs, 
  sfd$unit_covs
)
flock(
  f_occ = ~ s(uc1) + (1|species),
  f_det = ~ uc1 + (1|species),
  flocker_data = fd,
  refresh = 50, chains = 1, warmup = 5, iter = 200,
  control = list(adapt_engaged = FALSE, stepsize = .05, max_treedepth = 5),
  seed = 123
)

# rep varying model (error shown below)
sfd <- simulate_flocker_data()
fd <- make_flocker_data(
  sfd$obs, 
  sfd$unit_covs,
  sfd$event_covs
)
flock(
  f_occ = ~ s(uc1) + (1|species),
  f_det = ~ uc1 + ec1 + (1|species),
  flocker_data = fd,
  refresh = 50, chains = 1, warmup = 5, iter = 200,
  control = list(adapt_engaged = FALSE, stepsize = .05, max_treedepth = 5),
  seed = 123
)

Compiling Stan program...
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  parser failed badly; maybe try installing the V8 package
> sessionInfo()
R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.utf8  LC_CTYPE=English_Australia.utf8    LC_MONETARY=English_Australia.utf8
[4] LC_NUMERIC=C                       LC_TIME=English_Australia.utf8    

time zone: Australia/Sydney
tzcode source: internal

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

other attached packages:
[1] flocker_1.0-0

loaded via a namespace (and not attached):
  [1] tidyselect_1.2.0     dplyr_1.1.4          farver_2.1.1         loo_2.6.0            fastmap_1.1.1       
  [6] tensorA_0.36.2       shinystan_2.6.0      promises_1.2.0.1     shinyjs_2.1.0        digest_0.6.31       
 [11] estimability_1.4.1   mime_0.12            lifecycle_1.0.3      StanHeaders_2.32.6   ellipsis_0.3.2      
 [16] processx_3.8.1       magrittr_2.0.3       posterior_1.4.1      compiler_4.3.3       rlang_1.1.1         
 [21] tools_4.3.3          igraph_1.4.3         utf8_1.2.3           prettyunits_1.1.1    bridgesampling_1.1-2
 [26] htmlwidgets_1.6.2    curl_5.0.1           pkgbuild_1.4.0       plyr_1.8.8           dygraphs_1.1.1.6    
 [31] abind_1.4-5          miniUI_0.1.1.1       withr_2.5.0          grid_4.3.3           stats4_4.3.3        
 [36] fansi_1.0.4          xts_0.13.1           xtable_1.8-4         colorspace_2.1-0     inline_0.3.19       
 [41] ggplot2_3.4.4        emmeans_1.8.8        scales_1.2.1         gtools_3.9.4         MASS_7.3-60.0.1     
 [46] cli_3.6.1            mvtnorm_1.2-1        crayon_1.5.2         generics_0.1.3       RcppParallel_5.1.7  
 [51] rstudioapi_0.14      reshape2_1.4.4       rstan_2.32.6         stringr_1.5.1        splines_4.3.3       
 [56] shinythemes_1.2.0    bayesplot_1.10.0     assertthat_0.2.1     parallel_4.3.3       matrixStats_1.0.0   
 [61] base64enc_0.1-3      brms_2.20.4          vctrs_0.6.4          V8_4.4.2             boot_1.3-29         
 [66] Matrix_1.6-5         jsonlite_1.8.5       callr_3.7.3          crosstalk_1.2.0      glue_1.6.2          
 [71] codetools_0.2-19     ps_1.7.5             DT_0.28              distributional_0.3.2 stringi_1.7.12      
 [76] gtable_0.3.3         QuickJSR_1.1.3       later_1.3.1          munsell_0.5.0        tibble_3.2.1        
 [81] colourpicker_1.2.0   pillar_1.9.0         htmltools_0.5.5      Brobdingnag_1.2-9    R6_2.5.1            
 [86] shiny_1.7.4          lattice_0.22-5       markdown_1.7         backports_1.4.1      threejs_0.3.3       
 [91] httpuv_1.6.11        rstantools_2.3.1     Rcpp_1.0.10          coda_0.19-4          gridExtra_2.3       
 [96] nlme_3.1-164         checkmate_2.2.0      mgcv_1.9-0           zoo_1.8-12           pkgconfig_2.0.3   
jsocolar commented 5 months ago

Thanks for this report! I've done a bit of a dive here, and discovered that the issue is that rstan run on Windows has a problem with the character # included in a comment. Very strange; not something I expected!

So on my windows machine (which I don't typically use for Stan), the following compiles fine:

parameters {
  x;
}
model {
  target += normal_lpdf(x | 0, 1);
}

but this fails with your error:

// # test
parameters {
  x;
}
model {
  target += normal_lpdf(x | 0, 1);
}

Anyhow, it's an easy fix on my end. Hopefully I can find time by end-of-day today; if not certainly by the beginning of next week. I've also reported the issue to Stan devs. I'll be interested to learn if this was known/expected behavior!

jdyen commented 5 months ago

Thanks for getting onto this so quickly. I could submit a PR if you prefer but won't get a chance until next week.

jsocolar commented 5 months ago

Just got the pull open. I'll wait for checks before merging. Sounds like there's no rush from your end but if there is a rush for some reason (like you want to run a model over the weekend), you can grab the number-comment-fix branch.

jdyen commented 5 months ago

Fantastic. Running a test model on my MacBook right now and won't be back into this until next week. Thanks very much.