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 P8 database
load("/home/bing/training/P8/P 8 TrainingAndTesting.RData")
setwd("/home/bing/training/P8")
library(doMC)
library(kernlab)
library(caret)
registerDoMC(cores = 3)
# svm RBF kernel
cvcontrol <- trainControl(method = "LOOCV", number = 5, repeats = 5)
if (file.exists("P8.svmFit.RData")) {
load("P8.svmFit.RData")
} else {
P8.svmFit <- train(InputsTrain, TargetTrain, method = "svmRadial", tuneLength = 4,
trControl = cvcontrol, scaled = TRUE)
save(P8.svmFit, file = "P8.svmFit.RData")
}
# neural networks
if (file.exists("P8.nnetFit.RData")) {
load("P8.nnetFit.RData")
} else {
nnet.grid <- expand.grid(.size = c(7:15), .decay = c(1e-04, 2e-04, 0.005,
0.01))
P8.nnetFit <- train(InputsTrain, TargetTrain, method = "nnet", trControl = cvcontrol,
tuneGrid = nnet.grid)
save(P8.nnetFit, file = "P8.nnetFit.RData")
}
# random Forests
if (file.exists("P8.rfFit.RData")) {
load("P8.rfFit.RData")
} else {
library(randomForest)
P8.rfFit <- train(InputsTrain, TargetTrain, method = "rf", trControl = cvcontrol,
tuneLength = 3)
save(P8.rfFit, file = "P8.rfFit.RData")
}
# Linear Least Squares
if (file.exists("P8.lmFit.RData")) {
load("P8.lmFit.RData")
} else {
P8.lmFit <- train(InputsTrain, TargetTrain, method = "lm", trControl = cvcontrol,
tuneLength = 4)
save(P8.lmFit, file = "P8.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 = P8.svmFit, nnet = P8.nnetFit, randomForest = P8.rfFit,
lm = P8.lmFit)
P8.preValues <- extractPrediction(models, testX = InputsTest, testY = TargetTest)
plotObsVsPred(P8.preValues)
# calculate errors
P8.error <- function(model) {
pd <- predict(model, newdata = InputsTest)
modelErrors(pd, TargetTest)
}
rf.error <- P8.error(P8.rfFit)
nnet.error <- P8.error(P8.nnetFit)
svm.error <- P8.error(P8.svmFit)
lm.error <- P8.error(P8.lmFit)
P8.errorAll <- rbind(rf.error, nnet.error, svm.error, lm.error)
P8.errorAll
## MAE RMSE RELE
## rf.error 0.1274 0.1614 0.6751
## nnet.error 0.1332 0.1674 0.6934
## svm.error 0.1290 0.1681 0.6007
## lm.error 0.1361 0.1726 0.7077
save(P8.errorAll, file = "P8.errorAll.RData")