Your data
Paste data or load the example.
Result
Outcome equation (standard errors below)
| Term | OLS, selected rows | Two-step | Maximum likelihood |
|---|
Selection and correlation
| Quantity | Two-step | Maximum likelihood | ML interval |
|---|
Tests of ρ = 0 (no selection bias)
| Test | Statistic | Distribution | p |
|---|
Selection equation
| Term | Probit | p | Maximum likelihood |
|---|
Fitted selection probabilities
selected rows not selected. Little overlap means a strong selection equation; probabilities piled at 0 or 1 mean near-perfect prediction.
Method and citation
The model: a unit is selected when zγ + u > 0, and its outcome y = xβ + e is seen only then; (u, e) are bivariate normal with var(u) = 1, var(e) = σ² and correlation ρ. For a selected row E[y | x, selected] = xβ + ρσλ(zγ), with λ(c) = φ(c)/Φ(c) the inverse Mills ratio, so OLS on the selected rows omits a regressor whenever ρ ≠ 0. The selection equation holds the outcome regressors as well as the exclusion restrictions you list.
Two-step (Heckman 1979): a probit of selection on z gives γ; the regression of y on x and λ(zγ) in the selected rows gives β and βλ = ρσ; σ² = e′e/n1 + βλ² mean(δ), δ = λ(λ + zγ). The standard errors are Greene's (1981) correction for the induced heteroskedasticity and the estimated γ: σ²(X*′X*)−1[X*′(I − ρ²D)X* + Q](X*′X*)−1, Q = ρ²(X*′DZ)Vγ(Z′DX*). A two-step ρ outside [−1, 1] is set to ±1, as is usual.
Maximum likelihood (Heckman 1974): log L sums log Φ(−zγ) over unselected rows and log Φ((zγ + ρe/σ)/√(1 − ρ²)) − log σ + log φ(e/σ) over selected rows, in atanh ρ and log σ. It is maximised by damped Newton steps with an analytic gradient and a finite-difference Hessian, from the two-step estimates and again from ρ = 0, keeping the higher peak (and saying so if they differ). Conventional errors invert the Hessian; robust errors are the sandwich (White 1982); clustered errors sum each row's score within clusters (Liang & Zeger 1986) with the factor G/(G − 1). The intervals for ρ and σ are transformed from atanh ρ and log σ, so they stay in range.
Checks (Puhani 2000): whether any variable enters selection without entering the outcome, and the R² of λ on the outcome regressors in the selected rows; the page warns above 0.8 and 0.95. The CSV download carries a spreadsheet formula-injection guard on text cells.
Verified by tests in the source against statsmodels 0.15 and scipy 1.18 (run only as external programs to record reference numbers) on two specifications of the example: probit coefficients and standard errors, OLS with conventional, HC1 and clustered errors, the two-step coefficients and corrected standard errors (the correction recomputed separately in numpy) within 1e-12, and the maximum-likelihood estimates (within 1e-7) and standard errors of all three kinds (within 1e-5) against an independent scipy maximisation with statsmodels' numerical Hessian and scores. A Monte Carlo study of the intervals is in the source (eval/coverage-output.txt).
Limits
- The exclusion restriction cannot be tested. That a variable changes who is selected and has no direct effect on the outcome is an argument you make. Without one, identification rests on the normal distribution alone: in this tool's simulation with no exclusion restriction, the two-step interval was ten times wider and the maximum-likelihood interval covered the true coefficient only 79% of the time, against about 95% with one.
- Normality matters. Both estimators assume jointly normal errors; if the selection error is not normal the correction can be wrong even with a good exclusion restriction. Semiparametric selection estimators are not offered.
- Coefficients are for the latent outcome. β is the effect on y for everyone (for example the wage offer), not on the expected outcome among selected rows, which also moves through selection.
- One selection rule, binary. No ordered or multiple selection, no panel or fixed-effects versions, no weights; the two-step standard errors are the conventional ones only.
- Few clusters. Clustered errors are unreliable with fewer than about 30 to 50 clusters; the page warns below 30.
BibTeX
@article{heckman1979sample,
author = {Heckman, James J.}, title = {Sample Selection Bias as a Specification Error},
journal = {Econometrica}, volume = {47}, number = {1}, pages = {153--161}, year = {1979}, doi = {10.2307/1912352}
}
@article{greene1981sample,
author = {Greene, William H.}, title = {Sample Selection Bias as a Specification Error: A Comment},
journal = {Econometrica}, volume = {49}, number = {3}, pages = {795--798}, year = {1981}, doi = {10.2307/1911523}
}
@article{heckman1974shadow,
author = {Heckman, James J.}, title = {Shadow Prices, Market Wages, and Labor Supply},
journal = {Econometrica}, volume = {42}, number = {4}, pages = {679--694}, year = {1974}, doi = {10.2307/1913937}
}
@article{white1982maximum,
author = {White, Halbert}, title = {Maximum Likelihood Estimation of Misspecified Models},
journal = {Econometrica}, volume = {50}, number = {1}, pages = {1--25}, year = {1982}, doi = {10.2307/1912526}
}
@article{liang1986longitudinal,
author = {Liang, Kung-Yee and Zeger, Scott L.}, title = {Longitudinal Data Analysis Using Generalized Linear Models},
journal = {Biometrika}, volume = {73}, number = {1}, pages = {13--22}, year = {1986}, doi = {10.1093/biomet/73.1.13}
}
@article{puhani2000heckman,
author = {Puhani, Patrick A.}, title = {The Heckman Correction for Sample Selection and Its Critique},
journal = {Journal of Economic Surveys}, volume = {14}, number = {1}, pages = {53--68}, year = {2000}, doi = {10.1111/1467-6419.00104}
}
Questions
What is sample selection bias?
The outcome you want to explain is recorded only for some units, and whether a unit is recorded depends on things that also move the outcome. Wages exist only for people who work; if the people who choose to work are those with unusually good offers, a regression on workers describes them, not everyone. The bias does not shrink with more data.
What does the Heckman correction do?
It models selection with a probit and assumes the unobserved parts of selection and of the outcome are jointly normal with correlation rho. Selected rows then have an expected error of rho times sigma times the inverse Mills ratio, so adding that ratio to the regression (the two-step estimator) or fitting both equations together (maximum likelihood) removes the bias.
Do I need an exclusion restriction?
In practice, yes: a variable that changes who is selected but has no direct effect on the outcome, such as young children for whether a woman works. Without one the model is identified only by the shape of the normal distribution, the inverse Mills ratio is nearly a linear function of the regressors, and estimates swing with small changes. The page warns when there is none and reports how collinear the ratio is.
Should I report the two-step or the maximum likelihood estimates?
When the normal model holds, maximum likelihood is more efficient and its tests of rho are standard. The two-step estimator is simpler and somewhat less sensitive to how the likelihood is shaped, but its standard errors need the Heckman-Greene correction, which this page applies. Report both if they differ: a large gap is itself a sign that the model is fragile.
How do I test whether there is selection bias at all?
Test rho = 0. The page gives the likelihood-ratio test (the selection model against a probit and a regression fitted separately), the Wald test on atanh(rho), and the t-test on the inverse Mills ratio in the two-step regression. Not rejecting does not prove there is no selection, especially with a weak exclusion restriction.
Does it match Stata, R or Python?
On two specifications of the example data, the probit, OLS (conventional, HC1 and clustered) and the two-step coefficients and corrected standard errors agree with statsmodels and numpy within 1e-12. The maximum-likelihood estimates agree with an independent scipy maximisation within 1e-7 and all three kinds of standard error within 1e-5, the size of the error in the reference's numerical second derivatives. It has not been run against Stata heckman or R sampleSelection.
Is my data uploaded anywhere?
No. The file is read and analysed by JavaScript in this browser tab. There are no accounts, cookies or analytics vendors.