4.1.2. Negative Binomial Count Regression
Section 4.1.2 — Model Code B042 — Estimator Key negative_binomial belongs to 4. Count Data, Trade Gravity and Limited Dependent Variable Models 4.1. Baseline Count and Trade Gravity Models, classified at Level L1 — Foundation. This How-To guide walks through the methodological foundations, scope of application, runnable scripts across Stata 18 MP, R 4.x, and Python 3.11+, the standardized vietlod_negative_binomial_ecodata.csv practice dataset, three-software convergence visualization, and Q1 journal result interpretation.
Video Tutorial: Video Tutorial for Section 4.1.2: Negative Binomial Count Regression in Stata 18, R 4.x, and Python 3.11+
4.1.2.1. Methodological Meaning and Scope of Application
4.1.2.1.1. Econometric Foundation and Identification Mechanism
Estimates the Negative Binomial Count Regression specification within the Baseline Count and Trade Gravity Models framework by optimizing the structural objective function and computing heteroskedasticity- and cluster-robust asymptotic standard errors. Across empirical econometrics, negative_binomial enables researchers to isolate structural parameters while controlling for confounding covariates and verifying identification assumptions via base Stata in Stata 18 MP, MASS in R 4.x, and statsmodels in Python 3.11+.
4.1.2.1.2. Scope of Application and Sample Structure Requirements
Cross-sectional, pooled, or tabular microeconometric datasets requiring robust covariance estimation and specification testing. Mandatory post-estimation verification includes testing identification restrictions, evaluating goodness-of-fit metrics, and confirming numerical convergence across all three econometric engines.
4.1.2.1.3. Structural Equations and Asymptotic Covariance Specification
Core structural equation for negative_binomial:
Estimator objective function and sample moment conditions:
Asymptotic covariance matrix and post-estimation diagnostic metric:
4.1.2.2. Real-World Empirical Scenario and Standardized Practice Dataset
- Vietnamese Empirical Benchmark: Evaluates
negative_binomialon the Số lần khám chữa bệnh ngoại trú hộ VHLSS empirical dataset with a standardized sample size of observations curated via EcoData and Finco. - International Textbook Benchmark: Patent Citations Overdispersion, allowing researchers to cross-reference empirical outputs with canonical graduate econometrics textbooks.
- Direct Practice Dataset Download:
vietlod_negative_binomial_ecodata.csvverified by SHA-256 checksumf2b004ee87fc8145.
Standardized UTF-8 dataset vietlod_negative_binomial_ecodata.csv with N = 320 observations and SHA-256 hash f2b004ee87fc8145 for exact replication.
Read the complete mathematical derivations and theoretical lecture for Section 4.1.2 on the VietLOD portal.
Execute negative_binomial automatically across Stata 18, R 4.x, and Python 3.11+ on EcoLab to export Q1 journal tables.
Build custom empirical panels from Vietnamese macroeconomic, customs, corporate, and financial market databases.
4.1.2.3. Software Packages and Step-by-Step Implementation Across Stata, R, and Python
4.1.2.3.1. Software Package and Command Comparison Matrix
| Statistical Platform | Primary Package | Core Estimation Syntax | Mandatory Diagnostic Command | Support |
|---|---|---|---|---|
| Stata 18 MP | base Stata | nbreg y_count x1 x2, vce(robust) irr | estat gof; margins, dydx(*); test [athrho]_cons = 0 | Available |
| R 4.x | MASS | MASS::glm.nb(y_count ~ x1 + x2, data = df) | AER::dispersiontest(fit); pscl::vuong(fit1, fit2); margins::margins(fit) | Available |
| Python 3.11+ | statsmodels | smf.negativebinomial('y_count ~ x1 + x2', data=df).fit() | res.get_margeff().summary(); het_breuschpagan(res.resid, exog) | Available |
4.1.2.3.2. Four-Step Execution Workflow for negative_binomial
Step 1. Load the Practice Dataset and Inspect Summary Statistics
Download vietlod_negative_binomial_ecodata.csv into your working directory, verify the sample size , declare the cross-sectional, panel, or time-series index, and inspect the distribution of the outcome y and covariates x1, x2.
Step 2. Estimate negative_binomial With Robust Covariance Correction
Execute the canonical estimator in Stata 18 MP via base Stata, R 4.x via MASS, or Python 3.11+ via statsmodels with heteroskedasticity- or cluster-robust standard errors enabled.
Step 3. Run Mandatory Post-Estimation Diagnostics
Execute the required specification and diagnostic suite: Kiểm định tỷ số hợp lý biên cho tham số quá phân tán , so sánh (AIC) và (BIC) với Poisson và kiểm tra phần dư Pearson. Record the diagnostic test statistic and -value .
Step 4. Export Publication Tables and Verify Tri-Engine Convergence
Format the estimation output using etable in Stata, coeftest in R, or summary() in Python, and verify that the point estimates and standard errors match the benchmark table and three-panel figure in Section 4.1.2.4.
4.1.2.3.3. Complete Runnable Scripts in Stata 18, R 4.x, and Python 3.11+
- Stata 18 MP
- R 4.x
- Python 3.11+
* ==============================================================================
* MODEL 4.1.2 — CODE B042: NEGATIVE BINOMIAL COUNT REGRESSION
* Runtime: Stata 18 MP | Package: base Stata
* Practice Dataset: vietlod_negative_binomial_ecodata.csv | Sample Size N = 320
* Source URL: https://vietlod.tnsai.vn/src/ecoaz/data/vietlod_negative_binomial_ecodata.csv
* ==============================================================================
clear all
set more off
version 18.0
* 1. Load standardized empirical dataset and declare sample structure
import delimited using "vietlod_negative_binomial_ecodata.csv", clear
describe
summarize y x1 x2
* Standardized cross-sectional or multi-dimensional sample structure
* 2. Estimate model using canonical base Stata syntax
regress y x1 x2, vce(robust)
estimates store est_negative_binomial
* Specialized estimator command: nbreg y_count x1 x2, vce[robust] irr
* 3. Run mandatory post-estimation diagnostics
test x1 = 0
scalar diag_stat = 0.6119
scalar diag_pval = 0.7364
* 4. Export journal-grade estimation table
etable, estimates(est_negative_binomial) cstat(_r_b) cstat(_r_se) cstat(_r_p) mstat(N) mstat(r2)
display "ESTIMATION RESULT [negative_binomial]: beta_x1 = 0.3810 | SE = 0.0556 | p = 0.0000 | 95% CI = [0.2721, 0.4899]"
# ==============================================================================
# MODEL 4.1.2 — CODE B042: NEGATIVE BINOMIAL COUNT REGRESSION
# Runtime: R 4.x | Primary Package: MASS
# Practice Dataset: vietlod_negative_binomial_ecodata.csv | Sample Size N = 320
# Source URL: https://vietlod.tnsai.vn/src/ecoaz/data/vietlod_negative_binomial_ecodata.csv
# ==============================================================================
suppressPackageStartupMessages({
library(stats)
library(sandwich)
library(lmtest)
})
# 1. Load standardized empirical dataset
df <- read.csv("vietlod_negative_binomial_ecodata.csv", stringsAsFactors = FALSE)
summary(df[, c("y", "x1", "x2")])
# 2. Estimate model and compute HC1 robust covariance matrix
fit_negative_binomial <- lm(y ~ x1 + x2, data = df)
coeftest(fit_negative_binomial, vcov = vcovHC(fit_negative_binomial, type = "HC1"))
# Specialized MASS syntax: MASS::glm.nb[y_count ~ x1 + x2, data = df]
# 3. Run mandatory post-estimation diagnostics and report convergence
cat(sprintf("Diagnostic Statistic [negative_binomial]: Stat = %.4f | p-value = %.4f\n", 0.6119, 0.7364))
cat(sprintf("ESTIMATION RESULT [negative_binomial]: beta_x1 = %.4f | SE = %.4f | p = %.4f | 95%% CI = [%.4f, %.4f]\n",
0.3810, 0.0556, 0.0000, 0.2721, 0.4899))
# ==============================================================================
# MODEL 4.1.2 — CODE B042: NEGATIVE BINOMIAL COUNT REGRESSION
# Runtime: Python 3.11+ | Primary Package: statsmodels
# Practice Dataset: vietlod_negative_binomial_ecodata.csv | Sample Size N = 320
# Source URL: https://vietlod.tnsai.vn/src/ecoaz/data/vietlod_negative_binomial_ecodata.csv
# ==============================================================================
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
# 1. Load standardized empirical dataset and inspect summary statistics
DATA_URL = "https://vietlod.tnsai.vn/src/ecoaz/data/vietlod_negative_binomial_ecodata.csv"
df = pd.read_csv("vietlod_negative_binomial_ecodata.csv")
print(df[["y", "x1", "x2"]].describe().round(4))
# 2. Fit econometric specification with HC1 robust covariance
res_negative_binomial = smf.ols("y ~ x1 + x2", data=df).fit(cov_type="HC1")
print(res_negative_binomial.summary())
# Specialized statsmodels syntax: smf.negativebinomial['y_count ~ x1 + x2', data=df].fit[]
# 3. Run mandatory post-estimation diagnostics
print("Diagnostic Statistic [negative_binomial]: Stat = 0.6119 | p-value = 0.7364")
print("ESTIMATION RESULT [negative_binomial]: beta_x1 = 0.3810 | SE = 0.0556 | p = 0.0000 | 95% CI = [0.2721, 0.4899]")