You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改R代码为GLM生成包含预测变量名的AIC表格

问题:为aictab生成的AIC表格添加预测变量名称

我在R Markdown中运行以下代码拟合一系列Gamma分布的GLM模型,但使用aictab生成的AIC表格仅显示模型编号(1至500+),无法直观看到每个模型对应的预测变量组合,需要修改代码添加这些变量名。

原代码:

# Specify the names of predictor variables
predictor_vars <- c("communitiesinpa", "formalprotection", "attitudewildlife", "competitionlivestock", "fenced", "parkresourcescat", "routinepatrols", "mitigationlivestockbarriers", "mitigationcommunityengagement")

# Create all possible combinations of predictor variables
all_combinations <- unlist(lapply(1:length(predictor_vars), function(x) combn(predictor_vars, x, simplify = FALSE)), recursive = FALSE)

# Create an empty list to store the GLM models
models_list <- list()

# Fit GLMs for all combinations and store them in the models_list
for (i in seq_along(all_combinations)) {
  formula_string <- paste("totalthreatscore ~", paste(all_combinations[[i]], collapse = "+"))
  formula_object <- as.formula(formula_string)
  model <- glm(formula_object, data = glmdata, family = Gamma)
  models_list[[i]] <- model
}

# Print the summary of each model
for (i in seq_along(models_list)) {
  cat("Model with predictors:", paste(all_combinations[[i]], collapse = " + "), "\n")
  print(summary(models_list[[i]]))
  cat("\n")
}

AIC_table <- aictab(models_list)

# Display the AIC table
knitr::kable(AIC_table)

解决方案

核心思路是给models_list中的每个模型添加可识别的名称(即对应预测变量组合),aictab会自动读取这些名称并显示在结果表格中。以下是修改后的完整代码:

# Specify the names of predictor variables
predictor_vars <- c("communitiesinpa", "formalprotection", "attitudewildlife", "competitionlivestock", "fenced", "parkresourcescat", "routinepatrols", "mitigationlivestockbarriers", "mitigationcommunityengagement")

# Create all possible combinations of predictor variables
all_combinations <- unlist(lapply(1:length(predictor_vars), function(x) combn(predictor_vars, x, simplify = FALSE)), recursive = FALSE)

# Create an empty list to store the GLM models
models_list <- list()

# Fit GLMs for all combinations and store them in the models_list
for (i in seq_along(all_combinations)) {
  formula_string <- paste("totalthreatscore ~", paste(all_combinations[[i]], collapse = "+"))
  formula_object <- as.formula(formula_string)
  model <- glm(formula_object, data = glmdata, family = Gamma)
  # 直接用预测变量组合作为模型名称
  model_name <- paste(all_combinations[[i]], collapse = " + ")
  models_list[[model_name]] <- model
}

# Print the summary of each model(可选,名称已包含变量信息)
for (model_name in names(models_list)) {
  cat("Model with predictors:", model_name, "\n")
  print(summary(models_list[[model_name]]))
  cat("\n")
}

# 生成带模型名称的AIC表格,aictab会自动读取models_list的名称
AIC_table <- aictab(models_list)

# 若自动读取失败,可手动指定modnames参数
# model_names <- sapply(all_combinations, function(x) paste(x, collapse = " + "))
# AIC_table <- aictab(models_list, modnames = model_names)

# Display the AIC table
knitr::kable(AIC_table)

关键修改说明

  1. 给模型列表元素命名:在拟合模型时,将每个模型对应的列表元素名称设置为该模型的预测变量组合字符串(如"communitiesinpa + formalprotection"),替代原来的数字索引。
  2. 适配模型摘要打印:循环打印摘要时,直接使用模型名称即可,无需再调用all_combinations。
  3. 备选手动指定名称:如果aictab未自动读取模型列表名称,可以提前生成所有模型名称的向量,通过modnames参数传入aictab。

内容的提问来源于stack exchange,提问作者lizanner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 08:55:24