0% found this document useful (0 votes)
4 views10 pages

Negative Binomial Regression in Python

The document provides Python code for generating a synthetic dataset and fitting a Negative Binomial regression model to analyze accident counts based on various predictors. It explains Generalized Linear Models (GLMs), the Negative Binomial distribution, and concepts such as covariance types and degrees of freedom in regression analysis. Additionally, it discusses the scale parameter in GLMs and its relevance to model standard errors.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Negative Binomial Regression in Python

The document provides Python code for generating a synthetic dataset and fitting a Negative Binomial regression model to analyze accident counts based on various predictors. It explains Generalized Linear Models (GLMs), the Negative Binomial distribution, and concepts such as covariance types and degrees of freedom in regression analysis. Additionally, it discusses the scale parameter in GLMs and its relevance to model standard errors.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Code

import pandas as pd
import numpy as np
import [Link] as sm
import [Link] as smf

# Set seed for reproducibility


[Link](42)

# Generate synthetic dataset


n = 351
data = [Link]({
'MEDIAN': [Link](0, 2, size=n),
'FOOT_PATH': [Link](0, 2, size=n),
'PEDESTRIAN_CROSSING': [Link](0, 2, size=n) # Fixed here
})

# True coefficients used here (for simulation purposes)


beta_0 = 0.75
beta_1 = 0.01
beta_2 = -0.06
beta_3 = -0.15

# Linear predictor
eta = (beta_0
+ beta_1 * data['MEDIAN']
+ beta_2 * data['FOOT_PATH']
+ beta_3 * data['PEDESTRIAN_CROSSING'])

# Mean accident count (μ) using log link: μ = exp(η)


mu = [Link](eta)

# Generate response variable using Negative Binomial distribution


r = 2
p = r / (r + mu)
data['accident_count'] = [Link].negative_binomial(r, p)

# Fit Negative Binomial regression model


model = [Link](formula='accident_count ~ MEDIAN + FOOT_PATH +
PEDESTRIAN_CROSSING',
data=data,
family=[Link](link=[Link]
())).fit()

# Print the summary


print([Link]())
What is Generalized Linear Regression?
Generalized Linear Models (GLMs)

A Generalized Linear Model is an extension of the simple linear regression model that
allows:

• Response variables to follow non-normal distributions (like binomial, Poisson, etc.)


• The relationship between predictors and the response to be non-linear (using a link
function)

What is the Negative Binomial Distribution?


Definition:

The Negative Binomial distribution is used to model count data — like the number of
accidents — especially when the data has overdispersion.

When to Use It?


Use the Negative Binomial model instead of Poisson when:

• ✅ The response variable is a count (0, 1, 2, 3,...)


• The variance is greater than the mean (called overdispersion)
• Poisson regression doesn’t fit well because it assumes mean = variance

Example:

Let’s say you’re modelling the number of road accidents at intersections.

• Intersection A: 3 accidents
• Intersection B: 0 accidents
• Intersection C: 6 accidents
→ The variance (spread) is much larger than the average.

What is Covariance Type = Nonrobust?


Covariance matrix:

In regression analysis, the covariance matrix of the parameter estimates tells you about:

• The variability of your estimated coefficients (β\betaβ)


• It's used to compute:
o Standard errors
o t-values
o p-values
o confidence intervals

Nonrobust vs. Robust:

Type Meaning

Assumes model assumptions are all met perfectly (e.g., homoscedasticity, no


Nonrobust
autocorrelation). It uses the standard (naive) formula for standard errors.

Adjusts the standard errors to be more accurate if the data violates assumptions (like
Robust
unequal variance, outliers, etc.) — e.g., White’s robust SE, HC0-HC3.
What is DF Residual?
DF Residual = Number of Observations – Number of Parameters Estimated

It tells you how many independent pieces of information are left after estimating model
parameters. It's used to assess the model’s goodness-of-fit and calculate test statistics.

DF Residual=351−4=347

What is "Scale" in a GLM model?


The Scale parameter is used to adjust the standard errors of the model's coefficients. It
reflects how much the residuals deviate from the model's expectations.
In Negative Binomial / Poisson GLM:
For many GLMs (like Poisson or Negative Binomial), the scale is often set to 1.0 by
default because:

• These models assume the variance is a function of the mean


• There's no need for an extra scale parameter.

You might also like