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 statsmodels
url = "https://roualdes.sfo3.digitaloceanspaces.com/data/carnivora.csv"
df = pl.read_csv(url, null_values = ["NA"]).select("Family", "SB", "SW", "BW", "LS").drop_nulls()

Linear models have the following form, for \(y \sim \text{Normal}(m, s)\)

\[m = \beta_0 + \sum_{k=1}^K \beta_k x_k\]

Also checkout the webpage on correlation.

One Categorical Predictor

This model is sometimes called ANOVA.

y, X = pt.dmatrices("BW ~ C(Family)", data=df.to_pandas())
mod = sm.OLS(y, X)
fit = mod.fit()
fit.summary()
OLS Regression Results
Dep. Variable: BW R-squared: 0.439
Model: OLS Adj. R-squared: 0.364
Method: Least Squares F-statistic: 5.817
Date: Tue, 10 Nov 2026 Prob (F-statistic): 5.18e-05
Time: 14:20:53 Log-Likelihood: -419.00
No. Observations: 60 AIC: 854.0
Df Residuals: 52 BIC: 870.8
Df Model: 7
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept 104.8000 280.335 0.374 0.710 -457.733 667.333
C(Family)[T.Canidae] 101.6818 292.800 0.347 0.730 -485.865 689.229
C(Family)[T.Felidae] 313.2625 288.963 1.084 0.283 -266.584 893.109
C(Family)[T.Hyaenidae] 991.7000 343.339 2.888 0.006 302.740 1680.660
C(Family)[T.Mustelidae] -47.3393 289.529 -0.164 0.871 -628.321 533.643
C(Family)[T.Procyonidae] 6.3000 313.424 0.020 0.984 -622.631 635.231
C(Family)[T.Ursidae] 537.4000 323.703 1.660 0.103 -112.157 1186.957
C(Family)[T.Viverridae] -4.6750 297.340 -0.016 0.988 -601.332 591.982
Omnibus: 46.833 Durbin-Watson: 1.292
Prob(Omnibus): 0.000 Jarque-Bera (JB): 197.174
Skew: 2.177 Prob(JB): 1.53e-43
Kurtosis: 10.741 Cond. No. 24.3


Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

1 Numeric Predictor

This model is sometimes called linear regressin

y, X = pt.dmatrices("BW ~ LS", data=df.to_pandas())
mod = sm.OLS(y, X)
fit = mod.fit()
fit.summary()
OLS Regression Results
Dep. Variable: BW R-squared: 0.034
Model: OLS Adj. R-squared: 0.018
Method: Least Squares F-statistic: 2.052
Date: Tue, 10 Nov 2026 Prob (F-statistic): 0.157
Time: 14:29:07 Log-Likelihood: -435.31
No. Observations: 60 AIC: 874.6
Df Residuals: 58 BIC: 878.8
Df Model: 1
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept 389.7233 104.330 3.735 0.000 180.884 598.562
LS -41.8592 29.221 -1.433 0.157 -100.352 16.633
Omnibus: 44.006 Durbin-Watson: 1.063
Prob(Omnibus): 0.000 Jarque-Bera (JB): 123.886
Skew: 2.296 Prob(JB): 1.25e-27
Kurtosis: 8.335 Cond. No. 8.82


Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
# example 2
y, X = pt.dmatrices("np.log10(SB) ~ np.log10(SW)", data=df.to_pandas())
mod = sm.OLS(y, X)
fit = mod.fit()
fit.summary()
OLS Regression Results
Dep. Variable: np.log10(SB) R-squared: 0.916
Model: OLS Adj. R-squared: 0.915
Method: Least Squares F-statistic: 635.4
Date: Tue, 10 Nov 2026 Prob (F-statistic): 6.14e-33
Time: 14:30:54 Log-Likelihood: 24.654
No. Observations: 60 AIC: -45.31
Df Residuals: 58 BIC: -41.12
Df Model: 1
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept 1.0512 0.030 34.876 0.000 0.991 1.112
np.log10(SW) 0.6494 0.026 25.207 0.000 0.598 0.701
Omnibus: 29.465 Durbin-Watson: 1.496
Prob(Omnibus): 0.000 Jarque-Bera (JB): 68.650
Skew: -1.488 Prob(JB): 1.24e-15
Kurtosis: 7.313 Cond. No. 2.50


Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
plt.scatter(10 ** X[:, 1], 10 ** y[:, 0])
yhat = 10 ** fit.predict(X)
o = np.argsort(X[:, 1])
plt.plot(10 ** X[o, 1], yhat[o], color = "black")

# example 3
y, X = pt.dmatrices("SB ~ SW + I(SW ** 2)", data=df.to_pandas())
mod = sm.OLS(y, X)
fit = mod.fit()
fit.summary()
OLS Regression Results
Dep. Variable: SB R-squared: 0.929
Model: OLS Adj. R-squared: 0.927
Method: Least Squares F-statistic: 373.9
Date: Tue, 10 Nov 2026 Prob (F-statistic): 1.70e-33
Time: 14:35:48 Log-Likelihood: -274.78
No. Observations: 60 AIC: 555.6
Df Residuals: 57 BIC: 561.9
Df Model: 2
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept 26.4195 3.845 6.871 0.000 18.720 34.119
SW 1.9691 0.135 14.582 0.000 1.699 2.239
I(SW ** 2) -0.0025 0.000 -5.673 0.000 -0.003 -0.002
Omnibus: 3.680 Durbin-Watson: 1.637
Prob(Omnibus): 0.159 Jarque-Bera (JB): 2.893
Skew: 0.521 Prob(JB): 0.235
Kurtosis: 3.264 Cond. No. 2.64e+04


Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 2.64e+04. This might indicate that there are
strong multicollinearity or other numerical problems.