require(mlbench)
require(caret)
require(neuralnet)
require(earth)
require(e1071)
require(kernlab)
require(elasticnet)
require(AppliedPredictiveModeling)

7.2 Friedman (1991) introduced several benchmark data sets created by simulation. One of these simulations used the following nonlinear equation to create data:

\[\begin{equation} y = 10sin(\pi x_{1} x_{2}) + 20(x_{3} - 0.5)^2 + 10x_{4} + 5x_{5} + N(0, \sigma^2)\end{equation}\]

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:

set.seed(200)
trainingData <- mlbench.friedman1(200, sd = 1)

# Convert the 'x' data from a matrix to a data frame
trainingData$x <- data.frame(trainingData$x)

# Examine data using
featurePlot(trainingData$x, trainingData$y)

# Create 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. Which models appear to give the best performance? Does MARS select the informative predictors (those named X1 - X5)?

nnetGrid = expand.grid(.decay = c(0, 0.01, .1),
                       .size = c(1:10),
                       .bag = FALSE)
ctrl <- trainControl(method = "cv", number = 10)

nnetTune <- train(trainingData$x, trainingData$y,
                  method = "avNNet",
                  tuneGrid = nnetGrid,
                  trControl = ctrl,
                  preProc = c("center", "scale"),
                  linout = TRUE,
                  trace = FALSE,
                  MaxNWts = 10 * (ncol(trainingData$x) + 1) + 10 + 1,
                  maxit = 500)
## Warning: executing %dopar% sequentially: no parallel backend registered
nnetTune
## 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.427152  0.7727079  1.877410
##   0.00    2    2.434282  0.7673318  1.912576
##   0.00    3    2.055221  0.8290840  1.631535
##   0.00    4    1.962213  0.8453236  1.555980
##   0.00    5    2.450287  0.7765281  1.903426
##   0.00    6    3.053444  0.7112187  2.071329
##   0.00    7    3.585838  0.6846674  2.430196
##   0.00    8    5.332821  0.4212507  3.491635
##   0.00    9    5.978051  0.5547556  3.038404
##   0.00   10    3.215563  0.6188769  2.411575
##   0.01    1    2.398768  0.7703245  1.871203
##   0.01    2    2.388086  0.7723843  1.875899
##   0.01    3    2.068530  0.8262502  1.671387
##   0.01    4    1.996444  0.8370066  1.592990
##   0.01    5    1.989574  0.8437813  1.588518
##   0.01    6    2.270438  0.7957695  1.869313
##   0.01    7    2.139343  0.8244790  1.690482
##   0.01    8    2.231947  0.7999189  1.762487
##   0.01    9    2.415645  0.7749531  1.967882
##   0.01   10    2.457774  0.7645570  1.952877
##   0.10    1    2.403697  0.7687044  1.869519
##   0.10    2    2.429034  0.7668241  1.867627
##   0.10    3    2.089436  0.8255526  1.640594
##   0.10    4    2.015963  0.8373563  1.618945
##   0.10    5    2.091169  0.8212449  1.648915
##   0.10    6    2.161118  0.8140285  1.727497
##   0.10    7    2.346362  0.7889072  1.870386
##   0.10    8    2.274714  0.7939365  1.842615
##   0.10    9    2.366441  0.7818157  1.876866
##   0.10   10    2.318806  0.7794772  1.845449
## 
## 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 and bag
##  = FALSE.
nnetTune.pred <- predict(nnetTune, newdata = testData$x)
postResample(pred = nnetTune.pred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 2.2427881 0.8038198 1.6630160
marsGrid <- expand.grid(.degree = 1:2, .nprune = 2:38)
marsTuned <- train(trainingData$x, trainingData$y,
                   method = "earth",
                   tuneGrid = marsGrid,
                   trControl = trainControl(method = "cv"))

marsTuned
## Multivariate Adaptive Regression Spline 
## 
## 200 samples
##  10 predictor
## 
## No pre-processing
## 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.212446  0.3021419  3.4716858
##   1        3      3.472717  0.5129590  2.7594453
##   1        4      2.652065  0.7328246  2.0910260
##   1        5      2.490567  0.7690196  1.9306226
##   1        6      2.380927  0.7860229  1.8471215
##   1        7      1.880648  0.8646044  1.4552889
##   1        8      1.785272  0.8809243  1.4177585
##   1        9      1.718901  0.8866237  1.3399928
##   1       10      1.683972  0.8893395  1.2967812
##   1       11      1.664429  0.8920935  1.2693977
##   1       12      1.677539  0.8905058  1.2812632
##   1       13      1.658941  0.8941993  1.2597960
##   1       14      1.658941  0.8941993  1.2597960
##   1       15      1.658941  0.8941993  1.2597960
##   1       16      1.658941  0.8941993  1.2597960
##   1       17      1.658941  0.8941993  1.2597960
##   1       18      1.658941  0.8941993  1.2597960
##   1       19      1.658941  0.8941993  1.2597960
##   1       20      1.658941  0.8941993  1.2597960
##   1       21      1.658941  0.8941993  1.2597960
##   1       22      1.658941  0.8941993  1.2597960
##   1       23      1.658941  0.8941993  1.2597960
##   1       24      1.658941  0.8941993  1.2597960
##   1       25      1.658941  0.8941993  1.2597960
##   1       26      1.658941  0.8941993  1.2597960
##   1       27      1.658941  0.8941993  1.2597960
##   1       28      1.658941  0.8941993  1.2597960
##   1       29      1.658941  0.8941993  1.2597960
##   1       30      1.658941  0.8941993  1.2597960
##   1       31      1.658941  0.8941993  1.2597960
##   1       32      1.658941  0.8941993  1.2597960
##   1       33      1.658941  0.8941993  1.2597960
##   1       34      1.658941  0.8941993  1.2597960
##   1       35      1.658941  0.8941993  1.2597960
##   1       36      1.658941  0.8941993  1.2597960
##   1       37      1.658941  0.8941993  1.2597960
##   1       38      1.658941  0.8941993  1.2597960
##   2        2      4.212446  0.3021419  3.4716858
##   2        3      3.472717  0.5129590  2.7594453
##   2        4      2.693135  0.7245626  2.1305231
##   2        5      2.442820  0.7686431  1.9148821
##   2        6      2.344253  0.7846620  1.8172550
##   2        7      1.909792  0.8580800  1.4504219
##   2        8      1.733187  0.8809616  1.3349596
##   2        9      1.630642  0.8955227  1.2663309
##   2       10      1.511062  0.9113611  1.1657024
##   2       11      1.367457  0.9249030  1.0489712
##   2       12      1.303125  0.9319309  1.0039954
##   2       13      1.313746  0.9319897  1.0044841
##   2       14      1.304182  0.9341236  0.9987217
##   2       15      1.331727  0.9321630  1.0189439
##   2       16      1.335661  0.9327188  1.0209595
##   2       17      1.358706  0.9293399  1.0402368
##   2       18      1.358706  0.9293399  1.0402368
##   2       19      1.358706  0.9293399  1.0402368
##   2       20      1.358706  0.9293399  1.0402368
##   2       21      1.358706  0.9293399  1.0402368
##   2       22      1.358706  0.9293399  1.0402368
##   2       23      1.358706  0.9293399  1.0402368
##   2       24      1.358706  0.9293399  1.0402368
##   2       25      1.358706  0.9293399  1.0402368
##   2       26      1.358706  0.9293399  1.0402368
##   2       27      1.358706  0.9293399  1.0402368
##   2       28      1.358706  0.9293399  1.0402368
##   2       29      1.358706  0.9293399  1.0402368
##   2       30      1.358706  0.9293399  1.0402368
##   2       31      1.358706  0.9293399  1.0402368
##   2       32      1.358706  0.9293399  1.0402368
##   2       33      1.358706  0.9293399  1.0402368
##   2       34      1.358706  0.9293399  1.0402368
##   2       35      1.358706  0.9293399  1.0402368
##   2       36      1.358706  0.9293399  1.0402368
##   2       37      1.358706  0.9293399  1.0402368
##   2       38      1.358706  0.9293399  1.0402368
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 12 and degree = 2.
marsTuned.pred <- predict(marsTuned, newdata = testData$x)
postResample(pred = marsTuned.pred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 1.2803060 0.9335241 1.0168673
svmRTuned <- train(trainingData$x, trainingData$y,
                   method = "svmRadial",
                   preProc = c("center", "scale"),
                   tuneLength = 14,
                   trControl = trainControl(method = "cv"))

svmRTuned
## 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.510684  0.8103623  1.983875
##      0.50  2.244988  0.8234750  1.758509
##      1.00  2.055720  0.8428596  1.609290
##      2.00  1.952241  0.8516736  1.515053
##      4.00  1.896963  0.8579892  1.495612
##      8.00  1.896443  0.8580178  1.498113
##     16.00  1.893653  0.8585728  1.496280
##     32.00  1.893653  0.8585728  1.496280
##     64.00  1.893653  0.8585728  1.496280
##    128.00  1.893653  0.8585728  1.496280
##    256.00  1.893653  0.8585728  1.496280
##    512.00  1.893653  0.8585728  1.496280
##   1024.00  1.893653  0.8585728  1.496280
##   2048.00  1.893653  0.8585728  1.496280
## 
## Tuning parameter 'sigma' was held constant at a value of 0.07105405
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.07105405 and C = 16.
svmRTuned.pred <- predict(svmRTuned, newdata = testData$x)
postResample(pred = svmRTuned.pred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 2.0954656 0.8222975 1.5925227
knn.model <- train(x = trainingData$x,
                   y = trainingData$y,
                   method = "knn",
                   preProc = c("center", "scale"),
                   tuneLength = 10)
knn.model
## 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.586485  0.4975146  2.896836
##    7  3.500326  0.5230692  2.823611
##    9  3.392740  0.5664701  2.737414
##   11  3.326507  0.5955090  2.682394
##   13  3.319794  0.6100535  2.668172
##   15  3.306687  0.6246524  2.668343
##   17  3.298392  0.6384923  2.659521
##   19  3.301742  0.6489522  2.676516
##   21  3.321578  0.6503496  2.707272
##   23  3.332881  0.6574033  2.719900
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 17.
knn.pred <- predict(knn.model, newdata = testData$x)
postResample(pred = knn.pred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 3.2040595 0.6819919 2.5683461

Looking at the lowest RMSE score, the MARS model performed the best with a score of 1.28.

varImp(marsTuned)
## earth variable importance
## 
##     Overall
## X1   100.00
## X4    85.05
## X2    69.03
## X5    48.88
## X3    39.40
## X7     0.00
## X9     0.00
## X10    0.00
## X8     0.00
## X6     0.00

We can see that the model did indeed select the informative predictors X1 - X5.

7.5 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.

data("ChemicalManufacturingProcess")
chem <- ChemicalManufacturingProcess

# Impute NA's with mean
# We could simply use complete.cases(), but the question asks
# us to impute the data instead.
for(i in 1:ncol(chem)){
  chem[is.na(chem[, i]), i] <- mean(chem[, i], na.rm = TRUE)
}

smpl <- floor(0.75 * nrow(chem))
set.seed(500)

indices <- sample(seq_len(nrow(chem)),
                  size = smpl)
chem.train <- chem[indices, ]
chem.test <- chem[-indices, ]
c.nnetTune <- train(chem.train[, c(2:58)], chem.train$Yield,
                  method = "avNNet",
                  tuneGrid = nnetGrid,
                  trControl = ctrl,
                  preProc = c("center", "scale"),
                  linout = TRUE,
                  trace = FALSE,
                  MaxNWts = 10 * (ncol(chem.train[, c(2:58)]) + 1) + 10 + 1,
                  maxit = 500)

c.nnetTune
## Model Averaged Neural Network 
## 
## 132 samples
##  57 predictor
## 
## Pre-processing: centered (57), scaled (57) 
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 118, 118, 119, 120, 118, 120, ... 
## Resampling results across tuning parameters:
## 
##   decay  size  RMSE      Rsquared   MAE     
##   0.00    1    1.540484  0.3710816  1.282657
##   0.00    2    1.500332  0.3851505  1.234164
##   0.00    3    1.582423  0.3752830  1.290678
##   0.00    4    2.002771  0.2815988  1.686315
##   0.00    5    2.216152  0.2942267  1.778448
##   0.00    6    1.990009  0.3151599  1.562415
##   0.00    7    2.228044  0.2957739  1.793974
##   0.00    8    2.933695  0.2799302  2.233089
##   0.00    9    3.810005  0.1845336  2.823672
##   0.00   10    5.601943  0.1454208  3.893492
##   0.01    1    1.399486  0.4389382  1.122154
##   0.01    2    1.527744  0.4826590  1.257682
##   0.01    3    1.634446  0.4373145  1.294632
##   0.01    4    1.995117  0.3829491  1.486498
##   0.01    5    1.982764  0.3428744  1.581890
##   0.01    6    1.946596  0.3435212  1.461820
##   0.01    7    1.465854  0.4476063  1.176903
##   0.01    8    1.483262  0.4492530  1.242289
##   0.01    9    1.470047  0.4590768  1.206807
##   0.01   10    1.942021  0.2952918  1.582033
##   0.10    1    1.530769  0.4610690  1.170213
##   0.10    2    1.735851  0.3901363  1.377958
##   0.10    3    1.855225  0.4407307  1.449525
##   0.10    4    2.121765  0.4142139  1.670333
##   0.10    5    2.173475  0.4110713  1.577619
##   0.10    6    2.170186  0.3827452  1.617210
##   0.10    7    2.146464  0.3955249  1.598996
##   0.10    8    2.205528  0.3407110  1.578478
##   0.10    9    2.182696  0.3808684  1.532016
##   0.10   10    1.931678  0.3404045  1.524651
## 
## 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 = 1, decay = 0.01 and bag
##  = FALSE.
c.nnetTune.pred <- predict(c.nnetTune, newdata = chem.test[, c(2:58)])
postResample(pred = c.nnetTune.pred, obs = chem.test$Yield)
##      RMSE  Rsquared       MAE 
## 1.4274537 0.5065262 1.1813058
c.marsTuned <- train(chem.train[, c(2:58)], chem.train$Yield,
                   method = "earth",
                   tuneGrid = marsGrid,
                   trControl = trainControl(method = "cv"))

c.marsTuned
## 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, 119, ... 
## Resampling results across tuning parameters:
## 
##   degree  nprune  RMSE      Rsquared   MAE      
##   1        2      1.405640  0.4198687  1.1027672
##   1        3      1.204084  0.5527844  0.9411505
##   1        4      1.225335  0.5373171  0.9765592
##   1        5      1.283595  0.5129750  1.0365319
##   1        6      1.370793  0.4656111  1.1177620
##   1        7      1.376112  0.4761595  1.0992856
##   1        8      1.355913  0.4938236  1.0739463
##   1        9      1.407084  0.4719873  1.1167820
##   1       10      1.441237  0.4453103  1.1541519
##   1       11      1.438092  0.4521872  1.1519743
##   1       12      1.433341  0.4657832  1.1378658
##   1       13      1.442149  0.4606450  1.1454513
##   1       14      1.436164  0.4706398  1.1403077
##   1       15      1.436953  0.4662332  1.1444445
##   1       16      1.436953  0.4662332  1.1444445
##   1       17      1.434065  0.4696881  1.1445996
##   1       18      1.438190  0.4675597  1.1466872
##   1       19      1.458473  0.4507862  1.1704898
##   1       20      1.436769  0.4682910  1.1565554
##   1       21      1.449003  0.4596280  1.1619368
##   1       22      1.457259  0.4543248  1.1653079
##   1       23      1.493276  0.4394513  1.1962647
##   1       24      1.493276  0.4394513  1.1962647
##   1       25      1.493276  0.4394513  1.1962647
##   1       26      1.493276  0.4394513  1.1962647
##   1       27      1.493276  0.4394513  1.1962647
##   1       28      1.493276  0.4394513  1.1962647
##   1       29      1.493276  0.4394513  1.1962647
##   1       30      1.493276  0.4394513  1.1962647
##   1       31      1.493276  0.4394513  1.1962647
##   1       32      1.493276  0.4394513  1.1962647
##   1       33      1.493276  0.4394513  1.1962647
##   1       34      1.493276  0.4394513  1.1962647
##   1       35      1.493276  0.4394513  1.1962647
##   1       36      1.493276  0.4394513  1.1962647
##   1       37      1.493276  0.4394513  1.1962647
##   1       38      1.493276  0.4394513  1.1962647
##   2        2      1.405640  0.4198687  1.1027672
##   2        3      1.341527  0.4757920  1.0317014
##   2        4      1.244937  0.5363141  0.9895591
##   2        5      1.188321  0.5653586  0.9700304
##   2        6      1.255971  0.5242005  1.0006949
##   2        7      1.308966  0.5137854  1.0407030
##   2        8      1.248297  0.5533733  0.9902366
##   2        9      1.324066  0.5254465  1.0380904
##   2       10      1.322544  0.5414039  1.0367127
##   2       11      1.340566  0.5486054  1.0542886
##   2       12      1.338294  0.5414304  1.0573853
##   2       13      1.310544  0.5694702  1.0379755
##   2       14      1.320437  0.5780592  1.0501376
##   2       15      1.296171  0.5957070  1.0337207
##   2       16      1.298088  0.5845528  1.0362980
##   2       17      1.285853  0.5936393  1.0253206
##   2       18      1.273424  0.6069209  1.0156772
##   2       19      1.268778  0.5986489  0.9857518
##   2       20      1.476297  0.5586363  1.0852079
##   2       21      1.487704  0.5616790  1.1046292
##   2       22      1.477946  0.5731485  1.1089639
##   2       23      1.456536  0.5835862  1.0938680
##   2       24      1.473457  0.5803368  1.1098869
##   2       25      1.473457  0.5803368  1.1098869
##   2       26      1.473457  0.5803368  1.1098869
##   2       27      1.473457  0.5803368  1.1098869
##   2       28      1.473457  0.5803368  1.1098869
##   2       29      1.473457  0.5803368  1.1098869
##   2       30      1.473457  0.5803368  1.1098869
##   2       31      1.473457  0.5803368  1.1098869
##   2       32      1.473457  0.5803368  1.1098869
##   2       33      1.473457  0.5803368  1.1098869
##   2       34      1.473457  0.5803368  1.1098869
##   2       35      1.473457  0.5803368  1.1098869
##   2       36      1.473457  0.5803368  1.1098869
##   2       37      1.473457  0.5803368  1.1098869
##   2       38      1.473457  0.5803368  1.1098869
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 5 and degree = 2.
c.marsTuned.pred <- predict(c.marsTuned, newdata = chem.test[, c(2:58)])
postResample(pred = c.marsTuned.pred, obs = chem.test$Yield)
##      RMSE  Rsquared       MAE 
## 1.2474725 0.6218236 1.0000345
c.svmRTuned <- train(chem.train[, c(2:58)], chem.train$Yield,
                   method = "svmRadial",
                   preProc = c("center", "scale"),
                   tuneLength = 14,
                   trControl = trainControl(method = "cv"))

c.svmRTuned
## 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, 117, 119, 120, 119, 119, ... 
## Resampling results across tuning parameters:
## 
##   C        RMSE      Rsquared   MAE      
##      0.25  1.388537  0.4851079  1.1372444
##      0.50  1.279626  0.5334872  1.0577804
##      1.00  1.198902  0.5755363  0.9878005
##      2.00  1.129038  0.6250191  0.9332085
##      4.00  1.097768  0.6544295  0.9005805
##      8.00  1.116161  0.6441875  0.9166731
##     16.00  1.106722  0.6493945  0.9088355
##     32.00  1.106722  0.6493945  0.9088355
##     64.00  1.106722  0.6493945  0.9088355
##    128.00  1.106722  0.6493945  0.9088355
##    256.00  1.106722  0.6493945  0.9088355
##    512.00  1.106722  0.6493945  0.9088355
##   1024.00  1.106722  0.6493945  0.9088355
##   2048.00  1.106722  0.6493945  0.9088355
## 
## Tuning parameter 'sigma' was held constant at a value of 0.01184455
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01184455 and C = 4.
c.svmRTuned.pred <- predict(c.svmRTuned, newdata = chem.test[, c(2:58)])
postResample(pred = c.svmRTuned.pred, obs = chem.test$Yield)
##      RMSE  Rsquared       MAE 
## 1.1248444 0.6890303 0.7871013
c.knn <- train(x = chem.test[, c(2:58)],
                   y = chem.test$Yield,
                   method = "knn",
                   preProc = c("center", "scale"),
                   tuneLength = 10)
c.knn
## k-Nearest Neighbors 
## 
## 44 samples
## 57 predictors
## 
## Pre-processing: centered (57), scaled (57) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 44, 44, 44, 44, 44, 44, ... 
## Resampling results across tuning parameters:
## 
##   k   RMSE      Rsquared   MAE     
##    5  1.707041  0.4030052  1.384239
##    7  1.731494  0.3945085  1.408923
##    9  1.733158  0.4044277  1.407988
##   11  1.730961  0.4323474  1.403720
##   13  1.755071  0.4229475  1.418744
##   15  1.765401  0.4383027  1.428595
##   17  1.786617  0.4476249  1.451869
##   19  1.800604  0.4606678  1.462370
##   21  1.824636  0.4670124  1.478055
##   23  1.842358  0.4740013  1.490642
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 5.
predict.cknn <- predict(c.knn, newdata = chem.test[, c(2:58)])
postResample(pred = predict.cknn, obs = chem.test$Yield)
##      RMSE  Rsquared       MAE 
## 1.2070967 0.6928683 0.9762727

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

This time, the SVM model gives us the best RSME score with 1.125.

b) 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?

c.svmRTuned$finalModel
## Support Vector Machine object of class "ksvm" 
## 
## SV type: eps-svr  (regression) 
##  parameter : epsilon = 0.1  cost C = 4 
## 
## Gaussian Radial Basis kernel function. 
##  Hyperparameter : sigma =  0.0118445493876638 
## 
## Number of Support Vectors : 120 
## 
## Objective Function Value : -95.674 
## Training error : 0.056733

It doesn’t seem like the SVM object allows us to examine which predictors were most important. We can see that the final model uses 118 data points from the training set, or 67%.

lmFit <- train(x = chem.train[, c(2:58)], y = chem.train$Yield,
               method = 'lm', trControl = ctrl)
predict.lmFit <- predict(lmFit, newdata = chem.test[, c(2:58)])
postResample(pred = predict.lmFit, obs = chem.test$Yield)
##      RMSE  Rsquared       MAE 
## 1.5716518 0.4338667 1.2734679
plsTune <- train(chem.train[, c(2:58)], chem.train$Yield,
                 method = "pls",
                 tuneLength = 20,
                 trControl = ctrl,
                 preProc = c("center", "scale"))
predict.plsTune <- predict(plsTune, newdata = chem.test[, c(2:58)])
postResample(pred = predict.plsTune, obs = chem.test$Yield)
##      RMSE  Rsquared       MAE 
## 1.1789877 0.6568608 0.9391095
ridgeGrid <- data.frame(.lambda = seq(0, .1, length = 15))
ridgeRegFit <- train(chem.train[, c(2:58)], chem.train$Yield,
                     method = "ridge",
                     tuneGrid = ridgeGrid,
                     trControl = ctrl,
                     preProc = c("center", "scale"))
predict.ridgeRegFit <- predict(ridgeRegFit, newdata = chem.test[, c(2:58)])
postResample(pred = predict.ridgeRegFit, obs = chem.test$Yield)
##      RMSE  Rsquared       MAE 
## 1.1220704 0.6925314 0.8673341

However, we can look at the linear models, of which the penalized model seemed to perform best.

ridgeRegFit$finalModel
## 
## Call:
## elasticnet::enet(x = as.matrix(x), y = y, lambda = param$lambda)
## Sequence of  moves:
##      ManufacturingProcess32 ManufacturingProcess09 ManufacturingProcess13
## Var                      44                     21                     25
## Step                      1                      2                      3
##      ManufacturingProcess17 ManufacturingProcess36 BiologicalMaterial06
## Var                      29                     48                    6
## Step                      4                      5                    6
##      ManufacturingProcess06 ManufacturingProcess15 BiologicalMaterial03
## Var                      18                     27                    3
## Step                      7                      8                    9
##      ManufacturingProcess44 ManufacturingProcess11 ManufacturingProcess37
## Var                      56                     23                     49
## Step                     10                     11                     12
##      ManufacturingProcess39 ManufacturingProcess45 ManufacturingProcess34
## Var                      51                     57                     46
## Step                     13                     14                     15
##      BiologicalMaterial07 ManufacturingProcess04 ManufacturingProcess43
## Var                     7                     16                     55
## Step                   16                     17                     18
##      ManufacturingProcess23 ManufacturingProcess38 ManufacturingProcess07
## Var                      35                     50                     19
## Step                     19                     20                     21
##      ManufacturingProcess24 ManufacturingProcess30 ManufacturingProcess12
## Var                      36                     42                     24
## Step                     22                     23                     24
##      ManufacturingProcess28 BiologicalMaterial10 ManufacturingProcess14
## Var                      40                   10                     26
## Step                     25                   26                     27
##      ManufacturingProcess18 BiologicalMaterial12 ManufacturingProcess19
## Var                      30                   12                     31
## Step                     28                   29                     30
##      BiologicalMaterial09 ManufacturingProcess29 ManufacturingProcess05
## Var                     9                     41                     17
## Step                   31                     32                     33
##      ManufacturingProcess02 BiologicalMaterial01 ManufacturingProcess22
## Var                      14                    1                     34
## Step                     34                   35                     36
##      ManufacturingProcess35 ManufacturingProcess16 ManufacturingProcess20
## Var                      47                     28                     32
## Step                     37                     38                     39
##      ManufacturingProcess30 ManufacturingProcess31 ManufacturingProcess41
## Var                     -42                     43                     53
## Step                     40                     41                     42
##      ManufacturingProcess03 ManufacturingProcess30 BiologicalMaterial02
## Var                      15                     42                    2
## Step                     43                     44                   45
##      ManufacturingProcess40 ManufacturingProcess21 ManufacturingProcess27
## Var                      52                     33                     39
## Step                     46                     47                     48
##      ManufacturingProcess25 ManufacturingProcess42 BiologicalMaterial08
## Var                      37                     54                    8
## Step                     49                     50                   51
##      BiologicalMaterial05 ManufacturingProcess08 ManufacturingProcess01
## Var                     5                     20                     13
## Step                   52                     53                     54
##      ManufacturingProcess10 BiologicalMaterial04 ManufacturingProcess26
## Var                      22                    4                     38
## Step                     55                   56                     57
##      ManufacturingProcess33 BiologicalMaterial11   
## Var                      45                   11 60
## Step                     58                   59 60

The top ten of that model appears to be 9 ManufacturingProcesses and 1 BiologicalProcess.

c) 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?

This seems like a strange question as only one of the four models in this chapter allow you to examine the predictors, at least when using the train function. This is unlike the vanilla linear regression where you can see how each predictor has a specific weight, or the MARS model which does seem to rank the predictors.