在虹膜的例子中,我們可以很好地預測Setosa。這會導致Logit和MNLogit中(部分)完美分離的問題。
完美分離對預測有好處,但logit的參數會變爲無窮大。在這種情況下,我得到了一個Singular Matrix錯誤,而不是Nans,它使用相對最新版本的statsmodels master(在Windows上)。
離散模型的默認優化器是牛頓,當Hessian變成單數時失敗。其他不使用Hessian信息的優化器能夠完成優化。例如,使用 'BFGS',I得到
>>> mdl_fit = mdl.fit(method='bfgs')
Warning: Maximum number of iterations has been exceeded.
Current function value: 0.057112
Iterations: 35
Function evaluations: 37
Gradient evaluations: 37
e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new2_py27\statsmodels\statsmodels\base\model.py:471: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
"Check mle_retvals", ConvergenceWarning)
預測概率Setosa基本上是(1,0,0),也就是它們是完全預測
>>> fitted = mdl_fit.predict()
>>> fitted[y=='setosa'].min(0)
array([ 9.99497636e-01, 2.07389867e-11, 1.71740822e-38])
>>> fitted[y=='setosa'].max(0)
array([ 1.00000000e+00, 5.02363854e-04, 1.05778255e-20])
然而,因爲完美分離參數未被識別,這些值主要由優化器的停止標準確定,並且標準誤差非常大。
>>> print(mdl_fit.summary())
MNLogit Regression Results
==============================================================================
Dep. Variable: Species No. Observations: 150
Model: MNLogit Df Residuals: 140
Method: MLE Df Model: 8
Date: Mon, 20 Jul 2015 Pseudo R-squ.: 0.9480
Time: 04:08:04 Log-Likelihood: -8.5668
converged: False LL-Null: -164.79
LLR p-value: 9.200e-63
=====================================================================================
Species=versicolor coef std err z P>|z| [95.0% Conf. Int.]
--------------------------------------------------------------------------------------
Sepal.Length -1.4959 444.817 -0.003 0.997 -873.321 870.330
Sepal.Width -8.0560 282.766 -0.028 0.977 -562.267 546.155
Petal.Length 11.9301 374.116 0.032 0.975 -721.323 745.184
Petal.Width 1.7039 759.366 0.002 0.998 -1486.627 1490.035
const 1.6444 1550.515 0.001 0.999 -3037.309 3040.597
--------------------------------------------------------------------------------------
Species=virginica coef std err z P>|z| [95.0% Conf. Int.]
-------------------------------------------------------------------------------------
Sepal.Length -8.0348 444.835 -0.018 0.986 -879.896 863.827
Sepal.Width -15.8195 282.793 -0.056 0.955 -570.083 538.444
Petal.Length 22.1797 374.155 0.059 0.953 -711.152 755.511
Petal.Width 14.0603 759.384 0.019 0.985 -1474.304 1502.425
const -6.5053 1550.533 -0.004 0.997 -3045.494 3032.483
=====================================================================================
關於在statsmodels實施
的Logit檢查專門爲完美的分離,提高了可任選地減弱到警告異常。 對於像MNLogit這樣的其他型號,目前還沒有明確檢查完美分離,主要是因爲缺乏好的測試用例和容易識別的一般條件。 (如幾個問題仍然是開放的)
我的總體戰略:
當有收斂失敗,然後嘗試不同的優化器和不同的起始值(start_params
)。如果某些優化器成功,那麼它可能是一個困難的優化問題,無論是目標函數的曲率,解釋變量的嚴重程度還是類似的。有用的檢查是使用穩健優化器(如nm
或powell
)的參數估計值作爲更嚴格的優化器的初始值,如newton
或bfgs
。
如果某些優化器收斂後結果仍然不好,那麼它可能是Logit,Probit和其他幾個模型或單一或接近奇異設計矩陣的完美分離數據的固有問題。在那種情況下,模型必須改變。通過互聯網搜索可以找到完美分離的建議。
明白了。不是Matlab的mnrfit問題,但我需要用MNLogit來處理。感謝您幫助我理解和解決問題。 – StemOner
我不知道Matlab做了什麼,但返回隨機或任意未識別的結果對於統計數據包來說不是一個好的方法,儘管在某些情況下statsmodels仍然會這樣做。我只發現這與matlab中的完全分離有關:http://www.mathworks.com/matlabcentral/newsreader/view_thread/303156(隱藏這個問題並不意味着它已經消失。) – user333700