rmcelreath / rethinking

Statistical Rethinking course and book package
2.1k stars 596 forks source link

Chapter 16 Lynx Hare Model Change with Stan 2.33.0? #414

Closed emstruong closed 7 months ago

emstruong commented 7 months ago

Hello, I tried running model m16.5 in Chapter 16 of the book and it seems to me that it has troubles with Stan 2.33.0 +, but I'm not sure... Any advice?

library(rethinking)
#> Loading required package: cmdstanr
#> This is cmdstanr version 0.6.1
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: /home/XXXXXX/.cmdstan/cmdstan-2.33.1
#> - CmdStan version: 2.33.1
#> Loading required package: posterior
#> This is posterior version 1.5.0
#> 
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#> 
#>     mad, sd, var
#> The following objects are masked from 'package:base':
#> 
#>     %in%, match
#> Loading required package: parallel
#> rethinking (Version 2.40)
#> 
#> Attaching package: 'rethinking'
#> The following object is masked from 'package:stats':
#> 
#>     rstudent
data("Lynx_Hare")
data("Lynx_Hare_model")
dat_list <- list(N=nrow(Lynx_Hare),
                 pelts=  Lynx_Hare[,2:3])

m16.5 <- stan(
  model_code = Lynx_Hare_model,
  data = dat_list,
  chains = 3,
  cores = 3,
  control = list(adapt_delta = 0.95)
)
#> Warning in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 36, column 17: integrate_ode_rk45
#>     is deprecated and will be removed in Stan 3.0. Use ode_rk45 instead. 
#>     The new interface is slightly different, see:
#>     https://mc-stan.org/users/documentation/case-studies/convert_odes.html
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 55, column 2: Declaration
#>     of arrays by placing brackets after a variable name was removed in Stan
#>     2.33.0. Instead use the array keyword before the type. This can be
#>     changed automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 33, column 2: Declaration
#>     of arrays by placing brackets after a variable name was removed in Stan
#>     2.33.0. Instead use the array keyword before the type. This can be
#>     changed automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 30, column 2: Declaration
#>     of arrays by placing brackets after a variable name was removed in Stan
#>     2.33.0. Instead use the array keyword before the type. This can be
#>     changed automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 29, column 2: Declaration
#>     of arrays by placing brackets after a variable name was removed in Stan
#>     2.33.0. Instead use the array keyword before the type. This can be
#>     changed automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 28, column 2: Declaration
#>     of arrays by placing brackets after a variable name was removed in Stan
#>     2.33.0. Instead use the array keyword before the type. This can be
#>     changed automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 27, column 2: Declaration
#>     of arrays by placing brackets after a variable name was removed in Stan
#>     2.33.0. Instead use the array keyword before the type. This can be
#>     changed automa
#> tically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 23, column 2: Declaration
#>     of arrays by placing brackets after a variable name was removed in Stan
#>     2.33.0. Instead use the array keyword before the type. This can be
#>     changed automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 20, column 2: Declaration
#>     of arrays by placing brackets after a variable name was removed in Stan
#>     2.33.0. Instead use the array keyword before the type. This can be
#>     changed automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 5, column 28: Declaration
#>     of arrays by placing brackets after a type was removed in Stan 2.33.0.
#>     Instead use the array keyword before the type. This can be changed
#>     automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 5, column 16: Declaration
#>     of arrays by placing brackets after a type was removed in Stan 2.33.0.
#>     Instead use the array keyword before the type. This can be changed
#>     automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 4, column 16: Declaration
#>     of arrays by placing brackets after a type was removed in Stan 2.33.0.
#>     Instead use the array keyword before the type. This can be changed
#>     automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 3, column 16: Declaration
#>     of arrays by placing brackets after a type was removed in Stan 2.33.0.
#>     Instead use the array keyword before the type. This can be changed
#>     automatically using the auto-format flag to stanc
#> Error in '/tmp/RtmpWrUbUk/model-ad9f4af038a1.stan', line 2, column 2: Declaration
#>     of arrays by placing brackets after a type was removed in Stan 2.33.0.
#>     Instead use the array keyword before the type. This can be changed
#>     automati
#> cally using the auto-format flag to stanc
#> make: *** [make/program:50: /tmp/RtmpWrUbUk/model-ad9f4af038a1.hpp] Error 65
#> Error: An error occured during compilation! See the message above for more information.

Created on 2023-11-09 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.1 (2023-06-16) #> os Ubuntu 22.04.3 LTS #> system x86_64, linux-gnu #> ui X11 #> language en_CA:en #> collate en_CA.UTF-8 #> ctype en_CA.UTF-8 #> tz America/Toronto #> date 2023-11-09 #> pandoc 3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> ! package * version date (UTC) lib source #> abind 1.4-5 2016-07-21 [1] RSPM (R 4.3.0) #> P backports 1.4.1 2021-12-13 [?] CRAN (R 4.3.0) #> P checkmate 2.3.0 2023-10-25 [?] RSPM (R 4.3.0) #> P cli 3.6.1 2023-03-23 [?] CRAN (R 4.3.0) #> cmdstanr * 0.6.1 2023-11-10 [1] local #> P coda 0.19-4 2020-09-30 [?] CRAN (R 4.3.0) #> colorspace 2.1-0 2023-01-23 [1] RSPM (R 4.3.0) #> P digest * 0.6.33 2023-07-07 [?] CRAN (R 4.3.1) #> distributional 0.3.2 2023-03-22 [1] RSPM (R 4.3.0) #> P evaluate 0.23 2023-11-01 [?] RSPM (R 4.3.0) #> P fansi 1.0.5 2023-10-08 [?] RSPM (R 4.3.0) #> farver 2.1.1 2022-07-06 [1] RSPM (R 4.3.0) #> P fastmap 1.1.1 2023-02-24 [?] CRAN (R 4.3.0) #> P fs 1.6.3 2023-07-20 [?] CRAN (R 4.3.1) #> generics 0.1.3 2022-07-05 [1] RSPM (R 4.3.0) #> ggplot2 3.4.4 2023-10-12 [1] RSPM (R 4.3.0) #> P glue 1.6.2 2022-02-24 [?] CRAN (R 4.3.0) #> gtable 0.3.4 2023-08-21 [1] RSPM (R 4.3.0) #> P htmltools 0.5.7 2023-11-03 [?] RSPM (R 4.3.0) #> P knitr 1.45 2023-10-30 [?] RSPM (R 4.3.0) #> P lattice 0.21-8 2023-04-05 [3] CRAN (R 4.3.1) #> P lifecycle 1.0.4 2023-11-07 [?] RSPM (R 4.3.0) #> P loo 2.6.0 2023-03-31 [?] CRAN (R 4.3.0) #> P magrittr 2.0.3 2022-03-30 [?] CRAN (R 4.3.0) #> P MASS 7.3-60 2023-05-04 [3] CRAN (R 4.3.1) #> P matrixStats 1.1.0 2023-11-07 [?] RSPM (R 4.3.0) #> munsell 0.5.0 2018-06-12 [1] RSPM (R 4.3.0) #> P mvtnorm 1.2-3 2023-08-25 [?] CRAN (R 4.3.1) #> P pillar 1.9.0 2023-03-22 [?] CRAN (R 4.3.0) #> P pkgconfig 2.0.3 2019-09-22 [?] CRAN (R 4.3.0) #> posterior * 1.5.0 2023-10-31 [1] RSPM (R 4.3.0) #> P processx 3.8.2 2023-06-30 [?] CRAN (R 4.3.1) #> P ps 1.7.5 2023-04-18 [?] CRAN (R 4.3.0) #> P purrr 1.0.2 2023-08-10 [?] CRAN (R 4.3.1) #> P R.cache 0.16.0 2022-07-21 [?] CRAN (R 4.3.0) #> P R.methodsS3 1.8.2 2022-06-13 [?] CRAN (R 4.3.0) #> P R.oo 1.25.0 2022-06-12 [?] CRAN (R 4.3.0) #> P R.utils 2.12.2 2022-11-11 [?] CRAN (R 4.3.0) #> P R6 2.5.1 2021-08-19 [?] CRAN (R 4.3.0) #> P reprex 2.0.2 2022-08-17 [?] CRAN (R 4.3.0) #> rethinking * 2.40 2023-11-10 [1] Github (rmcelreath/rethinking@f3ac8de) #> P rlang 1.1.2 2023-11-04 [?] RSPM (R 4.3.0) #> P rmarkdown 2.25 2023-09-18 [?] RSPM (R 4.3.0) #> P rstudioapi 0.15.0 2023-07-07 [?] CRAN (R 4.3.1) #> scales 1.2.1 2022-08-20 [1] RSPM (R 4.3.0) #> P sessioninfo 1.2.2 2021-12-06 [?] CRAN (R 4.3.0) #> P shape 1.4.6 2021-05-19 [?] CRAN (R 4.3.0) #> P styler 1.10.2 2023-08-29 [?] RSPM (R 4.3.0) #> tensorA 0.36.2 2020-11-19 [1] RSPM (R 4.3.0) #> P tibble 3.2.1 2023-03-20 [?] CRAN (R 4.3.0) #> P utf8 1.2.4 2023-10-22 [?] RSPM (R 4.3.0) #> P vctrs 0.6.4 2023-10-12 [?] RSPM (R 4.3.0) #> P withr 2.5.2 2023-10-30 [?] RSPM (R 4.3.0) #> P xfun 0.41 2023-11-01 [?] RSPM (R 4.3.0) #> P yaml 2.3.7 2023-01-23 [?] CRAN (R 4.3.0) #> #> [1] /home/XXXXXXX/Documents/RStudio/rethinking-2e-learn/renv/library/R-4.3/x86_64-pc-linux-gnu #> [2] /home/XXXXXXX/.cache/R/renv/sandbox/R-4.3/x86_64-pc-linux-gnu/f291ea0c #> [3] /home/XXXXXXX/.asdf/installs/r/4.3.1/lib/R/library #> #> P ── Loaded and on-disk path mismatch. #> #> ────────────────────────────────────────────────────────────────────────────── ```
rmcelreath commented 7 months ago

Yes until I update the model code, you need to edit it so it uses the new array syntax. More details: https://mc-stan.org/cmdstanr/articles/deprecations.html

emstruong commented 7 months ago

Do you take pull requests? I'd like to try my hand at fixing it... Would you like the fix to check what version of Stan people are using?

rmcelreath commented 7 months ago

I do sometimes yes. I actually have a draft fix in my local branch. I will see if I can find time to review it and push up later today.

rmcelreath commented 7 months ago

Okay I just committed and push it up to Experimental. It runs for me, but still with one warning that I'd like to fix. https://github.com/rmcelreath/rethinking/commit/0c15281e9452e335fc73eba368f39cafde3ef131

emstruong commented 7 months ago

Okay I just committed and push it up to Experimental. It runs for me, but still with one warning that I'd like to fix. 0c15281

Thanks!

For anyone else that's interested, this particular commit can be obtained using

remotes::install_github("rmcelreath/rethinking@0c15281e9452e335fc73eba368f39cafde3ef131")

Also, I'm not sure if this was intentional, but when I re-ran the code, it seemed to require rstan this time around even though ulam_options$use_cmdstan <- TRUE.

rmcelreath commented 7 months ago

Re rstan, I couldn't repeat that issue. I don't have rstan installed, and it seemed to work. Any more details that might help me figure out the issue?

emstruong commented 7 months ago

Re rstan, I couldn't repeat that issue. I don't have rstan installed, and it seemed to work. Any more details that might help me figure out the issue?

Looking at it more carefully, I guess it's not a serious issue -- see the very bottom of this reprex regarding the error.

library(reprex)
library(rethinking)
#> Loading required package: cmdstanr
#> This is cmdstanr version 0.6.1
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: /home/mtruong/.cmdstan/cmdstan-2.33.1
#> - CmdStan version: 2.33.1
#> Loading required package: posterior
#> This is posterior version 1.5.0
#> 
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#> 
#>     mad, sd, var
#> The following objects are masked from 'package:base':
#> 
#>     %in%, match
#> Loading required package: parallel
#> rethinking (Version 2.41)
#> 
#> Attaching package: 'rethinking'
#> The following object is masked from 'package:stats':
#> 
#>     rstudent
# library(rstan)
data("Lynx_Hare")
data("Lynx_Hare_model")
dat_list <- list(N=nrow(Lynx_Hare),
                 pelts=  Lynx_Hare[,2:3])

# ulam_options$use_cmdstan <- TRUE

paste("My ulam_options is", ulam_options$use_cmdstan)
#> [1] "My ulam_options is TRUE"

m16.5 <- rethinking::stan(
  model_code = Lynx_Hare_model,
  data = dat_list,
  chains = 3,
  cores = 3,
  control = list(adapt_delta = 0.95)
)
#> Warning in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 22: integrate_ode_rk45
#>     is deprecated and will be removed in Stan 3.0. Use ode_rk45 instead. 
#>     The new interface is slightly different, see:
#>     https://mc-stan.org/users/documentation/case-studies/convert_odes.html
#> Running MCMC with 3 parallel chains...
#> 
#> Chain 1 Iteration:   1 / 1000 [  0%]  (Warmup)
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: lognormal_lpdf: Scale parameter is inf, but must be positive finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: lognormal_lpdf: Scale parameter is inf, but must be positive finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: lognormal_lpdf: Location parameter is -nan, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: integrate_ode_rk45:  Failed to integrate to next output time (2) in less than max_num_steps steps (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: lognormal_lpdf: Location parameter is -nan, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: lognormal_lpdf: Location parameter is -nan, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: integrate_ode_rk45: initial state[1] is inf, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: lognormal_lpdf: Scale parameter is inf, but must be positive finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 2 Iteration:   1 / 1000 [  0%]  (Warmup)
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: lognormal_lpdf: Scale parameter is inf, but must be positive finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: lognormal_lpdf: Scale parameter is inf, but must be positive finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: integrate_ode_rk45:  Failed to integrate to next output time (2) in less than max_num_steps steps (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: integrate_ode_rk45:  Failed to integrate to next output time (2) in less than max_num_steps steps (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: integrate_ode_rk45:  Failed to integrate to next output time (5) in less than max_num_steps steps (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: integrate_ode_rk45: initial state[1] is inf, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: integrate_ode_rk45:  Failed to integrate to next output time (2) in less than max_num_steps steps (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 3 Rejecting initial value:
#> Chain 3   Error evaluating the log probability at the initial value.
#> Chain 3 Exception: lognormal_lpdf: Location parameter is -nan, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 3 Exception: lognormal_lpdf: Location parameter is -nan, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 3 Iteration:   1 / 1000 [  0%]  (Warmup)
#> Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 3 Exception: lognormal_lpdf: Location parameter is -nan, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 3
#> Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 3 Exception: lognormal_lpdf: Location parameter is -nan, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 3
#> Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 3 Exception: integrate_ode_rk45:  Failed to integrate to next output time (2) in less than max_num_steps steps (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 3
#> Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 3 Exception: integrate_ode_rk45:  Failed to integrate to next output time (2) in less than max_num_steps steps (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 3
#> Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 3 Exception: lognormal_lpdf: Location parameter is -nan, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 55, column 6 to column 63)
#> Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 3
#> Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 3 Exception: integrate_ode_rk45: initial state[2] is inf, but must be finite! (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 3
#> Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 3 Exception: integrate_ode_rk45:  Failed to integrate to next output time (2) in less than max_num_steps steps (in '/tmp/RtmpxyKElk/model-6a844fa15eb9.stan', line 39, column 2 to line 42, column 65)
#> Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 3
#> Chain 1 Iteration: 100 / 1000 [ 10%]  (Warmup) 
#> Chain 2 Iteration: 100 / 1000 [ 10%]  (Warmup) 
#> Chain 3 Iteration: 100 / 1000 [ 10%]  (Warmup) 
#> Chain 1 Iteration: 200 / 1000 [ 20%]  (Warmup) 
#> Chain 3 Iteration: 200 / 1000 [ 20%]  (Warmup) 
#> Chain 2 Iteration: 200 / 1000 [ 20%]  (Warmup) 
#> Chain 1 Iteration: 300 / 1000 [ 30%]  (Warmup) 
#> Chain 3 Iteration: 300 / 1000 [ 30%]  (Warmup) 
#> Chain 1 Iteration: 400 / 1000 [ 40%]  (Warmup) 
#> Chain 2 Iteration: 300 / 1000 [ 30%]  (Warmup) 
#> Chain 3 Iteration: 400 / 1000 [ 40%]  (Warmup) 
#> Chain 1 Iteration: 500 / 1000 [ 50%]  (Warmup) 
#> Chain 1 Iteration: 501 / 1000 [ 50%]  (Sampling) 
#> Chain 2 Iteration: 400 / 1000 [ 40%]  (Warmup) 
#> Chain 3 Iteration: 500 / 1000 [ 50%]  (Warmup) 
#> Chain 3 Iteration: 501 / 1000 [ 50%]  (Sampling) 
#> Chain 1 Iteration: 600 / 1000 [ 60%]  (Sampling) 
#> Chain 3 Iteration: 600 / 1000 [ 60%]  (Sampling) 
#> Chain 2 Iteration: 500 / 1000 [ 50%]  (Warmup) 
#> Chain 2 Iteration: 501 / 1000 [ 50%]  (Sampling) 
#> Chain 3 Iteration: 700 / 1000 [ 70%]  (Sampling) 
#> Chain 1 Iteration: 700 / 1000 [ 70%]  (Sampling) 
#> Chain 2 Iteration: 600 / 1000 [ 60%]  (Sampling) 
#> Chain 3 Iteration: 800 / 1000 [ 80%]  (Sampling) 
#> Chain 1 Iteration: 800 / 1000 [ 80%]  (Sampling) 
#> Chain 3 Iteration: 900 / 1000 [ 90%]  (Sampling) 
#> Chain 2 Iteration: 700 / 1000 [ 70%]  (Sampling) 
#> Chain 3 Iteration: 1000 / 1000 [100%]  (Sampling) 
#> Chain 3 finished in 4.6 seconds.
#> Chain 1 Iteration: 900 / 1000 [ 90%]  (Sampling) 
#> Chain 2 Iteration: 800 / 1000 [ 80%]  (Sampling) 
#> Chain 1 Iteration: 1000 / 1000 [100%]  (Sampling) 
#> Chain 1 finished in 5.5 seconds.
#> Chain 2 Iteration: 900 / 1000 [ 90%]  (Sampling) 
#> Chain 2 Iteration: 1000 / 1000 [100%]  (Sampling) 
#> Chain 2 finished in 6.3 seconds.
#> 
#> All 3 chains finished successfully.
#> Mean chain execution time: 5.5 seconds.
#> Total execution time: 6.5 seconds.
#> Error in loadNamespace(x): there is no package called 'rstan'

Created on 2023-11-27 with reprex v2.0.2

emstruong commented 7 months ago

Also, here's an updated string for the Boxes_model

Boxed_model <-"data{
    int N;
    array[N] int y;
    array[N] int majority_first;
}
parameters{
    simplex[5] p;
}
model{
    vector[5] phi;

    // prior
    p ~ dirichlet( rep_vector(4,5) );

    // probability of data
    for ( i in 1:N ) {
        if ( y[i]==2 ) phi[1]=1; else phi[1]=0; // majority
        if ( y[i]==3 ) phi[2]=1; else phi[2]=0; // minority
        if ( y[i]==1 ) phi[3]=1; else phi[3]=0; // maverick
        phi[4]=1.0/3.0;                         // random
        if ( majority_first[i]==1 )             // follow first
            if ( y[i]==2 ) phi[5]=1; else phi[5]=0;
        else
            if ( y[i]==3 ) phi[5]=1; else phi[5]=0;

        // compute log( p_s * Pr(y_i|s )
        for ( j in 1:5 ) phi[j] = log(p[j]) + log(phi[j]);
        // compute average log-probability of y_i
        target += log_sum_exp( phi );
    }
}"

I'm curious -- should we prefer RStan or CmdStanR? The Stan website and the rethinking package seem to imply that we should be using CmdStanR, yet when I make a new Stan file through R-Studio, it seems to require the RStan package...

rmcelreath commented 7 months ago

Thanks for the model code. I guess that one is deprecated too now.

CmdStanR is the better choice. RStan is many patches behind in the binaries, because the CRAN policies make it hard to update. CmdStanR in contrast doesn't even need to updated when the core cmdstan library is updated. That's why I rewrote rethinking so that RStan is not required.

emstruong commented 7 months ago

Thanks for the model code. I guess that one is deprecated too now.

CmdStanR is the better choice. RStan is many patches behind in the binaries, because the CRAN policies make it hard to update. CmdStanR in contrast doesn't even need to updated when the core cmdstan library is updated. That's why I rewrote rethinking so that RStan is not required.

I see... Well I think that about solves my issue, so I'll go ahead and close it. Thank you for the help