Question 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 =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 simula tion). The package mlbench contains a function called mlbench.friedman1 that simulates these data:

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:

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
## performance values
postResample(pred = knnPred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 3.2040595 0.6819919 2.5683461

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

SVM Model

svmModel <- train(
  x = trainingData$x,
  y = trainingData$y,
  method = "svmRadial",
  preProcess = c("center", "scale"),
  tuneLength = 10  # Adjust tuning grid complexity
)

print(svmModel)
## 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.545335  0.7804647  2.015121
##     0.50  2.319786  0.7965148  1.830009
##     1.00  2.188349  0.8119636  1.726027
##     2.00  2.103655  0.8241314  1.655842
##     4.00  2.066879  0.8294322  1.631051
##     8.00  2.052681  0.8313929  1.623550
##    16.00  2.049867  0.8318312  1.621820
##    32.00  2.049867  0.8318312  1.621820
##    64.00  2.049867  0.8318312  1.621820
##   128.00  2.049867  0.8318312  1.621820
## 
## Tuning parameter 'sigma' was held constant at a value of 0.06802164
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.06802164 and C = 16.
svmPred <- predict(svmModel, newdata = testData$x)
svmResults <- postResample(pred = svmPred, obs = testData$y)
print(svmResults)
##      RMSE  Rsquared       MAE 
## 2.0864652 0.8236735 1.5854649

MARS Model

marsModel <- train(
  x = trainingData$x,
  y = trainingData$y,
  method = "earth",
  tuneLength = 10  
)

print(marsModel)
## Multivariate Adaptive Regression Spline 
## 
## 200 samples
##  10 predictor
## 
## No pre-processing
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 200, 200, 200, 200, 200, 200, ... 
## Resampling results across tuning parameters:
## 
##   nprune  RMSE      Rsquared   MAE     
##    2      4.447386  0.2254125  3.620675
##    3      3.790305  0.4344625  3.058704
##    4      2.801182  0.6884819  2.233531
##    6      2.493135  0.7492201  1.986528
##    7      2.089713  0.8239588  1.645996
##    9      1.816053  0.8673608  1.420333
##   10      1.819611  0.8674028  1.417343
##   12      1.832487  0.8651613  1.426371
##   13      1.845943  0.8632112  1.436005
##   15      1.854557  0.8617322  1.452920
## 
## Tuning parameter 'degree' was held constant at a value of 1
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 9 and degree = 1.
marsPred <- predict(marsModel, newdata = testData$x)
marsResults <- postResample(pred = marsPred, obs = testData$y)
print(marsResults)
##      RMSE  Rsquared       MAE 
## 1.7901760 0.8705315 1.3712537
importance <- varImp(marsModel, scale = FALSE)
print(importance)
## earth variable importance
## 
##    Overall
## X1 100.000
## X4  84.043
## X2  66.807
## X5  44.562
## X3  33.338
## X6   6.566

Response

The MARS model has the best performance metrics as indicated by the RMSE(1.7901760), R-squared(0.8705315) and lowest MAE(1.3712537). This is followed by the SVM mopdel and KNN.

When looking at the informative predictors, we see the appearance of variables X1-X5. Variable X1 has the highest importance score at 100, followed by X4, x2, x5 and x3.

Question 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")
cmp <- ChemicalManufacturingProcess %>%
  mutate(across(where(is.numeric), ~ ifelse(is.na(.), mean(., na.rm = TRUE), .)))
set.seed(123) 

trainIndex <- createDataPartition(cmp$Yield, p = 0.75, list = FALSE)
trainData <- cmp[trainIndex, ]
testData <- cmp[-trainIndex, ]

Part A

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

MARS model

set.seed(123)

# Train MARS model
marsModel <- train(
  Yield ~ ., 
  data = trainData,
  method = "earth",
  tuneLength = 10  
)

print(marsModel)
## Multivariate Adaptive Regression Spline 
## 
## 132 samples
##  57 predictor
## 
## No pre-processing
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 132, 132, 132, 132, 132, 132, ... 
## Resampling results across tuning parameters:
## 
##   nprune  RMSE      Rsquared   MAE     
##    2      1.509888  0.4039516  1.191275
##    3      1.478924  0.4809792  1.113587
##    5      1.781934  0.4637940  1.163027
##    7      1.758004  0.4585948  1.177874
##    8      1.769425  0.4549272  1.186874
##   10      1.869238  0.4279759  1.214972
##   12      2.160414  0.3755121  1.318558
##   13      2.313567  0.3636106  1.347241
##   15      2.696345  0.3475636  1.408677
##   17      3.119943  0.3213853  1.525825
## 
## Tuning parameter 'degree' was held constant at a value of 1
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 3 and degree = 1.
marsPred <- predict(marsModel, newdata = testData)
marsResults <- postResample(pred = marsPred, obs = testData$Yield)
print(marsResults)
##     RMSE Rsquared      MAE 
## 1.211602 0.533908 0.986864

Neural Network Model

nnModel <- train(
  Yield ~ ., 
  data = trainData,
  method = "nnet",
  tuneLength = 10,  
  linout = TRUE,    
  trace = FALSE     
)

print(nnModel)
## Neural Network 
## 
## 132 samples
##  57 predictor
## 
## No pre-processing
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 132, 132, 132, 132, 132, 132, ... 
## Resampling results across tuning parameters:
## 
##   size  decay         RMSE      Rsquared    MAE     
##    1    0.0000000000  1.960015  0.03110926  1.519394
##    1    0.0001000000  2.076199  0.05173730  1.549334
##    1    0.0002371374  1.948009  0.03158968  1.526581
##    1    0.0005623413  1.882527  0.07249589  1.483203
##    1    0.0013335214  2.050117  0.11902606  1.586306
##    1    0.0031622777  1.881991  0.08532039  1.494670
##    1    0.0074989421  1.835325  0.11082356  1.461582
##    1    0.0177827941  2.057528  0.10625402  1.580520
##    1    0.0421696503  2.547991  0.16345580  1.804024
##    1    0.1000000000  2.398180  0.08145960  1.689222
##    3    0.0000000000  1.978125  0.03818137  1.531590
##    3    0.0001000000  1.886678  0.04959945  1.499111
##    3    0.0002371374  2.083885  0.09841383  1.544625
##    3    0.0005623413  2.140942  0.06518891  1.614661
##    3    0.0013335214  2.276953  0.08601347  1.702477
##    3    0.0031622777  2.106439  0.06205764  1.592126
##    3    0.0074989421  2.191758  0.09359184  1.645406
##    3    0.0177827941  2.302654  0.08062679  1.685761
##    3    0.0421696503  2.200716  0.14430147  1.576756
##    3    0.1000000000  2.466946  0.10877246  1.770700
##    5    0.0000000000  2.066417  0.04239738  1.542457
##    5    0.0001000000  2.054862  0.05396063  1.557106
##    5    0.0002371374  2.047389  0.04914648  1.554570
##    5    0.0005623413  2.012539  0.09072607  1.531546
##    5    0.0013335214  1.885559  0.09802938  1.503291
##    5    0.0031622777  2.378170  0.15209714  1.740714
##    5    0.0074989421  2.075880  0.08676682  1.621641
##    5    0.0177827941  2.319200  0.08616123  1.681719
##    5    0.0421696503  2.520422  0.08653844  1.806253
##    5    0.1000000000  2.334483  0.11868599  1.698853
##    7    0.0000000000  2.020606  0.02904805  1.568278
##    7    0.0001000000  2.151813  0.04606674  1.591354
##    7    0.0002371374  1.954133  0.08282236  1.501982
##    7    0.0005623413  2.046082  0.11757808  1.521036
##    7    0.0013335214  2.097869  0.13973702  1.595728
##    7    0.0031622777  2.115260  0.10863074  1.589715
##    7    0.0074989421  2.177210  0.12447565  1.606489
##    7    0.0177827941  2.139168  0.14749192  1.600841
##    7    0.0421696503  2.095751  0.13627212  1.552764
##    7    0.1000000000  2.247212  0.12233435  1.623467
##    9    0.0000000000  2.066402  0.04260732  1.570760
##    9    0.0001000000  2.045406  0.05314662  1.579685
##    9    0.0002371374  2.229967  0.07276498  1.658349
##    9    0.0005623413  2.211862  0.11165304  1.650555
##    9    0.0013335214  2.141007  0.11072403  1.582636
##    9    0.0031622777  2.080815  0.12219520  1.572012
##    9    0.0074989421  2.095038  0.14036558  1.529236
##    9    0.0177827941  2.134859  0.14173619  1.584110
##    9    0.0421696503  2.084151  0.15923287  1.546142
##    9    0.1000000000  2.094930  0.19233631  1.564331
##   11    0.0000000000  1.941939  0.04601034  1.523672
##   11    0.0001000000  2.047947  0.07823522  1.568787
##   11    0.0002371374  1.981072  0.10647118  1.533675
##   11    0.0005623413  1.878092  0.12869727  1.488421
##   11    0.0013335214  2.059879  0.12081703  1.579799
##   11    0.0031622777  2.106651  0.13054593  1.514597
##   11    0.0074989421  1.940612  0.16165570  1.535683
##   11    0.0177827941  2.046501  0.17676180  1.524459
##   11    0.0421696503  2.110012  0.13044311  1.633137
##   11    0.1000000000  2.031404  0.16610465  1.529674
##   13    0.0000000000  2.064921  0.02528908  1.573681
##   13    0.0001000000  1.934204  0.10162220  1.487575
##   13    0.0002371374  1.873839  0.11164414  1.482373
##   13    0.0005623413  2.125435  0.12388944  1.580680
##   13    0.0013335214  2.044972  0.14536627  1.542199
##   13    0.0031622777  2.236232  0.17813591  1.587974
##   13    0.0074989421  2.035883  0.13061621  1.571524
##   13    0.0177827941  2.044227  0.15079600  1.604440
##   13    0.0421696503  1.814082  0.21929712  1.399858
##   13    0.1000000000  1.824025  0.21830547  1.433382
##   15    0.0000000000  1.993730  0.03950731  1.535605
##   15    0.0001000000  2.029191  0.06977408  1.568864
##   15    0.0002371374  1.947687  0.06442794  1.537491
##   15    0.0005623413  1.990536  0.11377023  1.571533
##   15    0.0013335214  1.970108  0.15119097  1.519606
##   15    0.0031622777  2.050932  0.14739741  1.545476
##   15    0.0074989421  1.940399  0.18190886  1.492900
##   15    0.0177827941  1.985217  0.18632967  1.532222
##   15    0.0421696503  1.924810  0.20518866  1.527877
##   15    0.1000000000  2.201126  0.13769855  1.577715
##   17    0.0000000000       NaN         NaN       NaN
##   17    0.0001000000       NaN         NaN       NaN
##   17    0.0002371374       NaN         NaN       NaN
##   17    0.0005623413       NaN         NaN       NaN
##   17    0.0013335214       NaN         NaN       NaN
##   17    0.0031622777       NaN         NaN       NaN
##   17    0.0074989421       NaN         NaN       NaN
##   17    0.0177827941       NaN         NaN       NaN
##   17    0.0421696503       NaN         NaN       NaN
##   17    0.1000000000       NaN         NaN       NaN
##   19    0.0000000000       NaN         NaN       NaN
##   19    0.0001000000       NaN         NaN       NaN
##   19    0.0002371374       NaN         NaN       NaN
##   19    0.0005623413       NaN         NaN       NaN
##   19    0.0013335214       NaN         NaN       NaN
##   19    0.0031622777       NaN         NaN       NaN
##   19    0.0074989421       NaN         NaN       NaN
##   19    0.0177827941       NaN         NaN       NaN
##   19    0.0421696503       NaN         NaN       NaN
##   19    0.1000000000       NaN         NaN       NaN
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were size = 13 and decay = 0.04216965.
nnPred <- predict(nnModel, newdata = testData)
nnResults <- postResample(pred = nnPred, obs = testData$Yield)
print(nnResults)
##      RMSE  Rsquared       MAE 
## 1.6498731 0.3415418 1.3010574

SVM

set.seed(123)
svmModel <- train(
  Yield ~ ., 
  data = trainData,
  method = "svmRadial",
  preProcess = c("center", "scale"),  
  tuneLength = 10                     
)

print(svmModel)
## Support Vector Machines with Radial Basis Function Kernel 
## 
## 132 samples
##  57 predictor
## 
## Pre-processing: centered (57), scaled (57) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 132, 132, 132, 132, 132, 132, ... 
## Resampling results across tuning parameters:
## 
##   C       RMSE      Rsquared   MAE      
##     0.25  1.445879  0.5100527  1.1487322
##     0.50  1.332377  0.5586923  1.0517378
##     1.00  1.262435  0.5915349  0.9931834
##     2.00  1.223224  0.6109892  0.9640553
##     4.00  1.211193  0.6172501  0.9521245
##     8.00  1.209999  0.6179105  0.9522781
##    16.00  1.208468  0.6189201  0.9514246
##    32.00  1.208468  0.6189201  0.9514246
##    64.00  1.208468  0.6189201  0.9514246
##   128.00  1.208468  0.6189201  0.9514246
## 
## Tuning parameter 'sigma' was held constant at a value of 0.01457266
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01457266 and C = 16.
svmPred <- predict(svmModel, newdata = testData)
svmResults <- postResample(pred = svmPred, obs = testData$Yield)
print(svmResults)
##      RMSE  Rsquared       MAE 
## 1.1146087 0.5936904 0.8691682

Response

In both the resampling and test set, the SVM performs the best across the performance metrics (RMSE, R-squared, MAE). The high performance tells us, that it performs well on training data but also on unseen data.

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?

Response

In the optimal model, ManufacturingProcess32 is once again the most overall variable. In the top there is still 3 biological variables and 7 Manufacturing variables. The level of importance among the variable however, is more evenly distributed in the optimal nonlinear regression model.

We can also see that the biological variables are higher in the list of importance. It’s worth noting that BiologicalMaterial06 is considered the most important variable.

importance <- varImp(svmModel, scale = FALSE)
print(importance)
## loess r-squared variable importance
## 
##   only 20 most important variables shown (out of 57)
## 
##                        Overall
## ManufacturingProcess32  0.3732
## BiologicalMaterial06    0.3136
## ManufacturingProcess17  0.3032
## ManufacturingProcess13  0.2993
## ManufacturingProcess36  0.2957
## BiologicalMaterial03    0.2837
## ManufacturingProcess09  0.2775
## ManufacturingProcess31  0.2546
## BiologicalMaterial02    0.2537
## ManufacturingProcess06  0.2295
## BiologicalMaterial12    0.2115
## ManufacturingProcess30  0.2069
## ManufacturingProcess33  0.2000
## BiologicalMaterial04    0.1864
## ManufacturingProcess29  0.1806
## BiologicalMaterial11    0.1684
## ManufacturingProcess11  0.1679
## BiologicalMaterial08    0.1551
## BiologicalMaterial01    0.1541
## ManufacturingProcess02  0.1537

Part C

Would you recommend any of the models you have developed to replace the permeability laboratory experiment?

Though the SVM model performs the best across the metrics RMSE(1.1146087), R-squared(0.5936904), and MAE(0.8691682), I don’t believe the model is strong enough to replace the permeability laboratory experiment.