基于以下R代码:# ①建立50×30的随机数据和30个变量 set.seed(123) X <- matrix(rnorm(50*30), ncol=30) y <- rnorm(50) # ②生成三组不同系数的线性模型 beta1 <- rnorm(30, mean=1, sd=0.5) beta2 <- rnorm(30, mean=2, sd=0.5) beta3 <- rnorm(30, mean=3, sd=0.5) # 定义一个函数用于计算线性回归的CV值 cv_linear <- function(X, y, k=10, lambda=NULL) { n <- nrow(X) if (is.null(lambda)) { lambda <- seq(0, 1, length.out=100) } mse <- rep(0, length(lambda)) folds <- sample(rep(1:k, length.out=n)) for (i in 1:k) { X_train <- X[folds!=i, ] y_train <- y[folds!=i] X_test <- X[folds==i, ] y_test <- y[folds==i] for (j in 1:length(lambda)) { fit <- glmnet(X_train, y_train, alpha=0, lambda=lambda[j]) y_pred <- predict(fit, newx=X_test) mse[j] <- mse[j] + mean((y_test - y_pred)^2) } } mse <- mse / k return(mse) } # ③(线性回归中)分别计算这三组的CV值 lambda <- seq(0, 1, length.out=100) mse1 <- cv_linear(X, y, lambda=lambda) mse2 <- cv_linear(X, y, lambda=lambda) mse3 <- cv_linear(X, y, lambda=lambda) # ④(岭回归中)分别画出这三组的两张图,每组两张图均以lambda为横坐标: library(glmnet) par(mfrow=c(2,3)) # 画Beta1的CV error图 plot(lambda, mse1, type="l", xlab="lambda", ylab="CV error", main="Beta1 CV error") # 画Beta1的Prediction error图 fit1 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse1)]) y_pred1 <- as.vector(predict(fit1, newx=X)) pred_error1 <- mean((y - y_pred1)^2) lambda <- as.vector(lambda) pred_error1 <- as.vector(pred_error1) if (length(lambda) != length(pred_error1)) { if (length(lambda) > length(pred_error1)) { pred_error1 <- rep(pred_error1, length.out = length(lambda)) } else { lambda <- rep(lambda, length.out = length(pred_error1)) } } plot(lambda, pred_error1, type="l", xlab="lambda", ylab="Prediction error", main="Beta1 Prediction error") # 画Beta2的CV error图 plot(lambda, mse2, type="l", xlab="lambda", ylab="CV error", main="Beta2 CV error") # 画Beta2的Prediction error图 fit2 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse2)]) y_pred2 <- predict(fit2, newx=X) pred_error2 <- mean((y - y_pred2)^2) plot(lambda, pred_error2, type="l", xlab="lambda", ylab="Prediction error", main="Beta2 Prediction error") # 画Beta3的CV error图 plot(lambda, mse3, type="l", xlab="lambda", ylab="CV error", main="Beta3 CV error") # 画Beta3的Prediction error图 fit3 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse3)]) y_pred3 <- predict(fit3, newx=X) pred_error3 <- mean((y - y_pred3)^2) plot(lambda, pred_error3, type="l", xlab="lambda", ylab="Prediction error", main="Beta3 Prediction error")。对每组的预测误差图进行代码修改
时间: 2024-02-22 17:59:55 浏览: 108
对每组的预测误差图进行代码修改,使其能够同时显示CV error和Prediction error:
```
par(mfrow=c(3,2))
# Beta1图:
fit1 <- glmnet(X, y, alpha=0, lambda=lambda)
y_pred1 <- as.vector(predict(fit1, newx=X))
pred_error1 <- mean((y - y_pred1)^2)
lambda <- as.vector(lambda)
pred_error1 <- as.vector(pred_error1)
if (length(lambda) != length(pred_error1)) {
if (length(lambda) > length(pred_error1)) {
pred_error1 <- rep(pred_error1, length.out = length(lambda))
} else {
lambda <- rep(lambda, length.out = length(pred_error1))
}
}
plot(lambda, mse1, type="l", xlab="lambda", ylab="CV error", main="Beta1 CV and Prediction error")
lines(lambda, pred_error1, col="red")
legend("topright", legend=c("CV error", "Prediction error"), col=c("black", "red"), lty=c(1,1), cex=0.8)
# Beta2图:
fit2 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse2)])
y_pred2 <- predict(fit2, newx=X)
pred_error2 <- mean((y - y_pred2)^2)
plot(lambda, mse2, type="l", xlab="lambda", ylab="CV error", main="Beta2 CV and Prediction error")
lines(lambda, pred_error2, col="red")
legend("topright", legend=c("CV error", "Prediction error"), col=c("black", "red"), lty=c(1,1), cex=0.8)
# Beta3图:
fit3 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse3)])
y_pred3 <- predict(fit3, newx=X)
pred_error3 <- mean((y - y_pred3)^2)
plot(lambda, mse3, type="l", xlab="lambda", ylab="CV error", main="Beta3 CV and Prediction error")
lines(lambda, pred_error3, col="red")
legend("topright", legend=c("CV error", "Prediction error"), col=c("black", "red"), lty=c(1,1), cex=0.8)
```
这样修改后,每组的图形都会同时显示CV error和Prediction error,方便比较不同lambda下的表现。
阅读全文