OLS¶
日期 | 2010-08-04(最后修改),2007-01-17(创建) |
---|
OLS 是普通最小二乘法的缩写。
该类估计多元回归模型并提供各种拟合统计量。要查看该类的实际操作,请下载 ols.py 文件并运行它(python ols.py)。这将使用模拟数据估计多元回归并提供输出。如果安装了 rpy(http://rpy.sourceforge.net/),它还会提供来自 R 的输出以验证结果。
要导入该类
In [ ]
#!python
import ols
导入该类后,您可以通过将数据传递给它来估计模型,如下所示
In [ ]
#!python
mymodel = ols.ols(y,x,y_varnm,x_varnm)
其中 y 是包含因变量数据的数组,x 包含自变量,y_varnm 是因变量变量标签的字符串,x_varnm 是自变量变量标签的列表。注意:模型会自动添加截距项和变量标签。
示例用法¶
In [ ]
#!python
>>> import ols
>>> from numpy.random import randn
>>> data = randn(100,5)
>>> y = data[:,0]
>>> x = data[:,1:]
>>> mymodel = ols.ols(y,x,'y',['x1','x2','x3','x4'])
>>> mymodel.p # return coefficient p-values
array([ 0.31883448, 0.7450663 , 0.95372471, 0.97437927, 0.09993078])
>>> mymodel.summary() # print results
==============================================================================
Dependent Variable: y
Method: Least Squares
Date: Thu, 28 Feb 2008
Time: 22:32:24
# obs: 100
# variables: 5
==============================================================================
variable coefficient std. Error t-statistic prob.
==============================================================================
const 0.107348 0.107121 1.002113 0.318834
x1 -0.037116 0.113819 -0.326100 0.745066
x2 0.006657 0.114407 0.058183 0.953725
x3 0.003617 0.112318 0.032201 0.974379
x4 0.186022 0.111967 1.661396 0.099931
==============================================================================
Models stats Residual stats
==============================================================================
R-squared 0.033047 Durbin-Watson stat 2.012949
Adjusted R-squared -0.007667 Omnibus stat 5.664393
F-statistic 0.811684 Prob(Omnibus stat) 0.058883
Prob (F-statistic) 0.520770 JB stat 6.109005
Log likelihood -145.182795 Prob(JB) 0.047146
AIC criterion 3.003656 Skew 0.327103
BIC criterion 3.133914 Kurtosis 4.018910
==============================================================================
== 注意 ==
库函数 [https://docs.scipy.org.cn/doc/numpy/reference/generated/numpy.linalg.lstsq.html numpy.linalg.lstsq()] 执行基本的 OLS 估计。
部分作者:VincentNijs、Unknown[103]、DavidLinke、AlanLue
附件