In this part, base on P7 build and tune 4 models ( svm,nnet,randomForest,lm) sperately by using package “caret”,with leave-one-out crossvalidation method of trainControl which number is 5,repeats is 5
load("/home/gong/prepareData/P 7 TrainingAndTesting.RData")
library(doMC)
library(kernlab)
library(PerformanceAnalytics)
library(caret)
registerDoMC(cores = 3)
# svm RBF kernel
cvcontrol <- trainControl(method = "LOOCV", number = 5, repeats = 5)
if (file.exists("P7.svmFit.RData")) {
load("P7.svmFit.RData")
} else {
P7.svmFit <- train(InputsTrain, TargetTrain, method = "svmRadial", tuneLength = 4,
trControl = cvcontrol, scaled = TRUE)
save(P7.svmFit, file = "P7.svmFit.RData")
}
# neural networks
if (file.exists("P7.nnetFit.RData")) {
load("P7.nnetFit.RData")
} else {
nnet.grid <- expand.grid(.size = c(7:15), .decay = c(1e-04, 2e-04, 0.005,
0.01))
P7.nnetFit <- train(InputsTrain, TargetTrain, method = "nnet", trControl = cvcontrol,
tuneGrid = nnet.grid)
save(P7.nnetFit, file = "P7.nnetFit.RData")
}
# random Forests
if (file.exists("P7.rfFit.RData")) {
load("P7.rfFit.RData")
} else {
library(randomForest)
P7.rfFit <- train(InputsTrain, TargetTrain, method = "rf", trControl = cvcontrol,
tuneLength = 3)
save(P7.rfFit, file = "P7.rfFit.RData")
}
# Linear Least Squares
if (file.exists("P7.lmFit.RData")) {
load("P7.lmFit.RData")
} else {
P7.lmFit <- train(InputsTrain, TargetTrain, method = "lm", trControl = cvcontrol,
tuneLength = 4)
save(P7.lmFit, file = "P7.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 = P7.svmFit, nnet = P7.nnetFit, randomForest = P7.rfFit,
lm = P7.lmFit)
P7.preValues <- extractPrediction(models, testX = InputsTest, testY = TargetTest)
plotObsVsPred(P7.preValues)
# calculate errors
P7.error <- function(model) {
pd <- predict(model, newdata = InputsTest)
modelErrors(pd, TargetTest)
}
rf.error <- P7.error(P7.rfFit)
nnet.error <- P7.error(P7.nnetFit)
svm.error <- P7.error(P7.svmFit)
lm.error <- P7.error(P7.lmFit)
errorAll <- rbind(rf.error, nnet.error, svm.error, lm.error)
errorAll
## MAE RMSE RELE
## rf.error 0.1236 0.1538 0.5972
## nnet.error 0.1231 0.1531 0.5746
## svm.error 0.1229 0.1569 0.5633
## lm.error 0.1307 0.1613 0.6366