Skip to content

statsmodels/statsmodels-formula-power-term

Rule statsmodels-formula-power-term
Pack statsmodels
Severity warning
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

In a statsmodels OLS formula, Patsy and Formulaic use ** to expand interactions. For a single predictor, x**2 gives the same term as x. In both engines, y ~ x + x**2 produced two columns and y ~ x + I(x**2) produced three.

smf.ols("y ~ x + x**2", data)
smf.ols("y ~ x + I(x**2)", data)

The rule checks additive formula literals in ols and OLS.from_formula, including raw strings and formulas with simple parenthesized terms. It reports a standalone predictor name raised to a literal integer of at least two. The edit preserves existing transforms such as I(z**2) and interaction expansions such as (a + b)**2.

Wrap a numeric power in I(...). Use xpd check --fix --unsafe-fixes to apply the edit. It changes the design matrix.

Patsy formula operators · Formulaic grammar

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

In a statsmodels OLS formula, x**2 expands interactions and does not square x. Write I(x**2) for a quadratic term.

Browse the other rules of the statsmodels pack.