2011-12-21 1119 views
30

所以這是一個非常簡單的問題,似乎無法弄清楚。爲什麼我用glm獲得「算法沒有收斂」和「擬合數值爲0或1」的警告?

我使用glm函數運行logit,但不斷收到與自變量有關的警告消息。它們存儲爲因素,我已將它們更改爲數字,但沒有運氣。我也將它們編碼爲0/1,但這也不起作用。

請幫忙!

> mod2 <- glm(winorlose1 ~ bid1, family="binomial") 
Warning messages: 
1: glm.fit: algorithm did not converge 
2: glm.fit: fitted probabilities numerically 0 or 1 occurred 

我也試圖在澤裏格柯,但類似的錯誤:

> mod2 = zelig(factor(winorlose1) ~ bid1, data=dat, model="logit") 
How to cite this model in Zelig: 
Kosuke Imai, Gary King, and Oliva Lau. 2008. "logit: Logistic Regression for Dichotomous Dependent Variables" in Kosuke Imai, Gary King, and Olivia Lau, "Zelig: Everyone's Statistical Software," http://gking.harvard.edu/zelig 
Warning messages: 
1: glm.fit: algorithm did not converge 
2: glm.fit: fitted probabilities numerically 0 or 1 occurred 

編輯:

> str(dat) 
'data.frame': 3493 obs. of 3 variables: 
$ winorlose1: int 2 2 2 2 2 2 2 2 2 2 ... 
$ bid1  : int 700 300 700 300 500 300 300 700 300 300 ... 
$ home  : int 1 0 1 0 0 0 0 1 0 0 ... 
- attr(*, "na.action")=Class 'omit' Named int [1:63021] 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 ... 
    .. ..- attr(*, "names")= chr [1:63021] "3494" "3495" "3496" "3497" ... 
+2

這將是不可能回答沒有關於您的數據的一些詳細信息。例如'str(dat)'。另外,這些是警告,而不是錯誤。有很大的不同。 – joran 2011-12-21 20:58:59

+1

我只是想指出,有一個'glm2'包聲稱實現融合'glm'沒有。我不知道這是否與這個問題有關。看到http://journal.r-project.org/archive/2011-2/RJournal_2011-2_Marschner.pdf – 2011-12-22 06:46:19

+0

因爲你似乎在處理分類數據,所以我會考慮將你的整數變量作爲因子。 dat $ home < - as.factor(dat $ home) – eamo 2013-09-20 16:11:20

回答

34

如果你看看?glm(甚至做一個谷歌搜索你的第二個警告消息)你可能偶然發現這個文件:

For the background to warning messages about ‘fitted probabilities numerically 0 or 1 occurred’ for binomial GLMs, see Venables & Ripley (2002, pp. 197–8).

現在,並不是每個人都有這本書。不過,假設它是猶太對我來說,做到這一點,這裏的相關段落:

There is one fairly common circumstance in which both convergence problems and the Hauck-Donner phenomenon can occur. This is when the fitted probabilities are extremely close to zero or one. Consider a medical diagnosis problem with thousands of cases and around 50 binary explanatory variable (which may arise from coding fewer categorical variables); one of these indicators is rarely true but always indicates that the disease is present. Then the fitted probabilities of cases with that indicator should be one, which can only be achieved by taking βi = ∞. The result from glm will be warnings and an estimated coefficient of around +/- 10. There has been fairly extensive discussion of this in the statistical literature, usually claiming non-existence of maximum likelihood estimates; see Sautner and Duffy (1989, p. 234).

一本書稍微詳細here評論的作者。所以這裏的教訓是仔細觀察你的預測變量的一個層次。 (和谷歌的警告信息!)

+7

+1好答案。只需補充一點:最好查看模型,模型診斷,有時還需要一個不同的模型。例如,嘗試一個分類樹。這可能會告訴你,(a)你有一個出色的預測器(好東西),或者(b)你有一些抽樣問題(壞事)。 – Iterator 2011-12-22 01:16:56

+1

這個答案是否只解決OP問題中的第二個警告?我在http://discuss.analyticsvidhya.com/t/warning-message-glm-fit-algorithm-did-not-converge/5299找到了調整參數「maxit」的建議(這在文檔中沒有列出'glm',但作爲'control'參數的一部分傳遞給'glm.fit',然後傳遞給'glm.control'),這似乎解決了第一個警告'1:glm.fit:算法沒有爲我融合。 – 2016-04-14 12:43:37

+0

我發現你的答案非常有用,但我仍然不明白如何根據你的答案解決問題。我的理解(基於你答案中的引用)是:我的一個預測變量的其中一個水平很少是真實的,但總是表明out out變量是0或1.首先,當然任何體面的統計方法都應該能夠處理這個問題嗎?其次,我如何找到預測變量,一旦我發現它,我該怎麼做呢? – par 2016-07-05 14:33:11

相關問題