当在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
Run Code Online (Sandbox Code Playgroud)
有没有办法获得优化算法的每个步骤的系数来跟踪其步骤?
显示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))
Run Code Online (Sandbox Code Playgroud)
输出将如下所示:
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
Run Code Online (Sandbox Code Playgroud)
要删除跟踪使用:
untrace(glm.fit)
Run Code Online (Sandbox Code Playgroud)
请注意,在trace调用中,coefold是glm.fit源代码内部使用的变量的名称,使用的数字是指源代码中的语句编号,因此如果glm.fit源更改,则可能需要更改.我正在使用"R版本3.2.2 Patched(2015-10-19 r69550)".