2014-10-28 48 views
1

我正在嘗試不同的方法來做邏輯迴歸。 我使用glm並得到警告,但仍然得到係數。因此,該公式起作用。r- glm2錯誤「遇到奇異適合」

logit<-glm(flag_compro~.,training, family=binomial("logit"),control = list(maxit = 50)) 

現在,我測試廣二輕,而且因爲它說,使用相同的模型作爲spefications GLM,我寫道:

logit2<-glm2(flag_compro~., training, family=binomial("logit")) 

但我得到了以下錯誤:

> logit2<-glm2(flag_compro~., training, family=binomial("logit")) 
Error in lm.fit(x = x[good, , drop = FALSE] * w, y = z * w, singular.ok = FALSE, : 
    singular fit encountered 

我認爲這與我可能存在多重無關性有關。如果是這樣的話,glm2軟件包有什麼可以解決的嗎?

+4

請重複舉例嗎? – 2014-10-28 02:50:19

+0

你可以在'glm2'中傳遞'singular.ok = TRUE'嗎? – rawr 2014-10-28 02:55:36

+1

沒有這樣的選項 – Bakaburg 2015-01-11 23:43:57

回答

2

glm函數默默地刪除列以修復奇異擬合,而glm2函數不會這樣做。一種解決方案是將數據與lmglm函數擬合,查看它丟棄的列,然後在使用`glm2'之前刪除它們。以下是一個簡單的可重複使用的示例。

請注意,明確刪除glm也適合使用這些列。

df <- data.frame(y = c(200, 1000, 100, 10, 10) 
      ,x1 = c(0, 0, 50, 50, 0) 
      ,x2 = c(0, 0, 350, 200, 0) 
      ,x3 = c(100, 0, 0, 200, 100) 
      ,x4 = c(200, 0, 50, 0, 200)) 
coef(lm(y ~ ., data = df)) # x4 dropped as predictor 
coef(glm(y ~ ., data = df)) # x4 dropped as predictor 

library(glm2) 
glm2(y ~ ., data = df) # gives singular fit error 
glm2(y ~ x1 + x2 + x3, data = df) # no singular fit error 

summary(lm(x4 ~ x1 + x2 + x3, data = df))$r.squared # x4 is a linear combination of x1-x3 

# If making predictions, should also remove columns before fitting with glm 
glm_fit <- glm(y ~ ., data = df) 
predict(glm_fit, newdata = df[1:4,]) # gives warning about misleading predictions 

glm_fit2 <- glm(y ~ x1 + x2 + x3, data = df) 
predict(glm_fit2, newdata = df[1:4,]) # no warning about misleading predictions 
+0

非常好的例子!不知道lm()會自動放棄預測變量。謝謝! - 但是,我嘗試glm2()的原因是glm()沒有收斂,因此切換到glm()以查找已刪除的預測變量是不可能的。但是,在glm()中使用maxit = 100解決了這個問題。 – steinbock 2016-09-23 13:04:38