# Load libraries
library(caret)
library(mlbench)
library(earth)
library(kernlab)
library(nnet)
library(AppliedPredictiveModeling)

Exercise 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 = 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.

Tune several models on these data.Which models appear to give the best performance? Does MARS select the informative predictors (those named X1–X5)?

Based on lowest RSME, highest Rsquared amd lowest MAE, the MARS model performed best to explain the variance in the non linear data. MARS was able to identify/select the most informative predictors X1 - X5 and their degrees of importance relative to X1.


Model Performance Metrics
Model RMSE R-squared MAE
KNN 3.2040595 0.6819919 2.5683461
MARS 1.1589948 0.9460418 0.9250230
SVM 2.0067929 0.8366234 1.5104528
NNET 2.3663646 0.7831041 1.7857183


KNN
set.seed(200)

# Create training data
trainingData <- mlbench.friedman1(200, sd = 1)

# 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
featurePlot(trainingData$x, trainingData$y)

# Create test data
# 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)

# 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.
# 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
# Set seed
set.seed(200)

# Generate training/test  data
trainingData <- mlbench.friedman1(200, sd = 1)
trainingData$x <- data.frame(trainingData$x)
testData <- mlbench.friedman1(5000, sd = 1)
testData$x <- data.frame(testData$x)

# MARS tuning grid for degrees and terms
marsGrid <- expand.grid(.degree = 1:2, 
                      .nprune = 2:38)

# Train MARS model with CV
marsTuned <- train(
  x = trainingData$x,
  y = trainingData$y,
  method = "earth",
  tuneGrid = marsGrid,
  trControl = trainControl(method = "cv"))

# View model results
head(marsTuned$results)
##    degree nprune     RMSE  Rsquared      MAE    RMSESD RsquaredSD     MAESD
## 1       1      2 4.418191 0.2220731 3.693914 0.5810075 0.13108125 0.5115431
## 38      2      2 4.418191 0.2220731 3.693914 0.5810075 0.13108125 0.5115431
## 2       1      3 3.587804 0.4951608 2.883269 0.4425236 0.11284760 0.4103648
## 39      2      3 3.587804 0.4951608 2.883269 0.4425236 0.11284760 0.4103648
## 3       1      4 2.649942 0.7213442 2.111172 0.4508144 0.07395655 0.3700952
## 40      2      4 2.649942 0.7213442 2.111172 0.4508144 0.07395655 0.3700952
marsTuned$bestTune
##    nprune degree
## 51     15      2
marsTuned
## 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.418191  0.2220731  3.6939136
##   1        3      3.587804  0.4951608  2.8832687
##   1        4      2.649942  0.7213442  2.1111721
##   1        5      2.291556  0.7964068  1.8323488
##   1        6      2.280375  0.8004019  1.7664797
##   1        7      1.784776  0.8754863  1.3753836
##   1        8      1.682249  0.8891793  1.2994890
##   1        9      1.646664  0.8954811  1.2539206
##   1       10      1.624818  0.8987479  1.2715170
##   1       11      1.610740  0.9019619  1.2576772
##   1       12      1.608672  0.8997193  1.2572225
##   1       13      1.634826  0.8969655  1.2749330
##   1       14      1.637957  0.8967907  1.2768206
##   1       15      1.637957  0.8967907  1.2768206
##   1       16      1.637957  0.8967907  1.2768206
##   1       17      1.637957  0.8967907  1.2768206
##   1       18      1.637957  0.8967907  1.2768206
##   1       19      1.637957  0.8967907  1.2768206
##   1       20      1.637957  0.8967907  1.2768206
##   1       21      1.637957  0.8967907  1.2768206
##   1       22      1.637957  0.8967907  1.2768206
##   1       23      1.637957  0.8967907  1.2768206
##   1       24      1.637957  0.8967907  1.2768206
##   1       25      1.637957  0.8967907  1.2768206
##   1       26      1.637957  0.8967907  1.2768206
##   1       27      1.637957  0.8967907  1.2768206
##   1       28      1.637957  0.8967907  1.2768206
##   1       29      1.637957  0.8967907  1.2768206
##   1       30      1.637957  0.8967907  1.2768206
##   1       31      1.637957  0.8967907  1.2768206
##   1       32      1.637957  0.8967907  1.2768206
##   1       33      1.637957  0.8967907  1.2768206
##   1       34      1.637957  0.8967907  1.2768206
##   1       35      1.637957  0.8967907  1.2768206
##   1       36      1.637957  0.8967907  1.2768206
##   1       37      1.637957  0.8967907  1.2768206
##   1       38      1.637957  0.8967907  1.2768206
##   2        2      4.418191  0.2220731  3.6939136
##   2        3      3.587804  0.4951608  2.8832687
##   2        4      2.649942  0.7213442  2.1111721
##   2        5      2.291556  0.7964068  1.8323488
##   2        6      2.280375  0.8004019  1.7664797
##   2        7      1.803484  0.8735581  1.3868197
##   2        8      1.654427  0.8922574  1.2862049
##   2        9      1.546581  0.9071585  1.2029829
##   2       10      1.521494  0.9109646  1.1652663
##   2       11      1.422808  0.9228462  1.1019263
##   2       12      1.355197  0.9296603  1.0571268
##   2       13      1.244245  0.9405589  0.9808320
##   2       14      1.262326  0.9389157  0.9857827
##   2       15      1.239616  0.9402979  0.9733206
##   2       16      1.255372  0.9388680  0.9884378
##   2       17      1.263593  0.9378911  0.9924776
##   2       18      1.263593  0.9378911  0.9924776
##   2       19      1.263593  0.9378911  0.9924776
##   2       20      1.263593  0.9378911  0.9924776
##   2       21      1.263593  0.9378911  0.9924776
##   2       22      1.263593  0.9378911  0.9924776
##   2       23      1.263593  0.9378911  0.9924776
##   2       24      1.263593  0.9378911  0.9924776
##   2       25      1.263593  0.9378911  0.9924776
##   2       26      1.263593  0.9378911  0.9924776
##   2       27      1.263593  0.9378911  0.9924776
##   2       28      1.263593  0.9378911  0.9924776
##   2       29      1.263593  0.9378911  0.9924776
##   2       30      1.263593  0.9378911  0.9924776
##   2       31      1.263593  0.9378911  0.9924776
##   2       32      1.263593  0.9378911  0.9924776
##   2       33      1.263593  0.9378911  0.9924776
##   2       34      1.263593  0.9378911  0.9924776
##   2       35      1.263593  0.9378911  0.9924776
##   2       36      1.263593  0.9378911  0.9924776
##   2       37      1.263593  0.9378911  0.9924776
##   2       38      1.263593  0.9378911  0.9924776
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 15 and degree = 2.
# Plot Tuning Results
plot(marsTuned, main = "MARS Model Tuning Results") 

# Predict test set
marsPred <- predict(marsTuned, newdata = testData$x)

# Test set performance metrics
postResample(pred = marsPred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 1.1589948 0.9460418 0.9250230
# Variable importance
varImp_mars <- varImp(marsTuned)

# Plot variable importance
plot(varImp_mars, main = "Variable Importance for MARS Model")

# MARS Model Diagnostics
plot(marsTuned$finalModel, main = "MARS Final Model Diagnostics") 


SVM
# Set seed
set.seed(200)

# Generate training/test data
trainingData <- mlbench.friedman1(200, sd = 1)
trainingData$x <- data.frame(trainingData$x)
testData <- mlbench.friedman1(5000, sd = 1)
testData$x <- data.frame(testData$x)

# SVM tuning grid
svmGrid <- expand.grid(
 sigma = kernlab::sigest(as.matrix(trainingData$x))[1],
 C = 2^seq(-2, 11, length = 14))

# Train SVM model with CV
svmTuned <- train(
  x = trainingData$x,
  y = trainingData$y,
  method = "svmRadial",
  tuneGrid = svmGrid,
  preProc = c("center", "scale"),
  trControl = trainControl(method = "cv"),
  metric = "RMSE")

# Model results
head(svmTuned$results)
##        sigma    C     RMSE  Rsquared      MAE    RMSESD RsquaredSD     MAESD
## 1 0.03413237 0.25 2.571925 0.7743376 2.071077 0.2400015 0.08920597 0.2627740
## 2 0.03413237 0.50 2.330949 0.7891793 1.860996 0.3092339 0.08345690 0.2851714
## 3 0.03413237 1.00 2.190281 0.8041334 1.740172 0.3448926 0.07427752 0.3022471
## 4 0.03413237 2.00 2.033562 0.8267485 1.604770 0.3201126 0.06069353 0.3073858
## 5 0.03413237 4.00 1.921808 0.8440954 1.516867 0.2992432 0.05308139 0.3068404
## 6 0.03413237 8.00 1.830444 0.8581127 1.445103 0.2865026 0.05416399 0.2843860
svmTuned$bestTune
##        sigma C
## 6 0.03413237 8
# Tuning plot
plot(svmTuned, main = "SVM Model Tuning Results") 

# Test set predictions & metrics
svmPred <- predict(svmTuned, newdata = testData$x)
postResample(pred = svmPred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 2.0067929 0.8366234 1.5104528
# Variable importance
svmVarImp <- varImp(svmTuned)

# Variable importance
plot(svmVarImp, main = "Variable Importance for SVM Model")

# SVM Model details
print(svmTuned$finalModel)
## Support Vector Machine object of class "ksvm" 
## 
## SV type: eps-svr  (regression) 
##  parameter : epsilon = 0.1  cost C = 8 
## 
## Gaussian Radial Basis kernel function. 
##  Hyperparameter : sigma =  0.0341323677577858 
## 
## Number of Support Vectors : 147 
## 
## Objective Function Value : -155.5642 
## Training error : 0.02686
nSV(svmTuned$finalModel)
## [1] 147
kernelf(svmTuned$finalModel)
## Gaussian Radial Basis kernel function. 
##  Hyperparameter : sigma =  0.0341323677577858


NeuralNet
# Set seed
set.seed(200)

# Generate training/test data
trainingData <- mlbench.friedman1(200, sd = 1)
trainingData$x <- data.frame(trainingData$x)
testData <- mlbench.friedman1(5000, sd = 1)
testData$x <- data.frame(testData$x)

# Neural net tuning grid 
nnetGrid <- expand.grid(
  size = 1:10,
  decay = c(0, 0.01, 0.1))

# Train neural net with CV
nnetTuned <- train(
  x = trainingData$x,
  y = trainingData$y,
  method = "nnet", 
  tuneGrid = nnetGrid,
  preProc = c("center", "scale"),
  trControl = trainControl(method = "cv"),
  linout = TRUE,
  trace = FALSE,
  maxit = 500,
  MaxNWts = 10 * (ncol(trainingData$x) + 1) + 10 + 1)

# Model results
head(nnetTuned$results)
##   size decay     RMSE  Rsquared      MAE    RMSESD RsquaredSD     MAESD
## 1    1  0.00 2.437191 0.7652028 1.911389 0.3778913 0.05749024 0.3244792
## 2    1  0.01 2.432988 0.7659100 1.905811 0.3816866 0.05623267 0.3274606
## 3    1  0.10 2.440477 0.7640614 1.909477 0.3993041 0.05547645 0.3385159
## 4    2  0.00 2.615029 0.7370369 2.113315 0.4106364 0.06701454 0.4449996
## 5    2  0.01 2.558425 0.7398896 2.055602 0.4925137 0.08833787 0.3978898
## 6    2  0.10 2.616198 0.7315751 2.102083 0.2575369 0.05807522 0.2347142
nnetTuned$bestTune
##   size decay
## 7    3     0
# Tuning plot
plot(nnetTuned, main = "Neural Network Model Tuning Results")

# Test set predictions
nnetPred <- predict(nnetTuned, newdata = testData$x)
postResample(pred = nnetPred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 2.3663646 0.7831041 1.7857183
# Variable importance
nnetVarImp <- varImp(nnetTuned)

# Variable importance plot
plot(nnetVarImp, main = "Variable Importance for Neural Network Model")



Exercise 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.


Load Data
data("ChemicalManufacturingProcess")

# Separate predictors and outcome variables
predictors <- ChemicalManufacturingProcess[, !names(ChemicalManufacturingProcess) %in% "Yield"]
outcome <- ChemicalManufacturingProcess$Yield

# Create a KNN imputation
imputation_model <- preProcess(predictors, method = "knnImpute")

# Apply the imputation model to fill in missing values
predictors_imputed <- predict(imputation_model, newdata = predictors)

# Recombine the imputed predictor variables with the original outcome
data_imputed <- cbind(predictors_imputed, Yield = outcome)


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

Based on these performance metrics, MARS is the best performing model, with the lowest RMSE (1.13), highest R-squared (0.64), and lowest MAE (0.88). SVM is next with moderate performance as well. KNN and NNET show weaker performance, particularly KNN with the lowest R-squared of 0.42.

However, the MARS R-squared valued explaining roughly 64% of the variance only has moderate predictive power, I would not recommend it as a model.

Model Performance Metrics
Model RMSE R-squared MAE
KNN 1.4383785 0.4220841 1.2071970
MARS 1.1285523 0.6370613 0.8805678
SVM 1.2382642 0.5849938 0.9442940
NNET 1.4275480 0.5169966 1.0926083


KNN
# Set seed
set.seed(200)

# Split data into training/test sets (using 75% for training)
trainIndex <- createDataPartition(data_imputed$Yield, p = 0.75, list = FALSE)
trainData <- data_imputed[trainIndex, ]
testData <- data_imputed[-trainIndex, ]

# Separate predictors and outcome for training data
x_train <- trainData[, !names(trainData) %in% "Yield"]
y_train <- trainData$Yield

# Separate predictors and outcome for test data
x_test <- testData[, !names(testData) %in% "Yield"]
y_test <- testData$Yield

# Train KNN model with CV
knnModel <- train(
  x = x_train,
  y = y_train,
  method = "knn",
  preProc = c("center", "scale"),
  tuneLength = 10,
  trControl = trainControl(method = "cv"))

# Model results
head(knnModel$results)
##    k     RMSE  Rsquared      MAE    RMSESD RsquaredSD     MAESD
## 1  5 1.359972 0.4721301 1.059979 0.3049140  0.1524457 0.2180544
## 2  7 1.348804 0.4750832 1.079011 0.2993717  0.1690338 0.2336064
## 3  9 1.333911 0.4919537 1.066791 0.2839310  0.1728645 0.1978271
## 4 11 1.337299 0.5005712 1.093707 0.2833658  0.1750383 0.1960178
## 5 13 1.341294 0.4967197 1.096107 0.2992423  0.1751587 0.2004236
## 6 15 1.374989 0.4665072 1.131406 0.2955441  0.1848535 0.1996309
knnModel$bestTune
##   k
## 3 9
# Tuning plot
plot(knnModel)

# Test set predictions & metrics
knnPred <- predict(knnModel, newdata = x_test)
postResample(pred = knnPred, obs = y_test)
##      RMSE  Rsquared       MAE 
## 1.4383785 0.4220841 1.2071970
# Variable importance
varImp(knnModel)
## loess r-squared variable importance
## 
##   only 20 most important variables shown (out of 57)
## 
##                        Overall
## ManufacturingProcess32  100.00
## ManufacturingProcess17   80.31
## BiologicalMaterial06     75.09
## ManufacturingProcess13   74.47
## BiologicalMaterial03     67.11
## ManufacturingProcess06   66.44
## ManufacturingProcess36   66.30
## BiologicalMaterial12     65.94
## ManufacturingProcess09   65.29
## BiologicalMaterial02     55.94
## ManufacturingProcess31   53.57
## ManufacturingProcess29   47.17
## ManufacturingProcess33   45.79
## BiologicalMaterial08     44.98
## ManufacturingProcess11   41.72
## ManufacturingProcess02   41.58
## BiologicalMaterial11     41.18
## BiologicalMaterial04     40.31
## BiologicalMaterial09     35.12
## BiologicalMaterial01     34.15
plot(varImp(knnModel))


MARS
# Set seed
set.seed(200)

# Split data into training/test sets
trainIndex <- createDataPartition(data_imputed$Yield, p = 0.75, list = FALSE)
trainData <- data_imputed[trainIndex, ]
testData <- data_imputed[-trainIndex, ]

# Separate predictors and outcome for training data
x_train <- trainData[, !names(trainData) %in% "Yield"]
y_train <- trainData$Yield

# Separate predictors and outcome for test data 
x_test <- testData[, !names(testData) %in% "Yield"]
y_test <- testData$Yield

# MARS tuning grid
marsGrid <- expand.grid(
 .degree = 1:2,
 .nprune = 2:38)

# Train MARS model with CV
marsTuned <- train(
  x = x_train,
  y = y_train,
  method = "earth",
  tuneGrid = marsGrid,
  trControl = trainControl(method = "cv"))

# Model results
head(marsTuned$results)
##    degree nprune     RMSE  Rsquared       MAE    RMSESD RsquaredSD     MAESD
## 1       1      2 1.373863 0.4641852 1.0914312 0.3101635  0.1490830 0.2047572
## 38      2      2 1.371014 0.4674806 1.0894104 0.3117095  0.1502947 0.2045887
## 2       1      3 1.213194 0.5946863 0.9923436 0.3130848  0.1314158 0.2698188
## 39      2      3 1.151606 0.6229947 0.9534037 0.1997636  0.1070254 0.1976835
## 3       1      4 1.150495 0.6372393 0.9417116 0.2546411  0.1345659 0.2198082
## 40      2      4 1.307310 0.5448013 1.0289397 0.3326252  0.1694827 0.2465038
marsTuned$bestTune
##   nprune degree
## 3      4      1
# Tuning plot
plot(marsTuned)

# Test set predictions & metrics
marsPred <- predict(marsTuned, newdata = x_test)
postResample(pred = marsPred, obs = y_test)
##      RMSE  Rsquared       MAE 
## 1.1285523 0.6370613 0.8805678
# Variable importance
varImp(marsTuned)
## earth variable importance
## 
##                        Overall
## ManufacturingProcess32  100.00
## ManufacturingProcess09   46.54
## ManufacturingProcess13    0.00
plot(varImp(marsTuned))


SVM
# Set seed
set.seed(200)

# Split data into training/test sets
trainIndex <- createDataPartition(data_imputed$Yield, p = 0.75, list = FALSE)
trainData <- data_imputed[trainIndex, ]
testData <- data_imputed[-trainIndex, ]

# Separate predictors and outcome for training data
x_train <- trainData[, !names(trainData) %in% "Yield"]
y_train <- trainData$Yield

# Separate predictors and outcome for test data
x_test <- testData[, !names(testData) %in% "Yield"]
y_test <- testData$Yield

# SVM tuning grid
svmGrid <- expand.grid(
 sigma = kernlab::sigest(as.matrix(x_train))[1],
 C = 2^seq(-2, 11, length = 14))

# Train SVM model with CV
svmTuned <- train(
  x = x_train,
  y = y_train,
  method = "svmRadial",
  tuneGrid = svmGrid,
  preProc = c("center", "scale"),
  trControl = trainControl(method = "cv"),
  metric = "RMSE")

# Model results
head(svmTuned$results)
##         sigma    C     RMSE  Rsquared       MAE    RMSESD RsquaredSD     MAESD
## 1 0.005113201 0.25 1.418520 0.4930477 1.1657529 0.2605062 0.11517217 0.1865589
## 2 0.005113201 0.50 1.308933 0.5279864 1.0802562 0.2223870 0.09848656 0.1781604
## 3 0.005113201 1.00 1.243171 0.5520036 1.0154208 0.1737705 0.10351787 0.1428009
## 4 0.005113201 2.00 1.212312 0.5663325 0.9661878 0.1539465 0.12856239 0.1375778
## 5 0.005113201 4.00 1.167104 0.5958077 0.9242702 0.1646867 0.15385490 0.1596887
## 6 0.005113201 8.00 1.173760 0.5949558 0.9337971 0.2042906 0.17586608 0.1817306
svmTuned$bestTune
##         sigma C
## 5 0.005113201 4
# Tuning plot
plot(svmTuned)

# Test set predictions & metrics
svmPred <- predict(svmTuned, newdata = x_test)
postResample(pred = svmPred, obs = y_test)
##      RMSE  Rsquared       MAE 
## 1.2382642 0.5849938 0.9442940
# Variable importance and model details
varImp(svmTuned)
## loess r-squared variable importance
## 
##   only 20 most important variables shown (out of 57)
## 
##                        Overall
## ManufacturingProcess32  100.00
## ManufacturingProcess17   80.31
## BiologicalMaterial06     75.09
## ManufacturingProcess13   74.47
## BiologicalMaterial03     67.11
## ManufacturingProcess06   66.44
## ManufacturingProcess36   66.30
## BiologicalMaterial12     65.94
## ManufacturingProcess09   65.29
## BiologicalMaterial02     55.94
## ManufacturingProcess31   53.57
## ManufacturingProcess29   47.17
## ManufacturingProcess33   45.79
## BiologicalMaterial08     44.98
## ManufacturingProcess11   41.72
## ManufacturingProcess02   41.58
## BiologicalMaterial11     41.18
## BiologicalMaterial04     40.31
## BiologicalMaterial09     35.12
## BiologicalMaterial01     34.15
plot(varImp(svmTuned))

print(svmTuned$finalModel)
## Support Vector Machine object of class "ksvm" 
## 
## SV type: eps-svr  (regression) 
##  parameter : epsilon = 0.1  cost C = 4 
## 
## Gaussian Radial Basis kernel function. 
##  Hyperparameter : sigma =  0.00511320051817451 
## 
## Number of Support Vectors : 111 
## 
## Objective Function Value : -138.8385 
## Training error : 0.154714
nSV(svmTuned$finalModel)
## [1] 111
kernelf(svmTuned$finalModel)
## Gaussian Radial Basis kernel function. 
##  Hyperparameter : sigma =  0.00511320051817451


NeuralNet
# Split data into training/test sets
trainIndex <- createDataPartition(data_imputed$Yield, p = 0.75, list = FALSE)
trainData <- data_imputed[trainIndex, ]
testData <- data_imputed[-trainIndex, ]

# Separate predictors and outcome for training data
chemx_train <- trainData[, !names(trainData) %in% "Yield"]
chemy_train <- trainData$Yield

# Separate predictors and outcome for test data
x_test <- testData[, !names(testData) %in% "Yield"]
y_test <- testData$Yield

# Neural net tuning grid 
nnetGrid <- expand.grid(
 size = 1:10,
 decay = c(0, 0.001, 0.01, 0.1, 0.3, 0.5))

# Train neural net with CV
nnetTuned <- train(
  x = chemx_train,
  y = chemy_train,
  method = "nnet", 
  tuneGrid = nnetGrid,
  preProc = c("center", "scale", "nzv"),
  trControl = trainControl(method = "cv"),
  linout = TRUE,
  trace = FALSE,
  maxit = 500,
  MaxNWts = 10 * (ncol(x_train) + 1) + 10 + 1)

# Model results
head(nnetTuned$results)
##   size decay     RMSE  Rsquared      MAE    RMSESD RsquaredSD     MAESD
## 1    1 0.000 1.755169 0.1768797 1.455565 0.3146626  0.1631902 0.1918572
## 2    1 0.001 1.856906 0.2455660 1.499637 0.3691828  0.2032322 0.2770922
## 3    1 0.010 1.644305 0.4661764 1.293050 0.4449435  0.1776262 0.2982960
## 4    1 0.100 1.409446 0.4911999 1.148571 0.3571027  0.2047787 0.3208863
## 5    1 0.300 2.042982 0.3583754 1.435583 1.2455139  0.2235291 0.6888214
## 6    1 0.500 2.403655 0.3376808 1.602566 1.5849201  0.2295900 0.7899551
nnetTuned$bestTune
##   size decay
## 4    1   0.1
# Tuning plot
plot(nnetTuned)

# Test set predictions & metrics
nnetPred <- predict(nnetTuned, newdata = x_test)
postResample(pred = nnetPred, obs = y_test)
##      RMSE  Rsquared       MAE 
## 1.4548170 0.4936753 1.1268198
# Variable importance
varImp(nnetTuned)
## nnet variable importance
## 
##   only 20 most important variables shown (out of 56)
## 
##                        Overall
## ManufacturingProcess25 100.000
## ManufacturingProcess26  68.272
## ManufacturingProcess29  51.321
## ManufacturingProcess32  50.918
## ManufacturingProcess18  46.904
## ManufacturingProcess20  44.807
## ManufacturingProcess33  30.757
## BiologicalMaterial11    29.972
## ManufacturingProcess27  28.433
## BiologicalMaterial12    23.543
## ManufacturingProcess31  18.689
## ManufacturingProcess10  12.612
## BiologicalMaterial05    11.702
## BiologicalMaterial01    11.407
## ManufacturingProcess04  11.239
## ManufacturingProcess09  11.091
## ManufacturingProcess13  10.590
## ManufacturingProcess17  10.528
## ManufacturingProcess45  10.513
## BiologicalMaterial09     9.928
plot(varImp(nnetTuned))


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

The MARS model was the best performing model. It selected only 3 manufacturing processes as the most important variables. In comparison to the linear PLS and Lasso models, all models identified ManufacturingProcess32 as the most important predictor. The other MARS variables ManufacturingProcess09 and ManufacturingProcess13 also appeared in the linear models but were interspersed between other manufacturing and biological predictors. The MARS model did not identify any biological variables.


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 MARS model identified three key manufacturing process variables (09, 13, and 32) as the most important predictors of yield; none of the biological materials were selected as significant predictors. The model achieved a moderate fit with an R-squared of 0.658. The model is additive suggesting that each manufacturing process variable independently affects the yield where their relationships with yield change.

The plots reveal distinct nonlinear relationships between the three manufacturing processes and yield. ManufacturingProcess13 shows a positive relationship with yield until it plateaus. ManufacturingProcess09 shows a negative relationship with yield until reaching a threshold. ManufacturingProcess32 demonstrates a positive impact on yield after crossing a threshold.

# MARS model summary
print(summary(marsTuned$finalModel))
## Call: earth(x=data.frame[132,57], y=c(38,42.44,42.0...), keepxy=TRUE, degree=1,
##             nprune=4)
## 
##                                      coefficients
## (Intercept)                             39.718231
## h(0.995762-ManufacturingProcess09)      -0.809311
## h(-1.18977-ManufacturingProcess13)       2.230866
## h(ManufacturingProcess32- -0.827442)     1.240221
## 
## Selected 4 of 22 terms, and 3 of 57 predictors (nprune=4)
## Termination condition: RSq changed by less than 0.001 at 22 terms
## Importance: ManufacturingProcess32, ManufacturingProcess09, ...
## Number of terms at each degree of interaction: 1 3 (additive model)
## GCV 1.282191    RSS 151.7746    GRSq 0.6246959    RSq 0.6582876
# Plot MARS model significant predictors
plotmo(marsTuned$finalModel,  
       main="Manufacturing Process Effects",
       degree1.names=c("Manufacturing Process 13", 
                      "Manufacturing Process 09",
                      "Manufacturing Process 32"),
       xlab=c("Process 13 Value", "Process 09 Value", "Process 32 Value"),
       ylab="Response",
       caption="",
       col.response="darkgray",
       lwd=1.5)
##  plotmo grid:    BiologicalMaterial01 BiologicalMaterial02 BiologicalMaterial03
##                             -0.156068          -0.06661163          -0.08122839
##  BiologicalMaterial04 BiologicalMaterial05 BiologicalMaterial06
##            -0.1404558          -0.02095088          -0.07218167
##  BiologicalMaterial07 BiologicalMaterial08 BiologicalMaterial09
##            -0.1313107         0.0003357286          -0.03626614
##  BiologicalMaterial10 BiologicalMaterial11 BiologicalMaterial12
##            -0.2180196           -0.1811404         -0.006316853
##  ManufacturingProcess01 ManufacturingProcess02 ManufacturingProcess03
##               0.1056672              0.5214313              0.3319348
##  ManufacturingProcess04 ManufacturingProcess05 ManufacturingProcess06
##               0.3424324            -0.09641047             -0.2229103
##  ManufacturingProcess07 ManufacturingProcess08 ManufacturingProcess09
##              -0.9580199              0.8941637              0.1292558
##  ManufacturingProcess10 ManufacturingProcess11 ManufacturingProcess12
##             -0.09005212             0.02020002             -0.4806937
##  ManufacturingProcess13 ManufacturingProcess14 ManufacturingProcess15
##              0.09066017              0.1216248            -0.07580629
##  ManufacturingProcess16 ManufacturingProcess17 ManufacturingProcess18
##              0.08160103             -0.1151653             0.06617593
##  ManufacturingProcess19 ManufacturingProcess20 ManufacturingProcess21
##              -0.1360039             0.07317798             -0.1744786
##  ManufacturingProcess22 ManufacturingProcess23 ManufacturingProcess24
##              -0.1218132            -0.01031118             -0.3162877
##  ManufacturingProcess25 ManufacturingProcess26 ManufacturingProcess27
##              0.06379054             0.05464677             0.06777473
##  ManufacturingProcess28 ManufacturingProcess29 ManufacturingProcess30
##               0.7064583              -0.066778             0.03954225
##  ManufacturingProcess31 ManufacturingProcess32 ManufacturingProcess33
##               0.1107323            -0.08632349              0.1836771
##  ManufacturingProcess34 ManufacturingProcess35 ManufacturingProcess36
##               0.1182687             0.03729394             -0.4269245
##  ManufacturingProcess37 ManufacturingProcess38 ManufacturingProcess39
##             -0.03063781              0.7174727               0.231727
##  ManufacturingProcess40 ManufacturingProcess41 ManufacturingProcess42
##              -0.4626528             -0.4405878              0.2027957
##  ManufacturingProcess43 ManufacturingProcess44 ManufacturingProcess45
##              -0.1865604              0.2946725              0.1522024