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(πx1x2) + 20(x3 − 0.5)2 + 10x4 + 5x5 + 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:
library(AppliedPredictiveModeling)
library(mlbench)
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(kernlab)
##
## Attaching package: 'kernlab'
## The following object is masked from 'package:ggplot2':
##
## alpha
library(earth)
## Loading required package: Formula
## Loading required package: plotmo
## Loading required package: plotrix
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.2.1
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ kernlab::alpha() masks ggplot2::alpha()
## ✖ purrr::cross() masks kernlab::cross()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ purrr::lift() masks caret::lift()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(ggplot2)
library(tidyr)
library(corrplot)
## corrplot 0.95 loaded
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)
set.seed(200)
#Tune several models on these data. For example:
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.654912 0.4779838 2.958475
## 7 3.529432 0.5118581 2.861742
## 9 3.446330 0.5425096 2.780756
## 11 3.378049 0.5723793 2.719410
## 13 3.332339 0.5953773 2.692863
## 15 3.309235 0.6111389 2.663046
## 17 3.317408 0.6201421 2.678898
## 19 3.311667 0.6333800 2.682098
## 21 3.316340 0.6407537 2.688887
## 23 3.326040 0.6491480 2.705915
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 15.
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.1750657 0.6785946 2.5443169
Other models
set.seed(200)
ctrl <- trainControl(
method = "cv",
preProcOptions = list(thresh = 0.75, cutoff = 0.75),
allowParallel = TRUE
)
#Multivariate Adaptive Regression Splines (MARS)
MARSTuned<-train(x = trainingData$x,
y = trainingData$y,trControl = ctrl, method = "earth", preProc = c("center", "scale"), tuneLength = 10)
MARSTuned
## Multivariate Adaptive Regression Spline
##
## 200 samples
## 10 predictor
##
## Pre-processing: centered (10), scaled (10)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 180, 180, 180, 180, 180, 180, ...
## Resampling results across tuning parameters:
##
## nprune RMSE Rsquared MAE
## 2 4.188280 0.3042527 3.460689
## 3 3.551182 0.4999832 2.837116
## 4 2.653143 0.7167280 2.128222
## 6 2.295006 0.7754603 1.853199
## 7 1.771950 0.8611767 1.391357
## 9 1.609816 0.8837307 1.299705
## 10 1.635035 0.8798236 1.309436
## 12 1.571561 0.8898750 1.253077
## 13 1.567577 0.8906927 1.250795
## 15 1.571673 0.8909652 1.245508
##
## 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 = 13 and degree = 1.
MARSPred <- predict(MARSTuned, newdata = testData$x)
postResample(pred = MARSPred, obs = testData$y)
## RMSE Rsquared MAE
## 1.8136467 0.8677298 1.3911836
summary(MARSTuned$finalModel)
## Call: earth(x=data.frame[200,10], y=c(18.46,16.1,17...), keepxy=TRUE, degree=1,
## nprune=13)
##
## coefficients
## (Intercept) 18.4519837
## h(0.507267-X1) -3.0621008
## h(0.325504-X2) -2.9168964
## h(X3- -0.804171) 5.8121260
## h(-0.216741-X3) 5.0428427
## h(X3- -0.216741) -6.5663297
## h(X3-0.453446) 4.2729218
## h(0.953812-X4) -2.7912381
## h(X4-0.953812) 2.5308498
## h(1.17878-X5) -1.3309220
## h(X5-1.17878) 3.0524126
## h(X6- -0.47556) -0.5557031
##
## Selected 12 of 18 terms, and 6 of 10 predictors (nprune=13)
## 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
#Support Vector Machines
SVMTuned<-train(x = trainingData$x,
y = trainingData$y, method = "svmRadial", trcontrol=ctrl, preProc = c("center", "scale"), tuneLength = 10)
SVMTuned
## 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.524455 0.7766769 1.999415
## 0.50 2.317116 0.7916001 1.818477
## 1.00 2.181613 0.8085383 1.701333
## 2.00 2.084954 0.8232131 1.624359
## 4.00 2.045763 0.8287447 1.594254
## 8.00 2.020359 0.8327513 1.574066
## 16.00 2.010833 0.8344219 1.566926
## 32.00 2.010833 0.8344219 1.566926
## 64.00 2.010833 0.8344219 1.566926
## 128.00 2.010833 0.8344219 1.566926
##
## Tuning parameter 'sigma' was held constant at a value of 0.05694532
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.05694532 and C = 16.
SVMPred <- predict(SVMTuned, newdata = testData$x)
postResample(pred = SVMPred, obs = testData$y)
## RMSE Rsquared MAE
## 2.0560938 0.8285636 1.5630242
#Neural Networks
set.seed(200)
nnetGrid <- expand.grid(.decay = c(0, 0.01, .1), .size = c(1:10), .bag = FALSE)
nnetTuned<-train(x = trainingData$x,
y = trainingData$y, method = "avNNet",trControl = ctrl, preProc = c("center", "scale"), linout = TRUE, trace = FALSE, maxit = 500)
## Warning: executing %dopar% sequentially: no parallel backend registered
nnetTuned
## Model Averaged Neural Network
##
## 200 samples
## 10 predictor
##
## Pre-processing: centered (10), scaled (10)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 180, 180, 180, 180, 180, 180, ...
## Resampling results across tuning parameters:
##
## size decay RMSE Rsquared MAE
## 1 0e+00 2.442297 0.7520774 1.933356
## 1 1e-04 2.433427 0.7576218 1.925130
## 1 1e-01 2.392327 0.7614507 1.873867
## 3 0e+00 2.025028 0.8274059 1.643469
## 3 1e-04 2.085907 0.8066880 1.709343
## 3 1e-01 2.218911 0.7869756 1.754763
## 5 0e+00 2.033497 0.8158002 1.621108
## 5 1e-04 2.169608 0.7981145 1.678564
## 5 1e-01 1.996828 0.8326874 1.636291
##
## 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 = 5, decay = 0.1 and bag = FALSE.
nnetPred <- predict(nnetTuned, newdata = testData$x)
postResample(pred = nnetPred, obs = testData$y)
## RMSE Rsquared MAE
## 2.0022007 0.8434368 1.5175581
Which models appear to give the best performance? Does MARS select the informative predictors (those named X1–X5)? Looking all models the MARS model seemed to have the optimal performance, as it had the highest R square and lowest RSME. The MARS seems to use the X1, X4, X2, X5, X3, and X6, while leaving X7, X8, X9, and X10 unused.
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. (a) Which nonlinear regression model gives the optimal resampling and test set performance? Neural network was optimal model with the lowest RSME and the highest R squared of 75%.
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? Neural network was the optimal model, the important variables were manufacturingProcess32, manufacturingProcess13, BiologicalMaterial06, manufacturingProcess17, manufacturingProcess06, manufacturingProcess09, biologicalMaterial03, biologicalMaterial12, manufacturingProcess36, manufacturingProcess31. Manufacturing process seems to dominate the model, just like in the linear optimal model. The non linear model and linear optimal model seem to mostly use manufacturingProcess32, but the linear models use a higher percentage of manufactoring processing
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? The manufacturing optimize the yield even though on the correlations plot some of the manufacturing variables yield a negative correlation while the three top biological variables had only positive correlation with the yield. Overall, the manufacturing variable had positive trending plots especially for manufacturingProcess32 and manufacturingProcess09.
set.seed(200)
data(ChemicalManufacturingProcess)
Prepro<-preProcess(ChemicalManufacturingProcess, method= "knnImpute")
ProcessPredictorImputed<-predict(Prepro, ChemicalManufacturingProcess)
ProcessPredictorImputed<-as.data.frame(ProcessPredictorImputed)
ProcessPredictor<-select(ProcessPredictorImputed, -Yield)
Yield_df<-ProcessPredictorImputed$Yield
ProcessPredictorf<-ProcessPredictor[, -nearZeroVar(ProcessPredictor)]
trainIndex2<-createDataPartition(Yield_df, p=0.8, list = FALSE)
train_data2<- ProcessPredictorf[trainIndex2,]
train_response2<-Yield_df[trainIndex2]
test_data2<-ProcessPredictorf[-trainIndex2,]
test_reponse2<-Yield_df[-trainIndex2]
Test models
set.seed(200)
#Neural network
nnetTuned2<-train(x=train_data2, y=train_response2, method = "avNNet",trControl = ctrl, preProc = c("center", "scale"), linout = TRUE, trace = FALSE, maxit = 500)
nnetTuned2
## Model Averaged Neural Network
##
## 144 samples
## 56 predictor
##
## Pre-processing: centered (56), scaled (56)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 130, 129, 129, 130, 131, 131, ...
## Resampling results across tuning parameters:
##
## size decay RMSE Rsquared MAE
## 1 0e+00 0.7767362 0.4172675 0.6060313
## 1 1e-04 0.7869078 0.4252119 0.6352456
## 1 1e-01 0.7400146 0.4921524 0.6108850
## 3 0e+00 0.7103220 0.5108607 0.5724642
## 3 1e-04 0.6818399 0.5600520 0.5426499
## 3 1e-01 0.6647829 0.5941768 0.5306884
## 5 0e+00 0.6767818 0.5580051 0.5441611
## 5 1e-04 0.6669306 0.5645378 0.5341059
## 5 1e-01 0.6049765 0.6372421 0.4899009
##
## 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 = 5, decay = 0.1 and bag = FALSE.
nnPred2 <- predict(nnetTuned2, newdata = test_data2)
postResample(pred = nnPred2, obs = test_reponse2)
## RMSE Rsquared MAE
## 0.5513593 0.7450527 0.4633381
set.seed(200)
#Multivariate Adaptive Regression Splines (MARS)
MARSTuned2<-train(x=train_data2, y=train_response2,trControl = ctrl, method = "earth", preProc = c("center", "scale"), tuneLength = 10)
MARSTuned2
## Multivariate Adaptive Regression Spline
##
## 144 samples
## 56 predictor
##
## Pre-processing: centered (56), scaled (56)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 130, 129, 129, 130, 131, 131, ...
## Resampling results across tuning parameters:
##
## nprune RMSE Rsquared MAE
## 2 0.7305860 0.4740376 0.5893344
## 3 0.6794024 0.5423917 0.5501780
## 5 0.6778467 0.5557293 0.5477346
## 7 0.6511443 0.5981027 0.5127111
## 8 0.6404873 0.6126385 0.5148017
## 10 0.6574463 0.5721047 0.5066429
## 12 0.6509779 0.5669008 0.5057417
## 13 0.6609500 0.5589534 0.5115388
## 15 0.6572804 0.5590509 0.5101976
## 17 0.6642686 0.5506309 0.5173140
##
## 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 = 8 and degree = 1.
MARSPred2 <- predict(MARSTuned2, newdata = test_data2)
postResample(pred = MARSPred2, obs = test_reponse2)
## RMSE Rsquared MAE
## 0.6814443 0.6493969 0.5743711
set.seed(200)
#Support Vector Machines
SVMTuned2<-train(x=train_data2, y=train_response2, method = "svmRadial", trcontrol=ctrl, preProc = c("center", "scale"), tuneLength = 10)
SVMTuned2
## Support Vector Machines with Radial Basis Function Kernel
##
## 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:
##
## C RMSE Rsquared MAE
## 0.25 0.7551757 0.4504962 0.6075408
## 0.50 0.7058869 0.5027595 0.5619368
## 1.00 0.6679951 0.5496216 0.5284799
## 2.00 0.6398477 0.5834119 0.5083169
## 4.00 0.6292318 0.5952732 0.5046131
## 8.00 0.6258463 0.5982749 0.5030975
## 16.00 0.6250836 0.5989852 0.5024398
## 32.00 0.6250836 0.5989852 0.5024398
## 64.00 0.6250836 0.5989852 0.5024398
## 128.00 0.6250836 0.5989852 0.5024398
##
## Tuning parameter 'sigma' was held constant at a value of 0.01363222
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01363222 and C = 16.
SVMPred2 <- predict(SVMTuned2, newdata = test_data2)
postResample(pred = SVMPred2, obs = test_reponse2)
## RMSE Rsquared MAE
## 0.7197698 0.6250410 0.5383834
#K-nearest neighbors
set.seed(200)
knnModel2 <- train(x=train_data2, y=train_response2, method = "knn", preProc = c("center", "scale"), tuneLength = 10)
knnModel2
## 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 0.7461337 0.4454446 0.5873234
## 7 0.7516585 0.4341527 0.5894300
## 9 0.7498239 0.4308225 0.5898844
## 11 0.7522287 0.4293038 0.5934021
## 13 0.7554237 0.4237246 0.5960096
## 15 0.7579265 0.4196074 0.6006487
## 17 0.7622554 0.4143155 0.6062340
## 19 0.7646199 0.4117789 0.6093864
## 21 0.7679065 0.4094706 0.6142844
## 23 0.7737020 0.4033178 0.6200534
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 5.
knnPred2 <- predict(knnModel2, newdata = test_data2)
postResample(pred = knnPred2, obs = test_reponse2)
## RMSE Rsquared MAE
## 0.8646243 0.4470237 0.6917894
Top Variables
nnetImp<-varImp(nnetTuned2, scale=TRUE)
nnetImp
## loess r-squared variable importance
##
## only 20 most important variables shown (out of 56)
##
## Overall
## ManufacturingProcess32 100.00
## BiologicalMaterial06 87.81
## ManufacturingProcess13 78.23
## BiologicalMaterial03 76.45
## BiologicalMaterial12 69.16
## ManufacturingProcess17 68.44
## ManufacturingProcess36 67.83
## ManufacturingProcess31 67.82
## ManufacturingProcess09 64.12
## ManufacturingProcess06 60.80
## ManufacturingProcess29 53.56
## BiologicalMaterial02 53.50
## ManufacturingProcess11 50.24
## BiologicalMaterial11 48.66
## ManufacturingProcess33 46.88
## ManufacturingProcess30 45.42
## BiologicalMaterial09 38.34
## BiologicalMaterial04 37.89
## BiologicalMaterial08 36.89
## ManufacturingProcess12 36.65
plot(nnetImp, top=10)
Variables correlation
Top_var<-as.data.frame(varImp(nnetTuned2)$importance)
Top_var<-Top_var|>
arrange(desc(Overall))|>
head(10)
Top_names<-rownames(Top_var)
Top_var<-train_data2[,Top_names]
Top_var$Yield<-ProcessPredictorImputed$Yield[trainIndex2]
featurePlot(train_data2[, Top_names],
train_response2,
plot = "scatter",
type = c("p", "smooth"),
layout = c(2, 5))
corrplot(cor(Top_var), method="number")