Exercise 7.2

Friedman (1991) introduced several benchmark data sets create by sim- ulation. One of these simulations used the following nonlinear equation to create data:

\(y = 10\sin({\pi x_1 x_2}) + 20(x_3 - 0.5)^2 +10x_4 + 5x_5 + N(0, \sigma^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)
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. For example:

K-Nearest Neighbors

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

Multivariate Adaptive Regression Splines (MARS)

library(earth)

# define grid
marsGrid <- expand.grid(.degree = 1:2, .nprune = 2:38)

# tune MARS
marsTuned <- train(
  trainingData$x, 
  trainingData$y,
  method = "earth",
  #Explicitly declare the candidate models to test
  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.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
##   1       16      1.666354  0.8879143  1.285545
##   1       17      1.666354  0.8879143  1.285545
##   1       18      1.666354  0.8879143  1.285545
##   1       19      1.666354  0.8879143  1.285545
##   1       20      1.666354  0.8879143  1.285545
##   1       21      1.666354  0.8879143  1.285545
##   1       22      1.666354  0.8879143  1.285545
##   1       23      1.666354  0.8879143  1.285545
##   1       24      1.666354  0.8879143  1.285545
##   1       25      1.666354  0.8879143  1.285545
##   1       26      1.666354  0.8879143  1.285545
##   1       27      1.666354  0.8879143  1.285545
##   1       28      1.666354  0.8879143  1.285545
##   1       29      1.666354  0.8879143  1.285545
##   1       30      1.666354  0.8879143  1.285545
##   1       31      1.666354  0.8879143  1.285545
##   1       32      1.666354  0.8879143  1.285545
##   1       33      1.666354  0.8879143  1.285545
##   1       34      1.666354  0.8879143  1.285545
##   1       35      1.666354  0.8879143  1.285545
##   1       36      1.666354  0.8879143  1.285545
##   1       37      1.666354  0.8879143  1.285545
##   1       38      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.833780  0.8622906  1.462210
##   2        8      1.688673  0.8883137  1.325754
##   2        9      1.557314  0.9002634  1.234207
##   2       10      1.463018  0.9133897  1.174354
##   2       11      1.350247  0.9265882  1.099432
##   2       12      1.305955  0.9344683  1.049853
##   2       13      1.261130  0.9357469  1.017123
##   2       14      1.286463  0.9315381  1.040156
##   2       15      1.337104  0.9297651  1.069602
##   2       16      1.337560  0.9294593  1.067973
##   2       17      1.318152  0.9322833  1.054436
##   2       18      1.324331  0.9319631  1.055971
##   2       19      1.324331  0.9319631  1.055971
##   2       20      1.324331  0.9319631  1.055971
##   2       21      1.324331  0.9319631  1.055971
##   2       22      1.324331  0.9319631  1.055971
##   2       23      1.324331  0.9319631  1.055971
##   2       24      1.324331  0.9319631  1.055971
##   2       25      1.324331  0.9319631  1.055971
##   2       26      1.324331  0.9319631  1.055971
##   2       27      1.324331  0.9319631  1.055971
##   2       28      1.324331  0.9319631  1.055971
##   2       29      1.324331  0.9319631  1.055971
##   2       30      1.324331  0.9319631  1.055971
##   2       31      1.324331  0.9319631  1.055971
##   2       32      1.324331  0.9319631  1.055971
##   2       33      1.324331  0.9319631  1.055971
##   2       34      1.324331  0.9319631  1.055971
##   2       35      1.324331  0.9319631  1.055971
##   2       36      1.324331  0.9319631  1.055971
##   2       37      1.324331  0.9319631  1.055971
##   2       38      1.324331  0.9319631  1.055971
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 13 and degree = 2.
marsPred <- predict(marsTuned, newdata = testData$x)
## The function 'postResample' can be used to get the test set > ## perforamnce values
postResample(pred = marsPred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 1.2803060 0.9335241 1.0168673

Neural Network

# remove highly correlated predictors
tooHigh <- findCorrelation(cor(trainingData$x), cutoff = .75)

# create the tuning grid
nnetGrid <- expand.grid(.decay = c(0, 0.01, .1),
                        .size = c(1:10),
                        ## The next option is to use bagging (see the
                        ## next chapter) instead of different random
                        ## seeds.
                        .bag = FALSE)

# tune NN
nnetTune <- train(trainingData$x, trainingData$y,
                  method = "avNNet",
                  tuneGrid = nnetGrid,
                  trControl = trainControl(method = "cv", number = 10),
                  ## Automatically standardize data prior to modeling
                  ## and prediction
                  preProc = c("center", "scale"),
                  linout = TRUE,
                  trace = FALSE,
                  MaxNWts = 10 * (ncol(trainingData$x) + 1) + 10 + 1,
                  maxit = 500)

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.419725  0.7600512  1.922185
##   0.00    2    2.495328  0.7467163  1.998203
##   0.00    3    2.040480  0.8281400  1.621116
##   0.00    4    2.191467  0.8086458  1.595739
##   0.00    5    2.194545  0.7947692  1.748113
##   0.00    6    2.995902  0.6849667  2.184923
##   0.00    7    3.762153  0.6349248  2.477336
##   0.00    8    5.805024  0.4514865  3.634359
##   0.00    9    4.867524  0.5705756  3.088120
##   0.00   10    5.828195  0.5032618  2.987421
##   0.01    1    2.441040  0.7589688  1.927828
##   0.01    2    2.408871  0.7645731  1.924567
##   0.01    3    2.094391  0.8207051  1.639697
##   0.01    4    2.019225  0.8369715  1.608601
##   0.01    5    2.197496  0.8115482  1.769553
##   0.01    6    2.184105  0.8123682  1.774378
##   0.01    7    2.284499  0.7969623  1.834786
##   0.01    8    2.476675  0.7577190  1.988770
##   0.01    9    2.432184  0.7720917  1.935645
##   0.01   10    2.462261  0.7654957  1.996303
##   0.10    1    2.451065  0.7561336  1.935321
##   0.10    2    2.439989  0.7533877  1.925577
##   0.10    3    2.142421  0.8119014  1.697563
##   0.10    4    2.021446  0.8367615  1.613084
##   0.10    5    2.058636  0.8303746  1.641050
##   0.10    6    2.151898  0.8133563  1.685223
##   0.10    7    2.168022  0.8182923  1.705153
##   0.10    8    2.339965  0.7874674  1.822966
##   0.10    9    2.278679  0.7959165  1.826874
##   0.10   10    2.298335  0.7926169  1.854024
## 
## 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(nnetTune, newdata = testData$x)
## The function 'postResample' can be used to get the test set > ## perforamnce values
postResample(pred = nnetPred, obs = testData$y)
##     RMSE Rsquared      MAE 
## 2.061504 0.830589 1.561138

Support Vector Machines

library(kernlab)
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.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.873466  0.8639462  1.479626
##     16.00  1.886675  0.8626888  1.496977
##     32.00  1.886675  0.8626888  1.496977
##     64.00  1.886675  0.8626888  1.496977
##    128.00  1.886675  0.8626888  1.496977
##    256.00  1.886675  0.8626888  1.496977
##    512.00  1.886675  0.8626888  1.496977
##   1024.00  1.886675  0.8626888  1.496977
##   2048.00  1.886675  0.8626888  1.496977
## 
## 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(svmRTuned, newdata = testData$x)
## The function 'postResample' can be used to get the test set > ## perforamnce values
postResample(pred = svmPred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 2.0515197 0.8294511 1.5564698

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

Multivariate Adaptive Regression Spline (MARS) has the best performance with the highest \(R^2\) of 0.9335241 and the lowest RMSE & MAE.

varImp(marsTuned)
## earth variable importance
## 
##    Overall
## X1  100.00
## X4   75.33
## X2   48.88
## X5   15.63
## X3    0.00

Using the varImp function, we can see that MARS select the informative predictors (X1-X5).

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

library("AppliedPredictiveModeling")

data(ChemicalManufacturingProcess)

# separate response and predictors
yield <- ChemicalManufacturingProcess$Yield
predictors <- ChemicalManufacturingProcess[, -1]

ncol(predictors)
## [1] 57
# Check NAs
sum(is.na(ChemicalManufacturingProcess))
## [1] 106
# Impute using KNN
preProc <- preProcess(
  ChemicalManufacturingProcess, 
  method = "knnImpute")

chemical_imp <- predict(preProc, ChemicalManufacturingProcess)

# check for missing values after imputation
sum(is.na(chemical_imp))
## [1] 0
library(dplyr)
predictors_imp <- chemical_imp %>% select(-Yield)

# split data into X & y
X <- predictors_imp
y <- chemical_imp$Yield

# find near-zero variance predictors
nzv_pred <- nearZeroVar(X)

# remove the near-zero variance predictors
filtered_pred <- X[ , -nzv_pred]

# Indexing for train/test split
split <- createDataPartition(y, p = 0.8, list = FALSE)

# train and test sets
train_x <- X[ split, , drop = FALSE]
test_x  <- X[-split, , drop = FALSE]
train_y <- y[ split]
test_y  <- y[-split]

Part A

# MARS
# define grid
marsGrid_chem <- expand.grid(.degree = 1:2, .nprune = 2:38)

# tune MARS
marsTuned_chem <- train(
  train_x, 
  train_y,
  method = "earth",
  #Explicitly declare the candidate models to test
  tuneGrid = marsGrid_chem,
  trControl = trainControl(method = "cv"))

# KNN
knnModel_chem <- train(
  x=train_x,
  y=train_y,
  method = "knn",
  preProc = c("center", "scale"),
  tuneLength = 10)


# NNET
# remove highly correlated predictors
tooHigh_chem <- findCorrelation(cor(train_x), cutoff = .75)

# remove highly correlated predictors
trainXnnet <- train_x[, -tooHigh]
testXnnet <- test_x[, -tooHigh]

# create the tuning grid
nnetGrid_chem <- expand.grid(.decay = c(0, 0.01, .1),
                        .size = c(1:10),
                        ## The next option is to use bagging (see the
                        ## next chapter) instead of different random
                        ## seeds.
                        .bag = FALSE)

# tune NN
nnetTune_chem <- train(trainXnnet, train_y,
                  method = "avNNet",
                  tuneGrid = nnetGrid_chem,
                  trControl = trainControl(method = "cv", number = 10),
                  ## Automatically standardize data prior to modeling
                  ## and prediction
                  preProc = c("center", "scale"),
                  linout = TRUE,
                  trace = FALSE,
                  MaxNWts = 10 * (ncol(trainXnnet) + 1) + 10 + 1,
                  maxit = 500)


# SVM
svmRTuned_chem <- train(train_x, train_y,
                   method = "svmRadial",
                   preProc = c("center", "scale"),
                   tuneLength = 14,
                   trControl = trainControl(method = "cv"))

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

## MARS
marsTuned_chem
## Multivariate Adaptive Regression Spline 
## 
## 144 samples
##  57 predictor
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 130, 130, 129, 131, 129, 129, ... 
## Resampling results across tuning parameters:
## 
##   degree  nprune  RMSE       Rsquared   MAE      
##   1        2      0.7631955  0.4363496  0.6109440
##   1        3      0.6440138  0.5864263  0.5254394
##   1        4      0.6109918  0.6196480  0.4955327
##   1        5      0.6170599  0.6083566  0.4989713
##   1        6      0.6069837  0.6194842  0.5004050
##   1        7      0.5970365  0.6345593  0.4883689
##   1        8      0.6000076  0.6243361  0.4882207
##   1        9      0.5735101  0.6593649  0.4753829
##   1       10      0.5612333  0.6775516  0.4532632
##   1       11      0.5809611  0.6508365  0.4760776
##   1       12      0.5677255  0.6636956  0.4689835
##   1       13      0.5553590  0.6800826  0.4576933
##   1       14      0.5613778  0.6808101  0.4579713
##   1       15      0.5528379  0.6855262  0.4521402
##   1       16      0.5808073  0.6605890  0.4687336
##   1       17      0.5818368  0.6585725  0.4681260
##   1       18      0.5818368  0.6585725  0.4681260
##   1       19      0.5818368  0.6585725  0.4681260
##   1       20      0.5818368  0.6585725  0.4681260
##   1       21      0.5818368  0.6585725  0.4681260
##   1       22      0.5818368  0.6585725  0.4681260
##   1       23      0.5818368  0.6585725  0.4681260
##   1       24      0.5818368  0.6585725  0.4681260
##   1       25      0.5818368  0.6585725  0.4681260
##   1       26      0.5818368  0.6585725  0.4681260
##   1       27      0.5818368  0.6585725  0.4681260
##   1       28      0.5818368  0.6585725  0.4681260
##   1       29      0.5818368  0.6585725  0.4681260
##   1       30      0.5818368  0.6585725  0.4681260
##   1       31      0.5818368  0.6585725  0.4681260
##   1       32      0.5818368  0.6585725  0.4681260
##   1       33      0.5818368  0.6585725  0.4681260
##   1       34      0.5818368  0.6585725  0.4681260
##   1       35      0.5818368  0.6585725  0.4681260
##   1       36      0.5818368  0.6585725  0.4681260
##   1       37      0.5818368  0.6585725  0.4681260
##   1       38      0.5818368  0.6585725  0.4681260
##   2        2      0.7631955  0.4363496  0.6109440
##   2        3      0.6470632  0.5924864  0.5270817
##   2        4      0.6396325  0.5916937  0.5207741
##   2        5      0.6283068  0.6111429  0.5132229
##   2        6      0.6258862  0.6087404  0.5145809
##   2        7      0.6131111  0.6232948  0.5085262
##   2        8      0.6049904  0.6312067  0.4991772
##   2        9      0.6234253  0.6211586  0.5146433
##   2       10      0.6220739  0.6225294  0.5094707
##   2       11      0.6377851  0.6153966  0.5146206
##   2       12      0.6517990  0.6086667  0.5180620
##   2       13      0.7143630  0.5457657  0.5440398
##   2       14      0.7023602  0.5587237  0.5395766
##   2       15      0.7127463  0.5526217  0.5458197
##   2       16      0.7346962  0.5365035  0.5554790
##   2       17      0.7287647  0.5424889  0.5605560
##   2       18      0.7416005  0.5414538  0.5628938
##   2       19      0.7401052  0.5429435  0.5626850
##   2       20      0.7437663  0.5429911  0.5655844
##   2       21      0.7461910  0.5405295  0.5672107
##   2       22      0.7510433  0.5480009  0.5761550
##   2       23      0.7501402  0.5503526  0.5774957
##   2       24      0.7524924  0.5516725  0.5761291
##   2       25      0.7501641  0.5525034  0.5769361
##   2       26      0.7523436  0.5511605  0.5775523
##   2       27      0.7499393  0.5524606  0.5769365
##   2       28      0.7499393  0.5524606  0.5769365
##   2       29      0.7499393  0.5524606  0.5769365
##   2       30      0.7499393  0.5524606  0.5769365
##   2       31      0.7499393  0.5524606  0.5769365
##   2       32      0.7499393  0.5524606  0.5769365
##   2       33      0.7499393  0.5524606  0.5769365
##   2       34      0.7499393  0.5524606  0.5769365
##   2       35      0.7499393  0.5524606  0.5769365
##   2       36      0.7499393  0.5524606  0.5769365
##   2       37      0.7499393  0.5524606  0.5769365
##   2       38      0.7499393  0.5524606  0.5769365
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 15 and degree = 1.
marsPred_chem <- predict(marsTuned_chem, newdata = test_x)
## The function 'postResample' can be used to get the test set > ## perforamnce values
postResample(pred = marsPred_chem, obs = test_y)
##      RMSE  Rsquared       MAE 
## 0.7154031 0.5393501 0.5966876
## KNN
knnModel_chem
## k-Nearest Neighbors 
## 
## 144 samples
##  57 predictor
## 
## Pre-processing: centered (57), scaled (57) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 144, 144, 144, 144, 144, 144, ... 
## Resampling results across tuning parameters:
## 
##   k   RMSE       Rsquared   MAE      
##    5  0.7743241  0.3894620  0.6182410
##    7  0.7798555  0.3832464  0.6290492
##    9  0.7758152  0.3912501  0.6243355
##   11  0.7702587  0.4001183  0.6233166
##   13  0.7736981  0.3979875  0.6297292
##   15  0.7731161  0.4044828  0.6290953
##   17  0.7761297  0.4024675  0.6316261
##   19  0.7765799  0.4063351  0.6330290
##   21  0.7808744  0.4026191  0.6371000
##   23  0.7810561  0.4061944  0.6365239
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 11.
knnPred_chem <- predict(knnModel_chem, newdata = test_x)
## The function 'postResample' can be used to get the test set > ## perforamnce values
postResample(pred = knnPred_chem, obs = test_y)
##      RMSE  Rsquared       MAE 
## 0.7644244 0.4958721 0.6055647
## NN
nnetTune_chem
## Model Averaged Neural Network 
## 
## 144 samples
##   0 predictor
## 
## Pre-processing:  (None) 
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 130, 129, 129, 131, 130, 130, ... 
## Resampling results across tuning parameters:
## 
##   decay  size  RMSE       Rsquared    MAE      
##   0.00    1    0.9789863  0.08449893  0.8050629
##   0.00    2    0.9800041  0.09941237  0.8060070
##   0.00    3    0.9794656  0.16338668  0.8055275
##   0.00    4    0.9794801  0.09903249  0.8054793
##   0.00    5    0.9797852  0.09081934  0.8057106
##   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.9794886  0.11190706  0.8056421
##   0.01    2    0.9796423  0.10281933  0.8056455
##   0.01    3    0.9801145  0.09457417  0.8060257
##   0.01    4    0.9797303  0.09889661  0.8056066
##   0.01    5    0.9798515  0.10958811  0.8055795
##   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.9800237  0.10497326  0.8058908
##   0.10    2    0.9796742  0.09944242  0.8055803
##   0.10    3    0.9796724  0.10270670  0.8055796
##   0.10    4    0.9797739  0.10478344  0.8056647
##   0.10    5    0.9796559  0.10263843  0.8055650
##   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 = 1, decay = 0 and bag = FALSE.
nnetPred_chem <- predict(nnetTune_chem, newdata = testXnnet)
## The function 'postResample' can be used to get the test set > ## perforamnce values
postResample(pred = nnetPred_chem, obs = test_y)
##       RMSE   Rsquared        MAE 
## 1.05293399 0.01133492 0.81950201
## SVM
svmRTuned_chem
## Support Vector Machines with Radial Basis Function Kernel 
## 
## 144 samples
##  57 predictor
## 
## Pre-processing: centered (57), scaled (57) 
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 128, 130, 129, 130, 129, 130, ... 
## Resampling results across tuning parameters:
## 
##   C        RMSE       Rsquared   MAE      
##      0.25  0.7507242  0.5017828  0.6091422
##      0.50  0.6812876  0.5623602  0.5528164
##      1.00  0.6279525  0.6066260  0.5076243
##      2.00  0.6141680  0.6082620  0.4894342
##      4.00  0.6149394  0.6039644  0.4983553
##      8.00  0.6145155  0.6051616  0.4983913
##     16.00  0.6117165  0.6084307  0.4959559
##     32.00  0.6117165  0.6084307  0.4959559
##     64.00  0.6117165  0.6084307  0.4959559
##    128.00  0.6117165  0.6084307  0.4959559
##    256.00  0.6117165  0.6084307  0.4959559
##    512.00  0.6117165  0.6084307  0.4959559
##   1024.00  0.6117165  0.6084307  0.4959559
##   2048.00  0.6117165  0.6084307  0.4959559
## 
## Tuning parameter 'sigma' was held constant at a value of 0.01425918
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01425918 and C = 16.
svmPred_chem <- predict(svmRTuned_chem, newdata = test_x)
## The function 'postResample' can be used to get the test set > ## perforamnce values
postResample(pred = svmPred_chem, obs = test_y)
##      RMSE  Rsquared       MAE 
## 0.6896534 0.5946555 0.5235667

The SVM model provides the optimal resampling and test set performance.

Part 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?

Below are the most important predictors in the nonlinear model.

varImp(svmRTuned_chem)
## loess r-squared variable importance
## 
##   only 20 most important variables shown (out of 57)
## 
##                        Overall
## BiologicalMaterial06    100.00
## ManufacturingProcess32   99.34
## ManufacturingProcess13   87.41
## ManufacturingProcess36   84.76
## BiologicalMaterial03     82.98
## BiologicalMaterial12     80.41
## ManufacturingProcess09   80.24
## ManufacturingProcess17   70.40
## ManufacturingProcess31   67.95
## ManufacturingProcess06   67.68
## BiologicalMaterial02     66.32
## BiologicalMaterial04     58.58
## BiologicalMaterial11     54.02
## ManufacturingProcess11   52.11
## ManufacturingProcess29   50.49
## BiologicalMaterial09     47.96
## ManufacturingProcess33   47.60
## BiologicalMaterial08     46.11
## BiologicalMaterial01     40.56
## ManufacturingProcess30   39.10

6 of the top ten are manufacturing process predictors, while the other 4 are biological material predictors. Both process and biological variables play an important role in determining chemical yield, but manufacturing processes contribute slightly more overall variance to yield. In the linear model, 6 of the top 10 predictors were process predictors, with the other 4 being biological predictors - so this is the same as the linear model.

Part 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?

library(tidyr)
top_vars <- c("ManufacturingProcess32", "BiologicalMaterial06", "ManufacturingProcess13",
              "ManufacturingProcess09", "BiologicalMaterial12", "BiologicalMaterial03",
              "ManufacturingProcess36", "BiologicalMaterial02", "ManufacturingProcess17",
              "ManufacturingProcess06")

chem_long <- chemical_imp %>%
  select(Yield, all_of(top_vars)) %>%
  pivot_longer(
    cols = -Yield,
    names_to = "Predictor",
    values_to = "Value"
  )

# Facet plot
ggplot(chem_long, aes(x = Value, y = Yield)) +
  geom_point(alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  facet_wrap(~ Predictor, scales = "free_x") +
  theme_minimal() +
  labs(title = "Relationships Between Top Predictors and Yield",
       x = "Predictor",
       y = "Yield") +
  theme(plot.title = element_text(hjust = 0.5))

All four of the biological predictors have a positive relationship with yield.

The manufacturing process predictors have a mixture of positive and negative relationships with yield. This indicates that certain process predictors enhance yield while others hinder it.