在进行以下代码时:# 50*30, 30个变量 set.seed(1) X = matrix(rnorm(1500), nrow = 50, ncol = 30) Y = rnorm(50) data = data.frame(Y, X) # 原始模型为线性,给出三组不同的原始模型系数 lm.fit1 = lm(Y ~ X[,1], data = data) lm.fit2 = lm(Y ~ X[,1:15], data = data) lm.fit3 = lm(Y ~ X, data = data) # 计算CV值 library(boot) cv.error1 = cv.glm(data[,c("Y", "X[,1]")], lm.fit1)$delta[1] cv.error2 = cv.glm(data[,c("Y", paste0("X[,",1:15,"]"))], lm.fit2)$delta[1] cv.error3 = cv.glm(data[,c("Y", paste0("X[,",1:30,"]"))], lm.fit3)$delta[1]。出现了以下错误:Error in [.data.frame(data, , c("Y", "X[,1]")) : 选择了未定义的列。请对原代码进行修改
时间: 2024-01-14 20:02:26 浏览: 86
acca
在计算CV值时,需要将X的列名改为字符串形式,使用字符串拼接函数paste0时需要在括号外加引号,修改后的代码如下:
```
library(boot)
cv.error1 = cv.glm(data[,c("Y", "X[,1]")], lm.fit1)$delta[1]
cv.error2 = cv.glm(data[,c("Y", paste0("X[,",1:15,"]"))], lm.fit2)$delta[1]
cv.error3 = cv.glm(data[,c("Y", paste0("X[,",1:30,"]"))], lm.fit3)$delta[1]
```
阅读全文