# Truncate neg\_binomial\_2\_log?

**URL:** <https://discourse.mc-stan.org/t/truncate-neg-binomial-2-log/1227>\
**Category:** Modeling\
**Created:** [July 13, 2017, 1:17pm UTC](https://discourse.mc-stan.org/t/truncate-neg-binomial-2-log/1227 "2017-07-13T13:17:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Christian](https://discourse.mc-stan.org/u/Christian)\
**Post date:** [July 13, 2017, 1:17pm UTC](https://discourse.mc-stan.org/t/truncate-neg-binomial-2-log/1227/1 "2017-07-13T13:17:50Z")

</div>

Hi,

I’m trying to estimate a count model with over-dispersion for a variable, that only contains positive integers. Therefore, I chose for a NegBin II model, i.e. the neg\_binomial\_2\_log distribution of Stan. My model is as follows:

> data {  
> int\<lower=1\> N; // no. observations  
> int\<lower=1\> P; //no. predictors  
> int\<lower=1\> y[N]; //min. 1  
> matrix[N, P] x;  
> }
> 
> parameters {  
> real intercept;  
> vector[P] beta; // one beta for each predictor  
> real\<lower=0\> phi; // over-dispersion parameter  
> }
> 
> model {  
> vector[N] mu;  
> mu = intercept + x\*beta;  
> for (n in 1:N) {  
> y[n] ~ neg\_binomial\_2\_log(mu[n], phi) T[1,]; // zero-truncated NegBin II?  
> }  
> }

Due to the value range of the DV, I need to use truncation, however, the code does not compile, as there seems to be no CDF for neg\_binomial\_2\_log. Is there an alternative to be still able to estimate this model?

Best regards  
Christian

---

<div class="post-metadata">

**Author:** ![Christian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Christian](https://discourse.mc-stan.org/u/Christian)\
**Post date:** [July 13, 2017, 1:37pm UTC](https://discourse.mc-stan.org/t/truncate-neg-binomial-2-log/1227/2 "2017-07-13T13:37:45Z")

</div>

Along the lines of [add poisson\_log\_lcdf and poisson\_log\_lccdf functions · Issue #2015 · stan-dev/stan · GitHub](https://github.com/stan-dev/stan/issues/2015) and [Error with Truncation on Negative Binomial Distribution (log alternative parameterization)](http://discourse.mc-stan.org/t/error-with-truncation-on-negative-binomial-distribution-log-alternative-parameterization/1051), would changing the code in the for-loop to:

> y[n] ~ neg\_binomial\_2\_log(mu[n], phi); // no truncation here  
> target += - log1m(neg\_binomial\_2\_log\_lpmf(0 | mu[n], phi)); // manually adjusting computation of likelihood

be a fruitful avenue?

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [July 13, 2017, 3:43pm UTC](https://discourse.mc-stan.org/t/truncate-neg-binomial-2-log/1227/3 "2017-07-13T15:43:53Z")

</div>

Yeah, that should be it.

What you’re doing is making sure your new pmf sums to one (and so is actually a pmf).

If your original pmf sums to 1 and you want to exclude some values of the random variable, your new pmf is the old one scaled by a factor of 1 / (1 - probability\_of\_all\_the\_stuff\_you\_removed) so that the probabilities will sum to one (which is what the line you found does).
