stan-dev / stan-dev/rstan

Rstan throwing inappropriate error message

Open
#1,072 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

Rstan throws the error Error in if (any(efbmi_per_chain < threshold)) { : missing value where TRUE/FALSE needed, which apparently, should not be the case (ref).

Description:

It happens when I try to fit the model with some data, but not others.
I am trying to model count C with two covariates: Treatment and Mice.

Reproducible Steps:

Code to get the data from dput

structure(list(Treatment = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 5, 
5, 5, 5, 5, 6, 6, 6, 6, 6, 4, 4, 4, 4, 4, 3, 3, 3), C_error = c(6227605.5, 
5844779.25, 1338474, 10365561.75, 6349542.75, 9831545.25, 10968084, 
15513791.25, 2564861.25, 6725652.75, 1609213.5, 716698.5, 991467.75, 
822666, 521778, 11940, 345215.25, 42237.75, 7611.75, 10746, 447.75, 
447.75, 149.25, 597, 149.25, 14477.25, 1492.5, 7910.25), C_good = c(1008.15, 
216.45, 8583.9, 5557.5, 3956.55, 2195.7, 16155.75, 9490.65, 8712.6, 
3281.85, 290.55, 651.3, 146.25, 165.75, 347.1, 144.3, 95.55, 
42.9, 50.7, 72.15, 152.1, 87.75, 95.55, 101.4, 101.4, 183.3, 
95.55, 801.45), Mice = 1:28), row.names = c(NA, -28L), class = c("tbl_df", 
"tbl", "data.frame"))

My stan model

data{
    int C[28];
    int Mice[28];
    int Treatment[28];
}
parameters{
    vector[6] bT;
    vector[28] bA;
}
model{
    vector[28] lambda;
    bA ~ normal( 0 , 5 );
    bT ~ normal( 5 , 10 );
    for ( i in 1:28 ) {
        lambda[i] = bT[Treatment[i]] + bA[Mice[i]];
        lambda[i] = exp(lambda[i]);
    }
    C ~ poisson( lambda );
}
Current Output:

The model runs when I use data = list(C = d$C_good, ....)
But when I change the count to data = list(C = d$C_error, ....), the model samples and throws the error

Running MCMC with 1 chain, with 1 thread(s) per chain...

Chain 1 Iteration:   1 / 1000 [  0%]  (Warmup) 
Chain 1 Iteration: 100 / 1000 [ 10%]  (Warmup) 
Chain 1 Iteration: 200 / 1000 [ 20%]  (Warmup) 
Chain 1 Iteration: 300 / 1000 [ 30%]  (Warmup) 
Chain 1 Iteration: 400 / 1000 [ 40%]  (Warmup) 
Chain 1 Iteration: 500 / 1000 [ 50%]  (Warmup) 
Chain 1 Iteration: 501 / 1000 [ 50%]  (Sampling) 
Chain 1 Iteration: 600 / 1000 [ 60%]  (Sampling) 
Chain 1 Iteration: 700 / 1000 [ 70%]  (Sampling) 
Chain 1 Iteration: 800 / 1000 [ 80%]  (Sampling) 
Chain 1 Iteration: 900 / 1000 [ 90%]  (Sampling) 
Chain 1 Iteration: 1000 / 1000 [100%]  (Sampling) 
Chain 1 finished in 6.2 seconds.
Warning: 444 of 500 (89.0%) transitions hit the maximum treedepth limit of 10.
See https://mc-stan.org/misc/warnings for details.

Error in if (any(efbmi_per_chain < threshold)) { : 
  missing value where TRUE/FALSE needed
RStan Version:

‘2.26.13’

R Version:

"R version 4.1.3 (2022-03-10)"

Operating System:

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the supplied R data and Stan model in RStan 2.26.13, comparing the C_good and C_error inputs. Trace the post-sampling diagnostic path that evaluates efbmi_per_chain, then determine what diagnostic or error should be reported when the sampled result is invalid. Done means the reproduction no longer produces an inappropriate missing-value error and the relevant behavior is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.