set.seed(200)
trainingData <- mlbench.friedman1(200, sd = 1)
trainingData$x <- data.frame(trainingData$x)
testData <- mlbench.friedman1(5000, sd = 1)
testData$x <- data.frame(testData$x)
knn_fit<- train(x=trainingData$x,y=trainingData$y, method = "knn", preProcess = c("center", "scale"), tuneLength = 10)
knnPred <- predict(knn_fit, newdata = testData$x)
postResample(pred = knnPred, obs = testData$y)
## RMSE Rsquared MAE
## 3.2040595 0.6819919 2.5683461
We now look at the variable importance of the knn fit.
varImp((knn_fit))
## loess r-squared variable importance
##
## Overall
## X4 100.0000
## X1 95.5047
## X2 89.6186
## X5 45.2170
## X3 29.9330
## X9 6.3299
## X10 5.5182
## X8 3.2527
## X6 0.8884
## X7 0.0000
It appears that variables X1-X10 are relevant to the KNN model with the exception of X7.
Let’s now look at a Multivariate Adaptive Regression Splines model (MARS):
MARS_fit<- train(x = trainingData$x, y = trainingData$y, method = "earth",
preProcess = c("center", "scale"), tuneLength = 10)
## Loading required package: earth
## Warning: package 'earth' was built under R version 4.0.5
## Loading required package: Formula
## Loading required package: plotmo
## Warning: package 'plotmo' was built under R version 4.0.5
## Loading required package: plotrix
##
## Attaching package: 'plotrix'
## The following object is masked from 'package:scales':
##
## rescale
## Loading required package: TeachingDemos
## Warning: package 'TeachingDemos' was built under R version 4.0.5
MARS_pred <- predict(MARS_fit, newdata = testData$x)
postResample(pred = MARS_pred, obs = testData$y)
## RMSE Rsquared MAE
## 1.776575 0.872700 1.358367
Having fit our model, lets see which variables are important to the fit.
varImp(MARS_fit)
## earth variable importance
##
## Overall
## X1 100.00
## X4 82.78
## X2 64.18
## X5 40.21
## X3 28.14
## X6 0.00
Looking at the output above, we can see that X1-X5 are relevant to the model which we know are the informative predictors.
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.
Our previous run resulted in the following metrics on the test set:
We’ll use the same setup to test some non-linear methods:
data("ChemicalManufacturingProcess")
ChemicalManufacturingProcess <- mice(data = ChemicalManufacturingProcess, m = 1, method = "pmm", seed = 123)
##
## iter imp variable
## 1 1 ManufacturingProcess01 ManufacturingProcess02 ManufacturingProcess03 ManufacturingProcess04 ManufacturingProcess05 ManufacturingProcess06 ManufacturingProcess07 ManufacturingProcess08 ManufacturingProcess10 ManufacturingProcess11 ManufacturingProcess12 ManufacturingProcess14 ManufacturingProcess22 ManufacturingProcess23 ManufacturingProcess24 ManufacturingProcess25 ManufacturingProcess26 ManufacturingProcess27 ManufacturingProcess28 ManufacturingProcess29 ManufacturingProcess30 ManufacturingProcess31 ManufacturingProcess33 ManufacturingProcess34 ManufacturingProcess35 ManufacturingProcess36 ManufacturingProcess40 ManufacturingProcess41
## 2 1 ManufacturingProcess01 ManufacturingProcess02 ManufacturingProcess03 ManufacturingProcess04 ManufacturingProcess05 ManufacturingProcess06 ManufacturingProcess07 ManufacturingProcess08 ManufacturingProcess10 ManufacturingProcess11 ManufacturingProcess12 ManufacturingProcess14 ManufacturingProcess22 ManufacturingProcess23 ManufacturingProcess24 ManufacturingProcess25 ManufacturingProcess26 ManufacturingProcess27 ManufacturingProcess28 ManufacturingProcess29 ManufacturingProcess30 ManufacturingProcess31 ManufacturingProcess33 ManufacturingProcess34 ManufacturingProcess35 ManufacturingProcess36 ManufacturingProcess40 ManufacturingProcess41
## 3 1 ManufacturingProcess01 ManufacturingProcess02 ManufacturingProcess03 ManufacturingProcess04 ManufacturingProcess05 ManufacturingProcess06 ManufacturingProcess07 ManufacturingProcess08 ManufacturingProcess10 ManufacturingProcess11 ManufacturingProcess12 ManufacturingProcess14 ManufacturingProcess22 ManufacturingProcess23 ManufacturingProcess24 ManufacturingProcess25 ManufacturingProcess26 ManufacturingProcess27 ManufacturingProcess28 ManufacturingProcess29 ManufacturingProcess30 ManufacturingProcess31 ManufacturingProcess33 ManufacturingProcess34 ManufacturingProcess35 ManufacturingProcess36 ManufacturingProcess40 ManufacturingProcess41
## 4 1 ManufacturingProcess01 ManufacturingProcess02 ManufacturingProcess03 ManufacturingProcess04 ManufacturingProcess05 ManufacturingProcess06 ManufacturingProcess07 ManufacturingProcess08 ManufacturingProcess10 ManufacturingProcess11 ManufacturingProcess12 ManufacturingProcess14 ManufacturingProcess22 ManufacturingProcess23 ManufacturingProcess24 ManufacturingProcess25 ManufacturingProcess26 ManufacturingProcess27 ManufacturingProcess28 ManufacturingProcess29 ManufacturingProcess30 ManufacturingProcess31 ManufacturingProcess33 ManufacturingProcess34 ManufacturingProcess35 ManufacturingProcess36 ManufacturingProcess40 ManufacturingProcess41
## 5 1 ManufacturingProcess01 ManufacturingProcess02 ManufacturingProcess03 ManufacturingProcess04 ManufacturingProcess05 ManufacturingProcess06 ManufacturingProcess07 ManufacturingProcess08 ManufacturingProcess10 ManufacturingProcess11 ManufacturingProcess12 ManufacturingProcess14 ManufacturingProcess22 ManufacturingProcess23 ManufacturingProcess24 ManufacturingProcess25 ManufacturingProcess26 ManufacturingProcess27 ManufacturingProcess28 ManufacturingProcess29 ManufacturingProcess30 ManufacturingProcess31 ManufacturingProcess33 ManufacturingProcess34 ManufacturingProcess35 ManufacturingProcess36 ManufacturingProcess40 ManufacturingProcess41
ChemicalManufacturingProcess <- mice::complete(ChemicalManufacturingProcess, 1)
set.seed(123)
train_test_split <- initial_split(ChemicalManufacturingProcess, prop = 0.80)
train <- training(train_test_split)
test <- testing(train_test_split)
features_train <- train[, -1]
y_train <- train[,1]
features_test <- test[, -1]
y_test <- test[,1]
ctrl <- trainControl(method = 'cv', number = 10)
\((a)\) Which nonlinear regression model gives the optimal resampling and test set performance?
knn <- train(features_train, y_train,
method="knn",
trControl = ctrl,
preProc = c('YeoJohnson', 'center', 'scale', 'pca'))
knn_predictions <- predict(knn, newdata=features_test)
knn
## k-Nearest Neighbors
##
## 141 samples
## 57 predictor
##
## Pre-processing: Yeo-Johnson transformation (27), centered (57), scaled
## (57), principal component signal extraction (57)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 127, 128, 125, 127, 128, 126, ...
## Resampling results across tuning parameters:
##
## k RMSE Rsquared MAE
## 5 1.341861 0.5098621 1.062598
## 7 1.358116 0.4881036 1.102625
## 9 1.392364 0.4661462 1.139923
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 5.
postResample(pred=knn_predictions,y_test)
## RMSE Rsquared MAE
## 1.1483501 0.6626058 0.9475111
nnetGrid <- expand.grid(size=c(1:10),
decay = c(0,0.01,.1),
bag = FALSE )
nnetTune <- train(features_train, y_train,
method="avNNet",
tuneGrid = nnetGrid,
preProc = c('YeoJohnson', 'center', 'scale', 'pca'),
linout=T,
trace=F,
MaxNWts=10 * (ncol(features_train)+1) + 10 + 1, maxit=500)
## Warning: executing %dopar% sequentially: no parallel backend registered
nnetTune
## Model Averaged Neural Network
##
## 141 samples
## 57 predictor
##
## Pre-processing: Yeo-Johnson transformation (27), centered (57), scaled
## (57), principal component signal extraction (57)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 141, 141, 141, 141, 141, 141, ...
## Resampling results across tuning parameters:
##
## size decay RMSE Rsquared MAE
## 1 0.00 1.660444 0.29444791 1.354168
## 1 0.01 1.426499 0.45250913 1.123151
## 1 0.10 1.412849 0.47226219 1.118842
## 2 0.00 1.507279 0.39345953 1.217148
## 2 0.01 1.373074 0.50104357 1.092403
## 2 0.10 1.481587 0.48387395 1.127596
## 3 0.00 2.092945 0.35557938 1.635466
## 3 0.01 1.840336 0.38190043 1.449070
## 3 0.10 1.857976 0.40021803 1.401618
## 4 0.00 2.412104 0.22485158 1.929662
## 4 0.01 1.921245 0.31338218 1.495299
## 4 0.10 1.895876 0.37571638 1.421616
## 5 0.00 2.485148 0.24419604 1.957258
## 5 0.01 1.704763 0.39801788 1.347274
## 5 0.10 2.056612 0.33983848 1.498018
## 6 0.00 3.219662 0.19909970 2.456685
## 6 0.01 1.720972 0.37885923 1.356747
## 6 0.10 2.054051 0.32070762 1.468125
## 7 0.00 4.379270 0.14936414 3.117547
## 7 0.01 1.610475 0.42579143 1.271070
## 7 0.10 2.052748 0.30909648 1.506220
## 8 0.00 6.088999 0.10498777 4.184413
## 8 0.01 1.710044 0.36218366 1.350626
## 8 0.10 1.944990 0.34529444 1.454777
## 9 0.00 7.787763 0.07223681 5.193777
## 9 0.01 1.797634 0.36624490 1.391814
## 9 0.10 1.918946 0.33460016 1.454235
## 10 0.00 9.873383 0.05443961 6.299442
## 10 0.01 1.989810 0.29937277 1.517033
## 10 0.10 1.867830 0.35178809 1.408637
##
## Tuning parameter 'bag' was held constant at a value of FALSE
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were size = 2, decay = 0.01 and bag = FALSE.
nn_predictions <- predict(nnetTune, newdata=features_test)
postResample(pred=nn_predictions,y_test)
## RMSE Rsquared MAE
## 1.4363498 0.4176806 1.1572885
svm <- train(features_train, y_train,
method="svmRadial",
trControl = ctrl,
preProc = c('YeoJohnson', 'center', 'scale', 'pca'))
svm
## Support Vector Machines with Radial Basis Function Kernel
##
## 141 samples
## 57 predictor
##
## Pre-processing: Yeo-Johnson transformation (27), centered (57), scaled
## (57), principal component signal extraction (57)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 127, 127, 128, 128, 128, 128, ...
## Resampling results across tuning parameters:
##
## C RMSE Rsquared MAE
## 0.25 1.501143 0.5174819 1.224633
## 0.50 1.372837 0.5376974 1.132132
## 1.00 1.246472 0.5926221 1.018897
##
## Tuning parameter 'sigma' was held constant at a value of 0.02775125
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.02775125 and C = 1.
svm_predictions <- predict(svm, newdata=features_test)
postResample(pred=svm_predictions,y_test)
## RMSE Rsquared MAE
## 1.324893 0.507160 1.124479
Looking at RMSE, the SVM model was just slightly better than the KNN model. However, both of these models did better than our elastic net model from chapter 6. The neural network model, however, did not do very well. It performed worse than our previous elastic net model.
| Model Name | RMSE | \(R^2\) | MAE |
|---|---|---|---|
| elastic net | 1.4203776 | 0.4330233 | 1.1207388 |
| KNN | 1.4203776 | 1.4203776 | 1.4203776 |
| Neural Net | 1.4203776 | 1.4203776 | 1.4203776 |
| SVM | 1.4203776 | 1.4203776 | 1.4203776 |
\((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?
Based on the metrics above, we chose the SVM model as our optimal model. Let’s now look at the variable importance:
svm_var_imp <- varImp(svm)
plot(svm_var_imp, top = 10)
Looking at the variable importance, we can see that the variable importance is split between the manufacturing and biological predictors. One does not outweight the other. They are both needed.
Our previous model from Ch.6 placed the following importance on the top features:
In looking at this, we can see that the importance is exactly the same as our previous run with the elastic net 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?
svm_imp_df <- rownames(svm_var_imp$importance %>%
data.frame() %>%
arrange(desc(Overall)) %>%
top_n(10, wt = Overall))
var_top_10 <- train[,names(train) %in% c(svm_imp_df, 'Yield')]# %>%
# bind_cols(Yield = ChemicalManufacturingProcess$Yield)
var_top_10 %>%
gather(variable, value, -Yield) %>%
ggplot(aes(x = value, y = Yield)) +
geom_point() +
facet_wrap(~variable, scales = 'free_x') +
labs(x = element_blank())
Looking at the results above, we see no differences than reported in 6.3. We can see that the features are strongly correlated. For Biological Material 02, 05, 06, and 12 as well as manufacturing process 09, and 32, the more we increase these values, the greater our yield will be. Conversely, for those not mentioned, these variables have negatively correlated relationships, meaning the more we can minimize these values, the higher our yield will be.