Statistics & probability

Negative binomial regression calculator with count exposure

Estimate a count regression whose variance can exceed its mean, while accounting for unequal observation time or opportunity.

Your numbers

Rows align with the response; intercept is explicit.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Estimate a count regression whose variance can exceed its mean, while accounting for unequal observation time or opportunity. The negative binomial NB2 model uses a log mean, an optional fixed exposure offset and one shared dispersion parameter. It returns fitted coefficients, dispersion, query means and likelihood diagnostics. Counts and exposure rates remain separate throughout the calculation.

THE FORMULAμ_i=e_i exp(x_i β), r=1/α; log L=Σ[logΓ(y_i+r)−logΓ(r)−logΓ(y_i+1)+r log(r/(r+μ_i))+y_i log(μ_i/(r+μ_i))]; Var(Y_i|x_i)=μ_i+αμ_i².

A worked example

The same ten counts 0,0,0,1,1,2,3,5,8,12 occur at x=−1,0,1, each with unit exposure. The fitted mean is 3.2 in all three groups, the intercept is log(3.2)=1.16315081 and the slope is zero. Estimated NB2 dispersion is about 1.428063. For query exposures 1,2,4 at those predictor rows, expected counts become 3.2,6.4,12.8 because exposure multiplies the mean.

Good to know

20–200 independent nonnegative integer counts ≤1,000,000; 1–4 full-rank explicit design columns including any intercept, so at most 3 predictors with an intercept. Design magnitudes ≤1,000; exposure in (0,1,000,000]. At most 50 query rows. Local NB2 MLE with coefficient bounds ±20 and log(alpha) in (−9,6); unresolved, boundary or nonpositive-curvature fits rejected. No coefficient intervals, random effects, weights or zero-inflation component.

Counts need an opportunity denominator

A maintenance team may observe failures over different machine-hours. Two failures in ten hours and two in one thousand hours are equal counts with very different rates. Enter the exposure separately so that the model represents that distinction.

The offset is log exposure with a coefficient fixed at one. Doubling exposure doubles the conditional expected count at unchanged predictors; it does not imply that the observed integer outcome will double. A zero exposure with a positive count is inconsistent with this model.

Negative binomial regression calculator with count exposure worked exampleExposure changes the expected countExposureExpected count at x = 00123403.26.49.612.8Mean count = 3.2 × exposure
Scroll across to see the full diagram.
The same ten counts 0,0,0,1,1,2,3,5,8,12 occur at x=−1,0,1, each with unit exposure. The fitted mean is 3.2 in all three groups, the intercept is log(3.2)=1.16315081 and the slope is zero. Estimated NB2 dispersion is about 1.428063. For query exposures 1,2,4 at those predictor rows, expected counts become 3.2,6.4,12.8 because exposure multiplies the mean.

Compare this with the Negative binomial failures before successes calculator. Distribution probabilities do not estimate a regression or exposure-adjusted covariate effects.

The NB2 dispersion rule

The model allows conditional variance μ+αμ². The extra quadratic term grows faster than the mean, so a single shared dispersion parameter can describe counts that vary much more than an ordinary Poisson model permits.

This is conditional dispersion after accounting for the entered predictors. Variation caused by missing covariates or dependence between rows is not automatically repaired by a larger α. Repeated measurements from the same machine still require an appropriate dependence model.

Preparing the regression matrix

Enter one design row for every count and keep the response, exposure and predictor ordering aligned. Include a column of ones if an intercept is wanted. Do not put exposure into the design merely to reproduce the fixed offset.

A full-rank matrix is necessary for identifiable coefficients. Rescale predictors measured in very large units and avoid columns that are exact combinations of others. If both hours and days describe the same observation duration, using both creates a redundant design.

A balanced example with unequal query exposure

The prefilled counts repeat across three predictor values. Each group totals 32 across ten rows, so symmetry gives a common fitted mean of 3.2 and zero slope. The likelihood then estimates dispersion from the spread around that mean.

At unit query exposure the count mean is 3.2. Query exposure two gives 6.4 and exposure four gives 12.8. Dividing each mean by its exposure recovers the same fitted rate, making this example a direct check that the offset has been applied once.

Reading an exponentiated coefficient

Under this log link, exp(β_j) multiplies the expected rate for a one-unit change in predictor j while other predictors stay fixed. It is a rate ratio, with units determined by the predictor scale.

A coefficient for age measured in years differs numerically from one for age measured in decades. Predictions can remain identical after correct rescaling. Conditional associations from an observational table do not establish an intervention effect.

Finite fit diagnostics and the Poisson boundary

The numerical routine searches several starts and reports the best resolved interior likelihood with positive local curvature. A successful fit establishes that this bounded procedure found a stable local candidate, rather than a proof of a unique global maximum.

Nearly Poisson data may push α toward zero, while poorly explained extreme counts can pull estimates toward search limits. The calculator rejects these boundary results. Reducing the optimizer tolerance cannot establish information that the sample does not contain.

Checking whether the fitted mean is useful

Compare observed counts with fitted means across exposure and predictor ranges. A systematic pattern in the residuals can reveal a missing trend even when the overall dispersion appears plausible. Observed zeros should be inspected alongside the upper tail.

This page does not assign a separate structural-zero class. If a genuine two-process explanation is plausible, a mixture model addresses a different question and introduces additional identifiability demands. Select that model from the data-generating story and validation evidence, not solely the number of zeros.

A few questions, answered

Can I enter a rate as the response?

No. Enter the original integer count and its exposure. A rate discards information about how much observation time produced it.

Does the calculator add an intercept?

No. Include a column of ones in both fitted and query design rows if an intercept is wanted.

Why can a Poisson-like dataset be rejected?

The estimated dispersion may reach the lower numerical boundary; this page reports an interior negative binomial fit rather than claiming a resolved boundary estimate.

Are prediction intervals included?

No. Query outputs are conditional means. Parameter uncertainty and predictive variation need additional calculations.

Find your calculator

Start typing to explore the library.