DS Regression Info
The top part of the regression table describes what was fitted and how well the model fits overall. This lesson explains each entry using our rides ~ temp_c model.
Dep. Variable: rides R-squared: 0.726
Model: OLS Adj. R-squared: 0.721
Method: Least Squares F-statistic: 153.6
Date: (date of the run) Prob (F-statistic): 6.10e-18
Time: (time of the run) Log-Likelihood: -327.05
No. Observations: 60 AIC: 658.1
Df Residuals: 58 BIC: 662.3
Df Model: 1
Covariance Type: nonrobustLeft column: what was fitted
| Entry | Meaning | Attribute |
|---|---|---|
| Dep. Variable | The variable being explained | model.model.endog_names |
| Model | OLS = Ordinary Least Squares | — |
| Method | How the coefficients were found | — |
| No. Observations | Rows used in the fit (rows with missing values are dropped!) | model.nobs |
| Df Model | Number of explanatory terms (excluding the intercept) | model.df_model |
| Df Residuals | Observations − estimated parameters = 60 − 2 | model.df_resid |
| Covariance Type | How standard errors are computed; nonrobust is the classic formula |
model.cov_type |
Always check No. Observations. If it's smaller than your table, some rows had missing values and were silently dropped.
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
rng = np.random.default_rng(42)
dates = pd.date_range("2024-04-01", periods=60)
temp = rng.uniform(4, 30, 60).round(1)
rain = np.where(rng.random(60) < 0.3, rng.uniform(1, 15, 60), 0).round(1)
humidity = rng.integers(35, 95, 60)
weekend = (dates.dayofweek >= 5).astype(int)
rides = (150 + 12 * temp - 8 * rain + 60 * weekend + rng.normal(0, 30, 60)).round().astype(int)
bikes = pd.DataFrame({"date": dates, "temp_c": temp, "rain_mm": rain,
"humidity": humidity, "weekend": weekend, "rides": rides})
model = smf.ols("rides ~ temp_c", data=bikes).fit()
print("Dep. Variable: ", model.model.endog_names)
print("No. Observations:", int(model.nobs))
print("Df Model: ", int(model.df_model))
print("Df Residuals: ", int(model.df_resid))
print("Covariance Type: ", model.cov_type)
# simulate a broken sensor: 5 days without temperature
broken = bikes.copy()
broken.loc[10:14, "temp_c"] = np.nan
print("With 5 missing temps, No. Observations =", int(smf.ols("rides ~ temp_c", data=broken).fit().nobs))Dep. Variable: rides
No. Observations: 60
Df Model: 1
Df Residuals: 58
Covariance Type: nonrobust
With 5 missing temps, No. Observations = 55
Right column: overall fit
| Entry | Meaning |
|---|---|
| R-squared | Share of the variance in rides explained by the model (0–1). See Regression R-Squared |
| Adj. R-squared | R² penalised for the number of predictors; use it to compare models of different size |
| F-statistic | Tests whether the model as a whole explains anything better than just the mean |
| Prob (F-statistic) | p-value of the F-test; tiny here (6.1 × 10⁻¹⁸), so the model is clearly better than nothing |
| Log-Likelihood | How probable the data is under the fitted model; higher (less negative) is better |
| AIC / BIC | Fit quality penalised for complexity; lower is better. Only compare models fitted on the same data |
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
rng = np.random.default_rng(42)
dates = pd.date_range("2024-04-01", periods=60)
temp = rng.uniform(4, 30, 60).round(1)
rain = np.where(rng.random(60) < 0.3, rng.uniform(1, 15, 60), 0).round(1)
humidity = rng.integers(35, 95, 60)
weekend = (dates.dayofweek >= 5).astype(int)
rides = (150 + 12 * temp - 8 * rain + 60 * weekend + rng.normal(0, 30, 60)).round().astype(int)
bikes = pd.DataFrame({"date": dates, "temp_c": temp, "rain_mm": rain,
"humidity": humidity, "weekend": weekend, "rides": rides})
for formula in ["rides ~ temp_c", "rides ~ humidity", "rides ~ temp_c + rain_mm + weekend"]:
m = smf.ols(formula, data=bikes).fit()
print(f"{formula:35} R2={m.rsquared:.3f} adjR2={m.rsquared_adj:6.3f} "
f"F={m.fvalue:6.1f} p(F)={m.f_pvalue:.2g} AIC={m.aic:.1f}")rides ~ temp_c R2=0.726 adjR2= 0.721 F= 153.6 p(F)=6.1e-18 AIC=658.1
rides ~ humidity R2=0.003 adjR2=-0.014 F= 0.2 p(F)=0.66 AIC=735.5
rides ~ temp_c + rain_mm + weekend R2=0.916 adjR2= 0.911 F= 202.4 p(F)=5.1e-30 AIC=591.5
Three models side by side:
- Humidity alone explains almost nothing: R² is near 0, adjusted R² is even negative, and the F-test's p-value is large — no better than predicting the mean every day.
- Temperature explains about 73 %.
- Temperature + rain + weekend reaches about 92 % and has the lowest AIC — the best of the three.
How the F-statistic relates to R²
For a model with k predictors and n observations, the F-statistic is a rescaled R²:
F = (R² / k) / ((1 − R²) / (n − k − 1))r2, k, n = 0.7258, 1, 60
F = (r2 / k) / ((1 - r2) / (n - k - 1))
print(round(F, 1))153.5
Same value as the table (up to rounding of R²). A large F means the explained part is large compared to the unexplained part, given the number of predictors used.
Tip: The F-test only says "this model beats a flat line". It says nothing about whether the model is good enough for your purpose — that depends on the size of the prediction errors, which you'll measure in Train/Test Split & Evaluation.
Test yourself
Your DataFrame has 60 rows, but No. Observations shows 55. What most likely happened?
- statsmodels sampled the data
- 5 rows had missing values in the model's variables and were dropped
- The intercept used 5 rows
- R-squared removed outliers
When comparing two models on the same data, which AIC is better?
- The higher one
- The lower one
- The one closest to 1
- AIC can't be compared
What does Df Residuals = 58 mean for a model fitted on 60 rows?
- 58 rows had errors
- 60 observations minus 2 estimated parameters (intercept and slope)
- 58 predictors were used
- The model is 58 % accurate