我正在尝试以不同的学习率在 R 中循环制作 gbm 模型。我想为每个模型计算一些统计数据并将它们与原始数据集结合起来。
但是我有一个错误,因为每次计算统计数据时,它都以与前一个相同的名称保存,因此存在错误。
我在循环结束时收到以下错误:
Error in `[<-.data.frame`(`*tmp*`, nl, value = list(dates = c(14824, 14825, :
duplicate subscripts for columns
火车数据基本上是带有日期、开盘价、收盘价等的股票价格数据。
以下是代码:
learningRateList <- as.numeric(7:9)*0.01
for (i in learningRateList){
modelNames <- paste("gbmModel", i, sep = "")
gbmModels <-gbm.step(data=train, gbm.x = reqCol, gbm.y = CloseCol,tree.complexity =9,learning.rate = i,bag.fraction = 0.75,family ="laplace",step.size=100 )
assign(modelNames, gbmModels)
#training data
#predict values for the training data set
predTrainGbm<-paste("gbmTrainPrediction", i, sep = "")
gbmTrainPrediction <- predict.gbm(gbmModels,train,n.trees=gbmModels$gbm.call$best.trees)
assign(predTrainGbm,gbmTrainPrediction)
#calculate mape for the predictions
mapeTrain<-paste("mapeGbmTrain", i, sep = "")
mapeTrainGbm<-regr.eval(train$Close,gbmTrainPrediction,stats = "mape")
assign(mapeTrain,mapeTrainGbm)
train<-cbind(train,predTrainGbm,mapeTrain)
#creating plots of actual vs predicted values
imageGbmName<-paste(fileCalculated,"Gbm Prediction",i,".png")
png(imageGbmName)
par(mfrow=c(2,1))
plot(train$Close,type="l",col="red",main = "Training set")
lines(gbmTrainPrediction,col="green")
plot(test$Close,type="l",col="red",main = "Test Set")
lines(gbmTestPrediction,col="green")
dev.off()
}