Questions

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(x_{1}x_{2}) + 20(x_{3} − 0.5)^{2} + 10x_{4} + 5x_{5} + 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:

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.

SVM

svmModel <- train(x = trainingData$x,
                  y = trainingData$y,
                  method = "svmRadial",
                  preProc = c("center", "scale"),
                  tuneLength = 10,
                  trcontrol = trainControl(method = "cv"))
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

MarsModel <- earth(x = trainingData$x,
                  y = trainingData$y)
MarsModel
## Selected 12 of 18 terms, and 6 of 10 predictors
## Termination condition: Reached nk 21
## Importance: X1, X4, X2, X5, X3, X6, X7-unused, X8-unused, X9-unused, ...
## Number of terms at each degree of interaction: 1 11 (additive model)
## GCV 2.540556    RSS 397.9654    GRSq 0.8968524    RSq 0.9183982

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

The “best” model appears to be the MARS model with an \(R_{2} = 0.918\). The model selected 6 of the 10 predictors as significant, erroneously also including X6 (though it is the least important of the 6 selected).

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)
## Warning: package 'AppliedPredictiveModeling' was built under R version 4.0.5
library(RANN)
## Warning: package 'RANN' was built under R version 4.0.5
data(ChemicalManufacturingProcess)

(Chem_Imp <- preProcess(ChemicalManufacturingProcess[,-c(1)], method=c('knnImpute')))
## Created from 152 samples and 57 variables
## 
## Pre-processing:
##   - centered (57)
##   - ignored (0)
##   - 5 nearest neighbor imputation (57)
##   - scaled (57)
chem_mod <- predict(Chem_Imp, ChemicalManufacturingProcess[,-c(1)])
remove_cols <- nearZeroVar(chem_mod, names = TRUE, 
                           freqCut = 2, uniqueCut = 20)
all_cols <- colnames(chem_mod)
chem_mod <- chem_mod[ , setdiff(all_cols,remove_cols)]
#chem_mod <- chem_mod[-c(2,3,4,59,172,173,175),]
train_row <- sort(sample(nrow(chem_mod), nrow(chem_mod)*.7))
train_x_set <- chem_mod[train_row,]
test_x_set <- chem_mod[-train_row,]
train_y_set <- ChemicalManufacturingProcess[train_row,1]
test_y_set <- ChemicalManufacturingProcess[-train_row,1]

(a)

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

kNN

knnModel <- train(x = train_x_set,
                  y = train_y_set,
                  method = "knn",
                  preProc = c("center", "scale"),
                  tuneLength = 10)
knnModel
## k-Nearest Neighbors 
## 
## 123 samples
##  50 predictor
## 
## Pre-processing: centered (50), scaled (50) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 123, 123, 123, 123, 123, 123, ... 
## Resampling results across tuning parameters:
## 
##   k   RMSE      Rsquared   MAE     
##    5  1.434487  0.4077273  1.124932
##    7  1.405968  0.4318636  1.106720
##    9  1.408936  0.4264825  1.117952
##   11  1.415762  0.4253858  1.126695
##   13  1.431836  0.4191594  1.145256
##   15  1.437432  0.4164616  1.152639
##   17  1.441583  0.4142297  1.155709
##   19  1.443147  0.4179219  1.157065
##   21  1.455580  0.4104289  1.170164
##   23  1.467183  0.4066211  1.182166
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 7.

SVM

svmModel <- train(x = train_x_set,
                  y = train_y_set,
                  method = "svmRadial",
                  preProc = c("center", "scale"),
                  tuneLength = 10,
                  trcontrol = trainControl(method = "cv"))
svmModel
## Support Vector Machines with Radial Basis Function Kernel 
## 
## 123 samples
##  50 predictor
## 
## Pre-processing: centered (50), scaled (50) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 123, 123, 123, 123, 123, 123, ... 
## Resampling results across tuning parameters:
## 
##   C       RMSE      Rsquared   MAE      
##     0.25  1.482107  0.4698143  1.1989810
##     0.50  1.375911  0.5164094  1.1172773
##     1.00  1.287309  0.5632101  1.0469294
##     2.00  1.245321  0.5827228  1.0098198
##     4.00  1.235720  0.5875945  0.9925924
##     8.00  1.234760  0.5879702  0.9898537
##    16.00  1.234752  0.5879703  0.9898385
##    32.00  1.234752  0.5879703  0.9898385
##    64.00  1.234752  0.5879703  0.9898385
##   128.00  1.234752  0.5879703  0.9898385
## 
## Tuning parameter 'sigma' was held constant at a value of 0.01883398
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01883398 and C = 16.

MARS

MarsModel <- earth(x = train_x_set,
                  y = train_y_set)
MarsModel
## Selected 11 of 21 terms, and 8 of 50 predictors
## Termination condition: RSq changed by less than 0.001 at 21 terms
## Importance: ManufacturingProcess32, ManufacturingProcess13, ...
## Number of terms at each degree of interaction: 1 10 (additive model)
## GCV 1.025592    RSS 86.75005    GRSq 0.6977093    RSq 0.7886971
plot(MarsModel, which = 1)

Based on the \(R_{2}\) values, the MARS model appears to be the best test set performance - only 9 predictors are selected in this model.

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

evimp(MarsModel)
##                        nsubsets   gcv    rss
## ManufacturingProcess32       10 100.0  100.0
## ManufacturingProcess13        9  68.4   70.9
## ManufacturingProcess09        8  52.5   56.5
## ManufacturingProcess37        6  25.2   34.1
## ManufacturingProcess35        5  19.3   28.8
## ManufacturingProcess33        4  15.0   24.5
## ManufacturingProcess04        2  10.8   17.1
## ManufacturingProcess42        1   3.8   10.7

Manufacturing processes dominate this list similar to that of our optimal linear model.

The linear model from our results for question 6.3 found Manufacturing Processes 20, 32, 6, 9, 13 and 36 as the most important. 3 of these predictors are also present in our MARS model above.

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

best_pred <- c("ManufacturingProcess32", "ManufacturingProcess09", "ManufacturingProcess13","ManufacturingProcess01", "ManufacturingProcess42","ManufacturingProcess43")
featurePlot(train_x_set[,best_pred], train_y_set)

Manufacturing processes 9 and 32 seem to have positive relationships with our predictor, while Process 13 shows a similar negative relationship. The relationships for Processes 1, 42 and 43 seem to be largely influenced by outliers in the data - though removing these outliers before the modelin led to significant drops in the accuracy of the models, suggesting they are not neccessarily erroneous datapoints.