library(fpp3)
library(tidyverse)
library(ggrepel)
library(seasonal)
library(fabletools)
library(corrplot)
library(GGally)
library(patchwork)
library(caret)
library(pls)
library(impute)
library(mlbench)
library(earth)
library(nnet)
library(kernlab)
theme_set(theme_bw())
Friedman (1991) introduced several benchmark data sets create by simulation. One of these simulations used the following nonlinear equation to create data:
\[ y =10 sin(\pi*x_1x_2) + 20(x_3 - 0.5)^2 +10x_4 +5x_5 +N(0,\sigma^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(mlbench)
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:
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
Which models appear to give the best performance? Does MARS select the informative predictors (those named X1–X5)?
Before generating a neural network model, I first wanted to check if any predictor variables are highly correlated. The code below shows that there are no correlations between predictor variables that are greater than 0.75.
findCorrelation(cor(trainingData$x), cutoff = 0.75)
## integer(0)
nnet_grid = expand.grid(.decay = c(0, 0.01, 0.1),
.size = c(1:10),
.bag = FALSE)
set.seed(4802)
nnet_model = train(trainingData$x, trainingData$y, method = "avNNet",
tuneGrid = nnet_grid, preProcess = c("center","scale"),
linout = TRUE, trace = FALSE,
MaxNWts = 10*(ncol(trainingData$x) + 1) + 10 + 1,
maxit = 500)
nnet_model
## Model Averaged Neural Network
##
## 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:
##
## decay size RMSE Rsquared MAE
## 0.00 1 2.600125 0.7204249 2.035368
## 0.00 2 2.553704 0.7303848 2.003862
## 0.00 3 2.538358 0.7424452 1.922709
## 0.00 4 3.100628 0.6819504 2.191491
## 0.00 5 3.577035 0.5991681 2.533216
## 0.00 6 5.307826 0.4431775 3.356184
## 0.00 7 5.434058 0.4272049 3.473349
## 0.00 8 7.433494 0.4661234 3.609297
## 0.00 9 3.791446 0.5857550 2.640720
## 0.00 10 3.155473 0.6514636 2.376080
## 0.01 1 2.566597 0.7276397 1.998363
## 0.01 2 2.551223 0.7294990 1.991843
## 0.01 3 2.365426 0.7691362 1.821402
## 0.01 4 2.432228 0.7583750 1.872857
## 0.01 5 2.604648 0.7310852 2.018973
## 0.01 6 2.697635 0.7191187 2.100526
## 0.01 7 2.863890 0.6874994 2.234187
## 0.01 8 2.757605 0.7018657 2.152543
## 0.01 9 2.689106 0.7174711 2.129243
## 0.01 10 2.724914 0.7052798 2.156867
## 0.10 1 2.553832 0.7290217 1.981168
## 0.10 2 2.589668 0.7213623 2.023610
## 0.10 3 2.388960 0.7661582 1.836397
## 0.10 4 2.374971 0.7695267 1.833734
## 0.10 5 2.458134 0.7592485 1.911910
## 0.10 6 2.582990 0.7334766 2.003378
## 0.10 7 2.589708 0.7351064 2.041848
## 0.10 8 2.535342 0.7434492 1.976423
## 0.10 9 2.530273 0.7442006 1.988391
## 0.10 10 2.517136 0.7419550 1.969616
##
## 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 = 3, decay = 0.01 and bag = FALSE.
nnet_predict = predict(nnet_model, testData$x)
nnet_values = data.frame(obs = testData$y, pred = nnet_predict)
defaultSummary(nnet_values)
## RMSE Rsquared MAE
## 2.1595912 0.8152672 1.6131842
The best neural network with model averaging based on the lowest RMSE value above has 3 hidden units and a weight decay of 0.01. This resulted in an RMSE value of 2.16 and R^2 value of 0.815.
mars_grid = expand.grid(.degree = 1:3,
.nprune = 2:40)
set.seed(4802)
mars_model = train(trainingData$x, trainingData$y, method = "earth",
tuneGrid = mars_grid, trControl = trainControl(method = "cv"))
mars_model
## 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.378071 0.2432829 3.6485378
## 1 3 3.560340 0.4982366 2.8866138
## 1 4 2.581736 0.7354391 2.0995607
## 1 5 2.233225 0.8048217 1.7791225
## 1 6 2.214488 0.8049561 1.7088842
## 1 7 1.774786 0.8734303 1.3709266
## 1 8 1.756862 0.8775695 1.3847561
## 1 9 1.701870 0.8852597 1.3317422
## 1 10 1.648466 0.8934587 1.2791910
## 1 11 1.606507 0.8978629 1.2477603
## 1 12 1.574868 0.9022085 1.2302352
## 1 13 1.580799 0.9020059 1.2467258
## 1 14 1.587905 0.9007707 1.2608518
## 1 15 1.588769 0.9007675 1.2625152
## 1 16 1.588769 0.9007675 1.2625152
## 1 17 1.588769 0.9007675 1.2625152
## 1 18 1.588769 0.9007675 1.2625152
## 1 19 1.588769 0.9007675 1.2625152
## 1 20 1.588769 0.9007675 1.2625152
## 1 21 1.588769 0.9007675 1.2625152
## 1 22 1.588769 0.9007675 1.2625152
## 1 23 1.588769 0.9007675 1.2625152
## 1 24 1.588769 0.9007675 1.2625152
## 1 25 1.588769 0.9007675 1.2625152
## 1 26 1.588769 0.9007675 1.2625152
## 1 27 1.588769 0.9007675 1.2625152
## 1 28 1.588769 0.9007675 1.2625152
## 1 29 1.588769 0.9007675 1.2625152
## 1 30 1.588769 0.9007675 1.2625152
## 1 31 1.588769 0.9007675 1.2625152
## 1 32 1.588769 0.9007675 1.2625152
## 1 33 1.588769 0.9007675 1.2625152
## 1 34 1.588769 0.9007675 1.2625152
## 1 35 1.588769 0.9007675 1.2625152
## 1 36 1.588769 0.9007675 1.2625152
## 1 37 1.588769 0.9007675 1.2625152
## 1 38 1.588769 0.9007675 1.2625152
## 1 39 1.588769 0.9007675 1.2625152
## 1 40 1.588769 0.9007675 1.2625152
## 2 2 4.378071 0.2432829 3.6485378
## 2 3 3.560340 0.4982366 2.8866138
## 2 4 2.581736 0.7354391 2.0995607
## 2 5 2.241136 0.8040712 1.7812318
## 2 6 2.258931 0.7994036 1.7405812
## 2 7 1.758737 0.8737680 1.3758509
## 2 8 1.721752 0.8811214 1.3373373
## 2 9 1.498037 0.9124752 1.2039503
## 2 10 1.441506 0.9187651 1.1393687
## 2 11 1.361138 0.9291852 1.0664668
## 2 12 1.264489 0.9382915 1.0072496
## 2 13 1.275805 0.9372768 1.0067499
## 2 14 1.229927 0.9415173 0.9693102
## 2 15 1.227926 0.9410659 0.9524590
## 2 16 1.228307 0.9409677 0.9560451
## 2 17 1.228307 0.9409677 0.9560451
## 2 18 1.228307 0.9409677 0.9560451
## 2 19 1.228307 0.9409677 0.9560451
## 2 20 1.228307 0.9409677 0.9560451
## 2 21 1.228307 0.9409677 0.9560451
## 2 22 1.228307 0.9409677 0.9560451
## 2 23 1.228307 0.9409677 0.9560451
## 2 24 1.228307 0.9409677 0.9560451
## 2 25 1.228307 0.9409677 0.9560451
## 2 26 1.228307 0.9409677 0.9560451
## 2 27 1.228307 0.9409677 0.9560451
## 2 28 1.228307 0.9409677 0.9560451
## 2 29 1.228307 0.9409677 0.9560451
## 2 30 1.228307 0.9409677 0.9560451
## 2 31 1.228307 0.9409677 0.9560451
## 2 32 1.228307 0.9409677 0.9560451
## 2 33 1.228307 0.9409677 0.9560451
## 2 34 1.228307 0.9409677 0.9560451
## 2 35 1.228307 0.9409677 0.9560451
## 2 36 1.228307 0.9409677 0.9560451
## 2 37 1.228307 0.9409677 0.9560451
## 2 38 1.228307 0.9409677 0.9560451
## 2 39 1.228307 0.9409677 0.9560451
## 2 40 1.228307 0.9409677 0.9560451
## 3 2 4.378071 0.2432829 3.6485378
## 3 3 3.560340 0.4982366 2.8866138
## 3 4 2.581736 0.7354391 2.0995607
## 3 5 2.241136 0.8040712 1.7812318
## 3 6 2.246827 0.8002981 1.7277032
## 3 7 1.758737 0.8737680 1.3758509
## 3 8 1.721752 0.8811214 1.3373373
## 3 9 1.477229 0.9150701 1.1914015
## 3 10 1.419305 0.9204392 1.1164024
## 3 11 1.351785 0.9294273 1.0519997
## 3 12 1.266124 0.9380586 1.0050513
## 3 13 1.250542 0.9399158 0.9899847
## 3 14 1.221082 0.9422206 0.9634470
## 3 15 1.218158 0.9420320 0.9497746
## 3 16 1.216933 0.9421312 0.9529263
## 3 17 1.216933 0.9421312 0.9529263
## 3 18 1.229465 0.9413206 0.9643659
## 3 19 1.229465 0.9413206 0.9643659
## 3 20 1.229465 0.9413206 0.9643659
## 3 21 1.229465 0.9413206 0.9643659
## 3 22 1.229465 0.9413206 0.9643659
## 3 23 1.229465 0.9413206 0.9643659
## 3 24 1.229465 0.9413206 0.9643659
## 3 25 1.229465 0.9413206 0.9643659
## 3 26 1.229465 0.9413206 0.9643659
## 3 27 1.229465 0.9413206 0.9643659
## 3 28 1.229465 0.9413206 0.9643659
## 3 29 1.229465 0.9413206 0.9643659
## 3 30 1.229465 0.9413206 0.9643659
## 3 31 1.229465 0.9413206 0.9643659
## 3 32 1.229465 0.9413206 0.9643659
## 3 33 1.229465 0.9413206 0.9643659
## 3 34 1.229465 0.9413206 0.9643659
## 3 35 1.229465 0.9413206 0.9643659
## 3 36 1.229465 0.9413206 0.9643659
## 3 37 1.229465 0.9413206 0.9643659
## 3 38 1.229465 0.9413206 0.9643659
## 3 39 1.229465 0.9413206 0.9643659
## 3 40 1.229465 0.9413206 0.9643659
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 16 and degree = 3.
mars_predict = predict(mars_model, testData$x)
mars_values = data.frame(obs = testData$y, pred = mars_predict) |>
rename("pred" = y)
defaultSummary(mars_values)
## RMSE Rsquared MAE
## 1.1492504 0.9471145 0.9158382
The best MARS model based on the lowest RMSE value above has 3 degrees of interaction with 16 terms in the pruned model. This resulted in an RMSE value of 1.15 and R^2 value of 0.947.
summary(mars_model)
## Call: earth(x=data.frame[200,10], y=c(18.46,16.1,17...), keepxy=TRUE, degree=3,
## nprune=16)
##
## coefficients
## (Intercept) 20.378441
## h(0.621722-X1) -15.512132
## h(X1-0.621722) 9.177132
## h(0.601063-X2) -17.940676
## h(X2-0.601063) 10.064356
## h(X3-0.281766) 11.590022
## h(0.447442-X3) 14.641640
## h(X3-0.447442) -12.924806
## h(X3-0.606015) 13.416764
## h(0.734892-X4) -10.074386
## h(X4-0.734892) 9.687149
## h(0.850094-X5) -5.385762
## h(0.218266-X1) * h(X2-0.601063) -55.372637
## h(X1-0.218266) * h(X2-0.601063) -27.542831
## h(X1-0.621722) * h(X2-0.295997) -26.527403
## h(0.649253-X1) * h(0.601063-X2) 26.129827
##
## Selected 16 of 18 terms, and 5 of 10 predictors (nprune=16)
## Termination condition: Reached nk 21
## Importance: X1, X4, X2, X5, X3, X6-unused, X7-unused, X8-unused, X9-unused, ...
## Number of terms at each degree of interaction: 1 11 4
## GCV 1.61518 RSS 210.6377 GRSq 0.934423 RSq 0.9568093
Based on the summary above, the MARS model did select the informative predictors (X1-X5), and none of the other predictors.
set.seed(4802)
svm_model = train(trainingData$x, trainingData$y, method = "svmRadial",
preProcess = c("center", "scale"), tuneLength = 14,
trControl = trainControl(method = "cv"))
svm_model
## Support Vector Machines with Radial Basis Function Kernel
##
## 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:
##
## C RMSE Rsquared MAE
## 0.25 2.509584 0.8034803 2.009365
## 0.50 2.252212 0.8169420 1.784809
## 1.00 2.092437 0.8355583 1.651000
## 2.00 1.997135 0.8499485 1.561288
## 4.00 1.911369 0.8596792 1.495321
## 8.00 1.873902 0.8642840 1.474052
## 16.00 1.878175 0.8641293 1.481154
## 32.00 1.878175 0.8641293 1.481154
## 64.00 1.878175 0.8641293 1.481154
## 128.00 1.878175 0.8641293 1.481154
## 256.00 1.878175 0.8641293 1.481154
## 512.00 1.878175 0.8641293 1.481154
## 1024.00 1.878175 0.8641293 1.481154
## 2048.00 1.878175 0.8641293 1.481154
##
## Tuning parameter 'sigma' was held constant at a value of 0.06237028
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.06237028 and C = 8.
svm_predict = predict(svm_model, testData$x)
svm_values = data.frame(obs = testData$y, pred = svm_predict)
defaultSummary(svm_values)
## RMSE Rsquared MAE
## 2.0515724 0.8294434 1.5565018
The best SVM model based on the lowest RMSE value above has a cost value of 2^8 (256). This resulted in an RMSE value of 2.05 and R^2 value of 0.829.
From the KNN model generated at the beginning of this exercise, the best KNN model had a k-parameter of 21. This resulted in an RMSE value of 3.26 and R^2 value of 0.689.
Based on the models above, the best model was the MARS model.
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)
data(ChemicalManufacturingProcess)
updated_chem = impute.knn(as.matrix(ChemicalManufacturingProcess))$data
updated_chem = as.data.frame(updated_chem)
anyNA(updated_chem)
## [1] FALSE
set.seed(80233)
train_chem_rows = createDataPartition(updated_chem$Yield, p = 0.75, list = FALSE)
train_chem = updated_chem[train_chem_rows,]
test_chem = updated_chem[-train_chem_rows,]
Before generating a neural network model, I first wanted to check if any predictor variables are highly correlated. The code below shows that the columns with correlations between predictor variables that are greater than 0.75, and then removes the highly correlated predictors.
Note: The 1 is added to the
high_corr variable when removing
the columns because the first column of the dataframes is
Yield, which is what we are trying to
predict.
high_corr = findCorrelation(cor(train_chem |> select(-Yield)), cutoff = 0.75)
high_corr
## [1] 2 4 6 1 8 11 31 27 30 44 21 42 26 41 25 54 56 38 37 43 52
new_train_chem = train_chem[, -(high_corr + 1)]
new_test_chem = test_chem[, -(high_corr + 1)]
nnet_grid = expand.grid(.decay = c(0, 0.01, 0.1),
.size = c(1:10),
.bag = FALSE)
set.seed(7341)
nnet_chem_model = train(Yield ~ ., data = new_train_chem, method = "avNNet",
tuneGrid = nnet_grid, preProcess = c("center","scale"),
linout = TRUE, trace = FALSE,
trControl = trainControl(method = "cv", number = 10),
MaxNWts = 10*(ncol(new_train_chem |> select(-Yield)) + 1) + 10 + 1,
maxit = 500)
nnet_chem_model
## Model Averaged Neural Network
##
## 132 samples
## 36 predictor
##
## Pre-processing: centered (36), scaled (36)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 120, 119, 119, 119, 119, 118, ...
## Resampling results across tuning parameters:
##
## decay size RMSE Rsquared MAE
## 0.00 1 2.789001 0.3406165 1.682686
## 0.00 2 1.437670 0.3900053 1.189159
## 0.00 3 1.748027 0.3341313 1.411094
## 0.00 4 1.750926 0.3588192 1.432224
## 0.00 5 1.766131 0.3830993 1.443572
## 0.00 6 1.782182 0.4168638 1.450813
## 0.00 7 1.867687 0.3658449 1.490570
## 0.00 8 2.217880 0.3161961 1.741854
## 0.00 9 2.893567 0.2928286 2.255287
## 0.00 10 3.406317 0.2799701 2.347300
## 0.01 1 1.419069 0.3970393 1.107671
## 0.01 2 1.384273 0.4931996 1.134108
## 0.01 3 1.568363 0.4084709 1.212835
## 0.01 4 1.885522 0.3616141 1.384238
## 0.01 5 1.639875 0.4389082 1.266816
## 0.01 6 1.548289 0.4486153 1.225754
## 0.01 7 1.658637 0.4257375 1.363792
## 0.01 8 1.633905 0.3542580 1.290075
## 0.01 9 1.604135 0.4067232 1.252074
## 0.01 10 1.526675 0.4114073 1.211381
## 0.10 1 1.425173 0.4165358 1.150672
## 0.10 2 1.383713 0.4948078 1.118391
## 0.10 3 1.514595 0.4423062 1.179080
## 0.10 4 1.971311 0.3461017 1.394111
## 0.10 5 1.711822 0.4222923 1.209717
## 0.10 6 1.754786 0.3698971 1.287918
## 0.10 7 1.573851 0.4195891 1.184246
## 0.10 8 1.706734 0.3709666 1.288970
## 0.10 9 1.747591 0.3428946 1.340058
## 0.10 10 1.569224 0.4293641 1.232898
##
## 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.1 and bag = FALSE.
nnet_chem_predict = predict(nnet_chem_model, new_test_chem |> select(-Yield))
nnet_chem_values = data.frame(obs = new_test_chem$Yield, pred = nnet_chem_predict)
defaultSummary(nnet_chem_values)
## RMSE Rsquared MAE
## 3.0305968 0.1023992 1.7303701
The best neural network with model averaging based on the lowest RMSE value above has 2 hidden units and a weight decay of 0.1. This resulted in an RMSE value of 3.03 and R^2 value of 0.102.
mars_grid = expand.grid(.degree = 1:3,
.nprune = 2:40)
set.seed(7341)
mars_chem_model = train(Yield ~ ., data = train_chem, method = "earth",
tuneGrid = mars_grid, trControl = trainControl(method = "cv"))
mars_chem_model
## Multivariate Adaptive Regression Spline
##
## 132 samples
## 57 predictor
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 120, 119, 119, 119, 119, 118, ...
## Resampling results across tuning parameters:
##
## degree nprune RMSE Rsquared MAE
## 1 2 1.308908 0.5025920 1.0558107
## 1 3 1.114945 0.6286728 0.9377205
## 1 4 1.132005 0.6219354 0.9447226
## 1 5 1.128411 0.6254150 0.9196658
## 1 6 1.141344 0.6174333 0.9350703
## 1 7 1.147370 0.6056328 0.9203240
## 1 8 1.206308 0.5651638 0.9804601
## 1 9 1.257526 0.5399667 1.0169854
## 1 10 1.261922 0.5319517 1.0214730
## 1 11 1.302288 0.5106897 1.0419028
## 1 12 1.297785 0.5264619 1.0349569
## 1 13 1.328159 0.5032770 1.0634157
## 1 14 1.332052 0.5052184 1.0658100
## 1 15 1.334002 0.5110139 1.0704707
## 1 16 1.325710 0.5158499 1.0626977
## 1 17 1.325710 0.5158499 1.0626977
## 1 18 1.290290 0.5325944 1.0378696
## 1 19 1.290290 0.5325944 1.0378696
## 1 20 1.285592 0.5333894 1.0324051
## 1 21 1.278865 0.5366411 1.0346187
## 1 22 1.278865 0.5366411 1.0346187
## 1 23 1.278865 0.5366411 1.0346187
## 1 24 1.278865 0.5366411 1.0346187
## 1 25 1.275613 0.5336659 1.0330750
## 1 26 1.306998 0.5202260 1.0432246
## 1 27 1.306998 0.5202260 1.0432246
## 1 28 1.306998 0.5202260 1.0432246
## 1 29 1.306998 0.5202260 1.0432246
## 1 30 1.306998 0.5202260 1.0432246
## 1 31 1.306998 0.5202260 1.0432246
## 1 32 1.306998 0.5202260 1.0432246
## 1 33 1.306998 0.5202260 1.0432246
## 1 34 1.306998 0.5202260 1.0432246
## 1 35 1.306998 0.5202260 1.0432246
## 1 36 1.306998 0.5202260 1.0432246
## 1 37 1.306998 0.5202260 1.0432246
## 1 38 1.306998 0.5202260 1.0432246
## 1 39 1.306998 0.5202260 1.0432246
## 1 40 1.306998 0.5202260 1.0432246
## 2 2 1.308908 0.5025920 1.0558107
## 2 3 1.141430 0.6228158 0.9352774
## 2 4 1.158901 0.6092168 0.9413353
## 2 5 1.166365 0.6048940 0.9442381
## 2 6 1.254262 0.5775388 1.0366038
## 2 7 1.272529 0.5852508 1.0267623
## 2 8 1.369435 0.5213127 1.0677705
## 2 9 1.329811 0.5467445 1.0531649
## 2 10 1.407809 0.5160623 1.1137728
## 2 11 1.415153 0.5042369 1.1114486
## 2 12 1.381144 0.5292926 1.0638331
## 2 13 1.618400 0.4763505 1.2373757
## 2 14 1.614466 0.4796532 1.2405233
## 2 15 1.671921 0.4546023 1.2806698
## 2 16 1.667226 0.4537171 1.2871553
## 2 17 1.698766 0.4422984 1.2958260
## 2 18 1.759413 0.4245390 1.3242830
## 2 19 1.795965 0.3922299 1.3593908
## 2 20 1.805761 0.3903833 1.3366156
## 2 21 1.739189 0.4328792 1.2913916
## 2 22 1.738096 0.4320929 1.2910665
## 2 23 1.760080 0.4107453 1.3082705
## 2 24 1.797545 0.3888238 1.3392703
## 2 25 1.793138 0.3909365 1.3265911
## 2 26 1.794319 0.3920696 1.3238340
## 2 27 1.807901 0.3925680 1.3258609
## 2 28 1.801922 0.3967298 1.3184236
## 2 29 1.794063 0.4032599 1.3056947
## 2 30 1.807510 0.3971118 1.3132394
## 2 31 1.807510 0.3971118 1.3132394
## 2 32 1.807510 0.3971118 1.3132394
## 2 33 1.807510 0.3971118 1.3132394
## 2 34 1.807510 0.3971118 1.3132394
## 2 35 1.807510 0.3971118 1.3132394
## 2 36 1.807510 0.3971118 1.3132394
## 2 37 1.807510 0.3971118 1.3132394
## 2 38 1.807510 0.3971118 1.3132394
## 2 39 1.807510 0.3971118 1.3132394
## 2 40 1.807510 0.3971118 1.3132394
## 3 2 1.308908 0.5025920 1.0558107
## 3 3 1.147472 0.6078198 0.9681672
## 3 4 1.140776 0.6200029 0.9542860
## 3 5 1.148137 0.6192478 0.9764628
## 3 6 1.235060 0.5832080 1.0217280
## 3 7 1.349351 0.5426843 1.0574805
## 3 8 1.413449 0.5246191 1.0907479
## 3 9 1.393721 0.5395723 1.0786387
## 3 10 1.425779 0.5274561 1.0926279
## 3 11 1.413161 0.5285638 1.0714164
## 3 12 1.440391 0.5075853 1.0956542
## 3 13 1.418397 0.5196066 1.0810493
## 3 14 1.470137 0.5008254 1.1180857
## 3 15 1.508231 0.4741307 1.1537832
## 3 16 1.517524 0.4698243 1.1622295
## 3 17 1.525953 0.4673685 1.1726388
## 3 18 1.565516 0.4645312 1.2048599
## 3 19 1.567273 0.4668983 1.2010816
## 3 20 1.589230 0.4605185 1.2097935
## 3 21 1.639710 0.4474978 1.2270827
## 3 22 1.618346 0.4514396 1.2181723
## 3 23 1.634106 0.4483228 1.2108597
## 3 24 1.634485 0.4480206 1.2157146
## 3 25 1.634485 0.4480206 1.2157146
## 3 26 1.634485 0.4480206 1.2157146
## 3 27 1.634485 0.4480206 1.2157146
## 3 28 1.634485 0.4480206 1.2157146
## 3 29 1.606453 0.4540901 1.1998264
## 3 30 1.606453 0.4540901 1.1998264
## 3 31 1.598189 0.4572474 1.1967040
## 3 32 1.598189 0.4572474 1.1967040
## 3 33 1.598189 0.4572474 1.1967040
## 3 34 1.598189 0.4572474 1.1967040
## 3 35 1.598189 0.4572474 1.1967040
## 3 36 1.598189 0.4572474 1.1967040
## 3 37 1.598189 0.4572474 1.1967040
## 3 38 1.598189 0.4572474 1.1967040
## 3 39 1.598189 0.4572474 1.1967040
## 3 40 1.598189 0.4572474 1.1967040
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 3 and degree = 1.
mars_chem_predict = predict(mars_chem_model, test_chem |> select(-Yield))
mars_chem_values = data.frame(obs = test_chem$Yield, pred = mars_chem_predict) |>
rename("pred" = y)
defaultSummary(mars_chem_values)
## RMSE Rsquared MAE
## 1.4363813 0.5233354 1.1656670
The best MARS model based on the lowest RMSE value above has 1 degree of interaction with 3 terms in the pruned model. This resulted in an RMSE value of 1.4 3and R^2 value of 0.523.
set.seed(7341)
svm_chem_model = train(Yield ~ ., data = train_chem, method = "svmRadial",
preProcess = c("center", "scale"), tuneLength = 14,
trControl = trainControl(method = "cv"))
svm_chem_model
## Support Vector Machines with Radial Basis Function Kernel
##
## 132 samples
## 57 predictor
##
## Pre-processing: centered (57), scaled (57)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 120, 119, 119, 119, 119, 118, ...
## Resampling results across tuning parameters:
##
## C RMSE Rsquared MAE
## 0.25 1.355990 0.5386967 1.1020073
## 0.50 1.223640 0.5881044 0.9791508
## 1.00 1.140322 0.6153200 0.8988857
## 2.00 1.077530 0.6397915 0.8455856
## 4.00 1.059133 0.6502812 0.8446928
## 8.00 1.059289 0.6518677 0.8532552
## 16.00 1.059289 0.6518677 0.8532552
## 32.00 1.059289 0.6518677 0.8532552
## 64.00 1.059289 0.6518677 0.8532552
## 128.00 1.059289 0.6518677 0.8532552
## 256.00 1.059289 0.6518677 0.8532552
## 512.00 1.059289 0.6518677 0.8532552
## 1024.00 1.059289 0.6518677 0.8532552
## 2048.00 1.059289 0.6518677 0.8532552
##
## Tuning parameter 'sigma' was held constant at a value of 0.01551543
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01551543 and C = 4.
svm_chem_predict = predict(svm_chem_model, test_chem |> select(-Yield))
svm_chem_values = data.frame(obs = test_chem$Yield, pred = svm_chem_predict)
defaultSummary(svm_chem_values)
## RMSE Rsquared MAE
## 1.3502107 0.5766327 1.0397160
The best SVM model based on the lowest RMSE value above has a cost value of 2^4 (16). This resulted in an RMSE value of 1.35 and R^2 value of 0.577.
set.seed(7341)
knn_chem_model = train(Yield ~ ., data = train_chem,
method = "knn", preProc = c("center", "scale"),
tuneLength = 10)
knn_chem_model
## k-Nearest Neighbors
##
## 132 samples
## 57 predictor
##
## Pre-processing: centered (57), scaled (57)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 132, 132, 132, 132, 132, 132, ...
## Resampling results across tuning parameters:
##
## k RMSE Rsquared MAE
## 5 1.398218 0.3727447 1.121508
## 7 1.380988 0.3803805 1.107876
## 9 1.381955 0.3773987 1.117934
## 11 1.380847 0.3812828 1.123590
## 13 1.389371 0.3748872 1.135566
## 15 1.396253 0.3715175 1.142379
## 17 1.403026 0.3672992 1.148086
## 19 1.412183 0.3630494 1.157282
## 21 1.423560 0.3539878 1.166732
## 23 1.430868 0.3499537 1.173288
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 11.
knn_chem_predict = predict(knn_chem_model, test_chem |> select(-Yield))
knn_chem_values = data.frame(obs = test_chem$Yield, pred = knn_chem_predict)
defaultSummary(knn_chem_values)
## RMSE Rsquared MAE
## 1.4553175 0.5575128 1.1313430
The best KNN model had a k-parameter of 11. This resulted in an RMSE value of 1.46 and R^2 value of 0.558.
Based on the analysis above, the SVM model resulted in the best test set performance.
plot(varImp(svm_chem_model), top = 10,
main = "Top 10 Important Predictors in SVM Model")
Using the SVM model from the previous exercise, 7 of the top 10 most important predictors are process variables, with Manufacturing Process 32 and 13 being the top 2 predictors. The top biological variable is Biological Material 03, which is the third most important variable overall.
Below is the importance chart generated for the Ridge Regression model used in the previous assignment. When comparing the outputs, both Manufacturing Process 32 and 13 are the top 2 predictors overall. Additionally, the 3 biological variables in the top 10 for the SVM model are all also in the top 10 for the Ridge Regression model. Overall, the linear and non-linear models share 8 of the top 10 predictors from an importance standpoint, albeit in a different order. The 2 unique to the non-linear model are Manufacturing Process 06 and 31.
chem_var_imp = varImp(svm_chem_model)$importance |>
arrange(desc(Overall))
chem_var_imp = chem_var_imp |>
mutate(variable = rownames(chem_var_imp)) |>
head(10)
top_chem_var = chem_var_imp$variable
chem_long = updated_chem |> select(top_chem_var, Yield) |>
pivot_longer(cols = -Yield, names_to = "predictor", values_to = "value")
chem_long |>
ggplot(aes(x = value, y = Yield)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", color = "red") +
facet_wrap(~predictor, scales = "free_x", nrow = 3) +
theme(aspect.ratio = 1)
The plots above show the relationship between the top 10 predictors for the optimal non-linear model and Yield. Looking at the 2 predictors that are unique to the 10 most important variables for the non-linear model, Manufacturing Process 06 appears to have a strong positive relationship with yield, suggesting this variable should be increased to increase the overall yield. Additionally, manufacturing Process 31 appears to have a negative relationship with yield. However, when looking at Manufacturing Process 31 specifically, there appears to be a major outlier at a value of 0. As a result, the below analysis looks at the relationship of Manufacturing Process 31 after removing this outlier.
chem_long |>
filter(!(predictor == "ManufacturingProcess31" & value == 0)) |>
ggplot(aes(x = value, y = Yield)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", color = "red") +
facet_wrap(~predictor, scales = "free_x", nrow = 3) +
theme(aspect.ratio = 1)
After removing the outlier, it appears that Manufacturing Process 31 has a strong negative relationship with yield, suggest that this variable should be decreased in order to increase yield.