【问题标题】:Is there a way to obtain coefficients for each step of the optimization algorithm in glm function?有没有办法在 glm 函数中获取优化算法的每一步的系数?
【发布时间】:2016-01-24 02:09:30
【问题描述】:

在 R 中执行 logit 回归时,可以在优化算法收敛(或不收敛)coefficients() 函数后获得系数:

library(MASS)
data(menarche)
glm.out = glm(cbind(Menarche, Total-Menarche) ~ Age,
               family=binomial(logit), data=menarche)
coefficients(glm.out)
## (Intercept)         Age 
## -21.226395    1.631968

有没有办法为优化算法的每一步获取系数以跟踪其步骤?

【问题讨论】:

    标签: r glm


    【解决方案1】:

    带有显示值的control= 参数导致打印偏差,trace 语句将导致打印系数值:

    trace(glm.fit, quote(print(coefold)), at = list(c(22, 4, 8, 4, 19, 3)))
    glm.out = glm(cbind(Menarche, Total-Menarche) ~ Age,
                         family=binomial(logit), data=menarche,
                         control = glm.control(trace = TRUE))
    

    输出将如下所示:

    Tracing glm.fit(x = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  .... step 22,4,8,4,19,3 
    NULL
    Deviance = 27.23412 Iterations - 1
    Tracing glm.fit(x = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  .... step 22,4,8,4,19,3 
    [1] -20.673652   1.589536
    Deviance = 26.7041 Iterations - 2
    Tracing glm.fit(x = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  .... step 22,4,8,4,19,3 
    [1] -21.206854   1.630468
    Deviance = 26.70345 Iterations - 3
    Tracing glm.fit(x = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  .... step 22,4,8,4,19,3 
    [1] -21.226370   1.631966
    Deviance = 26.70345 Iterations - 4
    

    要删除跟踪使用:

    untrace(glm.fit)
    

    请注意,在trace 调用中,coefold 是在glm.fit 源代码内部使用的变量的名称,并且使用的数字指的是源代码中的语句编号,因此如果出现以下情况,可能需要更改其中任何一个glm.fit 源更改。我正在使用“R version 3.2.2 Patched (2015-10-19 r69550)”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-18
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 2015-02-17
      • 1970-01-01
      • 2021-10-29
      相关资源
      最近更新 更多