\[ y = 10 sin(\pi x_1x_2) + 20(x_3 − 0.5)^2 + 10x_4 + 5x_5 + N(0, \sigma^2) \]
library(mlbench)
library(kableExtra)
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
set.seed(200)
trainingData <- mlbench.friedman1(200, sd = 1)
## We convert the 'x' data from a matrix to a data frame
## One reason is that this will give the columns names.
trainingData$x <- data.frame(trainingData$x)
## Look at the data using
featurePlot(trainingData$x, trainingData$y)
## or other methods.
## This creates a list with a vector 'y' and a matrix
## of predictors 'x'. Also simulate a large test set to
## estimate the true error rate with good precision:
testData <- mlbench.friedman1(5000, sd = 1)
testData$x <- data.frame(testData$x)
knnModel <- train(x = trainingData$x,
y = trainingData$y,
method = "knn",
preProc = c("center", "scale"),
tuneLength = 10)
knnModel
## k-Nearest Neighbors
##
## 200 samples
## 10 predictor
##
## Pre-processing: centered (10), scaled (10)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 200, 200, 200, 200, 200, 200, ...
## Resampling results across tuning parameters:
##
## k RMSE Rsquared MAE
## 5 3.466085 0.5121775 2.816838
## 7 3.349428 0.5452823 2.727410
## 9 3.264276 0.5785990 2.660026
## 11 3.214216 0.6024244 2.603767
## 13 3.196510 0.6176570 2.591935
## 15 3.184173 0.6305506 2.577482
## 17 3.183130 0.6425367 2.567787
## 19 3.198752 0.6483184 2.592683
## 21 3.188993 0.6611428 2.588787
## 23 3.200458 0.6638353 2.604529
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 17.
knnPred <- predict(knnModel, newdata = testData$x)
## The function 'postResample' can be used to get the test set perforamnce values
postResample(pred = knnPred, obs = testData$y)
## RMSE Rsquared MAE
## 3.2040595 0.6819919 2.5683461
knnAccuracy <- postResample(pred = knnPred, obs = testData$y)
SvmRadialModel <- train(x = trainingData$x,
y = trainingData$y,
method = "svmRadial",
tuneLength=10,
preProc = c("center", "scale"))
SvmRadialModel
## Support Vector Machines with Radial Basis Function Kernel
##
## 200 samples
## 10 predictor
##
## Pre-processing: centered (10), scaled (10)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 200, 200, 200, 200, 200, 200, ...
## Resampling results across tuning parameters:
##
## C RMSE Rsquared MAE
## 0.25 2.545335 0.7804647 2.015121
## 0.50 2.319786 0.7965148 1.830009
## 1.00 2.188349 0.8119636 1.726027
## 2.00 2.103655 0.8241314 1.655842
## 4.00 2.066879 0.8294322 1.631051
## 8.00 2.052681 0.8313929 1.623550
## 16.00 2.049867 0.8318312 1.621820
## 32.00 2.049867 0.8318312 1.621820
## 64.00 2.049867 0.8318312 1.621820
## 128.00 2.049867 0.8318312 1.621820
##
## Tuning parameter 'sigma' was held constant at a value of 0.06802164
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.06802164 and C = 16.
svmRadialPred <- predict(SvmRadialModel, newdata = testData$x)
#Use postResample function to get the test set performance values
postResample(pred = svmRadialPred, obs = testData$y)
## RMSE Rsquared MAE
## 2.0864652 0.8236735 1.5854649
svmRadialAccuracy <- postResample(pred = svmRadialPred, obs = testData$y)
nnetGrid <- expand.grid(.decay=c(0, 0.01, 0.1, 0.5, 0.9),
.size=c(1, 10, 15, 20),
.bag=FALSE)
nnet <- train(x = trainingData$x,
y = trainingData$y,
method = "avNNet",
tuneGrid = nnetGrid,
preProc = c("center", "scale"),
trace=FALSE,
linout=TRUE,
maxit=500)
nnet
## Model Averaged Neural Network
##
## 200 samples
## 10 predictor
##
## Pre-processing: centered (10), scaled (10)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 200, 200, 200, 200, 200, 200, ...
## Resampling results across tuning parameters:
##
## decay size RMSE Rsquared MAE
## 0.00 1 2.589902 0.7324403 2.014450
## 0.00 10 3.082909 0.6581005 2.345346
## 0.00 15 2.666684 0.7202739 2.135432
## 0.00 20 2.634394 0.7273102 2.116201
## 0.01 1 2.567190 0.7363972 1.993021
## 0.01 10 2.714509 0.7118994 2.173845
## 0.01 15 2.435366 0.7634558 1.935705
## 0.01 20 2.346376 0.7801847 1.850974
## 0.10 1 2.580129 0.7336990 2.000459
## 0.10 10 2.528971 0.7492960 2.003431
## 0.10 15 2.309856 0.7879857 1.823430
## 0.10 20 2.289300 0.7922572 1.799799
## 0.50 1 2.620985 0.7251648 2.034073
## 0.50 10 2.389468 0.7734132 1.893293
## 0.50 15 2.248817 0.7979988 1.778851
## 0.50 20 2.257951 0.7973906 1.768133
## 0.90 1 2.649162 0.7195330 2.057453
## 0.90 10 2.339031 0.7803865 1.849270
## 0.90 15 2.247236 0.7980673 1.774157
## 0.90 20 2.248629 0.7989807 1.770371
##
## Tuning parameter 'bag' was held constant at a value of FALSE
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were size = 15, decay = 0.9 and bag = FALSE.
nnetPred <- predict(nnet, newdata = testData$x)
# Use postResample function to get the test set performance values
postResample(pred = nnetPred, obs = testData$y)
## RMSE Rsquared MAE
## 1.894755 0.856176 1.441820
nnetAccuracy <- postResample(pred = nnetPred, obs = testData$y)
marsGrid <- expand.grid(.degree=1:2,
.nprune=2:20)
marsModel <- train(x = trainingData$x,
y = trainingData$y,
method = "earth",
tuneGrid = marsGrid,
preProc = c("center", "scale"))
## Loading required package: earth
## Loading required package: Formula
## Loading required package: plotmo
## Loading required package: plotrix
## Loading required package: TeachingDemos
marsModel
## Multivariate Adaptive Regression Spline
##
## 200 samples
## 10 predictor
##
## Pre-processing: centered (10), scaled (10)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 200, 200, 200, 200, 200, 200, ...
## Resampling results across tuning parameters:
##
## degree nprune RMSE Rsquared MAE
## 1 2 4.416233 0.2187486 3.630918
## 1 3 3.716956 0.4495842 2.981710
## 1 4 2.829383 0.6783492 2.285456
## 1 5 2.500824 0.7495068 2.002628
## 1 6 2.377484 0.7721684 1.897799
## 1 7 1.964112 0.8402449 1.540566
## 1 8 1.849850 0.8590078 1.446960
## 1 9 1.760287 0.8734284 1.379551
## 1 10 1.745434 0.8758361 1.358502
## 1 11 1.721573 0.8784527 1.333126
## 1 12 1.741003 0.8757510 1.341457
## 1 13 1.762562 0.8729306 1.355525
## 1 14 1.779852 0.8704986 1.376884
## 1 15 1.796118 0.8682289 1.386179
## 1 16 1.801970 0.8673854 1.392551
## 1 17 1.801970 0.8673854 1.392551
## 1 18 1.801970 0.8673854 1.392551
## 1 19 1.801970 0.8673854 1.392551
## 1 20 1.801970 0.8673854 1.392551
## 2 2 4.421087 0.2136318 3.622047
## 2 3 3.738888 0.4424544 3.004778
## 2 4 2.878704 0.6647839 2.315845
## 2 5 2.556082 0.7359640 2.039618
## 2 6 2.448842 0.7590378 1.941403
## 2 7 2.076809 0.8217323 1.631062
## 2 8 1.919449 0.8477097 1.506739
## 2 9 1.750995 0.8737507 1.383005
## 2 10 1.589003 0.8961730 1.262151
## 2 11 1.503925 0.9077370 1.182204
## 2 12 1.459184 0.9131975 1.139692
## 2 13 1.465062 0.9130346 1.140011
## 2 14 1.440347 0.9155789 1.123394
## 2 15 1.462849 0.9131866 1.141915
## 2 16 1.477622 0.9110707 1.148694
## 2 17 1.479198 0.9110804 1.152683
## 2 18 1.480900 0.9108744 1.151674
## 2 19 1.475717 0.9116473 1.146616
## 2 20 1.475717 0.9116473 1.146616
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 14 and degree = 2.
marsPred <- predict(marsModel, newdata = testData$x)
# Use postResample function to get the test set performance values
postResample(pred = marsPred, obs = testData$y)
## RMSE Rsquared MAE
## 1.2779993 0.9338365 1.0147070
marsAccuracy <- postResample(pred = marsPred, obs = testData$y)
accuracies <- rbind(marsAccuracy,svmRadialAccuracy,knnAccuracy,nnetAccuracy)
rownames(accuracies )<- c("MARS","SVM","KNN", "NeuralNet")
accuracies%>%
kable() %>%
kable_styling()
RMSE | Rsquared | MAE | |
---|---|---|---|
MARS | 1.277999 | 0.9338365 | 1.014707 |
SVM | 2.086465 | 0.8236735 | 1.585465 |
KNN | 3.204060 | 0.6819919 | 2.568346 |
NeuralNet | 1.894755 | 0.8561760 | 1.441820 |
Based on the above results we can find that MARS has the best accuracy when compared to KNN, SVM and Neural Network. RMSE value of MARS model is much lower when compared to other models.
varImp(marsModel)
## earth variable importance
##
## Overall
## X1 100.00
## X4 75.40
## X2 49.00
## X5 15.72
## X3 0.00
library(AppliedPredictiveModeling)
library(Amelia)
## Loading required package: Rcpp
## ##
## ## Amelia II: Multiple Imputation
## ## (Version 1.7.6, built: 2019-11-24)
## ## Copyright (C) 2005-2020 James Honaker, Gary King and Matthew Blackwell
## ## Refer to http://gking.harvard.edu/amelia/ for more information
## ##
library(missForest)
## Loading required package: randomForest
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
##
## margin
## Loading required package: foreach
## Loading required package: itertools
## Loading required package: iterators
library(nnet)
library(corrgram)
##
## Attaching package: 'corrgram'
## The following object is masked from 'package:lattice':
##
## panel.fill
library(ggplot2)
data(ChemicalManufacturingProcess)
missmap(ChemicalManufacturingProcess, col = c("red", "lightgreen"))
Original_df <- ChemicalManufacturingProcess
Imputed_df <- missForest(Original_df)
## missForest iteration 1 in progress...done!
## missForest iteration 2 in progress...done!
## missForest iteration 3 in progress...done!
## missForest iteration 4 in progress...done!
## missForest iteration 5 in progress...done!
df <- Imputed_df$ximp
data <- df[, 2:58]
target <- df[,1]
train <- createDataPartition(target, p=0.75)
train_pred <- data[train$Resample1,]
train_target <- target[train$Resample]
test_pred <- data[-train$Resample1,]
test_target <- target[-train$Resample1]
control <- trainControl(method = "cv", number=10)
set.seed(1)
knnModel <- train(x = train_pred,
y = train_target,
method = "knn",
tuneLength = 10)
knnModel
## k-Nearest Neighbors
##
## 132 samples
## 57 predictor
##
## No pre-processing
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 132, 132, 132, 132, 132, 132, ...
## Resampling results across tuning parameters:
##
## k RMSE Rsquared MAE
## 5 1.722106 0.2152919 1.400652
## 7 1.696309 0.2170071 1.382955
## 9 1.696599 0.2075752 1.378167
## 11 1.689217 0.2104431 1.371941
## 13 1.681288 0.2175028 1.371749
## 15 1.675921 0.2192990 1.365006
## 17 1.675337 0.2211266 1.358179
## 19 1.673974 0.2225404 1.359908
## 21 1.675164 0.2198840 1.358720
## 23 1.681794 0.2145043 1.356062
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 19.
knn.Predict <- predict(knnModel, newdata = test_pred)
# Use PostResample function
postResample(pred = knn.Predict, obs = test_target)
## RMSE Rsquared MAE
## 1.6591763 0.3388975 1.3013158
knnAccuracy <- postResample(pred = knn.Predict, obs = test_target)
set.seed(1)
svmModel <- train(x = train_pred,
y = train_target,
method='svmRadial',
tuneLength=14,
trControl = trainControl(method = "cv"),
preProc = c("center", "scale"))
svmModel
## Support Vector Machines with Radial Basis Function Kernel
##
## 132 samples
## 57 predictor
##
## Pre-processing: centered (57), scaled (57)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 119, 119, 119, 118, 119, 118, ...
## Resampling results across tuning parameters:
##
## C RMSE Rsquared MAE
## 0.25 1.426859 0.4930403 1.1553090
## 0.50 1.299018 0.5499885 1.0420521
## 1.00 1.189160 0.6049125 0.9514879
## 2.00 1.141489 0.6329814 0.9128962
## 4.00 1.141144 0.6373573 0.9156004
## 8.00 1.115601 0.6531527 0.8996701
## 16.00 1.113618 0.6546783 0.8981607
## 32.00 1.113618 0.6546783 0.8981607
## 64.00 1.113618 0.6546783 0.8981607
## 128.00 1.113618 0.6546783 0.8981607
## 256.00 1.113618 0.6546783 0.8981607
## 512.00 1.113618 0.6546783 0.8981607
## 1024.00 1.113618 0.6546783 0.8981607
## 2048.00 1.113618 0.6546783 0.8981607
##
## Tuning parameter 'sigma' was held constant at a value of 0.01398119
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01398119 and C = 16.
svm.Predict <- predict (svmModel, test_pred)
# Use PostResample function
postResample(pred = svm.Predict, obs = test_target)
## RMSE Rsquared MAE
## 1.0326586 0.7074344 0.8384875
svmRadialAccuracy <- postResample(pred = svm.Predict, obs = test_target)
set.seed(1)
nnetModel <- nnet(train_pred,
train_target,
size=5,
decay=0.01,
linout= T,
trace=F,
maxit = 500 ,
MaxNWts = 5 * (ncol(train_pred) + 1) + 5 + 1)
nnetPredict <- predict(nnetModel, test_pred)
# Use PostResample function
postResample(pred = nnetPredict, obs = test_target)
## RMSE Rsquared MAE
## 2.9292442 0.1673242 2.3167136
nnetAccuracy <- postResample(pred = nnetPredict, obs = test_target)
set.seed(1)
marsModel <- train(x = train_pred,
y = train_target,
method='earth',
tuneLength=10,
trControl = trainControl(method = "cv"))
marsModel
## Multivariate Adaptive Regression Spline
##
## 132 samples
## 57 predictor
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 119, 119, 119, 118, 119, 118, ...
## Resampling results across tuning parameters:
##
## nprune RMSE Rsquared MAE
## 2 1.414798 0.4364493 1.1195825
## 3 1.190013 0.5992537 0.9679315
## 5 1.119823 0.6159515 0.9068656
## 7 1.147665 0.6119270 0.9529042
## 8 1.167424 0.6026739 0.9707559
## 10 1.159271 0.6099304 0.9829971
## 12 1.226075 0.5719064 1.0406675
## 13 1.259002 0.5538101 1.0751039
## 15 1.305346 0.5356789 1.0979933
## 17 1.305006 0.5382960 1.1034235
##
## Tuning parameter 'degree' was held constant at a value of 1
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 5 and degree = 1.
mars.Predict <- predict (marsModel, test_pred)
# Use PostResample function
postResample(pred = mars.Predict, obs = test_target)
## RMSE Rsquared MAE
## 1.2250658 0.6062770 0.9706621
marsAccuracy <- postResample(pred = mars.Predict, obs = test_target)
accuracies1 <- rbind(marsAccuracy,svmRadialAccuracy,knnAccuracy,nnetAccuracy)
rownames(accuracies1)<- c("MARS","SVM","KNN", "NeuralNet")
accuracies1%>%
kable() %>%
kable_styling()
RMSE | Rsquared | MAE | |
---|---|---|---|
MARS | 1.225066 | 0.6062770 | 0.9706621 |
SVM | 1.032659 | 0.7074344 | 0.8384875 |
KNN | 1.659176 | 0.3388975 | 1.3013158 |
NeuralNet | 2.929244 | 0.1673242 | 2.3167136 |
SVM model was slightly performing better than MARS model. Here we can observe the least RMSE for SVM.
varImp(svmModel)
## loess r-squared variable importance
##
## only 20 most important variables shown (out of 57)
##
## Overall
## ManufacturingProcess32 100.00
## ManufacturingProcess13 94.26
## ManufacturingProcess17 80.60
## BiologicalMaterial06 67.90
## ManufacturingProcess09 64.43
## BiologicalMaterial03 62.21
## BiologicalMaterial12 58.04
## ManufacturingProcess36 55.82
## ManufacturingProcess06 51.42
## BiologicalMaterial02 50.70
## ManufacturingProcess31 46.60
## ManufacturingProcess11 43.88
## ManufacturingProcess30 39.49
## ManufacturingProcess33 38.76
## ManufacturingProcess12 37.05
## ManufacturingProcess29 36.88
## BiologicalMaterial08 36.68
## BiologicalMaterial11 36.49
## BiologicalMaterial09 33.27
## BiologicalMaterial04 33.09
plot(varImp(svmModel), top=10)
Manufacturing process 32 and Maunfacturing process 13 are in top 3 list for the selected SVM model. In top 10 we got 6 Manufacturing processes and 4 Biological processes. By this we can say Manufacturing processes dominates Biological processes.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:randomForest':
##
## combine
## The following object is masked from 'package:kableExtra':
##
## group_rows
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
cor_model <- train_pred %>% select(ManufacturingProcess32, ManufacturingProcess13, ManufacturingProcess17, BiologicalMaterial12, BiologicalMaterial03, ManufacturingProcess09, BiologicalMaterial06, ManufacturingProcess36, ManufacturingProcess06, BiologicalMaterial02)
corrgram(cor_model, order=TRUE, upper.panel=panel.cor)