7.2

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)
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
## Warning in as.POSIXlt.POSIXct(Sys.time()): unknown timezone 'zone/tz/2020d.1.0/
## zoneinfo/America/New_York'
library(magrittr) 
library(dplyr)  
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
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)

Tune several models on these data.

KNN

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.565620  0.4887976  2.886629
##    7  3.422420  0.5300524  2.752964
##    9  3.368072  0.5536927  2.715310
##   11  3.323010  0.5779056  2.669375
##   13  3.275835  0.6030846  2.628663
##   15  3.261864  0.6163510  2.621192
##   17  3.261973  0.6267032  2.616956
##   19  3.286299  0.6281075  2.640585
##   21  3.280950  0.6390386  2.643807
##   23  3.292397  0.6440392  2.656080
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 15.
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.1750657 0.6785946 2.5443169

SVM

svmRModel <- train(x=trainingData$x, y=trainingData$y, 
                  method="svmRadial", 
                  preProcess=c("center", "scale"), 
                  tuneLength=20)
svmRModel
## 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.600536  0.7740370  2.070216
##        0.50  2.402691  0.7877362  1.887487
##        1.00  2.277967  0.8018302  1.776410
##        2.00  2.193637  0.8141532  1.712248
##        4.00  2.168900  0.8168553  1.691505
##        8.00  2.170623  0.8164075  1.696651
##       16.00  2.171543  0.8162341  1.696794
##       32.00  2.171543  0.8162341  1.696794
##       64.00  2.171543  0.8162341  1.696794
##      128.00  2.171543  0.8162341  1.696794
##      256.00  2.171543  0.8162341  1.696794
##      512.00  2.171543  0.8162341  1.696794
##     1024.00  2.171543  0.8162341  1.696794
##     2048.00  2.171543  0.8162341  1.696794
##     4096.00  2.171543  0.8162341  1.696794
##     8192.00  2.171543  0.8162341  1.696794
##    16384.00  2.171543  0.8162341  1.696794
##    32768.00  2.171543  0.8162341  1.696794
##    65536.00  2.171543  0.8162341  1.696794
##   131072.00  2.171543  0.8162341  1.696794
## 
## Tuning parameter 'sigma' was held constant at a value of 0.06651357
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.06651357 and C = 4.
svmRPred <- predict(svmRModel, newdata=testData$x)
svmRPR <- postResample(pred=svmRPred, obs=testData$y)
svmRPR
##      RMSE  Rsquared       MAE 
## 2.0682337 0.8274206 1.5638719

Neural Network

library(nnet)
nnetFit <- nnet(trainingData$x, trainingData$y,
                size = 5,
                decay = 0.01,
                linout = TRUE,
                trace = FALSE,
                maxit = 500,
                MaxNWts=5 * (ncol(trainingData$x) + 1) + 5 + 1)

nnetPred=predict(nnetFit,testData$x)
postResample(pred = nnetPred, obs = testData$y)
##     RMSE Rsquared      MAE 
## 2.499253 0.761204 1.893948

###MARS Unfortunately the MARS package did not work for me. Could not install for some reason.

#library(earth)
#marsFit <- earth(trainingData$x, trainingData$y)
#marsGrid = expand.grid(.degree = 1:2, .nprune = 2:38) 
#marsModel = train(x = trainingData$x, 
#                  y = trainingData$y, 
#                  method = "earth", 
#                  tuneGrid = marsGrid, 
#                  trControl = trainControl(method = "cv", 
#                                           number = 10))

Which models appear to give the best performance? Does MARS select the informative predictors (those named X1–X5)?

## MARS package did not work for me.

7.5a)

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.

Which nonlinear regression model gives the optimal resampling and test set performance?

library(AppliedPredictiveModeling)
## Warning: package 'AppliedPredictiveModeling' was built under R version 3.4.4
data(ChemicalManufacturingProcess)
dim(ChemicalManufacturingProcess)
## [1] 176  58
knn_model <- preProcess(ChemicalManufacturingProcess, "knnImpute")
df <- predict(knn_model, ChemicalManufacturingProcess)

df <- df%>%select_at(vars(-one_of(nearZeroVar(., names = TRUE))))

in_train <- createDataPartition(df$Yield, times = 1, p = 0.8, list = FALSE)
train_df <- df[in_train, ]
test_df <- df[-in_train, ]

###KNN

knn_model <- train(
  Yield ~ ., data = train_df, method = "knn",
  center = TRUE,
  scale = TRUE,
  trControl = trainControl("cv", number = 10),
  tuneLength = 25
)
knn_model
## k-Nearest Neighbors 
## 
## 144 samples
##  56 predictor
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 130, 130, 131, 129, 129, 130, ... 
## Resampling results across tuning parameters:
## 
##   k   RMSE       Rsquared   MAE      
##    5  0.7260160  0.5267677  0.5769830
##    7  0.7468102  0.5074432  0.5905056
##    9  0.7575675  0.5078606  0.6096355
##   11  0.7572407  0.5158351  0.6128133
##   13  0.7615613  0.5243049  0.6103254
##   15  0.7745523  0.4999495  0.6224218
##   17  0.7849278  0.4829650  0.6313283
##   19  0.7918989  0.4765270  0.6377654
##   21  0.7984768  0.4621544  0.6445015
##   23  0.8049482  0.4596135  0.6493005
##   25  0.8082630  0.4608712  0.6523860
##   27  0.8121306  0.4630014  0.6543677
##   29  0.8178176  0.4607051  0.6577542
##   31  0.8229837  0.4553180  0.6621410
##   33  0.8237594  0.4616403  0.6618034
##   35  0.8350231  0.4426253  0.6680268
##   37  0.8403128  0.4353314  0.6700182
##   39  0.8451615  0.4364068  0.6753105
##   41  0.8467969  0.4317912  0.6784253
##   43  0.8489007  0.4361050  0.6796290
##   45  0.8504775  0.4432354  0.6803492
##   47  0.8560303  0.4417025  0.6853719
##   49  0.8639161  0.4245858  0.6918424
##   51  0.8673473  0.4229959  0.6966843
##   53  0.8672099  0.4305794  0.6965331
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 5.
knn_predictions <- predict(knn_model, test_df)
postResample(pred = knn_predictions, obs = test_df$Yield)
##      RMSE  Rsquared       MAE 
## 0.5886968 0.6212766 0.4728644

###SVM

SVM_model <- train(
  Yield ~ ., data = train_df, method = "svmRadial",
  center = TRUE,
  scale = TRUE,
  trControl = trainControl(method = "cv"),
  tuneLength = 25
)
SVM_model
## Support Vector Machines with Radial Basis Function Kernel 
## 
## 144 samples
##  56 predictor
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 130, 131, 129, 129, 128, 130, ... 
## Resampling results across tuning parameters:
## 
##   C           RMSE       Rsquared   MAE      
##         0.25  0.7598759  0.5319167  0.6172971
##         0.50  0.7001846  0.5800715  0.5771984
##         1.00  0.6383843  0.6381623  0.5248135
##         2.00  0.6188822  0.6499979  0.4986247
##         4.00  0.6218002  0.6382372  0.5028194
##         8.00  0.6111060  0.6468982  0.4944172
##        16.00  0.6107367  0.6473759  0.4942189
##        32.00  0.6107367  0.6473759  0.4942189
##        64.00  0.6107367  0.6473759  0.4942189
##       128.00  0.6107367  0.6473759  0.4942189
##       256.00  0.6107367  0.6473759  0.4942189
##       512.00  0.6107367  0.6473759  0.4942189
##      1024.00  0.6107367  0.6473759  0.4942189
##      2048.00  0.6107367  0.6473759  0.4942189
##      4096.00  0.6107367  0.6473759  0.4942189
##      8192.00  0.6107367  0.6473759  0.4942189
##     16384.00  0.6107367  0.6473759  0.4942189
##     32768.00  0.6107367  0.6473759  0.4942189
##     65536.00  0.6107367  0.6473759  0.4942189
##    131072.00  0.6107367  0.6473759  0.4942189
##    262144.00  0.6107367  0.6473759  0.4942189
##    524288.00  0.6107367  0.6473759  0.4942189
##   1048576.00  0.6107367  0.6473759  0.4942189
##   2097152.00  0.6107367  0.6473759  0.4942189
##   4194304.00  0.6107367  0.6473759  0.4942189
## 
## Tuning parameter 'sigma' was held constant at a value of 0.0141604
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.0141604 and C = 16.
svm_predictions <- predict(SVM_model, test_df)
postResample(pred = svm_predictions, obs = test_df$Yield)
##      RMSE  Rsquared       MAE 
## 0.5855824 0.5852346 0.4627231
nnet_grid <- expand.grid(.decay = c(0, 0.01, .1), .size = c(1:10), .bag = FALSE)
nnet_maxnwts <- 5 * ncol(train_df) + 5 + 1
nnet_model <- train(
  Yield ~ ., data = train_df, method = "avNNet",
  center = TRUE,
  scale = TRUE,
  tuneGrid = nnet_grid,
  trControl = trainControl(method = "cv"),
  linout = TRUE,
  trace = FALSE,
  MaxNWts = nnet_maxnwts,
  maxit = 500
)

nnet_model
## Model Averaged Neural Network 
## 
## 144 samples
##  56 predictor
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 130, 129, 130, 129, 131, 128, ... 
## Resampling results across tuning parameters:
## 
##   decay  size  RMSE       Rsquared   MAE      
##   0.00    1    0.8106764  0.4396935  0.6599616
##   0.00    2    0.8293342  0.4935572  0.6710575
##   0.00    3    0.6974592  0.5851408  0.5428590
##   0.00    4    0.8024840  0.4954802  0.6465848
##   0.00    5    0.6992573  0.6102522  0.5599087
##   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    0.8242010  0.4996650  0.6640230
##   0.01    2    0.7733056  0.5824999  0.6107808
##   0.01    3    0.7624698  0.5474645  0.6029362
##   0.01    4    0.6408157  0.6488854  0.5076003
##   0.01    5    0.6748181  0.6196554  0.5335214
##   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    0.7837701  0.5166837  0.6360558
##   0.10    2    0.7223224  0.5842305  0.5797513
##   0.10    3    0.6709759  0.6361814  0.5468508
##   0.10    4    0.6701337  0.6037376  0.5419905
##   0.10    5    0.6330946  0.6480259  0.5209858
##   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 = 5, decay = 0.1 and bag = FALSE.
nnet_predictions <- predict(nnet_model, test_df)
postResample(pred = nnet_predictions, obs = test_df$Yield)
##      RMSE  Rsquared       MAE 
## 0.6086070 0.6236451 0.5003112

7.5b)

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(knn_model, 10)
## loess r-squared variable importance
## 
##   only 20 most important variables shown (out of 56)
## 
##                        Overall
## ManufacturingProcess32  100.00
## ManufacturingProcess13   97.22
## ManufacturingProcess17   86.08
## BiologicalMaterial06     81.51
## ManufacturingProcess36   80.70
## BiologicalMaterial03     75.79
## BiologicalMaterial12     69.48
## ManufacturingProcess09   68.79
## ManufacturingProcess31   67.25
## BiologicalMaterial02     64.68
## ManufacturingProcess06   64.34
## BiologicalMaterial11     53.81
## ManufacturingProcess33   51.48
## BiologicalMaterial04     45.82
## ManufacturingProcess11   45.27
## ManufacturingProcess30   43.72
## ManufacturingProcess29   43.61
## BiologicalMaterial09     42.90
## BiologicalMaterial01     42.04
## BiologicalMaterial08     40.55
varImp(SVM_model, 10)
## loess r-squared variable importance
## 
##   only 20 most important variables shown (out of 56)
## 
##                        Overall
## ManufacturingProcess32  100.00
## ManufacturingProcess13   97.22
## ManufacturingProcess17   86.08
## BiologicalMaterial06     81.51
## ManufacturingProcess36   80.70
## BiologicalMaterial03     75.79
## BiologicalMaterial12     69.48
## ManufacturingProcess09   68.79
## ManufacturingProcess31   67.25
## BiologicalMaterial02     64.68
## ManufacturingProcess06   64.34
## BiologicalMaterial11     53.81
## ManufacturingProcess33   51.48
## BiologicalMaterial04     45.82
## ManufacturingProcess11   45.27
## ManufacturingProcess30   43.72
## ManufacturingProcess29   43.61
## BiologicalMaterial09     42.90
## BiologicalMaterial01     42.04
## BiologicalMaterial08     40.55
varImp(nnet_model, 10)
## loess r-squared variable importance
## 
##   only 20 most important variables shown (out of 56)
## 
##                        Overall
## ManufacturingProcess32  100.00
## ManufacturingProcess13   97.22
## ManufacturingProcess17   86.08
## BiologicalMaterial06     81.51
## ManufacturingProcess36   80.70
## BiologicalMaterial03     75.79
## BiologicalMaterial12     69.48
## ManufacturingProcess09   68.79
## ManufacturingProcess31   67.25
## BiologicalMaterial02     64.68
## ManufacturingProcess06   64.34
## BiologicalMaterial11     53.81
## ManufacturingProcess33   51.48
## BiologicalMaterial04     45.82
## ManufacturingProcess11   45.27
## ManufacturingProcess30   43.72
## ManufacturingProcess29   43.61
## BiologicalMaterial09     42.90
## BiologicalMaterial01     42.04
## BiologicalMaterial08     40.55

7.5c

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?

ggplot(train_df, aes(ManufacturingProcess32, Yield)) +
  geom_point()

ggplot(train_df, aes(ManufacturingProcess13, Yield)) +
  geom_point()

ggplot(train_df, aes(BiologicalMaterial06, Yield)) +
  geom_point()

ggplot(train_df, aes(BiologicalMaterial03, Yield)) +
  geom_point()