Logo

Minimal Ordinary Least Squares ExamplesΒΆ

In [1]: import numpy as np

In [2]: import statsmodels.api as sm

In [3]: nsample = 100

In [4]: x = np.linspace(0,10, 100)

In [5]: X = sm.add_constant(np.column_stack((x, x**2)))

In [6]: beta = np.array([1, 0.1, 10])

In [7]: y = np.dot(X, beta) + np.random.normal(size=nsample)

In [8]: results = sm.OLS(y, X).fit()

In [9]: print results.summary()
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.974
Model:                            OLS   Adj. R-squared:                  0.973
Method:                 Least Squares   F-statistic:                     1815.
Date:                Fri, 29 Jun 2012   Prob (F-statistic):           1.41e-77
Time:                        22:00:47   Log-Likelihood:                -140.29
No. Observations:                 100   AIC:                             286.6
Df Residuals:                      97   BIC:                             294.4
Df Model:                           2                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
x1             1.1124      0.136      8.190      0.000         0.843     1.382
x2             0.0938      0.013      7.135      0.000         0.068     0.120
const          9.5979      0.294     32.660      0.000         9.015    10.181
==============================================================================
Omnibus:                        0.347   Durbin-Watson:                   2.204
Prob(Omnibus):                  0.841   Jarque-Bera (JB):                0.125
Skew:                           0.078   Prob(JB):                        0.939
Kurtosis:                       3.075   Cond. No.                         144.
==============================================================================

This Page