Error with Truncation on Negative Binomial Distribution (log alternative parameterization)

Hello,

I am trying to adjust some code from a previous post: Code for Poisson Hurdle Model in Documentation is Slow

In this post, they are using a poisson distribution for the hurdle model but I was trying to simply adjust it for a negative binomial, and then eventually turn it into a regression model. When I add truncation to the neg_binomial_2_log() in the code below, I get the very vague error:

Error in stanc(file = file, model_code = model_code, model_name = model_name, :
c++ exception (unknown reason).

However, if I remove the truncation, the code runs fine.

data {
    int<lower=0> N;
    int<lower=0> y[N];
  }
  parameters {
    real<lower=0, upper=1> theta;

    real alpha;
    real<lower=0> phi;

  }
  model {
    for (n in 1:N) {
      (y[n] == 0) ~ bernoulli(theta);
      if (y[n] > 0)

        y[n] ~ neg_binomial_2_log(alpha, phi) T[1,];
    }
  }
  generated quantities{
    real<lower = 0> lambda;
      lambda = exp(alpha);
 }

The R code I am using for an example data is:

sim_data <- list(
  N = 10000,
  y = c(RawDat0, RawDatNB)
)

fit2 <- stan(
  file = "log_nb_hurdle.stan",
  data = sim_data
)

Any thoughts on how to make this run correctly?

Thanks,
Mike

Truncation doesn’t work for PMFs. You have to subtract the log PMF at 0 from target.

Based on the parameters in the altered code above, the 0 contribution from the negative binomial should be

Pr(y = 0) = [φ / (λ + φ)]^φ

where λ = exp(α).

Then would I just subtract the log of 1 - Pr(y = 0) from the target? Something along the lines of the following inside the if statement:
target += -log1m((phi/(exp(alpha)+phi))^phi);

Yeah

Awesome. Thanks for the help!