Getting syntax error while using `neg_binomial_2_lupmf`


I am writing this code in a R version 4.0 with rstan version 2.21.x

model_code <- "
// a basic GLM example
data {
  int<lower=1> N;    // rows of data
  vector[N] x;       // predictor
  int<lower=0> y[N]; // response
}
parameters {
  real<lower=0> phi; // neg. binomial dispersion parameter
  real b0;  // intercept
  real b1;  // slope
}
model {
  // priors:
  phi ~ cauchy(0, 3);
  b0 ~ normal(0, 5);
  b1 ~ normal(0, 5);
  // data model:
  target += neg_binomial_2_lupmf(y | b0 + b1 * x, phi);
}
"
mod <- stan_model(model_code = model_code)

The above code throws the following error,

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in 'model46857a400aac_44eb016e60d737798188749d5f0feb88' at line 21, column 32
  -------------------------------------------------
    19:   b1 ~ normal(0, 5);

    20:   // data model:

    21:   target += neg_binomial_2_lupmf(y | b0 + b1 * x, phi);
                                       ^
    22: }
  -------------------------------------------------
PARSER EXPECTED: "("

I am really confused about the syntax error. Not sure how to proceed. Any help would be appreciated greatly.

1 Like

In Stan 2.21 you don’t have the _lupmf signature (see 13.2 Negative Binomial Distribution (alternative parameterization) | Stan Functions Reference), so I assume this is the reason for the message

1 Like

Just to expand a bit on what @aakhmetz wrote: the CRAN version of rstan is unfortunately quite old. There are stupid technical reasons that currently prevent us from updating the CRAN version, but we are trying to sort this out. If you need access to recent features, you can either use cmdstanr or install latest rstan via https://mc-stan.org/r-packages/

In your specific situation, you can get exactly equivalent model that works with 2.21 by writing

y ~ neg_binomial_2(b0 + b1 * x, phi);

Best of luck with your model!

1 Like

Thank you so much for the replies, I would definitely switch to cmdstan.

Thanks, I am a bit confused about the error message though. I mean a function is not present, that should not raise PARSER EXPECTED: "(", right?

I think the parser is getting confused when there’s a vertical bar in something that’s not recognized as a probability density function. It gives a more reasonable error if you write

  target += neg_binomial_2_lupmf(y , b0 + b1 * x, phi);
2 Likes