rules/linearmodels/formula-without-constant.md
linearmodels adds no intercept to a formula, where statsmodels and R add one, so the estimates change. Write 1 + at the start of the right side.
| 1 | -model = PooledOLS.from_formula("lwage ~ exper", data) |
| 2 | -iv = IV2SLS.from_formula("lwage ~ exper + [educ ~ fatheduc + motheduc]", data) |
| 3 | +model = PooledOLS.from_formula("lwage ~ 1 + exper", data) |
| 4 | +iv = IV2SLS.from_formula("lwage ~ 1 + exper + [educ ~ fatheduc + motheduc]", data) |
What the rule reports
The rule reports a formula with no 1 term in from_formula of PooledOLS, RandomEffects,
BetweenOLS, FamaMacBeth, PanelOLS and the system models. It also reports such a formula in
from_formula of IV2SLS, IVLIML, IVGMM, IVGMMCUE, IVJIVE and OLS when the formula has
an instrument block in brackets. linearmodels adds no intercept to such formulas, where statsmodels
and R add one. "y ~ x" is therefore a regression through the origin in linearmodels and a
regression with an intercept in statsmodels.
The rule checks literal formulas. It leaves out computed formulas, FirstDifferenceOLS, which refuses a constant, and factor models, which use their own formula syntax.
How to fix it
xpd check --fix --unsafe-fixes inserts 1 + right after the first ~ in literal formula strings. It is unsafe because a regression through the origin (- 1) may have been intended.
On 7.1 and on 7.0, the models gave the following coefficient of exper on the wage panel, and of
educ for IV2SLS on the mroz data:
| Model | Without 1 | With 1 |
|---|---|---|
PooledOLS | 0.2184 | 0.0338, with an intercept of 1.4289 |
RandomEffects | 0.1119 | 0.0589 |
BetweenOLS | 0.2364 | -0.0229 |
IV2SLS with an instrument block | 0.0769 | 0.0664 |
The instrument models for one equation add the intercept themselves when the formula has no block
in brackets. On 7.1, 7.0 and 5.0, IV2SLS, IVLIML, IVGMM and IVGMMCUE returned Intercept
and exper for "lwage ~ exper", so the rule leaves the form alone on the four classes.
The rule stays silent when:
- the formula says
- 1or0 +, which states that no intercept is meant - the formula names a column that looks like a constant, such as
const - the formula has a
C()term, whose dummies span the intercept - a
PanelOLSformula has effects, or the call hasother_effects, which absorb the intercept
A model without an intercept can be meant, so the rule is a warning.