Ordinary least squares in Python
The summary table is the point: coefficients with their uncertainty.
import numpy as np
import statsmodels.api as sm
rng = np.random.default_rng(0)
practice = np.arange(1, 61)
tpm = 70 + 0.7 * practice + rng.normal(0, 4, size=60)
X = sm.add_constant(practice)
model = sm.OLS(tpm, X).fit()
print(model.params.round(4))
print(model.bse.round(4), model.pvalues.round(6))
print(round(model.rsquared, 4), round(model.rsquared_adj, 4))
print(model.conf_int(alpha=0.05).round(3))
How it works
add_constantis required, or the model has no intercept.fitreturns a results object, not the model.params,bseandpvaluesare the numbers behind the table.
Keywords and builtins used here
asprintround
The run, in numbers
- Lines
- 14
- Characters to type
- 406
- Tokens
- 139
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 79 seconds.
Step 1 of 3 in Linear models, step 1 of 19 in Statistics with statsmodels.