用R写一个stacking代码,基模型用RF、SVM、XGBoost,对训练集做5折交叉验证,模型精度评价指标用R2、RMSE和MAE
时间: 2023-12-03 10:45:42 浏览: 94
以下是一个基于RandomForest、SVM和XGBoost的stacking代码,使用R2、RMSE和MAE作为评价指标,对训练集进行5折交叉验证:
```
library(caret)
library(randomForest)
library(e1071)
library(xgboost)
# 读取数据
data <- read.csv("data.csv")
# 拆分数据集为训练集和测试集
set.seed(123)
trainIndex <- createDataPartition(data$target, p = .8, list = FALSE)
train <- data[trainIndex, ]
test <- data[-trainIndex, ]
# 定义基模型
rf_model <- randomForest(target ~ ., data = train)
svm_model <- svm(target ~ ., data = train)
xgb_model <- xgboost(data = as.matrix(train[,1:9]), label = train$target, nrounds = 100, verbose = FALSE)
# 交叉验证
folds <- createFolds(train$target, k = 5, list = TRUE)
numFolds <- length(folds)
# 定义stacking训练集和测试集
stack_train <- data.frame(matrix(NA, nrow = nrow(train), ncol = 3))
colnames(stack_train) <- c("RF", "SVM", "XGB")
stack_test <- data.frame(matrix(NA, nrow = nrow(test), ncol = 3))
colnames(stack_test) <- c("RF", "SVM", "XGB")
# 训练基模型
for (i in 1:numFolds) {
trainIndex <- unlist(folds[i])
cv_train <- train[trainIndex, ]
cv_test <- train[-trainIndex, ]
# 训练RF
rf_pred <- predict(rf_model, cv_test)
stack_train[-trainIndex, "RF"] <- rf_pred
stack_test[, "RF"] <- stack_test[, "RF"] + predict(rf_model, newdata = test) / numFolds
# 训练SVM
svm_pred <- predict(svm_model, cv_test)
stack_train[-trainIndex, "SVM"] <- svm_pred
stack_test[, "SVM"] <- stack_test[, "SVM"] + predict(svm_model, newdata = test) / numFolds
# 训练XGB
xgb_pred <- predict(xgb_model, as.matrix(cv_test[,1:9]))
stack_train[-trainIndex, "XGB"] <- xgb_pred
stack_test[, "XGB"] <- stack_test[, "XGB"] + predict(xgb_model, as.matrix(test[,1:9])) / numFolds
}
# 计算stacking训练集上的R2、RMSE和MAE
stack_train$target <- train$target
stack_model <- lm(target ~ ., data = stack_train)
stack_train_pred <- predict(stack_model, stack_train)
stack_train_r2 <- R2(stack_train$target, stack_train_pred)
stack_train_rmse <- RMSE(stack_train$target, stack_train_pred)
stack_train_mae <- MAE(stack_train$target, stack_train_pred)
# 计算stacking测试集上的R2、RMSE和MAE
stack_test_pred <- predict(stack_model, stack_test)
stack_test_r2 <- R2(test$target, stack_test_pred)
stack_test_rmse <- RMSE(test$target, stack_test_pred)
stack_test_mae <- MAE(test$target, stack_test_pred)
# 打印结果
cat(paste0("Stacking Training R2: ", round(stack_train_r2, 4), "\n"))
cat(paste0("Stacking Training RMSE: ", round(stack_train_rmse, 4), "\n"))
cat(paste0("Stacking Training MAE: ", round(stack_train_mae, 4), "\n"))
cat(paste0("Stacking Testing R2: ", round(stack_test_r2, 4), "\n"))
cat(paste0("Stacking Testing RMSE: ", round(stack_test_rmse, 4), "\n"))
cat(paste0("Stacking Testing MAE: ", round(stack_test_mae, 4), "\n"))
```
注意:以上代码仅供参考,具体实现应根据数据集和模型的不同进行相应修改。
阅读全文