import polars as pl
import scipy as sp
import numpy as np
import patsy as pt # uv add patsy pyarrow
import matplotlib.pyplot as plt
import statsmodels.api as sm # uv add statsmodelsUnique Intercepts (shared slope)
# possum -> finches
url = "https://roualdes.sfo3.digitaloceanspaces.com/data/finches.csv"
df = pl.read_csv(url)y, X = pt.dmatrices("winglength ~ C(island) + taillength", data = df.to_pandas())
mod = sm.OLS(y, X)
fit = mod.fit()
fit.summary()| Dep. Variable: | winglength | R-squared: | 0.588 |
| Model: | OLS | Adj. R-squared: | 0.569 |
| Method: | Least Squares | F-statistic: | 30.44 |
| Date: | Tue, 17 Nov 2026 | Prob (F-statistic): | 2.39e-12 |
| Time: | 12:34:46 | Log-Likelihood: | -160.70 |
| No. Observations: | 68 | AIC: | 329.4 |
| Df Residuals: | 64 | BIC: | 338.3 |
| Df Model: | 3 | ||
| Covariance Type: | nonrobust |
| coef | std err | t | P>|t| | [0.025 | 0.975] | |
| Intercept | 19.0489 | 5.531 | 3.444 | 0.001 | 8.000 | 30.098 |
| C(island)[T.sancristobal] | 0.2158 | 0.730 | 0.295 | 0.769 | -1.243 | 1.675 |
| C(island)[T.santacruz] | 0.8963 | 0.865 | 1.037 | 0.304 | -0.831 | 2.624 |
| taillength | 1.1866 | 0.125 | 9.484 | 0.000 | 0.937 | 1.437 |
| Omnibus: | 2.880 | Durbin-Watson: | 1.731 |
| Prob(Omnibus): | 0.237 | Jarque-Bera (JB): | 2.385 |
| Skew: | -0.457 | Prob(JB): | 0.303 |
| Kurtosis: | 3.076 | Cond. No. | 760. |
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
slope interpretation: For every 1 mm increase in tail length, we expected the finches of the Galapagos’ wing length grows by 1.187 mm.
intercept interpretation: A finch from the Galapagos island Floreana with a tail length of 0 mm, is expected to have a wing length of 19.05 mm
intercept interpretation: A finch from the Galapagos island Santa Cruz with a tail length of 0 mm, is expected to have a wing length of 19.946 (=19.05 + 0.896) mm
Unique Intercepts and Unique Slopes
y, X = pt.dmatrices("winglength ~ C(island) * taillength", data = df.to_pandas())
mod = sm.OLS(y, X)
fit = mod.fit()
fit.summary()| Dep. Variable: | winglength | R-squared: | 0.602 |
| Model: | OLS | Adj. R-squared: | 0.570 |
| Method: | Least Squares | F-statistic: | 18.74 |
| Date: | Tue, 17 Nov 2026 | Prob (F-statistic): | 2.67e-11 |
| Time: | 12:34:46 | Log-Likelihood: | -159.53 |
| No. Observations: | 68 | AIC: | 331.1 |
| Df Residuals: | 62 | BIC: | 344.4 |
| Df Model: | 5 | ||
| Covariance Type: | nonrobust |
| coef | std err | t | P>|t| | [0.025 | 0.975] | |
| Intercept | 20.2341 | 8.555 | 2.365 | 0.021 | 3.132 | 37.336 |
| C(island)[T.sancristobal] | 5.4694 | 12.298 | 0.445 | 0.658 | -19.113 | 30.052 |
| C(island)[T.santacruz] | -16.0580 | 15.124 | -1.062 | 0.292 | -46.290 | 14.174 |
| taillength | 1.1597 | 0.194 | 5.976 | 0.000 | 0.772 | 1.548 |
| C(island)[T.sancristobal]:taillength | -0.1179 | 0.277 | -0.425 | 0.672 | -0.673 | 0.437 |
| C(island)[T.santacruz]:taillength | 0.3917 | 0.347 | 1.128 | 0.264 | -0.303 | 1.086 |
| Omnibus: | 4.060 | Durbin-Watson: | 1.738 |
| Prob(Omnibus): | 0.131 | Jarque-Bera (JB): | 3.324 |
| Skew: | -0.525 | Prob(JB): | 0.190 |
| Kurtosis: | 3.263 | Cond. No. | 2.72e+03 |
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 2.72e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
intercept interpretation: For a finch from Floreana with a tail length of 0mm, we expect wing length to be 20.23 mm.
intercept interpretation: For a finch from San Cristobal with a tail length of 0mm, we expect wing length to be 25.7 (=20.23 + 5.47) mm.
slope interpretation: For a finch from the island San Cristobal, for every 1mm increase in tail length, we expect a wing length to increase by 1.04 (1.16 + -.12)mm.
prediction: For a finch from the island San Cristobal with a tail length of 40mm, we expect a wing length of 67.38mm
xnew = np.array([1, 1, 0, 40, 40, 0])
fit.predict(xnew)array([67.37618299])
Plot for Unique Intercepts and Unique Slopes Model
for idx, (name, gdf) in enumerate(df.sort("island").group_by("island")):
plt.scatter(gdf["taillength"], gdf["winglength"], label=f"{name[0]}")
Xnew = np.c_[np.tile(isls[idx], (101, 1)), x[:, np.newaxis] * isls[idx]]
yhat = fit.predict(Xnew)
plt.plot(x, yhat)
plt.legend()