TExcelTAD
Home › Data Science › DS Regression Coefficients

DS Regression Coefficients

The coefficients are the heart of a regression model: they are the estimated intercept and slopes. This lesson is about reading them correctly — their meaning, their units, and their uncertainty.

text
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept    135.8133     19.205      7.072      0.000      97.371     174.255
temp_c        12.5449      1.012     12.393      0.000      10.519      14.571

Reading the coef column

The model is:

text
predicted rides = 135.81 + 12.54 × temp_c
  • temp_c = 12.54: for each additional degree Celsius, the model predicts about 12.5 more rides per day. The unit is rides per °C.
  • Intercept = 135.81: the predicted rides at 0 °C. Our data starts at 4.2 °C, so this is a mathematical anchor more than a real-world forecast.

Coefficients turn into predictions by plugging in values — exactly what model.predict does:

python · try it yourself
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()
b0 = model.params["Intercept"]
b1 = model.params["temp_c"]

t = 18
by_hand = b0 + b1 * t
by_model = model.predict(pd.DataFrame({"temp_c": [t]}))[0]
print(f"by hand:  {by_hand:.2f}")
print(f"by model: {by_model:.2f}")
print(f"a 5-degree warmer day adds about {5 * b1:.0f} rides")
by hand:  361.62
by model: 361.62
a 5-degree warmer day adds about 63 rides

Units change the coefficient, not the model

A coefficient's size depends on the unit of its variable. Measure temperature in Fahrenheit and the slope shrinks, because one °F is a smaller step than one °C — but the predictions are identical:

python · try it yourself
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})

bikes["temp_f"] = bikes["temp_c"] * 9 / 5 + 32
m_c = smf.ols("rides ~ temp_c", data=bikes).fit()
m_f = smf.ols("rides ~ temp_f", data=bikes).fit()

print("slope per °C:", round(m_c.params["temp_c"], 3))
print("slope per °F:", round(m_f.params["temp_f"], 3), "=", round(m_c.params["temp_c"] * 5 / 9, 3))
print("same predictions:", np.allclose(m_c.fittedvalues, m_f.fittedvalues))
slope per °C: 12.545
slope per °F: 6.969 = 6.969
same predictions: True

So never call a coefficient "big" or "small" without its unit. To compare predictors measured in different units, look at their t-values or standardise the variables first.

Uncertainty: standard error and confidence interval

The coefficient is an estimate from 60 days. Another 60 days would give a slightly different value. Two columns describe this uncertainty:

  • std err — the typical amount the estimate would vary from sample to sample (≈ 1.01 rides per °C).
  • [0.025, 0.975] — the 95 % confidence interval, roughly coef ± 2 × std err. We're fairly confident the true slope lies between 10.5 and 14.6 rides per °C.
python · try it yourself
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()
ci = model.conf_int()
ci.columns = ["lower 2.5%", "upper 97.5%"]
ci["estimate"] = model.params
print(ci.round(2))

# 30 days instead of 60 -> wider interval
half = smf.ols("rides ~ temp_c", data=bikes.iloc[:30]).fit()
print()
print("width with 60 days:", round(ci.loc["temp_c", "upper 97.5%"] - ci.loc["temp_c", "lower 2.5%"], 2))
lo, hi = half.conf_int().loc["temp_c"]
print("width with 30 days:", round(hi - lo, 2))
           lower 2.5%  upper 97.5%  estimate
Intercept       97.37       174.26    135.81
temp_c          10.52        14.57     12.54

width with 60 days: 4.05
width with 30 days: 5.96

Less data, wider interval: with half the days, we know the slope much less precisely.

Note: The true rule we used to simulate the data has a slope of 12 rides per °C. The interval from 60 days (10.52 to 14.57) contains it — that's what a 95 % interval should do in about 95 % of samples.

Coefficients with several predictors

With more than one explanatory variable, each coefficient means: the change in the prediction when this variable rises by one unit and all the others stay the same. The Multiple Regression lesson explores this in depth, and the Linear Regression Case puts it to work.

Test yourself

With rides = 135.81 + 12.54 × temp_c, what is the predicted change in rides when it gets 2 °C warmer?

  1. +12.54
  2. +25.08
  3. +135.81
  4. +148.35

The 95 % confidence interval for a slope is [10.52, 14.57]. What happens to it if you collect much more data?

  1. It gets wider
  2. It gets narrower
  3. It stays the same
  4. It moves to zero

Temperature is converted from °C to °F. What happens to the regression?

  1. Predictions change
  2. The slope changes (per °F), but predictions stay the same
  3. R-squared drops
  4. The intercept stays the same