rglm

Is there a way to obtain coefficients for each step of the optimization algorithm in glm function?


When one performs a logit regression in R, it is possible to obtain coefficients after the optimization algorithm has converged (or not) with coefficients() function:

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

Is there a way to obtain coefficients for each step of the optimization algorithm to trace its steps?


Solution

  • The internals of glm.fit have changed (see comment from @John) so use this instead. It does not rely on line positions of the internals but rather intercepts each instance of cat in glm.fit and adds a message to iteration message so although it still depends on the internals it should be a bit less fragile. This worked for me in R 4.1 and 4.2.

    library(MASS)
    data(menarche)
    
    trace(glm.fit, quote(cat <- function(...) {
      base::cat(...)
      if (...length() >= 3 && identical(..3, " Iterations - ")) print(coefold)
    }))
    glm.out = glm(cbind(Menarche, Total-Menarche) ~ Age,
                         family=binomial(logit), data=menarche,
                         control = glm.control(trace = TRUE))
    untrace(glm.fit)
    

    Previous solution

    The control= argument with the value shown causes the deviance to print and the trace statement will cause the coefficient values to print:

    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))
    

    The output will look like this:

    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
    

    To remove the trace use:

    untrace(glm.fit)
    

    Note that in the trace call, coefold is the name of a variable used internally in glm.fit source code and the numbers used refer to statement numbers in the source code and so either could need to be changed if glm.fit source changes. I am using "R version 3.2.2 Patched (2015-10-19 r69550)".