原题要求是用逐步回归法确定一个线性模型
以下是原代码
# 安装并加载leaps包
install.packages("leaps")
library(leaps)
# 输入数据
x1 <- c(7, 1, 11, 11, 7, 11, 3, 1, 2, 21, 1, 11, 10)
x2 <- c(26, 29, 56, 31, 52, 55, 71, 31, 54, 47, 40, 66, 68)
x3 <- c(6, 15, 8, 8, 6, 9, 17, 22, 18, 4, 23, 9, 8)
x4 <- c(60, 52, 20, 47, 33, 22, 6, 44, 22, 26, 34, 12, 12)
y <- c(78.5, 74.3, 104.3, 87.6, 95.9, 109.2, 102.7, 72.5, 93.1,
115.9, 83.8, 113.3, 109.4)
# 将数据转换为矩阵格式
x <- cbind(x1, x2, x3, x4)
colnames(x) <- c("x1", "x2", "x3", "x4")
# 进行逐步回归分析
model <- regsubsets(y ~ ., data = as.data.frame(x), nvmax = 4)
summary(model)
# 输出最优模型
best_model_index <- which.min(model$cp)
best_model <- model[best_model_index, ]
print(best_model)
这是报错信息:Error in model[best_model_index, ] : 量度数目不对