在statsmodels中添加来自WLS回归函数的2D参数

时间:2014-04-29 15:49:20

标签: python arrays numpy regression statsmodels

我使用statsmodels逐步添加WLS regression functions的参数。

我有一个我声明的10x3数据集X:

X = np.array([[1,2,3],[1,2,3],[4,5,6],[1,2,3],[4,5,6],[1,2,3],[1,2,3],[4,5,6],[4,5,6],[1,2,3]])

这是我的数据集,我有一个10x2 endog向量,如下所示:

z =
[[  3.90311860e-322   2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]
 [  0.00000000e+000  -2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]
 [  0.00000000e+000  -2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]
 [  0.00000000e+000  -2.00000000e+000]
 [  0.00000000e+000  -2.00000000e+000]
 [  0.00000000e+000   2.00000000e+000]]

导入import statsmodels.api as sm后,我执行此操作:

g = np.zeros([3, 2]) # g(x) is a function that will store the regression parameters
mod_wls = sm.WLS(z, X)
temp_g = mod_wls.fit()
print temp_g.params

我得到了这个输出:

[[ -5.92878775e-323  -2.77777778e+000]
 [ -4.94065646e-324  -4.44444444e-001]
 [  4.94065646e-323   1.88888889e+000]]

现在问题是,我需要逐步添加参数。所以我需要做的是在循环中运行sm.WLS,每次都有一个新的endog向量,并在每次迭代中存储拟合的参数。我需要累积累加存储的参数以获得新的回归函数。我的目标是在g中添加它们。 为此,我修改了上面的代码来执行此操作:

g = np.zeros([3, 2]) # g(x) is a function that will store the regression parameters
for j in range(m):     
    # some code to build a generate z vector
    mod_wls = sm.WLS(z, X) # each time this line is called, z has new values
    temp_g = mod_wls.fit()
    np.vstack((temp_g.params, g)) # append the parameters from temp_g to g

所以这就是我正在做的事情。 我实际上在endog矢量是一维数组之前运行它,一切都很好。

但是如何使用2D数组呢?在附加g的参数后打印出temp_gtemp_g的值时,这就是输出:

g after stacking: 
[[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

所以g似乎没有添加来自temp_g的参数行。 我在这里做错了什么?

0 个答案:

没有答案