在下面的代码中,我对数据集中的前20000条记录进行了带有交叉验证的NN。数据集包含8个预测变量。
首先,我将数据分为两部分: 前20.000行(训练集) 最后4003行(样本测试集之外)
我已经进行了2次跑步: 运行1)具有3个预测变量的运行 运行2)使用所有8个预测变量运行(请参见下面的代码)。
基于距火车集20.000行内的交叉验证,RMSE(用于最佳参数设置)从2.30(运行1)提高到2.11(运行2)。
尽管当我从样本外测试集中测试了4003行中的两个模型时,RMSE仅将可忽略性从2.64(运行1)提高到2.63(运行2)。
从这些矛盾中可以得出什么结论?
谢谢!
### R code from Applied Predictive Modeling (2013) by Kuhn and Johnson.
### Chapter 7: Non-Linear Regression Models
### Required packages: AppliedPredictiveModeling, caret, doMC (optional),
### earth, kernlab, lattice, nnet
################################################################################
library(caret)
### Load the data
mydata <- read.csv(file="data.csv", header=TRUE, sep=",")
validatiex <- mydata[20001:24003,c(1:8)]
validatiey <- mydata[20001:24003,9]
mydata <- mydata[1:20000,]
x <- mydata[,c(1:8)]
y <- mydata[,9]
parti <- createDataPartition(y, times = 1, p=0.8, list = FALSE)
x_train <- x[parti,]
x_test <- x[-parti,]
y_train <- y[parti]
y_test <- y[-parti]
set.seed(100)
indx <- createFolds(y_train, returnTrain = TRUE)
ctrl <- trainControl(method = "cv", index = indx)
## train neural net:
nnetGrid <- expand.grid(decay = c(.1),
size = c(5, 15, 30),
bag = FALSE)
set.seed(100)
nnetTune <- train(x = x_train, y = y_train,
method = "avNNet",
tuneGrid = nnetGrid,
trControl = ctrl,
preProc = c("center", "scale"),
linout = TRUE,
trace = FALSE,
MaxNWts = 30 * (ncol(x_train) + 1) + 30 + 1,
maxit = 1000,
repeats = 25,
allowParallel = FALSE)
nnetTune
plot(nnetTune)
predictions <- predict(nnetTune, validatiex, type="raw")
mse <- mean((validatiey - predictions)^2)
mse <- sqrt(mse)
print (mse)