7.2

7.2. Friedman (1991) introduced several benchmark data sets create by simulation. One of these simulations used the following nonlinear equation to create data:
\[y = 10 sin(\pi x_1x_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(caret)
## Warning: package 'caret' was built under R version 4.4.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.4.3
## Loading required package: lattice
# From textbook

library(mlbench)
## Warning: package 'mlbench' was built under R version 4.4.3
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)
# From textbook

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.
# From textbook 

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

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

SVM

svmModel <- train(x=trainingData$x,
                  y=trainingData$y,
                  method = "svmRadial",
                  preProc = c("center", "scale"),
                  tuneLength = 10)

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.

MARS

mars <- train(x=trainingData$x, 
              y=trainingData$y, 
              method = "earth",
              preProc = c("center", "scale"),
              tuneLength = 10)
## Loading required package: earth
## Warning: package 'earth' was built under R version 4.4.3
## Loading required package: Formula
## Loading required package: plotmo
## Warning: package 'plotmo' was built under R version 4.4.3
## Loading required package: plotrix
## Warning: package 'plotrix' was built under R version 4.4.3
mars
## Multivariate Adaptive Regression Spline 
## 
## 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:
## 
##   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.

KNN performance

knnperf <- predict(knnModel, newdata=testData$x)
postResample(pred = knnperf, obs =testData$y)
##      RMSE  Rsquared       MAE 
## 3.2040595 0.6819919 2.5683461

SVM performance

svmperf <- predict(svmModel, newdata=testData$x)
postResample(pred = svmperf, obs=testData$y)
##      RMSE  Rsquared       MAE 
## 2.0864652 0.8236735 1.5854649

MARS performance

marsperf <- predict(mars, newdata = testData$x)
postResample(pred =marsperf, obs=testData$y)
##      RMSE  Rsquared       MAE 
## 1.7901760 0.8705315 1.3712537

Overall performances

The MARS model had the lowest RMSE and highest Rsquared indicating that this model had the best performance.

varImp(mars)
## earth variable importance
## 
##    Overall
## X1  100.00
## X4   82.92
## X2   64.47
## X5   40.67
## X3   28.65
## X6    0.00

The top 5 informative variables by importance are 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.

# From textbook

library(AppliedPredictiveModeling)
## Warning: package 'AppliedPredictiveModeling' was built under R version 4.4.3
data(ChemicalManufacturingProcess)
# Impute missing values via knn
library(impute)
chemicals <- impute.knn(as.matrix(ChemicalManufacturingProcess), rng.seed = 123)
chemicals <- as.data.frame(chemicals$data)

# Test/train split
training_chemicals <- createDataPartition(chemicals$Yield, p = 0.8, list = FALSE)
train_predictors_chemicals <- chemicals[training_chemicals,-1]
train_yield <- chemicals[training_chemicals, 1]
test_predictors_chemicals <- chemicals[-training_chemicals,-1]
test_yield <- chemicals[-training_chemicals, 1]

KNN

suppressWarnings(chemicals_knn <- train(x=train_predictors_chemicals,
                       y=train_yield,
                       method = "knn", 
                       preProc = c("center", "scale"),
                       tuneLength=10))
knn_predict <- predict(chemicals_knn, test_predictors_chemicals)
yield_knn <-data.frame(obs=test_yield, pred=knn_predict)
print(defaultSummary(yield_knn))
##      RMSE  Rsquared       MAE 
## 1.4239614 0.4011076 1.1771250

MARS

suppressWarnings(chemicals_mars <-train(train_predictors_chemicals,
                                        train_yield,
                                        method='earth',
                                        preProc = c('center', 'scale')))

mars_predict <- predict(chemicals_mars, test_predictors_chemicals)
yield_mars <- data.frame(obs=test_yield, pred=mars_predict)
yield_mars$pred <- yield_mars$y
yield_mars <-yield_mars[,-2]
print(defaultSummary(yield_mars))
##     RMSE Rsquared      MAE 
## 1.402311 0.424434 1.110419

SVM

suppressWarnings(chemicals_svm <- train(train_predictors_chemicals,
                                        train_yield,
                                        method = "svmRadial",
                                        preProc = c("center", "scale")))

svm_predict <-predict(chemicals_svm, test_predictors_chemicals)
yield_svm <- data.frame(obs=test_yield, pred=svm_predict)
print(defaultSummary(yield_svm))
##      RMSE  Rsquared       MAE 
## 1.2654699 0.5270595 1.0447860

a

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

Based on the models above (knn, svm, and mars), SVM had the lowest RSME and highest Rsquared value indicating that this model is the most optimal model for the test set.

b

  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?
varImp(chemicals_svm, scale =FALSE)
## loess r-squared variable importance
## 
##   only 20 most important variables shown (out of 57)
## 
##                        Overall
## ManufacturingProcess32  0.3704
## BiologicalMaterial06    0.3484
## ManufacturingProcess36  0.3045
## BiologicalMaterial03    0.3010
## ManufacturingProcess13  0.2987
## BiologicalMaterial02    0.2817
## ManufacturingProcess17  0.2812
## ManufacturingProcess09  0.2706
## ManufacturingProcess31  0.2687
## BiologicalMaterial12    0.2574
## ManufacturingProcess06  0.2456
## BiologicalMaterial11    0.2212
## ManufacturingProcess33  0.2203
## BiologicalMaterial04    0.1998
## ManufacturingProcess29  0.1991
## BiologicalMaterial01    0.1690
## BiologicalMaterial08    0.1664
## ManufacturingProcess11  0.1659
## BiologicalMaterial09    0.1514
## ManufacturingProcess30  0.1458

The top 10 predictors in the SVM model are manufacturing processes 32,36,13,17,09,31 and biological material 06,03,02,12. In my previous assignment of predictors in the linear model, the top predictors were manufacturing processes 32,17,13,09,36,06,33 and biological material 06,02,03.

There are many overlapping predictors in the linear and SVM models, including manufacturing processes 32,36,13,17,09 and biological material 02,03,06.

In both models, the manufacturing processes seem to dominate the lists. However there are much fewer biological material variables compared to manufacturing processes so there are actually a higher presence of biological materials in the top predictors lists relative to the number of variables.

c

  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?
ggplot(chemicals, aes(x=ManufacturingProcess32, y=Yield)) + geom_point()

ggplot(chemicals, aes(x=BiologicalMaterial06, y=Yield)) + geom_point()

ggplot(chemicals, aes(x=ManufacturingProcess36, y = Yield)) + geom_point()

The top 3 predictors have been plotted. For manufacturing process 32 and biological material 06, points further right tend to lead to higher yield but some points that are far right result in lower yield. In manufacturing process 36, the yield appears to be higher towards the left but the furthest left points result in a more blunted effect.