In this part, build and tune 4 models (svm,neurons network,Linear Least Squares,randomForest) sperately by using package “caret”,with leave-one-out crossvalidation method of trainControl which number is 5,repeats is 5,base on P9 database
load("/home/bing/training/P9/P 9 TrainingAndTesting.RData")
setwd("/home/bing/training/P9")
library(doMC)
library(kernlab)
library(caret)
registerDoMC(cores = 3)
# svm RBF kernel
cvcontrol <- trainControl(method = "LOOCV", number = 5, repeats = 5)
if (file.exists("P9.svmFit.RData")) {
load("P9.svmFit.RData")
} else {
P9.svmFit <- train(InputsTrain, TargetTrain, method = "svmRadial", tuneLength = 4,
trControl = cvcontrol, scaled = TRUE)
save(P9.svmFit, file = "P9.svmFit.RData")
}
# neural networks
if (file.exists("P9.nnetFit.RData")) {
load("P9.nnetFit.RData")
} else {
nnet.grid <- expand.grid(.size = c(7:15), .decay = c(1e-04, 2e-04, 0.005,
0.01))
P9.nnetFit <- train(InputsTrain, TargetTrain, method = "nnet", trControl = cvcontrol,
tuneGrid = nnet.grid)
save(P9.nnetFit, file = "P9.nnetFit.RData")
}
# random Forests
if (file.exists("P9.rfFit.RData")) {
load("P9.rfFit.RData")
} else {
library(randomForest)
P9.rfFit <- train(InputsTrain, TargetTrain, method = "rf", trControl = cvcontrol,
tuneLength = 3)
save(P9.rfFit, file = "P9.rfFit.RData")
}
# Linear Least Squares
if (file.exists("P9.lmFit.RData")) {
load("P9.lmFit.RData")
} else {
P9.lmFit <- train(InputsTrain, TargetTrain, method = "lm", trControl = cvcontrol,
tuneLength = 4)
save(P9.lmFit, file = "P9.lmFit.RData")
}
In this part, I will make prediction according different models,plot and calculate the errors
# the function to caculate the model errors
modelErrors <- function(predicted, actual) {
sal <- vector(mode = "numeric", length = 3)
names(sal) <- c("MAE", "RMSE", "RELE")
meanPredicted <- mean(predicted)
meanActual <- mean(actual)
sumPred <- sum((predicted - meanPredicted)^2)
sumActual <- sum((actual - meanActual)^2)
n <- length(actual)
p3 <- vector(mode = "numeric", length = n)
for (i in c(1:n)) {
if (actual[i] == 0) {
p3[i] <- abs(predicted[i])
} else {
p3[i] <- ((abs(predicted[i] - actual[i]))/actual[i])
}
}
sal[1] <- mean(abs(predicted - actual))
sal[2] <- sqrt(sum((predicted - actual)^2)/n)
sal[3] <- mean(p3)
sal
}
# Predicting different models and plot the prediction values and true
# values
models <- list(svm = P9.svmFit, nnet = P9.nnetFit, randomForest = P9.rfFit,
lm = P9.lmFit)
P9.preValues <- extractPrediction(models, testX = InputsTest, testY = TargetTest)
plotObsVsPred(P9.preValues)
# calculate errors
P9.error <- function(model) {
pd <- predict(model, newdata = InputsTest)
modelErrors(pd, TargetTest)
}
rf.error <- P9.error(P9.rfFit)
nnet.error <- P9.error(P9.nnetFit)
svm.error <- P9.error(P9.svmFit)
lm.error <- P9.error(P9.lmFit)
P9.errorAll <- rbind(rf.error, nnet.error, svm.error, lm.error)
P9.errorAll
## MAE RMSE RELE
## rf.error 0.1281 0.1561 0.6087
## nnet.error 0.1248 0.1531 0.5558
## svm.error 0.1262 0.1596 0.5739
## lm.error 0.1336 0.1609 0.6220
save(P9.errorAll, file = "P9.errorAll.RData")