R语言anova()比较函数异常:无法读取第二个模型数据
R中
anova()模型比较异常问题 问题背景
在RStudio中使用anova()函数进行模型比较,此前功能运行正常,但修改变量名后出现异常。构建的两个线性回归模型单独运行均可输出正确结果,但执行模型比较时输出不完整,同时出现警告信息。
重现代码
# Fit Model 1 model1 <- lm(log_Income2005 ~ Educ16plus, data = grade_income) # Calculate and display the ANOVA results for Model 1 cat("\n\nModel 1 ANOVA Results: ------------------------------------------\n") model1_result <- anova(model1) model1_result # Extract R-squared, F-statistic, and Residual standard error for Model 2 r_squared_model1 <- summary(model1)$r.squared f_statistic_model1 <- summary(model1)$fstatistic[1] residual_standard_error_model1 <- summary(model1)$sigma # Display F-statistic and Residual standard error for Model 2 cat("F-Statistic for Model 1:", f_statistic_model1, "\n") cat("Residual Standard Error for Model 1:", residual_standard_error_model1, "\n") cat("R-Squared for Model 1:", r_squared_model1, "\n") ############################################################# # Fit Model 2 model2 <- lm(log_Income2005 ~ Educ16, data = grade_income) # Calculate and display the ANOVA results for Model 2 cat("\n\nModel 2 ANOVA Results: ------------------------------------------\n") model2_result <- anova(model2) model2_result # Extract R-squared, F-statistic, and Residual standard error for Model 2 r_squared_model2 <- summary(model2)$r.squared f_statistic_model2 <- summary(model2)$fstatistic[1] residual_standard_error_model2 <- summary(model2)$sigma # Display F-statistic and Residual standard error for Model 2 cat("F-Statistic for Model 2:", f_statistic_model2, "\n") cat("Residual Standard Error for Model 2:", residual_standard_error_model2, "\n") cat("R-Squared for Model 2:", r_squared_model2, "\n") ############################################################## # Perform an ANOVA comparison and display the results cat("\n\nANOVA Comparison of Model's 1 & 2: --------------------------------\n") compare1_2_result <- anova(model1, model2) compare1_2_result
异常输出与警告
模型比较时输出不完整:
Analysis of Variance Table Model 1: log_Income2005 ~ Educ16plus Model 2: log_Income2005 ~ Educ16 Res.Df RSS Df Sum of Sq F Pr(>F) 1 2582 2357.2 2 2582 2387.6 0 -30.399
同时出现两个警告:
Warning: Ignoring unknown parameters: Educ2Warning: longer object length is not a multiple of shorter object length
已尝试的解决方法
已尝试清除缓存、重启RStudio、重命名变量/文件、新建项目/会话、更新RStudio等方法,均未解决问题,怀疑非代码本身问题,寻求技术支持。
内容的提问来源于stack exchange,提问作者Adam E
相关产品推荐
相关产品推荐

