statsmodel线性回归(ols)-Python的鲁棒性问题
我正在使用统计模型测试一些基本类别回归: 我建立了一个确定性模型 Y=X+Z 其中X可以取3个值(a、b或c),Z只能取2个值(d或e)。 在那个阶段,模型是完全确定的,我为每个变量设置了权重,如下所示 a的重量=1 b的重量=2 c的重量=3 d的重量=1 e的重量=2 因此,如果X=a,则1(X=a)为1,否则为0,则模型简单如下: Y=1(X=a)+2*1(X=b)+3*1(X=c)+1(Z=d)+2*1(Z=e) 使用以下代码,生成不同的变量并运行回归statsmodel线性回归(ols)-Python的鲁棒性问题,python,statsmodels,Python,Statsmodels,我正在使用统计模型测试一些基本类别回归: 我建立了一个确定性模型 Y=X+Z 其中X可以取3个值(a、b或c),Z只能取2个值(d或e)。 在那个阶段,模型是完全确定的,我为每个变量设置了权重,如下所示 a的重量=1 b的重量=2 c的重量=3 d的重量=1 e的重量=2 因此,如果X=a,则1(X=a)为1,否则为0,则模型简单如下: Y=1(X=a)+2*1(X=b)+3*1(X=c)+1(Z=d)+2*1(Z=e) 使用以下代码,生成不同的变量并运行回归 from statsmodels.
from statsmodels.formula.api import ols
nbData = 1000
rand1 = np.random.uniform(size=nbData)
rand2 = np.random.uniform(size=nbData)
a = 1 * (rand1 <= (1.0/3.0))
b = 1 * (((1.0/3.0)< rand1) & (rand1< (4/5.0)))
c = 1-b-a
d = 1 * (rand2 <= (3.0/5.0))
e = 1-d
weigths = [1,2,3,1,2]
y = a+2*b+3*c+4*d+5*e
df = pd.DataFrame({'y':y, 'a':a, 'b':b, 'c':c, 'd':d, 'e':e})
mod = ols(formula='y ~ a + b + c + d + e - 1', data=df)
res = mod.fit()
print(res.summary())
但是当我将数据点的数量增加到(比如)600时,回归产生了非常糟糕的结果。我在Excel和R中尝试过类似的回归,无论数据点的数量如何,它们都能产生一致的结果。有没有人知道在解释这种行为时,STATSOLS模型是否有一些限制,或者我是否遗漏了什么
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.167
Model: OLS Adj. R-squared: 0.161
Method: Least Squares F-statistic: 29.83
Date: Wed, 16 Sep 2015 Prob (F-statistic): 1.23e-22
Time: 03:08:04 Log-Likelihood: -701.02
No. Observations: 600 AIC: 1412.
Df Residuals: 595 BIC: 1434.
Df Model: 4
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [95.0% Conf. Int.]
------------------------------------------------------------------------------
a 5.8070 1.15e+13 5.05e-13 1.000 -2.26e+13 2.26e+13
b 6.4951 1.15e+13 5.65e-13 1.000 -2.26e+13 2.26e+13
c 6.9033 1.15e+13 6.01e-13 1.000 -2.26e+13 2.26e+13
d -1.1927 1.15e+13 -1.04e-13 1.000 -2.26e+13 2.26e+13
e -0.1685 1.15e+13 -1.47e-14 1.000 -2.26e+13 2.26e+13
==============================================================================
Omnibus: 67.153 Durbin-Watson: 0.328
Prob(Omnibus): 0.000 Jarque-Bera (JB): 70.964
Skew: 0.791 Prob(JB): 3.89e-16
Kurtosis: 2.419 Cond. No. 7.70e+14
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The smallest eigenvalue is 9.25e-28. This might indicate that there are
strong multicollinearity problems or that the design matrix is singular.
正如F先生所提到的,主要问题是,在这种情况下,statsmodel OLS似乎无法处理共线性pb以及Excel/R,但如果不是为每个
a、b、c、d和e定义一个变量,而是定义一个变量X
和一个Z
,可以等于a,b或c
和d或e
resp,则回归工作正常。Ie使用以下内容更新代码:
df['X'] = ['c']*len(df)
df.X[df.b!=0] = 'b'
df.X[df.a!=0] = 'a'
df['Z'] = ['e']*len(df)
df.Z[df.d!=0] = 'd'
mod = ols(formula='y ~ X + Z - 1', data=df)
导致预期的结果
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 1.000
Model: OLS Adj. R-squared: 1.000
Method: Least Squares F-statistic: 2.684e+27
Date: Thu, 17 Sep 2015 Prob (F-statistic): 0.00
Time: 06:22:43 Log-Likelihood: 2.5096e+06
No. Observations: 100000 AIC: -5.019e+06
Df Residuals: 99996 BIC: -5.019e+06
Df Model: 3
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [95.0% Conf. Int.]
------------------------------------------------------------------------------
X[a] 5.0000 1.85e-14 2.7e+14 0.000 5.000 5.000
X[b] 6.0000 1.62e-14 3.71e+14 0.000 6.000 6.000
X[c] 7.0000 2.31e-14 3.04e+14 0.000 7.000 7.000
Z[T.e] 1.0000 1.97e-14 5.08e+13 0.000 1.000 1.000
==============================================================================
Omnibus: 145.367 Durbin-Watson: 1.353
Prob(Omnibus): 0.000 Jarque-Bera (JB): 9729.487
Skew: -0.094 Prob(JB): 0.00
Kurtosis: 1.483 Cond. No. 2.29
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
你也可以发布Excel或R代码吗?我的期望是出现多重共线性问题:如果您有一个变量可以接受N个不同的分类值,那么应该用N-1列虚拟表示,而不是N列虚拟表示,因为给定前N-1列,第N列是完全确定的。例如,变量Z完全由单个虚拟列确定。我很惊讶其他回归工具能给出一致的结果。很好的例子,我从未见过这样的案例。我猜你刚刚达到广义逆的阈值,numpy.pinv。在第一种情况下,解是降秩的,df_模型等于3。在第二种情况下,存在足够的数值噪声,因此可以使用等于4的df_模型来识别秩。这可能有利于从numpy的默认值降低pinv阈值。R可能会产生更稳定但同样未知的结果,因为默认情况下它使用旋转QR分解AFAIK。斯塔塔完美地降下了共线柱。这仍然有点令人惊讶。您的数据的数据类型是什么,即mod.exog.dtype
?我的数据类型是float64,有一个失败的示例。pinv的阈值有点太低,np.linalg.pinv(mod.exog,rcond=1.5e-15)。dot(mod.endog)
而不是默认的1e-15将修复我得到的随机情况。但是正如警告和F先生指出的,在单数或近似单数情况下依赖精度问题是很脆弱的。@F先生:对于excel,我只使用linest,对于R,代码只使用myreg
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 1.000
Model: OLS Adj. R-squared: 1.000
Method: Least Squares F-statistic: 2.684e+27
Date: Thu, 17 Sep 2015 Prob (F-statistic): 0.00
Time: 06:22:43 Log-Likelihood: 2.5096e+06
No. Observations: 100000 AIC: -5.019e+06
Df Residuals: 99996 BIC: -5.019e+06
Df Model: 3
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [95.0% Conf. Int.]
------------------------------------------------------------------------------
X[a] 5.0000 1.85e-14 2.7e+14 0.000 5.000 5.000
X[b] 6.0000 1.62e-14 3.71e+14 0.000 6.000 6.000
X[c] 7.0000 2.31e-14 3.04e+14 0.000 7.000 7.000
Z[T.e] 1.0000 1.97e-14 5.08e+13 0.000 1.000 1.000
==============================================================================
Omnibus: 145.367 Durbin-Watson: 1.353
Prob(Omnibus): 0.000 Jarque-Bera (JB): 9729.487
Skew: -0.094 Prob(JB): 0.00
Kurtosis: 1.483 Cond. No. 2.29
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.