R语言GLM模型summary未显示Deviance Residuals的问题咨询
解决GLM模型summary输出中添加偏差残差统计的问题
要让glm模型的summary()输出默认包含**偏差残差(Deviance Residuals)**的五数统计(最小值、第一四分位数、中位数、第三四分位数、最大值),可以通过两种方式实现:
方法一:覆盖默认的print.summary.glm函数
直接修改R中默认的glm摘要打印函数,让它在输出中自动加入偏差残差部分:
# 定义新的print.summary.glm函数,覆盖默认行为 print.summary.glm <- function (x, digits = max(3, getOption("digits") - 3), symbolic.cor = x$symbolic.cor, signif.stars = getOption("show.signif.stars"), ...) { # 打印模型调用语句 cat("\nCall:\n", paste(deparse(x$call), sep = "\n", collapse = "\n"), "\n\n", sep = "") # 新增:计算并打印偏差残差统计 dev_resid <- resid(x$model, type = "deviance") cat("Deviance Residuals: \n") print(summary(dev_resid)[c("Min.", "1st Qu.", "Median", "3rd Qu.", "Max.")], digits = digits) cat("\n") # 以下为原默认函数的其余逻辑,保持不变 if (length(x$aliased) == 0) { cat("No Coefficients\n") } else { if (!is.null(x$coefficients)) { coefs <- x$coefficients if (is.matrix(coefs)) { printCoefmat(coefs, digits = digits, signif.stars = signif.stars, na.print = "NA", ...) } else { cat("Coefficients:\n") print(coefs, digits = digits) } } if (length(x$aliased) > 0) { cat("\n(Dispersion parameter for ", x$family$family, " family taken to be ", format(x$dispersion), ")\n\n", sep = "") df <- x$df if (df[2] > 0) { cat(" Null deviance: ", format(signif(x$null.deviance, digits)), " on ", df[1], " degrees of freedom\n", sep = "") cat("Residual deviance: ", format(signif(x$deviance, digits)), " on ", df[2], " degrees of freedom\n", sep = "") } else { cat("Residual deviance: ", format(signif(x$deviance, digits)), " on ", df[2], " degrees of freedom\n", sep = "") cat("(Null deviance undefined for the multinomial family)\n", sep = "") } cat("AIC: ", format(signif(x$aic, digits)), "\n\n", sep = "") if (!is.null(x$iter)) { cat("Number of ", x$method, " iterations: ", x$iter, "\n", sep = "") } if (!is.null(x$na.action)) { cat("\n", x$na.action, "\n", sep = "") } if (!is.null(x$correlation) && symbolic.cor) { cat("\nCorrelation of Coefficients:\n") tmp <- x$correlation diag(tmp) <- NA print(tmp, digits = digits, na.print = "") } else if (!is.null(x$correlation)) { cat("\nCorrelation matrix not shown by default, as it is symmetric.\n") cat("Use print(x, correlation=TRUE) to see it.\n") } } } invisible(x) }
使用方式
运行上述函数后,直接调用summary(model)即可得到包含偏差残差的输出:
model <- glm(am ~ disp + hp, data=mtcars, family=binomial) summary(model)
方法二:自定义打印函数(不修改全局默认行为)
如果不想覆盖全局的print.summary.glm函数,可以写一个自定义函数来组合输出:
# 自定义打印函数,包含偏差残差 print_glm_with_devresid <- function(model_summary) { # 打印模型调用语句 cat("\nCall:\n", paste(deparse(model_summary$call), sep = "\n", collapse = "\n"), "\n\n", sep = "") # 计算并打印偏差残差统计 dev_resid <- resid(model_summary$model, type = "deviance") cat("Deviance Residuals: \n") print(summary(dev_resid)[c("Min.", "1st Qu.", "Median", "3rd Qu.", "Max.")], digits = max(3, getOption("digits") - 3)) cat("\n") # 打印系数表 if (!is.null(model_summary$coefficients)) { coefs <- model_summary$coefficients if (is.matrix(coefs)) { printCoefmat(coefs, digits = max(3, getOption("digits") - 3), signif.stars = getOption("show.signif.stars"), na.print = "NA") } else { cat("Coefficients:\n") print(coefs, digits = max(3, getOption("digits") - 3)) } } # 打印分散参数、偏差值、AIC等信息 cat("\n(Dispersion parameter for ", model_summary$family$family, " family taken to be ", format(model_summary$dispersion), ")\n\n", sep = "") df <- model_summary$df if (df[2] > 0) { cat(" Null deviance: ", format(signif(model_summary$null.deviance, max(3, getOption("digits") - 3))), " on ", df[1], " degrees of freedom\n", sep = "") cat("Residual deviance: ", format(signif(model_summary$deviance, max(3, getOption("digits") - 3))), " on ", df[2], " degrees of freedom\n", sep = "") } else { cat("Residual deviance: ", format(signif(model_summary$deviance, max(3, getOption("digits") - 3))), " on ", df[2], " degrees of freedom\n", sep = "") cat("(Null deviance undefined for the multinomial family)\n", sep = "") } cat("AIC: ", format(signif(model_summary$aic, max(3, getOption("digits") - 3))), "\n\n", sep = "") # 打印迭代次数 if (!is.null(model_summary$iter)) { cat("Number of ", model_summary$method, " iterations: ", model_summary$iter, "\n", sep = "") } invisible(model_summary) }
使用方式
先生成模型摘要,再调用自定义函数:
model <- glm(am ~ disp + hp, data=mtcars, family=binomial) mod_sum <- summary(model) print_glm_with_devresid(mod_sum)
内容的提问来源于stack exchange,提问作者Nauru
相关产品推荐
相关产品推荐

