HSI
From: Bayesian Models for Astrophysical Data, Cambridge Univ. Press
(c) 2017, Joseph M. Hilbe, Rafael S. de Souza and Emille E. O. Ishida
you are kindly asked to include the complete citation if you used this material in a publication
​
​
Code 3.2 Ordinary least squares regression in Python without formula.
==================================================
import numpy as np
import statsmodels.api as sm
from scipy.stats import uniform, norm
# Data
np.random.seed(1056) # set seed to replicate example
nobs = 250 # number of obs in model
x1 = uniform.rvs(size=nobs) # random uniform variable
alpha = 2.0 # intercept
beta = 3.0 # slope
xb = alpha + beta * x1 # linear predictor
y = norm.rvs(loc=xb, scale=1.0, size=nobs) # create y as adjusted random normal variate
# Fit
unity_vec = np.full((nobs,),1, np.float) # unity vector
X = np.column_stack((unity_vec, x1)) # build data matrix with intercept
results = sm.OLS(y, X).fit()
# Output
print(str(results.summary()))
==================================================
Output on screen:
​
​
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.455
Model: OLS Adj. R-squared: 0.453
Method: Least Squares F-statistic: 207.0
Date: Sat, 17 Dec 2016 Prob (F-statistic): 1.56e-34
Time: 02:15:19 Log-Likelihood: -339.11
No. Observations: 250 AIC: 682.2
Df Residuals: 248 BIC: 689.3
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [95.0% Conf. Int.]
-------------------------------------------------------------------------------------------------------------------------------------
const 2.0398 0.127 16.010 0.000 1.789 2.291
x1 3.1363 0.218 14.387 0.000 2.707 3.566
==============================================================================
Omnibus: 1.101 Durbin-Watson: 1.967
Prob(Omnibus): 0.577 Jarque-Bera (JB): 1.186
Skew: -0.109 Prob(JB): 0.553
Kurtosis: 2.743 Cond. No. 4.69
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.