bhylu.com

Advertisements

Data Science Regression P-Values

Understand the slope test as one narrow inferential question.

How it works

The default two-sided slope test in linregress tests a zero slope under its statistical assumptions. It does not test whether every prediction is accurate or whether the predictor causes the response. A nonzero slope can be practically tiny, while a useful effect can be estimated imprecisely in a small sample. Report the estimated change and uncertainty, not only a threshold label.

Run the code

Try in playground opens the correct scipy editor and preloads the complete source. Choose Run to execute; loading does not execute it. The isolated browser runtime does not read personal PC files.

Local review: Python 3.12.14, NumPy 2.5.4, Pandas 3.0.6 and SciPy 1.18.0. Browser review: Pyodide 0.27.7, Python 3.12.7, NumPy 2.0.2, Pandas 2.2.3 and SciPy 1.14.1 in their respective editors. Save the download as regression-p-values.py and run python regression-p-values.py in your separate environment. Apache and MySQL are not needed. Environment setup.

Download the complete source

Example

regression-p-values.py

import numpy as np
from scipy import stats
x = np.arange(1., 7.)
y = np.array([8., 12., 13., 18., 18., 24.])
fit = stats.linregress(x, y)
t = fit.slope / fit.stderr
independent_p = 2*stats.t.sf(abs(t), df=x.size-2)
assert np.isclose(fit.pvalue, independent_p, rtol=1e-10, atol=1e-14)
print("Slope p-value agrees with a two-sided t calculation using n-2 degrees of freedom")

Expected result

Slope p-value agrees with a two-sided t calculation using n-2 degrees of freedom

The t statistic divides the fitted slope by its estimated standard error, and the residual degrees of freedom account for estimating two coefficients. This independent formula checks the software contract. It does not prove independent, correctly specified errors or rule out a confounding event schedule.

Technical references: NumPy, Pandas and SciPy. For evaluation design see the scikit-learn guide to leakage; these examples do not require scikit-learn.