Friedman (1991) introduced several benchmark data sets create by simulation. One of these simulations used the following nonlinear equation to create data:
Which models appear to give the best performance? Does MARS select the informative predictors (those named X1–X5)?
Mars appears to give the best performance with its RMSE and MAE being the lowest, and R2 being he highest.
Mars does select the informative predictors of x1-x5, but x3 seems to not have any importance.
library(mlbench)
library(caret)
## Warning: package 'caret' was built under R version 4.4.3
## Loading required package: ggplot2
## Loading required package: lattice
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)
library(caret)
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
## perforamnce values
postResample(pred = knnPred, obs = testData$y)
## RMSE Rsquared MAE
## 3.2040595 0.6819919 2.5683461
marsGrid <- expand.grid(.degree = 1:2, .nprune = 2:30)
# tune
marsTune <- train(trainingData$x, trainingData$y,
method = "earth",
tuneGrid = marsGrid,
trControl = trainControl(method = "cv"))
## 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
marsTune
## Multivariate Adaptive Regression Spline
##
## 200 samples
## 10 predictor
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 180, 180, 180, 180, 180, 180, ...
## Resampling results across tuning parameters:
##
## degree nprune RMSE Rsquared MAE
## 1 2 4.462296 0.2176253 3.697979
## 1 3 3.720663 0.4673821 2.949121
## 1 4 2.680039 0.7094916 2.123848
## 1 5 2.333538 0.7781559 1.856629
## 1 6 2.367933 0.7754329 1.901509
## 1 7 1.809983 0.8656526 1.414985
## 1 8 1.692656 0.8838936 1.333678
## 1 9 1.704958 0.8845683 1.339517
## 1 10 1.688559 0.8842495 1.309838
## 1 11 1.669043 0.8886165 1.296522
## 1 12 1.645066 0.8892796 1.271981
## 1 13 1.655570 0.8886896 1.271232
## 1 14 1.666354 0.8879143 1.285545
## 1 15 1.666354 0.8879143 1.285545
## 1 16 1.666354 0.8879143 1.285545
## 1 17 1.666354 0.8879143 1.285545
## 1 18 1.666354 0.8879143 1.285545
## 1 19 1.666354 0.8879143 1.285545
## 1 20 1.666354 0.8879143 1.285545
## 1 21 1.666354 0.8879143 1.285545
## 1 22 1.666354 0.8879143 1.285545
## 1 23 1.666354 0.8879143 1.285545
## 1 24 1.666354 0.8879143 1.285545
## 1 25 1.666354 0.8879143 1.285545
## 1 26 1.666354 0.8879143 1.285545
## 1 27 1.666354 0.8879143 1.285545
## 1 28 1.666354 0.8879143 1.285545
## 1 29 1.666354 0.8879143 1.285545
## 1 30 1.666354 0.8879143 1.285545
## 2 2 4.440854 0.2204755 3.686796
## 2 3 3.697203 0.4714312 2.938566
## 2 4 2.664266 0.7149235 2.119566
## 2 5 2.313371 0.7837374 1.852172
## 2 6 2.335796 0.7875253 1.841919
## 2 7 1.833780 0.8622906 1.462210
## 2 8 1.688673 0.8883137 1.325754
## 2 9 1.557314 0.9002634 1.234207
## 2 10 1.463018 0.9133897 1.174354
## 2 11 1.350247 0.9265882 1.099432
## 2 12 1.305955 0.9344683 1.049853
## 2 13 1.261130 0.9357469 1.017123
## 2 14 1.286463 0.9315381 1.040156
## 2 15 1.337104 0.9297651 1.069602
## 2 16 1.337560 0.9294593 1.067973
## 2 17 1.318152 0.9322833 1.054436
## 2 18 1.324331 0.9319631 1.055971
## 2 19 1.324331 0.9319631 1.055971
## 2 20 1.324331 0.9319631 1.055971
## 2 21 1.324331 0.9319631 1.055971
## 2 22 1.324331 0.9319631 1.055971
## 2 23 1.324331 0.9319631 1.055971
## 2 24 1.324331 0.9319631 1.055971
## 2 25 1.324331 0.9319631 1.055971
## 2 26 1.324331 0.9319631 1.055971
## 2 27 1.324331 0.9319631 1.055971
## 2 28 1.324331 0.9319631 1.055971
## 2 29 1.324331 0.9319631 1.055971
## 2 30 1.324331 0.9319631 1.055971
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 13 and degree = 2.
marsPred <- predict(marsTune, testData$x)
postResample(marsPred, testData$y)
## RMSE Rsquared MAE
## 1.2803060 0.9335241 1.0168673
varImp(marsTune)
## earth variable importance
##
## Overall
## X1 100.00
## X4 75.33
## X2 48.88
## X5 15.63
## X3 0.00
plot(varImp(marsTune))
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.
SVM gives the optimal resampling and test set performance with lowest RMSE and MAE and the highest R2
Manufacturingprocess32 is the most important in the optimal nonlinear regression model from the plot. From the 10 variables, process variables and the material variables are the same amount. The two predictors are the same.
From the plot, Manufacturingprocess32 has the highest positive correlation with yield. Two of the top ten variables are negatively correlated with yield.
It seems that the biological predictors have a positive relationship with the yield, while the manufacturing processes vary with their relationship with the yield.
library(AppliedPredictiveModeling)
## Warning: package 'AppliedPredictiveModeling' was built under R version 4.4.3
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(corrplot)
## corrplot 0.95 loaded
data(ChemicalManufacturingProcess)
miss <- preProcess(ChemicalManufacturingProcess, method = "bagImpute")
Chemical <- predict(miss, ChemicalManufacturingProcess)
Chemical <- Chemical[, -nearZeroVar(Chemical)]
index <- createDataPartition(Chemical$Yield, p = .8, list = FALSE)
train_x <- Chemical[index, -1]
train_y <- Chemical[index, 1]
test_x <- Chemical[-index, -1]
test_y <- Chemical[-index, 1]
knnModel <- train(train_x, train_y,
method = "knn",
preProc = c("center", "scale"),
tuneLength = 10)
knnModel
## k-Nearest Neighbors
##
## 144 samples
## 56 predictor
##
## Pre-processing: centered (56), scaled (56)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 144, 144, 144, 144, 144, 144, ...
## Resampling results across tuning parameters:
##
## k RMSE Rsquared MAE
## 5 1.381224 0.3990807 1.103515
## 7 1.356936 0.4155788 1.080061
## 9 1.361377 0.4108383 1.092732
## 11 1.350230 0.4243657 1.100710
## 13 1.357541 0.4211747 1.104899
## 15 1.363686 0.4189184 1.108411
## 17 1.371492 0.4143570 1.116156
## 19 1.380404 0.4115224 1.127228
## 21 1.384393 0.4135101 1.131031
## 23 1.391532 0.4121049 1.136172
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 11.
knnPred <- predict(knnModel, test_x)
## The function 'postResample' can be used to get the test set
## perforamnce values
postResample(pred = knnPred, test_y)
## RMSE Rsquared MAE
## 1.4039134 0.5669964 1.1765057
marsGrid <- expand.grid(.degree = 1:2, .nprune = 2:30)
# tune
marsTune <- train(train_x, train_y,
method = "earth",
tuneGrid = marsGrid,
trControl = trainControl(method = "cv"))
marsTune
## Multivariate Adaptive Regression Spline
##
## 144 samples
## 56 predictor
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 129, 129, 130, 130, 129, 130, ...
## Resampling results across tuning parameters:
##
## degree nprune RMSE Rsquared MAE
## 1 2 1.281781 0.5226726 1.0496050
## 1 3 1.184483 0.5849470 0.9398063
## 1 4 1.163172 0.5997102 0.9344817
## 1 5 1.232089 0.5494297 0.9554011
## 1 6 1.209185 0.5864540 0.9546436
## 1 7 1.218051 0.5717105 0.9533332
## 1 8 1.222471 0.5781783 0.9647441
## 1 9 1.242943 0.5624306 0.9949135
## 1 10 1.251811 0.5679442 0.9959946
## 1 11 1.270463 0.5642196 1.0019692
## 1 12 1.282921 0.5630747 1.0195283
## 1 13 1.307248 0.5523690 1.0478904
## 1 14 1.311518 0.5476267 1.0516343
## 1 15 1.318972 0.5470814 1.0636929
## 1 16 1.320506 0.5475500 1.0648859
## 1 17 1.320587 0.5477294 1.0633398
## 1 18 1.320587 0.5477294 1.0633398
## 1 19 1.318566 0.5479335 1.0561959
## 1 20 1.318566 0.5479335 1.0561959
## 1 21 1.318566 0.5479335 1.0561959
## 1 22 1.318566 0.5479335 1.0561959
## 1 23 1.318566 0.5479335 1.0561959
## 1 24 1.318566 0.5479335 1.0561959
## 1 25 1.318566 0.5479335 1.0561959
## 1 26 1.318566 0.5479335 1.0561959
## 1 27 1.318566 0.5479335 1.0561959
## 1 28 1.318566 0.5479335 1.0561959
## 1 29 1.318566 0.5479335 1.0561959
## 1 30 1.333043 0.5505933 1.0766590
## 2 2 1.281781 0.5226726 1.0496050
## 2 3 1.222550 0.5545575 0.9700388
## 2 4 1.197176 0.5708551 0.9483650
## 2 5 1.243373 0.5472929 0.9870546
## 2 6 1.265766 0.5321028 0.9984874
## 2 7 1.308037 0.5202368 1.0333974
## 2 8 1.353961 0.4902878 1.0875388
## 2 9 1.268720 0.5525268 1.0189859
## 2 10 1.246543 0.5716079 1.0121757
## 2 11 1.229396 0.5869111 1.0029395
## 2 12 1.153878 0.6189550 0.9531902
## 2 13 1.175851 0.6201798 0.9634786
## 2 14 1.141421 0.6332435 0.9270837
## 2 15 1.198365 0.6166521 0.9573058
## 2 16 1.188103 0.6189144 0.9507168
## 2 17 1.177971 0.6235288 0.9386817
## 2 18 1.176905 0.6291218 0.9247471
## 2 19 1.229698 0.6133093 0.9485674
## 2 20 1.253873 0.6087386 0.9662421
## 2 21 1.247021 0.6116944 0.9695293
## 2 22 1.248767 0.6116780 0.9694493
## 2 23 1.250926 0.6120831 0.9765237
## 2 24 1.251507 0.6149208 0.9753773
## 2 25 1.251507 0.6149208 0.9753773
## 2 26 1.251507 0.6149208 0.9753773
## 2 27 1.251507 0.6149208 0.9753773
## 2 28 1.251507 0.6149208 0.9753773
## 2 29 1.251507 0.6149208 0.9753773
## 2 30 1.251507 0.6149208 0.9753773
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 14 and degree = 2.
marsPred <- predict(marsTune, test_x)
postResample(marsPred, test_y)
## RMSE Rsquared MAE
## 1.0522885 0.7213996 0.8686884
svmRTune <- train(train_x, train_y,
method = "svmRadial",
preProc = c("center", "scale"),
tuneLength = 20,
trControl = trainControl(method = "cv"))
svmRTune
## Support Vector Machines with Radial Basis Function Kernel
##
## 144 samples
## 56 predictor
##
## Pre-processing: centered (56), scaled (56)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 129, 130, 130, 130, 131, 130, ...
## Resampling results across tuning parameters:
##
## C RMSE Rsquared MAE
## 0.25 1.402229 0.4755614 1.1277842
## 0.50 1.310692 0.5252437 1.0442802
## 1.00 1.243616 0.5703446 0.9800799
## 2.00 1.228951 0.5767181 0.9680163
## 4.00 1.203515 0.5885005 0.9490970
## 8.00 1.183267 0.6007059 0.9407434
## 16.00 1.180667 0.6030502 0.9381668
## 32.00 1.180667 0.6030502 0.9381668
## 64.00 1.180667 0.6030502 0.9381668
## 128.00 1.180667 0.6030502 0.9381668
## 256.00 1.180667 0.6030502 0.9381668
## 512.00 1.180667 0.6030502 0.9381668
## 1024.00 1.180667 0.6030502 0.9381668
## 2048.00 1.180667 0.6030502 0.9381668
## 4096.00 1.180667 0.6030502 0.9381668
## 8192.00 1.180667 0.6030502 0.9381668
## 16384.00 1.180667 0.6030502 0.9381668
## 32768.00 1.180667 0.6030502 0.9381668
## 65536.00 1.180667 0.6030502 0.9381668
## 131072.00 1.180667 0.6030502 0.9381668
##
## Tuning parameter 'sigma' was held constant at a value of 0.01406868
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01406868 and C = 16.
svmRPred <- predict(svmRTune, test_x)
postResample(svmRPred, test_y)
## RMSE Rsquared MAE
## 1.1337756 0.6882189 0.9013860
larsTune <- train(train_x, train_y,
method = "lars",
metric = "Rsquared",
tuneLength = 20,
preProc = c("center", "scale"))
lars_predict <- predict(larsTune, test_x)
plot(varImp(svmRTune), top = 10,
main = "Non linear")
plot(varImp(larsTune), top = 10,
main = "linear")
top10 <- varImp(svmRTune)$importance |>
arrange(-Overall) |>
head(10)
Chemical |>
select(c("Yield", row.names(top10))) |>
cor() |>
corrplot()