top of page

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 2.3 Example of linear regression in Python.
==================================================

import numpy as np
import statsmodels.formula.api as smf


# Data
y   = np.array([13, 15, 9, 17, 8, 5, 19, 23, 10, 7, 10, 6])                   # continuous response variable
x1 = np.array([1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0])                                 # binary predictor
x2 = np.array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])                                 # categorical predictor

 

mydata = {}                                                                                      # create data dictionary
mydata['x1'] = x1
mydata['x2'] = x2
mydata['y']   = y

 

# Fit  using ordinary least squares
results = smf.ols(formula = 'y ~ x1 + x2',  data = mydata).fit()


# Output
print(str(results.summary()))

==================================================

Output on screen:                                                                      

​

​

                                                                 OLS Regression Results                            
==============================================================================
Dep. Variable:                                         y                                          R-squared:                                         0.763
Model:                                               OLS                                          Adj. R-squared:                                 0.711
Method:                               Least Squares                                         F-statistic:                                          14.51
Date:                               Sat, 17 Dec 2016                                         Prob (F-statistic):                          0.00153
Time:                                            01:22:54                                         Log-Likelihood:                            -28.579
No. Observations:                                  1                                           AIC:                                                  63.16
Df Residuals:                                           9                                          BIC:                                                  64.61
Df Model:                                                2                                         
Covariance Type:                        nonrobust                                         
==============================================================================
                             coef                    std err                     t                        P>|t|                           [95.0% Conf. Int.]
------------------------------------------------------------------------------------------------------------------------------------
Intercept         38.8333                     5.090              7.630                      0.000                             27.319    50.347
x1                  -14.5000                     3.024             -4.795                      0.001                           -21.340     -7.660
x2                    -9.8750                     1.852             -5.333                      0.000                           -14.064     -5.686
==============================================================================
Omnibus:                                        0.687                          Durbin-Watson:                                                  2.112
Prob(Omnibus):                              0.709                          Jarque-Bera (JB):                                               0.467
Skew:                                             -0.426                         Prob(JB):                                                            0.792
Kurtosis:                                          2.545                         Cond. No.                                                            16.5
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

 

bottom of page