Rstan works but cmdstan does not for a beta binomial model

OK. It seems a bug happened somewhere/sometime with beta-binomial. I think @stevebronder knows more.

Do you have the most recent version of StanHeaders (2.19.2)? A bug was introduced in the beta_binomial_lpmf function in September 2019 and was fixed in December 2019. So the current cmdstanr is fine, and also with StanHeaders 2.19.2 I get the expected results:

Inference for Stan model: b2e7f450efb7d805e3dfc1526f53245a.
2 chains, each with iter=1000; warmup=500; thin=1;
post-warmup draws per chain=500, total post-warmup draws=1000.

           mean se_mean   sd      2.5%       25%       50%       75%     97.5%
a          0.30    0.00 0.00      0.29      0.29      0.30      0.30      0.31
theta      3.50    0.00 0.08      3.33      3.44      3.50      3.56      3.66
lp__  -80579.81    0.05 1.01 -80582.46 -80580.29 -80579.49 -80579.04 -80578.79
      n_eff Rhat
a       601    1
theta   681    1
lp__    423    1

Hmm, I have a more recent version?

> packageVersion("StanHeaders")
[1] ‘2.21.0.1’

Just to be clear, I have the opposite problem than that of the OP in that cmdstan works but rstan does not.

I got this laptop about a month ago, so this is a pretty fresh install of everything. The problem I’m experiencing with running the OP’s reproducible example seems exactly what Daniel Simpson reported above, i.e. 90% of iterations hitting maxtreedepth warning, running much slower, etc.

So you may have picked up an unreleased version of StanHeaders that contains the bug. Unless you have any reason not to, I’d suggest to remove that and use the CRAN version which works.

@mcol thanks for the suggestion. I did remove.packages(), install.packages() and it didn’t change anything.

help(package=“StanHeaders”) shows the following:

Version: 2.21.0-1
License: BSD_3_clause + file LICENSE
NeedsCompilation: yes
Packaged: 2020-01-11 06:57:21 UTC; ben
Repository: CRAN
Date/Publication: 2020-01-19 18:00:02 UTC
Built: R 3.6.2; x86_64-w64-mingw32; 2020-01-28 10:30:35 UTC; windows
Archs: i386, x64

Ah, you are on Windows. I had a look at the package and indeed it contains the buggy version. You could try replacing file StanHeaders/include/stan/math/prim/scal/prob/beta_binomial_lpmf.hpp from your installed StanHeaders package with the attached. I’ve hand-patched it, so I hope I haven’t messed up anything.
beta_binomial_lpmf.hpp (6.7 KB)

1 Like

@mcol Yes, I don’t have much of a choice where I work =) Thank you for looking into this! I can live without this functionality for a little while anyway. I know I can run cmdstan for now, if I really need it. Can you give me a rough estimate of when a fixed version will be available for CRAN? I am demo’ing Stan to my teammates and I am concerned about portability of my examples when they fall in love with this great software.

Only @bgoodri can answer about newer rstan releases. As far as I understand, there are some holdups that he’s been fighting against for months, due to changes to Stan code and CRAN policies.

@bgoodri doesn’t even know. We seemed to make some progress on the segfault at the meeting this morning. Downloading an isolated .hpp file to the right place on your disk usually works.

1 Like

Yikes can we flag this bug? I spent about a week debugging a model after updating Stanheaders to finally realize the beta binomial function wasn’t working… :O

1 Like

I’m still having this problem with the beta binomial function. Code which previously worked across multiple datasets and installations of rstan now does not. I can get it to run smoothly if I use the binomial instead or binomial with beta prior on the probability parameter.

What is the best way to fix this when using Windows and rstan?

I’m using the following:
rstan 2.21.2
StanHeaders 2.21.0-7
R version 4.0.4
Windows 10 build 19041

You can try the experimental preview of RStan 2.26, which should have the patched file.

To install, in a clean R session (with RStan not loaded), run:

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

Thank you @andrjohns - that worked perfectly for me!

1 Like