This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

OLS

OLS is an abbreviation for ordinary least squares.

The class estimates a multi-variate regression model and provides a variety of fit-statistics. To see the class in action download the ols.py file and run it (python ols.py). This will estimate a multi-variate regression using simulated data and provide output. It will also provide output from R to validate the results if you have rpy installed (http://rpy.sourceforge.net/).

To import the class:

   1 import ols

After importing the class you can estimate a model by passing data to it as follows

   1 mymodel = ols.ols(y,x,y_varnm,x_varnm)

where y is an array with data for the dependent variable, x contains the independent variables, y_varnm, is a string with the variable label for the dependent variable, and x_varnm is a list of variable labels for the independent variables. Note: An intercept term and variable label is automatically added to the model.

Example Usage

   1 >>> import ols
   2 >>> from numpy.random import randn
   3 >>> data = randn(100,5)
   4 >>> y = data[:,0]
   5 >>> x = data[:,1:]
   6 >>> mymodel = ols.ols(y,x,'y',['x1','x2','x3','x4'])
   7 >>> mymodel.p               # return coefficient p-values
   8 array([ 0.31883448,  0.7450663 ,  0.95372471,  0.97437927,  0.09993078])
   9 >>> mymodel.summary()       # print results
  10 ==============================================================================
  11 Dependent Variable: y
  12 Method: Least Squares
  13 Date: Thu, 28 Feb 2008
  14 Time: 22:32:24
  15 # obs:             100
  16 # variables:         5
  17 ==============================================================================
  18 variable     coefficient     std. Error      t-statistic     prob.
  19 ==============================================================================
  20 const           0.107348      0.107121      1.002113      0.318834
  21 x1             -0.037116      0.113819     -0.326100      0.745066
  22 x2              0.006657      0.114407      0.058183      0.953725
  23 x3              0.003617      0.112318      0.032201      0.974379
  24 x4              0.186022      0.111967      1.661396      0.099931
  25 ==============================================================================
  26 Models stats                         Residual stats
  27 ==============================================================================
  28 R-squared             0.033047         Durbin-Watson stat   2.012949
  29 Adjusted R-squared   -0.007667         Omnibus stat         5.664393
  30 F-statistic           0.811684         Prob(Omnibus stat)   0.058883
  31 Prob (F-statistic)    0.520770         JB stat              6.109005
  32 Log likelihood       -145.182795       Prob(JB)             0.047146
  33 AIC criterion         3.003656         Skew                 0.327103
  34 BIC criterion         3.133914         Kurtosis             4.018910
  35 ==============================================================================

Note

Library function numpy.linalg.lstsq() performs basic OLS estimation.

SciPy: Cookbook/OLS (last edited 2015-10-24 17:48:23 by anonymous)