【问题标题】:What do these R glm error messages mean: "Error: no valid set of coefficients has been found: please supply starting values"这些 R glm 错误消息是什么意思:“错误:未找到有效的系数集:请提供起始值”
【发布时间】:2016-06-07 16:19:00
【问题描述】:

这里有两个相关的问题,但它们不是我的重复问题,因为第一个问题具有特定于数据集的解决方案,第二个问题涉及startoffset 一起提供时glm 的失败。

https://stackoverflow.com/questions/31342637/error-please-supply-starting-valueshttps://stackoverflow.com/questions/8212063/r-glm-starting-values-not-accepted-log-link

我有以下数据集:

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 &lt;- 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 时出现问题。

【问题讨论】:

    标签: r glm


    【解决方案1】:

    fit.glm 代码中有两个地方以错误 no valid set of coefficients has been found: please supply starting values 终止。在一种情况下,当某些计算的偏差变得无限时,另一种情况似乎是在提供无效的etastartmustart 选项时发生。

    另见答案,详细阐述:How do I use a custom link function in glm?

    当您尝试对概率(0 到 1 之间的值)进行回归时,我想您需要指定不等于 0 或 1 的起始值:

    lm2 <- glm(data = dt, formula = response ~ probs, family = binomial(link='identity'), start=c(0.5,0.5))
    

    这会引发大量警告并以溢出终止,可能是因为示例的人为性质。

    更改公式以使用 logit 链接(因为您希望根据您的问题进行逻辑回归)消除警告(并且不需要起始参数):

        lm2 <- glm(data = dt, formula = response ~ probs, family = binomial(link='logit')
    

    【讨论】:

    • glm 的帮助完全令人困惑:start: starting values for the parameters in the linear predictor. 我认为这些是对协变量系数的初步猜测。事实上,如果你只提供 1 个起始值,你会得到这个错误:length of 'start' should equal 2 and correspond to initial coefs for c("(Intercept)", "probs") 我的初始起始值没有理由失败,因为数据(以及你注意到做作的)使用这些值。
    • @Alex 如果您提供有关真实数据的信息会更容易。这个例子看起来像一个面板集,pglm 更适合?
    • 我的真实数据可能有点过于嘈杂,无法在此处显示。我正在模拟一个回归,我希望估计与类别级别相关的概率,并且当实际概率非常接近于零时我遇到了问题。
    • @Alex 使用“logit”链接拟合逻辑回归不会产生错误 - 为什么特别需要“身份”链接?我从未在 ?family 中使用过它,它没有将“身份”列为二项式的有效链接函数。此外,如果您想估计类别概率,为什么不对 names 使用虚拟变量方法?
    • 您建议的最终模型不是 OP 所模拟的模型。
    【解决方案2】:

    irased 认为错误可能来自herehere。两者都在迭代重新加权最小二乘的主循环中。

    第一次检查可以失败任何偏差不是有限的。在您的情况下(以及与二项式系列的所有链接函数),这些来自binomial("identity")$dev.resids,它调用this C function。如果平均值 mu 超出 (0,1)(即超出有效范围),这在某些情况下可以将 log 评估为负值。

    如果任何线性预测变量 eta 或平均值 mu 无效,我们将到达第二个分支,并且我们处于第一次迭代中,在这种情况下 coefoldNULL

    if (!(valideta(eta) && validmu(mu))) {
      if(is.null(coefold))
        stop("no valid set of coefficients has been found: please supply starting values", call. = FALSE)
      # ...
    }
    

    看看你正在使用的家庭,validetavalidmu

    with(binomial("identity"), {
        print(valideta)
        print(validmu)
    })
    #R> function (eta) 
    #R> TRUE
    #R> <environment: namespace:stats>
    #R> function (mu) 
    #R> all(is.finite(mu)) && all(mu > 0 & mu < 1)
    #R> <bytecode: 0x55de9ffd4448>
    #R> <environment: 0x55dea8ee2418>
    

    这是有道理的,因为概率,手段,必须在 (0,1) 之间。因此,我们可以得出结论,在迭代重新加权最小二乘期间,某些均值必须在某个点超出 (0,1) 范围。

    您使用的链接函数不保证均值在 (0,1) 范围内,因为反向链接函数是

    binomial("identity")$linkinv
    #R> function (eta) 
    #R> eta
    #R> <environment: namespace:stats>
    

    这是你的问题。不保证或签入glm 以确保一切都有效。但是,某些链接功能始终满足此约束。指定起始值可能使您在迭代重新加权最小二乘期间不会进入均值无效的区域。

    我高度怀疑这与以下事实有关,即某些响应以真正的概率为零生成,这会导致probs 的系数接近 1 时出现问题。

    是的,这正是问题所在。只需将您的示例替换为

    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')]
    
    tmp <- dt$probs
    tmp <- pmin(pmax(tmp, .Machine$double.eps), 1 - .Machine$double.eps)
    dt$probs_logit <- log(tmp / (1 - tmp))
    fit <- glm(data = dt, formula = response ~ probs_logit - 1, family = binomial("logit"))
    #R> Warning message:
    #R> glm.fit: fitted probabilities numerically 0 or 1 occurred
    summary(fit)
    #R> 
    #R> Call:
    #R> glm(formula = response ~ probs_logit - 1, family = binomial("logit"), 
    #R>     data = dt)
    #R> 
    #R> Deviance Residuals: 
    #R>     Min       1Q   Median       3Q      Max  
    #R> -2.4320  -0.6616   0.0000   0.4519   1.8038  
    #R> 
    #R> Coefficients:
    #R>             Estimate Std. Error z value Pr(>|z|)    
    #R> probs_logit  1.02336    0.09468   10.81   <2e-16 ***
    #R> ---
    #R> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    #R> 
    #R> (Dispersion parameter for binomial family taken to be 1)
    #R> 
    #R>     Null deviance: 693.15  on 500  degrees of freedom
    #R> Residual deviance: 355.18  on 499  degrees of freedom
    #R> AIC: 357.18
    #R> 
    #R> Number of Fisher Scoring iterations: 8
    #R> 
    

    给您一个警告,但允许您在截断和转换概率后从几乎正确的模型进行模拟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2015-06-21
      • 1970-01-01
      相关资源
      最近更新 更多