Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r-glm2错误“;“遇到奇异拟合”;_R_Glm_Logistic Regression - Fatal编程技术网

r-glm2错误“;“遇到奇异拟合”;

r-glm2错误“;“遇到奇异拟合”;,r,glm,logistic-regression,R,Glm,Logistic Regression,我正在尝试不同的方法来做逻辑回归。 我使用glm并得到警告,但仍然得到了系数。所以这个公式是有效的 logit<-glm(flag_compro~.,training, family=binomial("logit"),control = list(maxit = 50)) logit函数glm会自动删除列,以纠正奇异匹配,而glm2函数不会这样做。一种解决方案是使用lm或glm函数拟合数据,查看它删除了哪些列,并在使用“glm2”之前删除这些列。下面是一个简单的可复制示例来演示 注意,

我正在尝试不同的方法来做逻辑回归。 我使用glm并得到警告,但仍然得到了系数。所以这个公式是有效的

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

logit函数
glm
会自动删除列,以纠正奇异匹配,而
glm2
函数不会这样做。一种解决方案是使用
lm
glm
函数拟合数据,查看它删除了哪些列,并在使用“glm2”之前删除这些列。下面是一个简单的可复制示例来演示

注意,从
glm
fit中明确删除这些列也是一个好主意

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

df可复制示例请?您能在
glm2
中传递
singular.ok=TRUE
吗?没有这样的选项glm2不适合明显的多线性数据集。我会使用惩罚回归作为glmnet。在任何情况下,你都应该按照本的要求发布一个可复制的例子。非常好的例子!我不知道lm()会自动删除预测值。谢谢然而,我尝试glm2()的原因是glm()没有收敛,因此切换到glm()以查找丢弃的预测值是不可能的。但是,在glm()中使用maxit=100可以解决这个问题。
> 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
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