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=10sin(π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)
## 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)

## 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)
ctrl = trainControl(method='cv', number = 10)

Tune several models on these data. KNN was provided, I also tried NN, MARS, SVM to compare:

library(caret)
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
#NN
#check correlations above 0.75
cutoff <- findCorrelation(cor(trainingData$x), cutoff = .75)
# resulted no variable.

# tuning grid
nnetGrid <- expand.grid(.decay = c(0, 0.01, .1),
                        .size = c(1:10))


# 10-fold cross-validation 
ctrl <- trainControl(method = "cv", number = 10)

set.seed(200)

# tune
nnetTune <- train(trainingData$x, trainingData$y,
                  method = "nnet",
                  tuneGrid = nnetGrid,
                  trControl = ctrl,
                  preProc = c("center", "scale"),
                  linout = TRUE,
                  trace = FALSE,
                  MaxNWts = 10 * (ncol(trainingData$x) + 1) + 10 + 1,
                  maxit = 500)

nnetTune
## 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.611167  0.7034326  2.057976
##   0.00    2    2.598785  0.7234665  2.050280
##   0.00    3    2.297113  0.7838555  1.861644
##   0.00    4    2.368843  0.7648841  1.865206
##   0.00    5    2.645231  0.7432411  2.085563
##   0.00    6    2.755188  0.7166304  2.232277
##   0.00    7    5.890779  0.4576737  3.525435
##   0.00    8    4.864418  0.5024423  3.226141
##   0.00    9    3.560161  0.6246193  2.840954
##   0.00   10    7.918946  0.4288706  4.285780
##   0.01    1    2.381033  0.7641663  1.871496
##   0.01    2    2.584223  0.7329824  2.039030
##   0.01    3    2.149663  0.8043669  1.700742
##   0.01    4    2.581742  0.7251690  2.060772
##   0.01    5    2.552497  0.7241392  2.087565
##   0.01    6    2.723551  0.7268775  2.152613
##   0.01    7    3.135450  0.6400705  2.466251
##   0.01    8    3.169068  0.6323455  2.496601
##   0.01    9    3.283663  0.6212971  2.589913
##   0.01   10    3.201798  0.6355282  2.580713
##   0.10    1    2.392301  0.7614537  1.873847
##   0.10    2    2.516063  0.7411240  1.959889
##   0.10    3    2.300698  0.7631105  1.799187
##   0.10    4    2.420099  0.7688275  1.973457
##   0.10    5    2.376897  0.7815448  1.884554
##   0.10    6    2.391501  0.7705770  1.893266
##   0.10    7    2.790691  0.7118671  2.266089
##   0.10    8    2.992085  0.6806902  2.370561
##   0.10    9    3.152233  0.6543316  2.480312
##   0.10   10    3.203597  0.6701593  2.600855
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were size = 3 and decay = 0.01.
nnPred <- predict(nnetTune, testData$x)
postResample(nnPred, testData$y)
##      RMSE  Rsquared       MAE 
## 2.2831109 0.7952037 1.7216052

MARS

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

set.seed(200)
marsTuned <- train(trainingData$x, trainingData$y,
                   method = "earth",
                   tuneGrid = marsGrid,
                   trControl = ctrl)
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.188280  0.3042527  3.460689
##   1        3      3.551182  0.4999832  2.837116
##   1        4      2.653143  0.7167280  2.128222
##   1        5      2.405769  0.7562160  1.948161
##   1        6      2.295006  0.7754603  1.853199
##   1        7      1.771950  0.8611767  1.391357
##   1        8      1.647182  0.8774867  1.299564
##   1        9      1.609816  0.8837307  1.299705
##   1       10      1.635035  0.8798236  1.309436
##   1       11      1.571915  0.8896147  1.260711
##   1       12      1.571561  0.8898750  1.253077
##   1       13      1.567577  0.8906927  1.250795
##   1       14      1.571673  0.8909652  1.245508
##   1       15      1.571673  0.8909652  1.245508
##   1       16      1.571673  0.8909652  1.245508
##   1       17      1.571673  0.8909652  1.245508
##   1       18      1.571673  0.8909652  1.245508
##   1       19      1.571673  0.8909652  1.245508
##   1       20      1.571673  0.8909652  1.245508
##   1       21      1.571673  0.8909652  1.245508
##   1       22      1.571673  0.8909652  1.245508
##   1       23      1.571673  0.8909652  1.245508
##   1       24      1.571673  0.8909652  1.245508
##   1       25      1.571673  0.8909652  1.245508
##   1       26      1.571673  0.8909652  1.245508
##   1       27      1.571673  0.8909652  1.245508
##   1       28      1.571673  0.8909652  1.245508
##   1       29      1.571673  0.8909652  1.245508
##   1       30      1.571673  0.8909652  1.245508
##   1       31      1.571673  0.8909652  1.245508
##   1       32      1.571673  0.8909652  1.245508
##   1       33      1.571673  0.8909652  1.245508
##   1       34      1.571673  0.8909652  1.245508
##   1       35      1.571673  0.8909652  1.245508
##   1       36      1.571673  0.8909652  1.245508
##   1       37      1.571673  0.8909652  1.245508
##   1       38      1.571673  0.8909652  1.245508
##   2        2      4.188280  0.3042527  3.460689
##   2        3      3.551182  0.4999832  2.837116
##   2        4      2.615256  0.7216809  2.128763
##   2        5      2.344223  0.7683855  1.890080
##   2        6      2.275048  0.7762472  1.807779
##   2        7      1.841464  0.8418935  1.457945
##   2        8      1.641647  0.8839822  1.288520
##   2        9      1.535119  0.9002991  1.214772
##   2       10      1.473254  0.9101555  1.158761
##   2       11      1.379476  0.9207735  1.080991
##   2       12      1.285380  0.9283193  1.033426
##   2       13      1.267261  0.9328905  1.014726
##   2       14      1.261797  0.9327541  1.009821
##   2       15      1.266663  0.9320714  1.005751
##   2       16      1.270858  0.9322465  1.009757
##   2       17      1.263778  0.9327687  1.007653
##   2       18      1.263778  0.9327687  1.007653
##   2       19      1.263778  0.9327687  1.007653
##   2       20      1.263778  0.9327687  1.007653
##   2       21      1.263778  0.9327687  1.007653
##   2       22      1.263778  0.9327687  1.007653
##   2       23      1.263778  0.9327687  1.007653
##   2       24      1.263778  0.9327687  1.007653
##   2       25      1.263778  0.9327687  1.007653
##   2       26      1.263778  0.9327687  1.007653
##   2       27      1.263778  0.9327687  1.007653
##   2       28      1.263778  0.9327687  1.007653
##   2       29      1.263778  0.9327687  1.007653
##   2       30      1.263778  0.9327687  1.007653
##   2       31      1.263778  0.9327687  1.007653
##   2       32      1.263778  0.9327687  1.007653
##   2       33      1.263778  0.9327687  1.007653
##   2       34      1.263778  0.9327687  1.007653
##   2       35      1.263778  0.9327687  1.007653
##   2       36      1.263778  0.9327687  1.007653
##   2       37      1.263778  0.9327687  1.007653
##   2       38      1.263778  0.9327687  1.007653
## 
## 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.

Check predictors importance. Based on the result below, predictors X1,4,2,5,3 (top 5 from the list) will be used for prediction.

varImp(marsTuned)
## earth variable importance
## 
##    Overall
## X1  100.00
## X4   75.24
## X2   48.74
## X5   15.53
## X3    0.00
marsTuned$results[32,1:5]
##    degree nprune     RMSE  Rsquared      MAE
## 53      2     17 1.263778 0.9327687 1.007653
mars_pred <- predict(marsTuned, newdata = testData$x)
postResample(obs = testData$y, pred=mars_pred[,1])
##      RMSE  Rsquared       MAE 
## 1.1722635 0.9448890 0.9324923
#SVM
set.seed(100)
svmRTuned <- train(trainingData$x, trainingData$y,
                   method = "svmRadial",
                   preProc = c("center", "scale"),
                   tuneLength = 14,
                   trControl = ctrl)
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.530787  0.7922715  2.013175
##      0.50  2.259539  0.8064569  1.789962
##      1.00  2.099789  0.8274242  1.656154
##      2.00  2.002943  0.8412934  1.583791
##      4.00  1.943618  0.8504425  1.546586
##      8.00  1.918711  0.8547582  1.532981
##     16.00  1.920651  0.8536189  1.536116
##     32.00  1.920651  0.8536189  1.536116
##     64.00  1.920651  0.8536189  1.536116
##    128.00  1.920651  0.8536189  1.536116
##    256.00  1.920651  0.8536189  1.536116
##    512.00  1.920651  0.8536189  1.536116
##   1024.00  1.920651  0.8536189  1.536116
##   2048.00  1.920651  0.8536189  1.536116
## 
## Tuning parameter 'sigma' was held constant at a value of 0.06509124
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.06509124 and C = 8.
svm_pred <- predict(svmRTuned, newdata = testData$x)

postResample(obs = testData$y, pred=svm_pred)
##      RMSE  Rsquared       MAE 
## 2.0631908 0.8275736 1.5662213

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

In the above, we can see that the best model to use would be the MARS model, with RMSE,Rsquared and MAE score to be 1.1589948, 0.9460418 and 0.9250230. It has relatvely low RMSE and mAE score and a high Rsquared number. The MARS model does select informative predictors (X1-X5). In order of importance (most to less important): X1, X4, X2, X5, X3 with score as follow: X1-100.00000, X4-75.23592,X2-48.72974 ,X5-15.51884, and X3-0.

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.

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

After data imputation, data splitting, and pre-processing stepsm, I will be using KNN, NN, MARS and SVM model and compare them all.
Result showing below indicating the best model are SVM, with the lowest RMSE and MAE value 1.14 and 0.75. plus the highest rsquared value of 0.75.

library(AppliedPredictiveModeling)
data(ChemicalManufacturingProcess)

# imputation
miss <- preProcess(ChemicalManufacturingProcess, method = "bagImpute")
Chemical <- predict(miss, ChemicalManufacturingProcess)

# filtering low frequencies
Chemical <- Chemical[, -nearZeroVar(Chemical)]

set.seed(624)

# index for training
index <- createDataPartition(Chemical$Yield, p = .8, list = FALSE)

# train 
train_x <- Chemical[index, -1]
train_y <- Chemical[index, 1]

# test
test_x <- Chemical[-index, -1]
test_y <- Chemical[-index, 1]

KNN:

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

knnModel
## k-Nearest Neighbors 
## 
## 144 samples
##  56 predictor
## 
## Pre-processing: centered (56), scaled (56) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 144, 144, 144, 144, 144, 144, ... 
## Resampling results across tuning parameters:
## 
##   k   RMSE      Rsquared   MAE     
##    5  1.471125  0.3330992  1.161484
##    7  1.447346  0.3519621  1.150975
##    9  1.439505  0.3614781  1.153856
##   11  1.440067  0.3597565  1.157491
##   13  1.446347  0.3556436  1.165135
##   15  1.437409  0.3693582  1.165991
##   17  1.448196  0.3618152  1.176400
##   19  1.452990  0.3601724  1.182114
##   21  1.456702  0.3606783  1.183356
##   23  1.457503  0.3658775  1.185981
## 
## 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, test_x)
## The function 'postResample' can be used to get the test set
## perforamnce values
postResample(pred = knnPred, test_y)
##      RMSE  Rsquared       MAE 
## 1.5262067 0.6187302 1.1800625

NN:

#NN
# remove predictors to ensure maximum abs pairwise corr between predictors < 0.75
tooHigh <- findCorrelation(cor(train_x), cutoff = .75)

# removing 21 variables
train_x_nnet <- train_x[, -tooHigh]
test_x_nnet <- test_x[, -tooHigh]

# create a tuning grid
nnetGrid <- expand.grid(.decay = c(0, 0.01, .1),
                        .size = c(1:10))

# 10-fold cross-validation to make reasonable estimates
ctrl <- trainControl(method = "cv", number = 10)

set.seed(100)

# tune
nnetTune <- train(train_x_nnet, train_y,
                  method = "nnet",
                  tuneGrid = nnetGrid,
                  trControl = ctrl,
                  preProc = c("center", "scale"),
                  linout = TRUE,
                  trace = FALSE,
                  MaxNWts = 10 * (ncol(train_x_nnet) + 1) + 10 + 1,
                  maxit = 500)

nnetTune
## Neural Network 
## 
## 144 samples
##  35 predictor
## 
## Pre-processing: centered (35), scaled (35) 
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 129, 130, 130, 130, 130, 130, ... 
## Resampling results across tuning parameters:
## 
##   decay  size  RMSE      Rsquared   MAE     
##   0.00    1    1.653183  0.2181442  1.345706
##   0.00    2    2.534424  0.2369899  1.836159
##   0.00    3    3.171155  0.2925081  2.287581
##   0.00    4    3.712864  0.1207665  2.918384
##   0.00    5    3.431782  0.1500205  2.741843
##   0.00    6    4.519146  0.1324394  3.247006
##   0.00    7    4.572852  0.1511347  3.586819
##   0.00    8    4.897815  0.1777553  3.195740
##   0.00    9    6.323278  0.1664817  4.360992
##   0.00   10    8.667370  0.1152399  5.988899
##   0.01    1    1.667606  0.3154025  1.388167
##   0.01    2    2.265838  0.1993149  1.714076
##   0.01    3    2.332248  0.2440199  1.842895
##   0.01    4    3.014612  0.1547884  2.259713
##   0.01    5    2.548550  0.2082747  1.952267
##   0.01    6    2.616978  0.2014794  2.005287
##   0.01    7    2.701263  0.1879326  2.110111
##   0.01    8    2.918798  0.1889725  2.273816
##   0.01    9    2.608475  0.2538457  2.126104
##   0.01   10    3.326500  0.2346732  2.486645
##   0.10    1    1.618516  0.3543468  1.325739
##   0.10    2    1.852789  0.3901490  1.390430
##   0.10    3    2.908159  0.1839385  2.024600
##   0.10    4    2.656395  0.2030849  1.958837
##   0.10    5    2.943660  0.2220188  2.101576
##   0.10    6    2.548716  0.2985445  1.890078
##   0.10    7    2.174937  0.3069096  1.702733
##   0.10    8    2.795824  0.2115189  1.979132
##   0.10    9    2.282975  0.2389936  1.862757
##   0.10   10    2.656864  0.2044295  1.990448
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were size = 1 and decay = 0.1.
nnPred <- predict(nnetTune, test_x_nnet)

postResample(nnPred, test_y)
##      RMSE  Rsquared       MAE 
## 1.5064579 0.5140357 1.1159762

MARS:

#MARS
# create a tuning grid
marsGrid <- expand.grid(.degree = 1:2, .nprune = 2:38)

set.seed(100)

# tune
marsTune <- train(train_x, train_y,
                  method = "earth",
                  tuneGrid = marsGrid,
                  trControl = trainControl(method = "cv"))

marsTune
## Multivariate Adaptive Regression Spline 
## 
## 144 samples
##  56 predictor
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 129, 130, 130, 130, 130, 130, ... 
## Resampling results across tuning parameters:
## 
##   degree  nprune  RMSE      Rsquared   MAE      
##   1        2      1.382295  0.4386629  1.1032611
##   1        3      1.240867  0.5448952  0.9985512
##   1        4      1.259935  0.5341424  1.0107010
##   1        5      1.245790  0.5272274  1.0113559
##   1        6      1.269935  0.5136793  1.0204522
##   1        7      1.310209  0.5055710  1.0295204
##   1        8      1.288293  0.5221112  1.0036609
##   1        9      1.293021  0.5193283  1.0156268
##   1       10      1.286486  0.5258144  1.0107051
##   1       11      1.350612  0.5108572  1.0494019
##   1       12      1.354690  0.5164837  1.0502417
##   1       13      1.371710  0.5124198  1.0535178
##   1       14      1.386234  0.5064731  1.0729218
##   1       15      1.377159  0.5169364  1.0708723
##   1       16      1.377159  0.5169364  1.0708723
##   1       17      1.377159  0.5169364  1.0708723
##   1       18      1.377159  0.5169364  1.0708723
##   1       19      1.377159  0.5169364  1.0708723
##   1       20      1.377159  0.5169364  1.0708723
##   1       21      1.377159  0.5169364  1.0708723
##   1       22      1.377159  0.5169364  1.0708723
##   1       23      1.377159  0.5169364  1.0708723
##   1       24      1.377159  0.5169364  1.0708723
##   1       25      1.377159  0.5169364  1.0708723
##   1       26      1.377159  0.5169364  1.0708723
##   1       27      1.377159  0.5169364  1.0708723
##   1       28      1.377159  0.5169364  1.0708723
##   1       29      1.377159  0.5169364  1.0708723
##   1       30      1.377159  0.5169364  1.0708723
##   1       31      1.377159  0.5169364  1.0708723
##   1       32      1.377159  0.5169364  1.0708723
##   1       33      1.377159  0.5169364  1.0708723
##   1       34      1.377159  0.5169364  1.0708723
##   1       35      1.377159  0.5169364  1.0708723
##   1       36      1.377159  0.5169364  1.0708723
##   1       37      1.377159  0.5169364  1.0708723
##   1       38      1.377159  0.5169364  1.0708723
##   2        2      1.382295  0.4386629  1.1032611
##   2        3      1.237952  0.5375297  1.0083290
##   2        4      1.253568  0.5221886  1.0335088
##   2        5      1.204199  0.5507043  0.9713244
##   2        6      1.241877  0.5180123  1.0022903
##   2        7      1.228535  0.5360710  0.9772064
##   2        8      1.236188  0.5297973  0.9891217
##   2        9      1.224202  0.5377333  0.9943605
##   2       10      1.196350  0.5532418  0.9855648
##   2       11      1.217007  0.5502910  1.0105749
##   2       12      1.236600  0.5473328  1.0021900
##   2       13      1.227170  0.5587354  0.9909744
##   2       14      1.263470  0.5599646  1.0158323
##   2       15      1.230580  0.5620079  1.0103784
##   2       16      1.241609  0.5506318  0.9964320
##   2       17      1.233933  0.5689345  0.9858733
##   2       18      1.241566  0.5806316  1.0029570
##   2       19      1.236775  0.5859195  0.9987440
##   2       20      1.317821  0.5266260  1.0648319
##   2       21      1.388138  0.5126592  1.1035179
##   2       22      1.402762  0.5068048  1.1134955
##   2       23      1.396884  0.5054997  1.1196368
##   2       24      1.380184  0.5113281  1.1059875
##   2       25      1.380184  0.5113281  1.1059875
##   2       26      1.386388  0.5070473  1.1174699
##   2       27      1.380683  0.5101973  1.1123044
##   2       28      1.361918  0.5211907  1.0932094
##   2       29      1.366147  0.5191619  1.0957169
##   2       30      1.366147  0.5191619  1.0957169
##   2       31      1.366147  0.5191619  1.0957169
##   2       32      1.360840  0.5200205  1.0921339
##   2       33      1.360840  0.5200205  1.0921339
##   2       34      1.360840  0.5200205  1.0921339
##   2       35      1.360840  0.5200205  1.0921339
##   2       36      1.360840  0.5200205  1.0921339
##   2       37      1.360840  0.5200205  1.0921339
##   2       38      1.360840  0.5200205  1.0921339
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 10 and degree = 2.
marsPred <- predict(marsTune, test_x)
postResample(marsPred, test_y)
##      RMSE  Rsquared       MAE 
## 1.3464789 0.6138875 0.9826902

SVM:

#SVM
set.seed(100)

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

svmRTune
## Support Vector Machines with Radial Basis Function Kernel 
## 
## 144 samples
##  56 predictor
## 
## Pre-processing: centered (56), scaled (56) 
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 129, 130, 130, 130, 130, 130, ... 
## Resampling results across tuning parameters:
## 
##   C        RMSE      Rsquared   MAE      
##      0.25  1.413177  0.4630126  1.1760898
##      0.50  1.314625  0.5018046  1.0947625
##      1.00  1.217731  0.5647210  1.0095889
##      2.00  1.164634  0.5994161  0.9630243
##      4.00  1.124391  0.6199423  0.9192936
##      8.00  1.119796  0.6170091  0.9287431
##     16.00  1.118734  0.6174115  0.9308110
##     32.00  1.118734  0.6174115  0.9308110
##     64.00  1.118734  0.6174115  0.9308110
##    128.00  1.118734  0.6174115  0.9308110
##    256.00  1.118734  0.6174115  0.9308110
##    512.00  1.118734  0.6174115  0.9308110
##   1024.00  1.118734  0.6174115  0.9308110
##   2048.00  1.118734  0.6174115  0.9308110
## 
## Tuning parameter 'sigma' was held constant at a value of 0.0139359
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.0139359 and C = 16.
svmRPred <- predict(svmRTune, test_x)
postResample(svmRPred, test_y)
##      RMSE  Rsquared       MAE 
## 1.1412463 0.7513994 0.8006586

compare all:

rbind(knn = postResample(knnPred, test_y),
      nn = postResample(nnPred, test_y),
      mars = postResample(marsPred, test_y),
      svmR = postResample(svmRPred, test_y))
##          RMSE  Rsquared       MAE
## knn  1.526207 0.6187302 1.1800625
## nn   1.506458 0.5140357 1.1159762
## mars 1.346479 0.6138875 0.9826902
## svmR 1.141246 0.7513994 0.8006586
  1. 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?

Here is the list of predictors with importantce from top: ManufacturingProcess32,36,09,13,31 nad 06, along with biologicalMaterial06,03,02 and 12 are the top 10. From the comparison below, seems top ten important predictors are the same as the top ten predictors from the optimal linear model(used LARS).

varImp(svmRTune)
## loess r-squared variable importance
## 
##   only 20 most important variables shown (out of 56)
## 
##                        Overall
## ManufacturingProcess32  100.00
## BiologicalMaterial06     89.32
## BiologicalMaterial03     77.48
## ManufacturingProcess36   76.64
## ManufacturingProcess09   73.90
## ManufacturingProcess13   73.24
## ManufacturingProcess31   67.06
## BiologicalMaterial02     66.92
## BiologicalMaterial12     64.94
## ManufacturingProcess06   59.23
## ManufacturingProcess17   53.07
## BiologicalMaterial11     49.11
## BiologicalMaterial04     48.27
## ManufacturingProcess11   45.42
## ManufacturingProcess29   45.31
## ManufacturingProcess33   44.62
## BiologicalMaterial01     40.70
## BiologicalMaterial08     38.19
## ManufacturingProcess30   35.52
## BiologicalMaterial09     29.60
#from linear model:
set.seed(100)
larsTune <- train(train_x, train_y, 
                  method = "lars", 
                  metric = "Rsquared",
                  tuneLength = 20, 
                  trControl = ctrl, 
                  preProc = c("center", "scale"))

lars_predict <- predict(larsTune, test_x)

plot(varImp(svmRTune), top = 10,
     main = "Nonlinear: Top 10 Important Predictors")

plot(varImp(larsTune), top = 10,
     main = "Linear: Top 10 Important Predictors")

  1. 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?

From the plot below, we can see among the top predictors, ManufacturingProcess32 has the highest positive correlation with Yield but other than that most other manufactureingProcess tends to negatively correlated with yield whereas biological materials tiends to positively corelated with yield, with BiologicalMaterial 06 and 03 from the top of the list.

library("corrplot")
## corrplot 0.95 loaded
top10 <- varImp(svmRTune)$importance |>
  arrange(-Overall) |>
  head(10)

Chemical |>
  select(c("Yield", row.names(top10))) |>
  cor() |>
  corrplot()