R语言logistic回归如何同时输出系数与自由度等拟合指标
原因说明
你现有代码的最后两行仅提取了模型汇总结果中的回归系数列,丢弃了自由度、偏差、AIC等其他拟合指标,因此只能输出系数结果。
修改后的代码
直接调用summary()输出完整的glm模型汇总结果即可,如需匹配你要求的4位小数精度,可按如下方式修改:
# 需提前加载用到的依赖包 library(dplyr) library(ggplot2) # msleep数据集来自ggplot2包 data = filter(msleep,vore =='carni' | vore == 'herbi') data$vore = ifelse(data$vore == 'carni',0,1) up1 = glm(formula = vore~sleep_total, data, family=binomial()) # 先将系数四舍五入到4位小数 up1$coefficients <- round(up1$coefficients,4) # 输出完整拟合指标 summary(up1)
如果需要完全自定义输出格式和你给出的示例完全对齐,也可以手动拼接各指标输出:
cat("Coefficients:\n") print(round(coef(up1),4)) cat(paste0("Degrees of Freedom: ", up1$df.null, " Total (i.e. Null); ", up1$df.residual, " Residual\n")) cat(paste0("Null Deviance:\t", round(up1$null.deviance,2), "\n")) cat(paste0("Residual Deviance: ", round(up1$deviance,2), "\tAIC: ", round(up1$aic,2), "\n"))
内容的提问来源于stack exchange,提问作者max
相关产品推荐
相关产品推荐

