在运行以下R代码时:# 分别绘制三组岭回归的图 # 绘制第一组交叉验证误差图 ggplot(cv1$glmnet.fit$cvm, aes(x = seq_along(lambda), y = cvm)) + scale_x_reverse() + geom_line() + labs(title = "Cross-validation Error Plot for First Model") # 绘制第一组预测误差图 yhat1 <- predict(ridge1, s = cv1$lambda.1se, newx = X) ggplot(data.frame(y = y, yhat = yhat1), aes(x = y, y = yhat)) + geom_abline() + geom_point() + labs(title = "Predicted vs. Actual Plot for First Model") # 绘制第二组交叉验证误差图 ggplot(cv2$glmnet.fit$cvm, aes(x = seq_along(lambda), y = cvm)) + scale_x_reverse() + geom_line() + labs(title = "Cross-validation Error Plot for Second Model") # 绘制第二组预测误差图 yhat2 <- predict(ridge2, s = cv2$lambda.1se, newx = X) ggplot(data.frame(y = y, yhat = yhat2), aes(x = y, y = yhat)) + geom_abline() + geom_point() + labs(title = "Predicted vs. Actual Plot for Second Model") # 绘制第三组交叉验证误差图 ggplot(cv3$glmnet.fit$cvm, aes(x = seq_along(lambda), y = cvm)) + scale_x_reverse() + geom_line() + labs(title = "Cross-validation Error Plot for Third Model") # 绘制第三组预测误差图 yhat3 <- predict(ridge3, s = cv3$lambda.1se, newx = X) ggplot(data.frame(y = y, yhat = yhat3), aes(x = y, y = yhat)) + geom_abline() + geom_point() + labs(title = "Predicted vs. Actual Plot for Third Model")。发生以下问题:Error in `geom_line()`: ! Problem while computing aesthetics. ℹ Error occurred in the 1st layer. Caused by error in `FUN()`: ! object 'cvm' not found Run `rlang::last_trace()` to see where the error occurred.。请对原代码进行修正
时间: 2023-12-12 14:06:01 浏览: 59
这个问题的原因是在绘制交叉验证误差图和预测误差图时,R无法找到`cvm`变量。可能是因为没有正确地定义`cv1$glmnet.fit$cvm`,`cv2$glmnet.fit$cvm`和`cv3$glmnet.fit$cvm`。
修正代码如下:
```R
# 分别绘制三组岭回归的图
# 绘制第一组交叉验证误差图
ggplot(data = data.frame(lambda = cv1$glmnet.fit$lambda, cvm = cv1$glmnet.fit$cvm),
aes(x = log(lambda), y = cvm)) +
geom_line() +
scale_x_reverse() +
labs(title = "Cross-validation Error Plot for First Model")
# 绘制第一组预测误差图
yhat1 <- predict(ridge1, s = cv1$glmnet.fit$lambda.1se, newx = X)
ggplot(data.frame(y = y, yhat = yhat1), aes(x = y, y = yhat)) +
geom_abline() +
geom_point() +
labs(title = "Predicted vs. Actual Plot for First Model")
# 绘制第二组交叉验证误差图
ggplot(data = data.frame(lambda = cv2$glmnet.fit$lambda, cvm = cv2$glmnet.fit$cvm),
aes(x = log(lambda), y = cvm)) +
geom_line() +
scale_x_reverse() +
labs(title = "Cross-validation Error Plot for Second Model")
# 绘制第二组预测误差图
yhat2 <- predict(ridge2, s = cv2$glmnet.fit$lambda.1se, newx = X)
ggplot(data.frame(y = y, yhat = yhat2), aes(x = y, y = yhat)) +
geom_abline() +
geom_point() +
labs(title = "Predicted vs. Actual Plot for Second Model")
# 绘制第三组交叉验证误差图
ggplot(data = data.frame(lambda = cv3$glmnet.fit$lambda, cvm = cv3$glmnet.fit$cvm),
aes(x = log(lambda), y = cvm)) +
geom_line() +
scale_x_reverse() +
labs(title = "Cross-validation Error Plot for Third Model")
# 绘制第三组预测误差图
yhat3 <- predict(ridge3, s = cv3$glmnet.fit$lambda.1se, newx = X)
ggplot(data.frame(y = y, yhat = yhat3), aes(x = y, y = yhat)) +
geom_abline() +
geom_point() +
labs(title = "Predicted vs. Actual Plot for Third Model")
```
在修正后的代码中,我们将`cv1$glmnet.fit$cvm`、`cv2$glmnet.fit$cvm`和`cv3$glmnet.fit$cvm`变量放到了绘图数据框中,并且使用了正确的变量名`lambda`和`cvm`。同时,我们还将横坐标改为了对数坐标,以更好地显示不同的正则化参数对模型的影响。
阅读全文