ols¶
Ordinary least squares fitting with formulaic design matrices and HC3 robust standard errors.
Statsmodels-free drop-in replacement for statsmodels.OLS.
Accepts any narwhals-compatible DataFrame (pandas, polars, …).
- ols(formula, data)[source]¶
Fit an OLS model using a formulaic formula string.
- Parameters:
formula – Formula string, e.g.
"y ~ x1 + x2".data – DataFrame containing all variables. Any narwhals-compatible frame (pandas, polars, …) is accepted.
- Return type:
OLSResult- Returns:
OLSResult – Drop-in replacement for statsmodels OLS results.
- Parameters:
formula (str)
data (Any)
Examples
>>> import interlace >>> result = interlace.ols("y ~ x1 + x2", df) >>> result.params
OLSResult attributes¶
Attribute |
Type |
Description |
|---|---|---|
|
|
Named coefficient estimates; supports |
|
|
Residuals |
|
|
Fitted values |
|
|
|
|
|
Design matrix |
|
|
Response vector |
|
|
Column names from the formula |
|
|
Original formula string |
|
native frame |
Original data as passed by the caller |
|
|
Residual degrees of freedom: |
|
|
Mean squared error of residuals: |
OLSResult methods¶
hc3_bse()¶
HC3 heteroskedasticity-consistent standard errors, matching statsmodels to 6 significant figures.
h_ii = diag(X (XᵀX)⁻¹ Xᵀ)
d_i = eᵢ² / (1 − hᵢᵢ)²
cov_HC3 = (XᵀX)⁻¹ (Xᵀ diag(d) X) (XᵀX)⁻¹
se_HC3 = sqrt(diag(cov_HC3))
Returns np.ndarray of shape (p,).
predict(data)¶
Re-evaluates the RHS formula on new data via the stored formulaic ModelSpec.
Accepts any narwhals-compatible frame.
Returns np.ndarray of shape (n_new,).
Examples¶
Basic fit¶
import interlace
result = interlace.ols("score ~ age + education", data=df)
print(result.params)
# Intercept 50.0
# age 1.5
# education 3.2
# dtype: float64
HC3 robust standard errors¶
se = result.hc3_bse()
import pandas as pd
print(pd.Series(se, index=result.params.index))
Prediction on new data¶
import pandas as pd
new_df = pd.DataFrame({"age": [30, 40], "education": [16, 18]})
preds = result.predict(new_df)
Polars input¶
import polars as pl
df_pl = pl.from_pandas(df)
result = interlace.ols("score ~ age + education", data=df_pl)
See also¶
quantreg — quantile regression fitting
Influence diagnostics — influence diagnostics (uses
ols()internally)