【发布时间】:2016-06-07 16:19:00
【问题描述】:
这里有两个相关的问题,但它们不是我的重复问题,因为第一个问题具有特定于数据集的解决方案,第二个问题涉及start 与offset 一起提供时glm 的失败。
我有以下数据集:
library(data.table)
df <- data.frame(names = factor(1:10))
set.seed(0)
df$probs <- c(0, 0, runif(8, 0, 1))
df$response = lapply(df$probs, function(i){
rbinom(50, 1, i)
})
dt <- data.table(df)
dt <- dt[, list(response = unlist(response)), by = c('names', 'probs')]
这样dt 是:
> dt
names probs response
1: 1 0.0000000 0
2: 1 0.0000000 0
3: 1 0.0000000 0
4: 1 0.0000000 0
5: 1 0.0000000 0
---
496: 10 0.9446753 0
497: 10 0.9446753 1
498: 10 0.9446753 1
499: 10 0.9446753 1
500: 10 0.9446753 1
我正在尝试使用 lm2 <- glm(data = dt, formula = response ~ probs, family = binomial(link='identity')) 将逻辑回归模型与身份链接拟合。
这给出了一个错误:
Error: no valid set of coefficients has been found: please supply starting values
我尝试通过提供 start 参数来修复它,但随后又出现了另一个错误。
> lm2 <- glm(data = dt, formula = response ~ probs, family = binomial(link='identity'), start = c(0, 1))
Error: cannot find valid starting values: please specify some
在这一点上,这些错误对我来说毫无意义,我不知道该怎么办。
编辑:@iraserd 对这个问题有了更多的了解。使用start = c(0.5, 0.5),我得到:
> lm2 <- glm(data = dt, formula = response ~ probs, family = binomial(link='identity'), start = c(0.5, 0.5))
There were 25 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: step size truncated: out of bounds
2: step size truncated: out of bounds
3: step size truncated: out of bounds
4: step size truncated: out of bounds
5: step size truncated: out of bounds
6: step size truncated: out of bounds
7: step size truncated: out of bounds
8: step size truncated: out of bounds
9: step size truncated: out of bounds
10: step size truncated: out of bounds
11: step size truncated: out of bounds
12: step size truncated: out of bounds
13: step size truncated: out of bounds
14: step size truncated: out of bounds
15: step size truncated: out of bounds
16: step size truncated: out of bounds
17: step size truncated: out of bounds
18: step size truncated: out of bounds
19: step size truncated: out of bounds
20: step size truncated: out of bounds
21: step size truncated: out of bounds
22: step size truncated: out of bounds
23: step size truncated: out of bounds
24: step size truncated: out of bounds
25: glm.fit: algorithm stopped at boundary value
和
> summary(lm2)
Call:
glm(formula = response ~ probs, family = binomial(link = "identity"),
data = dt, start = c(0.5, 0.5))
Deviance Residuals:
Min 1Q Median 3Q Max
-2.4023 -0.6710 0.3389 0.4641 1.7897
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.486e-08 1.752e-06 0.008 0.993
probs 9.995e-01 2.068e-03 483.372 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 69312 on 49999 degrees of freedom
Residual deviance: 35984 on 49998 degrees of freedom
AIC: 35988
Number of Fisher Scoring iterations: 24
我高度怀疑这与以下事实有关,即某些响应是以真正的概率为零生成的,这会导致probs 的系数接近 1 时出现问题。
【问题讨论】: