有没有办法在glm函数中获取优化算法每一步的系数?
Is there a way to obtain coefficients for each step of the optimization algorithm in glm function?
当在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
有没有办法获取优化算法每一步的系数来追踪其步骤?
glm.fit 的内部结构已更改(请参阅@John 的评论),因此请改用它。它不依赖于内部结构的行位置,而是拦截 glm.fit 中的每个 cat 实例,并将消息添加到迭代消息中,因此尽管它仍然依赖于内部结构,但它应该不那么脆弱。这在 R 4.1 和 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)
上一个解决方案
具有显示值的 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 版本 3.2.2 已修补 (2015-10-19 r69550)”。
当在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
有没有办法获取优化算法每一步的系数来追踪其步骤?
glm.fit 的内部结构已更改(请参阅@John 的评论),因此请改用它。它不依赖于内部结构的行位置,而是拦截 glm.fit 中的每个 cat 实例,并将消息添加到迭代消息中,因此尽管它仍然依赖于内部结构,但它应该不那么脆弱。这在 R 4.1 和 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)
上一个解决方案
具有显示值的 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 版本 3.2.2 已修补 (2015-10-19 r69550)”。