Linear Regression

import pandas as pd
import numpy as np
import scipy.stats as spicy

import plotnine as pn
import statsmodels.api as sm
df = pd.read_csv("https://raw.githubusercontent.com/roualdes/data/refs/heads/master/penguins.csv")
fit = sm.OLS.from_formula("body_mass_g ~ sex + flipper_length_mm", data = df).fit()
fit.summary()
OLS Regression Results
Dep. Variable: body_mass_g R-squared: 0.806
Model: OLS Adj. R-squared: 0.805
Method: Least Squares F-statistic: 684.8
Date: Tue, 07 Apr 2026 Prob (F-statistic): 3.53e-118
Time: 13:35:03 Log-Likelihood: -2427.2
No. Observations: 333 AIC: 4860.
Df Residuals: 330 BIC: 4872.
Df Model: 2
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -5410.3002 285.798 -18.931 0.000 -5972.515 -4848.085
sex[T.male] 347.8503 40.342 8.623 0.000 268.491 427.209
flipper_length_mm 46.9822 1.441 32.598 0.000 44.147 49.817
Omnibus: 0.262 Durbin-Watson: 1.710
Prob(Omnibus): 0.877 Jarque-Bera (JB): 0.376
Skew: 0.051 Prob(JB): 0.829
Kurtosis: 2.870 Cond. No. 2.95e+03


Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 2.95e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
dfn = df.dropna(subset = ["sex", "body_mass_g"])

pn.ggplot(dfn) + \
    pn.geom_point(pn.aes("flipper_length_mm", "body_mass_g"))  + \
    pn.stat_smooth(pn.aes("flipper_length_mm", "body_mass_g"), method = "lm", se = False)

fit = sm.OLS.from_formula("body_mass_g ~ sex * flipper_length_mm", data = df).fit()
fit.summary()
OLS Regression Results
Dep. Variable: body_mass_g R-squared: 0.806
Model: OLS Adj. R-squared: 0.804
Method: Least Squares F-statistic: 455.2
Date: Tue, 07 Apr 2026 Prob (F-statistic): 1.04e-116
Time: 13:49:29 Log-Likelihood: -2427.2
No. Observations: 333 AIC: 4862.
Df Residuals: 329 BIC: 4878.
Df Model: 3
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -5443.9607 440.283 -12.365 0.000 -6310.086 -4577.836
sex[T.male] 406.8015 587.303 0.693 0.489 -748.541 1562.144
flipper_length_mm 47.1527 2.226 21.179 0.000 42.773 51.532
sex[T.male]:flipper_length_mm -0.2942 2.924 -0.101 0.920 -6.047 5.458
Omnibus: 0.262 Durbin-Watson: 1.711
Prob(Omnibus): 0.877 Jarque-Bera (JB): 0.375
Skew: 0.051 Prob(JB): 0.829
Kurtosis: 2.871 Cond. No. 8.24e+03


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