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:

KNN

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

MARS

marsModel <- train(x = trainingData$x,
                  y = trainingData$y,
                  method = "earth",
                  preProc = c('center','scale'),
                  tuneLength = 10)
marsModel
## 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.383438  0.2405683  3.597961
##    3      3.645469  0.4745962  2.930453
##    4      2.727602  0.7035031  2.184240
##    6      2.331605  0.7835496  1.833420
##    7      1.976830  0.8421599  1.562591
##    9      1.804342  0.8683110  1.410395
##   10      1.787676  0.8711960  1.386944
##   12      1.821005  0.8670619  1.419893
##   13      1.858688  0.8617344  1.445459
##   15      1.871033  0.8607099  1.457618
## 
## 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 = 10 and degree = 1.
marsPred <- predict(marsModel, testData$x)
## The function 'postResample' can be used to get the test set performance values
postResample(pred = marsPred, obs = testData$y)
##     RMSE Rsquared      MAE 
## 1.776575 0.872700 1.358367
plotmo(marsModel)
##  plotmo grid:    X1        X2       X3        X4        X5        X6        X7
##           0.5139349 0.5106664 0.537307 0.4445841 0.5343299 0.4975981 0.4688035
##        X8        X9       X10
##  0.497961 0.5288716 0.5359218

varImp(marsModel)
## earth variable importance
## 
##     Overall
## X1  100.000
## X4   84.065
## X2   66.860
## X5   44.678
## X3   33.508
## X6    7.475
## X8    0.000
## X7    0.000
## X9    0.000
## X10   0.000

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.622074  0.7670398  2.078363
##     0.50  2.428366  0.7782023  1.908488
##     1.00  2.289163  0.7958353  1.789174
##     2.00  2.197788  0.8087076  1.717945
##     4.00  2.142765  0.8172903  1.671395
##     8.00  2.122176  0.8203240  1.663161
##    16.00  2.119565  0.8206712  1.661351
##    32.00  2.119565  0.8206712  1.661351
##    64.00  2.119565  0.8206712  1.661351
##   128.00  2.119565  0.8206712  1.661351
## 
## Tuning parameter 'sigma' was held constant at a value of 0.06424631
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.06424631 and C = 16.
svmPred <- predict(svmModel, newdata = testData$x)
## The function 'postResample' can be used to get the test set performance values
postResample(pred = svmPred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 2.0767136 0.8251819 1.5775457

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

The optimal MARS model uses nprnue of 10 and degree of 1 and achieves RMSE of 1.777. It clearly identifies X1-X5 as most important predictors, although it also gives a weight of ~7.5 to an artificial non-informative X6.
The final values used for the SVM model were sigma = 0.06424631 and C = 16 but RMSE of 2.0767136.
Based on RMSE, MARS performed the best, followed by SVM and then KNN.

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)
features <- subset(ChemicalManufacturingProcess,select= -Yield)
yield <- subset(ChemicalManufacturingProcess,select=Yield)

prep <- preProcess(features, method=c('scale','center','knnImpute'))
prep_features <- predict(prep,features)
# Train and test
set.seed(1)
split <- createDataPartition(yield$Yield,p=0.75,list=FALSE)
x_train <- prep_features[split,]
y_train <- yield[split,]
x_test <- prep_features[-split,]
y_test <- yield[-split,]

# Additional preprocessing
## remove near zero variance predictors that carry no information
pred_to_remove <- nearZeroVar(features)
x_train <- x_train[-pred_to_remove]
x_test <- x_test[-pred_to_remove]

## Remove highly correlated features
corThresh <- 0.9
tooHigh <- findCorrelation(cor(x_train),corThresh)
x_train <- x_train[,-tooHigh]
x_test <- x_test[,-tooHigh]

set.seed(1)
ctrl <- trainControl(method='cv',number=10)

MARS and SVM

mars_model <- train(x=x_train,y=y_train, method='earth', trControl=ctrl, tuneLength = 10) 
mars_model
## Multivariate Adaptive Regression Spline 
## 
## 132 samples
##  46 predictor
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 119, 119, 119, 118, 119, 118, ... 
## Resampling results across tuning parameters:
## 
##   nprune  RMSE      Rsquared   MAE      
##    2      1.372090  0.4604998  1.0760093
##    3      1.243573  0.5434117  1.0100316
##    5      1.140726  0.6273039  0.9149526
##    7      1.101811  0.6285942  0.8849517
##    9      1.142892  0.6109131  0.9209667
##   10      1.162364  0.5951641  0.9753395
##   12      1.231896  0.5589386  1.0199752
##   14      1.261992  0.5527090  1.0300597
##   16      1.255176  0.5575749  1.0306023
##   18      1.318385  0.5331236  1.0821733
## 
## 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 = 7 and degree = 1.
plot(mars_model,metric="Rsquared")

set.seed(1)
predictions <- predict(mars_model,x_test)
postResample(obs = y_test,pred = predictions)
##      RMSE  Rsquared       MAE 
## 1.4718783 0.4722602 1.1294749
svm_model <- train(x=x_train,y=y_train, method='svmRadial', trControl=ctrl, tuneLength = 10) 
svm_model
## Support Vector Machines with Radial Basis Function Kernel 
## 
## 132 samples
##  46 predictor
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 119, 119, 119, 118, 119, 118, ... 
## Resampling results across tuning parameters:
## 
##   C       RMSE      Rsquared   MAE      
##     0.25  1.386893  0.5246701  1.1453592
##     0.50  1.260493  0.5721034  1.0451024
##     1.00  1.147968  0.6359321  0.9511145
##     2.00  1.077210  0.6725454  0.8721820
##     4.00  1.089107  0.6451939  0.8612450
##     8.00  1.106047  0.6238061  0.8774081
##    16.00  1.104814  0.6245272  0.8771238
##    32.00  1.104814  0.6245272  0.8771238
##    64.00  1.104814  0.6245272  0.8771238
##   128.00  1.104814  0.6245272  0.8771238
## 
## Tuning parameter 'sigma' was held constant at a value of 0.01454058
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01454058 and C = 2.
plot(svm_model,metric="Rsquared")

set.seed(1)
predictions <- predict(svm_model,x_test)
postResample(obs = y_test,pred = predictions)
##      RMSE  Rsquared       MAE 
## 1.2467442 0.6321083 0.9892592

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

MARS uses nprune = 7 and degree = 1 and results in 1.101811/1.4718783 RMSE for train/test sets while achieving Rsquared of 0.4722602.
SVM performs better on both train adn test, 1.077210 and 1.2467442 respectively while also achieving higher R squared of 0.6321083.
Although SVM appears to do better, PLS model produced slightly better results with 4 components and lowest test RMSE of 1.2080628 and highest R squared of 0.6388046.

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

plot(varImp(svm_model, scale = FALSE), top=20,scales = list(y = list(cex = 0.8)))

SVM produces the same top 10 as optimal non-linear model.

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

feature_imp <- varImp(svm_model, scale = FALSE)
feature_imp_order <- order(feature_imp$importance,decreasing=TRUE)
top5 = rownames(feature_imp$importance)[feature_imp_order[c(1:5)]]

featurePlot(x_train[, top5],y_train,plot = "scatter")

4 out of the top 5 predictors has a positive correlation with the target, the relationships appears to be linear between all top 5 and the target potentially explaining why more complex models tend to underperform.