DS Regression P-Value
A coefficient can be non-zero just by chance. Even if humidity had no effect on bike rentals at all, a sample of 60 days would almost never produce a slope of exactly 0. The p-value helps decide whether an estimated coefficient is distinguishable from zero, or could easily be noise.
The hypothesis test behind each row
For every coefficient, statsmodels tests:
- Null hypothesis H₀: the true coefficient is 0 (the variable has no linear relationship with the target, given the other variables).
- Alternative H₁: the true coefficient is not 0.
It computes the t-value — how many standard errors the estimate lies from 0:
t = coef / std errand the p-value — the probability of seeing a t-value at least this far from 0 if H₀ were true. A small p-value means "this would be surprising if the true coefficient were 0".
The common convention is to call a coefficient statistically significant when p < 0.05.
A real effect and a non-effect
We fit a model with temperature, rain, humidity and weekend. Remember how the data was simulated: humidity plays no role in the rule.
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 + rain_mm + humidity + weekend", data=bikes).fit()
table = model.summary2().tables[1][["Coef.", "Std.Err.", "t", "P>|t|"]]
print(table.round(4))
print()
print("significant at 5%:", list(model.pvalues[model.pvalues < 0.05].index)) Coef. Std.Err. t P>|t|
Intercept 161.6366 17.6086 9.1794 0.0000
temp_c 11.5007 0.5874 19.5784 0.0000
rain_mm -9.1300 1.0768 -8.4784 0.0000
humidity -0.0731 0.2221 -0.3291 0.7434
weekend 64.3135 9.7247 6.6134 0.0000
significant at 5%: ['Intercept', 'temp_c', 'rain_mm', 'weekend']
- Temperature, rain and weekend have p-values far below 0.05: their effects are clearly real.
- Humidity has a coefficient of −0.07 rides per % with a standard error of 0.22. Its t-value is only −0.33 and its p-value is 0.74 — exactly the kind of result we expect for a variable with no effect.
Computing t and p yourself
The t-value and p-value come from the t-distribution with the model's residual degrees of freedom. SciPy reproduces them:
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
from scipy import stats
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 + rain_mm + humidity + weekend", data=bikes).fit()
coef = model.params["humidity"]
se = model.bse["humidity"]
t = coef / se
p = 2 * stats.t.sf(abs(t), df=model.df_resid) # two-sided test
print(f"t = {t:.3f} (statsmodels: {model.tvalues['humidity']:.3f})")
print(f"p = {p:.4f} (statsmodels: {model.pvalues['humidity']:.4f})")t = -0.329 (statsmodels: -0.329)
p = 0.7434 (statsmodels: 0.7434)
How chance creates small p-values
The 0.05 threshold means that for a truly useless variable, you'll still get p < 0.05 about 5 % of the time. Let's add 100 columns of pure random noise, one at a time, and count how many look "significant":
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})
noise_rng = np.random.default_rng(123)
p_values = []
for i in range(100):
bikes["noise"] = noise_rng.normal(size=60)
m = smf.ols("rides ~ temp_c + rain_mm + weekend + noise", data=bikes).fit()
p_values.append(m.pvalues["noise"])
p_values = np.array(p_values)
print("noise variables with p < 0.05:", (p_values < 0.05).sum(), "of 100")
print("smallest p-value:", p_values.min().round(4))noise variables with p < 0.05: 6 of 100
smallest p-value: 0.0072
A few pure-noise variables pass the test. If you try many variables and report only the "significant" ones, you'll publish noise — a practice called p-hacking.
Interpreting p-values correctly
| Statement | Correct? |
|---|---|
| "p = 0.74, so humidity has no effect." | ✗ — we just found no evidence of an effect; a small effect could be hidden by noise |
| "p = 0.001 means a 0.1 % chance that H₀ is true." | ✗ — p is computed assuming H₀ is true |
| "p < 0.05 means the effect is large or important." | ✗ — with lots of data, tiny effects become significant. Look at the coefficient and its interval |
| "p < 0.05: the data would be unusual if the true coefficient were 0." | ✓ |
Tip: Report the coefficient with its confidence interval first, and the p-value second. "+12 rides per °C (95 % CI 10–14)" says far more than "p < 0.001".
Test yourself
What is the null hypothesis tested for each coefficient in the regression table?
- The coefficient is positive
- The true coefficient is 0
- The model is perfect
- The p-value is 0.05
A coefficient is 3.0 with a standard error of 1.5. What is its t-value?
- 0.5
- 1.5
- 2.0
- 4.5
Humidity's p-value is 0.74. The best conclusion is…
- Humidity definitely has no effect
- The data gives no evidence that humidity affects rides
- Humidity reduces rides by 74 %
- The model is wrong