library(ggplot2)
library(magrittr)
library(caret)
library(tidyverse)
Friedman (1991) introduced several benchmark data sets create by simulation. One of these simulations used the following nonlinear equation to create data:
y = 10 sin(πx1x2) + 20(x3 − 0.5)2 + 10x4 + 5x5 + N(0, σ2)
where the x values are random variables uniformly distributed between [0, 1] (there are also 5 other non-informative variables also created in the simulation). The package mlbench contains a function called mlbench.friedman1 that simulates these data:
library(mlbench)
set.seed(200)
trainingData <- mlbench.friedman1(200, sd = 1)
trainingData$x <- data.frame(trainingData$x)
featurePlot(trainingData$x, trainingData$y)
testData <- mlbench.friedman1(5000, sd = 1)
testData$x <- data.frame(testData$x)
Tune several models on these data.
Tracker
tracker <- data.frame(matrix(vector(), 0, 3,
dimnames=list(c(), c("RMSE","Rsquared","MAE"))),
stringsAsFactors=F)
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)
tracker <- rbind(tracker, postResample(pred = knnPred, obs = testData$y))
library(earth)
control <- trainControl(method = "cv")
marsGrid <- expand.grid(.degree = 1:2, .nprune = 2:15)
marsModel <- train(trainingData$x, trainingData$y,
method = "earth",
tuneGrid = marsGrid,
preProcess = c("center", "scale"),
tuneLength = 10,
trControl = control)
marsModel
## Multivariate Adaptive Regression Spline
##
## 200 samples
## 10 predictor
##
## Pre-processing: centered (10), scaled (10)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 180, 180, 180, 180, 180, 180, ...
## Resampling results across tuning parameters:
##
## degree nprune RMSE Rsquared MAE
## 1 2 4.462296 0.2176253 3.697979
## 1 3 3.720663 0.4673821 2.949121
## 1 4 2.680039 0.7094916 2.123848
## 1 5 2.333538 0.7781559 1.856629
## 1 6 2.367933 0.7754329 1.901509
## 1 7 1.809983 0.8656526 1.414985
## 1 8 1.692656 0.8838936 1.333678
## 1 9 1.704958 0.8845683 1.339517
## 1 10 1.688559 0.8842495 1.309838
## 1 11 1.669043 0.8886165 1.296522
## 1 12 1.645066 0.8892796 1.271981
## 1 13 1.655570 0.8886896 1.271232
## 1 14 1.666354 0.8879143 1.285545
## 1 15 1.666354 0.8879143 1.285545
## 2 2 4.440854 0.2204755 3.686796
## 2 3 3.697203 0.4714312 2.938566
## 2 4 2.664266 0.7149235 2.119566
## 2 5 2.313371 0.7837374 1.852172
## 2 6 2.335796 0.7875253 1.841919
## 2 7 1.833248 0.8623489 1.461538
## 2 8 1.695822 0.8883658 1.329030
## 2 9 1.555106 0.9028532 1.221365
## 2 10 1.497805 0.9088251 1.158054
## 2 11 1.419280 0.9207646 1.139722
## 2 12 1.326566 0.9315939 1.066200
## 2 13 1.266877 0.9354482 1.002983
## 2 14 1.256694 0.9349307 1.006273
## 2 15 1.311401 0.9316487 1.039213
##
## 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)
tracker <- rbind(tracker, postResample(pred = marsPred, obs = testData$y))
nNetGrid <- expand.grid(.decay = c(0, 0.01, .1), .size = c(1:10), .bag = FALSE)
nNetMaxnwts <- 5 * (ncol(trainingData$x) + 1) + 5 + 1
nNetModel <- train(x = trainingData$x,
y = trainingData$y,
method = "avNNet",
preProcess = c("center", "scale"),
tuneGrid = nNetGrid,
trControl = control,
linout = TRUE,
trace = FALSE,
MaxNWts = nNetMaxnwts,
maxit = 500)
nNetModel
## Model Averaged Neural Network
##
## 200 samples
## 10 predictor
##
## Pre-processing: centered (10), scaled (10)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 180, 180, 180, 180, 180, 180, ...
## Resampling results across tuning parameters:
##
## decay size RMSE Rsquared MAE
## 0.00 1 2.409360 0.7626158 1.918956
## 0.00 2 2.495497 0.7466800 1.998154
## 0.00 3 2.042870 0.8276737 1.620556
## 0.00 4 2.218146 0.8058404 1.608860
## 0.00 5 2.290307 0.7791256 1.785130
## 0.00 6 NaN NaN NaN
## 0.00 7 NaN NaN NaN
## 0.00 8 NaN NaN NaN
## 0.00 9 NaN NaN NaN
## 0.00 10 NaN NaN NaN
## 0.01 1 2.441045 0.7589837 1.927773
## 0.01 2 2.408586 0.7646177 1.924190
## 0.01 3 2.094390 0.8207054 1.639698
## 0.01 4 2.019234 0.8369700 1.608611
## 0.01 5 2.186633 0.8126958 1.765935
## 0.01 6 NaN NaN NaN
## 0.01 7 NaN NaN NaN
## 0.01 8 NaN NaN NaN
## 0.01 9 NaN NaN NaN
## 0.01 10 NaN NaN NaN
## 0.10 1 2.451052 0.7561366 1.935306
## 0.10 2 2.439995 0.7533865 1.925580
## 0.10 3 2.142421 0.8119015 1.697562
## 0.10 4 2.021443 0.8367619 1.613081
## 0.10 5 2.058645 0.8303731 1.641058
## 0.10 6 NaN NaN NaN
## 0.10 7 NaN NaN NaN
## 0.10 8 NaN NaN NaN
## 0.10 9 NaN NaN NaN
## 0.10 10 NaN NaN NaN
##
## 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 = 4, decay = 0.01 and bag = FALSE.
nNetPred <- predict(nNetModel, newdata = testData$x)
tracker <- rbind(tracker, postResample(pred = nNetPred, obs = testData$y))
svmModel <- train(x = trainingData$x,
y = trainingData$y,
method = "svmRadial",
preProcess = c("center", "scale"),
tuneLength = 10,
trControl = control)
svmModel
## Support Vector Machines with Radial Basis Function Kernel
##
## 200 samples
## 10 predictor
##
## Pre-processing: centered (10), scaled (10)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 180, 180, 180, 180, 180, 180, ...
## Resampling results across tuning parameters:
##
## C RMSE Rsquared MAE
## 0.25 2.485843 0.8015980 1.997058
## 0.50 2.217552 0.8191439 1.783734
## 1.00 2.038394 0.8395080 1.621050
## 2.00 1.931284 0.8537910 1.510868
## 4.00 1.875643 0.8624807 1.471216
## 8.00 1.873494 0.8639409 1.479643
## 16.00 1.886621 0.8626959 1.496953
## 32.00 1.886621 0.8626959 1.496953
## 64.00 1.886621 0.8626959 1.496953
## 128.00 1.886621 0.8626959 1.496953
##
## Tuning parameter 'sigma' was held constant at a value of 0.0623323
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.0623323 and C = 8.
svmPred <- predict(svmModel, newdata = testData$x)
tracker <- rbind(tracker, postResample(pred = svmPred, obs = testData$y))
colnames(tracker) <- c("RMSE","Rsquared","MAE")
tracker$NAME <- c("KNN", "MARS", "Neural Network", "SVM")
tracker <- tracker %>% relocate("NAME") %>% arrange(RMSE)
tracker
## NAME RMSE Rsquared MAE
## 1 MARS 1.277999 0.9338365 1.014707
## 2 SVM 2.051520 0.8294511 1.556470
## 3 Neural Network 2.061504 0.8305890 1.561138
## 4 KNN 3.204059 0.6819919 2.568346
Which models appear to give the best performance? Does MARS select the informative predictors (those named X1–X5)?
From the table above, we can see that the MARS model gave the best performance.
varImp(marsModel)
## earth variable importance
##
## Overall
## X1 100.00
## X4 75.40
## X2 49.00
## X5 15.72
## X3 0.00
The MARS model important variables output shows that the model did select the informative predictors (X1-X5).
Exercise 6.3 describes data for a chemical manufacturing process. Use the same data imputation, data splitting, and pre-processing steps as before and train several nonlinear regression models.
library(AppliedPredictiveModeling)
data("ChemicalManufacturingProcess")
preProcess <- preProcess(ChemicalManufacturingProcess,
method = c("BoxCox", "knnImpute", "center", "scale"))
predPreProcess <- predict(preProcess, ChemicalManufacturingProcess)
predPreProcess$Yield = ChemicalManufacturingProcess$Yield
ind <- sample(seq_len(nrow(predPreProcess)), size = floor(0.85 * nrow(predPreProcess)))
train <- predPreProcess[ind, ]
test <- predPreProcess[-ind, ]
## KNN Model
knnModel2 <- train(Yield ~., data = train,
method = "knn",
preProcess = c("center", "scale"),
tuneLength = 10)
knnPred2 <- predict(knnModel2, newdata = test)
## Neural Networks Model
nNetModel2 <- train(Yield ~., data = train,
method = "avNNet",
tuneGrid = nNetGrid,
trControl = control,
linout = TRUE,
trace = FALSE,
MaxNWts = 5 * (ncol(train)) + 5 + 1,
maxit = 500
)
nNetPred2 <- predict(nNetModel2, newdata = test)
## MARS Model
marsModel2 <- train(Yield ~., data=train,
method = "earth",
tuneGrid = marsGrid,
trControl = control)
marsPred2 <- predict(marsModel2, newdata = test)
## SVM Model
svmModel2 <- train(Yield ~., data=train,
method = "svmRadial",
tuneLength = 15,
trControl = control)
svmPred2 <- predict(svmModel2, newdata = test)
Which nonlinear regression model gives the optimal resampling and test set performance?
as.data.frame(rbind(
"mars" = postResample(pred = marsPred2, obs = test$Yield),
"svm" = postResample(pred = svmPred2, obs = test$Yield),
"net" = postResample(pred = nNetPred2, obs = test$Yield),
"knn" = postResample(pred = knnPred2, obs = test$Yield)
)) %>% arrange(RMSE)
## RMSE Rsquared MAE
## svm 1.014420 0.6276394 0.8039693
## mars 1.134006 0.4894321 0.8214562
## knn 1.340323 0.2917927 1.0227407
## net 1.388866 0.4166196 1.0445468
If we look at the RMSE values from both tables, we can see that the SVM model gives the optimal test performance.
Which predictors are most important in the optimal nonlinear regression model? Do either the biological or process variables dominate the list? How do the top ten important predictors compare to the top ten predictors from the optimal linear model?
varImp(svmModel2, 10)
## loess r-squared variable importance
##
## only 20 most important variables shown (out of 57)
##
## Overall
## ManufacturingProcess32 100.00
## ManufacturingProcess13 98.51
## BiologicalMaterial06 86.05
## ManufacturingProcess17 79.53
## ManufacturingProcess36 76.05
## ManufacturingProcess09 74.29
## BiologicalMaterial03 73.83
## BiologicalMaterial02 73.69
## BiologicalMaterial12 71.02
## ManufacturingProcess06 66.79
## ManufacturingProcess31 65.61
## BiologicalMaterial11 55.47
## ManufacturingProcess29 53.14
## BiologicalMaterial04 51.72
## BiologicalMaterial08 50.22
## ManufacturingProcess11 48.82
## ManufacturingProcess33 48.56
## BiologicalMaterial01 46.91
## ManufacturingProcess02 46.26
## BiologicalMaterial09 41.69
The SVM model was optimal for nonlinear regression, and the table above shows the most important predictors. There seems to be an equal number of biological and process variables.
The results for 6.3 shows that the optimal linear model was the PLS model. The most important variables list from the PLS model showed that the Manufacturing Process variables were dominating.
Explore the relationships between the top predictors and the response for the predictors that are unique to the optimal nonlinear regression model. Do these plots reveal intuition about the biological or process predictors and their relationship with yield?
varI <- varImp(svmModel2)$importance
varI <- cbind(rownames(varI), data.frame(varI, row.names=NULL))
colnames(varI) <- c("Predictor","Overall")
varI <- varI %>% arrange(Overall) %>% tail(10) %>% select(Predictor)
variables <- as.vector(varI$Predictor)
featurePlot(predPreProcess[,variables], predPreProcess$Yield)
Looking at the relationship of 10 most important variables with the target variable, we see that a majority of them are tightly clustered and have a linear relationship. All biological material variables are clusters, but some manufacturing process variables a unique relationship (variable 6 and variable 10).