RStudio中逐步多元线性回归运行停滞问题求助
问题
我有6个自变量和5个因变量,用R代码实现逐步多元线性回归:每次回归后移除p值>0.05的自变量,循环到仅保留p值<0.05的变量。代码对前3个因变量运行正常,但处理第4个因变量输出最终回归摘要时停滞;用示例数据测试时,仅处理第一个因变量就出现停滞。我已经用Excel手动完成所有回归并得到有效结果,附上示例数据和代码,请求排查问题。
示例数据
df <- structure(list(IndVar1 = c(114156L, 93447L, 113909L, 103983L, 111403L, 93071L, 115366L, 98578L, 98487L, 105592L, 102727L, 108970L, 112604L, 103223L, 95176L, 100051L), IndVar2 = c(131937L, 128287L, 108688L, 94270L, 137057L, 100127L, 130482L, 109484L, 97535L, 139141L, 122752L, 124523L, 111948L, 92846L, 104659L, 112592L), IndVar3 = c(10967L, 9733L, 9485L, 10251L, 9426L, 6040L, 7413L, 6648L, 9862L, 6605L, 9364L, 8749L, 8621L, 10285L, 9700L, 8926L), IndVar4 = c(16260L, 13994L, 14936L, 11645L, 15831L, 11916L, 11227L, 14199L, 16051L, 15330L, 14205L, 13756L, 14175L, 12530L, 15572L, 15048L), IndVar5 = c(9772L, 8682L, 6849L, 10667L, 9223L, 7591L, 10003L, 7660L, 6431L, 8149L, 9111L, 10551L, 8448L, 10296L, 6291L, 10131L), IndVar6 = c(12643L, 15848L, 11522L, 11114L, 15946L, 16181L, 13360L, 11343L, 12682L, 13412L, 16175L, 11187L, 16494L, 14926L, 14569L, 12954L), DepVar1 = c(1674605L, 1731694L, 1910052L, 1955383L, 1698939L, 1563494L, 1573640L, 1836676L, 1726679L, 1768668L, 1679431L, 1890720L, 1837590L, 1677332L, 1794722L, 1715671L), DepVar2 = c(12721L, 12169L, 9924L, 4358L, 9992L, 5290L, 11456L, 10956L, 7254L, 4089L, 12761L, 10360L, 4999L, 5387L, 8468L, 7446L), DepVar3 = c(2.55, 1.73, 1.08, 0.93, 1.88, 1.77, 2.62, 2.13, 2.32, 1.68, 0.99, 1.84, 1.4, 1.34, 0.93, 2), DepVar4 = c(42820L, 28394L, 38820L, 50756L, 30729L, 39528L, 30783L, 39551L, 38429L, 46858L, 29727L, 52171L, 37714L, 30164L, 43782L, 43410L), DepVar5 = c(2.7, 2.59, 1.93, 1.48, 2.03, 1.81, 1.63, 1.53, 2.7, 1.87, 2.33, 2.15, 2.09, 2.35, 2.48, 1.54)), class = "data.frame", row.names = c(NA, -16L))
原代码
# Function to perform stepwise regression perform_stepwise_regression <- function(dependent_variable, independent_variables, data) { cat("====================================================================== ") cat("Dependent Variable: ", dependent_variable, " ") cat("====================================================================== ") # Fit initial model lm_model <- lm(as.formula(paste(dependent_variable, "~", paste(independent_variables, collapse = " + "))), data = df) while (TRUE) { # Check p-values p_values <- summary(lm_model)$coefficients[, 4] # Identify variables with the highest p-value > 0.05 variable_to_remove <- names(p_values[p_values == max(p_values)]) # Break the loop if no variable has a p-value > 0.05 if (max(p_values) <= 0.05) { break } # Create a new formula excluding the variable predictors_to_include <- setdiff(names(lm_model$model)[-1], variable_to_remove) new_formula <- as.formula(paste(dependent_variable, "~", paste(predictors_to_include, collapse = " + "))) # Fit a new model lm_model <- lm(new_formula, data = df) } # Create the final formula using the single remaining independent variable final_formula <- as.formula(paste(dependent_variable, "~", predictors_to_include)) # Fit the final model using the created formula final_model <- lm(final_formula, data = df) # Print the final summary in the console print(summary(lm_model)) } # Example usage: dependent_variables <- c("DepVar1", "DepVar2", "DepVar3", "DepVar4", "DepVar5") independent_variables <- c("IndVar1", "IndVar2", "IndVar3", "IndVar4", "IndVar5", "IndVar6") for (dependent_variable in dependent_variables) { perform_stepwise_regression(dependent_variable, independent_variables, df) }
问题排查与修复
核心问题分析
- 截距项干扰死循环:原代码提取的p值包含截距项,当所有自变量p值都达标时,截距项的p值可能大于0.05,导致循环无法终止。
- 多变量移除逻辑错误:当多个自变量有相同最大p值时,
variable_to_remove会变成向量,setdiff处理时可能意外删除多个变量,引发后续模型构建异常。 - 冗余的模型拟合:循环结束后已得到最终模型,无需重复拟合;且
predictors_to_include在循环外可能未正确赋值(如初始模型就满足终止条件时)。
修复后的代码
# 逐步多元线性回归函数 perform_stepwise_regression <- function(dependent_variable, independent_variables, data) { cat("======================================================================\n") cat("因变量: ", dependent_variable, "\n") cat("======================================================================\n") # 初始化当前保留的自变量 current_predictors <- independent_variables # 拟合初始模型 lm_model <- lm(as.formula(paste(dependent_variable, "~", paste(current_predictors, collapse = " + "))), data = data) while (TRUE) { # 提取自变量的p值(排除截距项) coeff_summary <- summary(lm_model)$coefficients if (nrow(coeff_summary) <= 1) { # 只剩截距项时终止 break } p_values <- coeff_summary[-1, 4] max_p <- max(p_values, na.rm = TRUE) # 所有自变量p值都达标,终止循环 if (max_p <= 0.05) { break } # 仅移除第一个p值最大的自变量 variable_to_remove <- names(p_values)[p_values == max_p][1] # 更新保留的自变量列表 current_predictors <- setdiff(current_predictors, variable_to_remove) # 拟合新模型 lm_model <- lm(as.formula(paste(dependent_variable, "~", paste(current_predictors, collapse = " + "))), data = data) } # 输出最终模型结果 print(summary(lm_model)) } # 运行示例 dependent_variables <- c("DepVar1", "DepVar2", "DepVar3", "DepVar4", "DepVar5") independent_variables <- c("IndVar1", "IndVar2", "IndVar3", "IndVar4", "IndVar5", "IndVar6") for (dep_var in dependent_variables) { perform_stepwise_regression(dep_var, independent_variables, df) }
修复说明
- 排除截距项干扰:提取p值时直接跳过截距项,只关注自变量的显著性。
- 单变量移除逻辑:当多个自变量p值相同时,仅移除第一个,避免一次删除多个变量导致模型不稳定。
- 清晰的变量追踪:用
current_predictors直接管理当前保留的自变量,避免依赖模型内部的变量名,逻辑更直观。 - 增加边界终止条件:当模型只剩截距项时主动终止循环,彻底避免死循环。
内容的提问来源于stack exchange,提问作者Doctor K
相关产品推荐
相关产品推荐

