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(πx_1x_2)+20(x3 −0.5)^2 +10x_4 +5x_5 +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:

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.426070  0.5384105  2.735971
##    7  3.358310  0.5554782  2.653725
##    9  3.286518  0.5821513  2.580048
##   11  3.255192  0.6032095  2.558699
##   13  3.259073  0.6133974  2.574079
##   15  3.274781  0.6194655  2.601801
##   17  3.304555  0.6213184  2.634957
##   19  3.325879  0.6282695  2.669575
##   21  3.342864  0.6314736  2.691948
##   23  3.362049  0.6378854  2.714370
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 11.

RMSE was used to select the optimal model using the smallest value. The final value used for the model was k = 19.

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.1172384 0.6595098 2.4896405

Which models appear to give the best performance? Does MARS select the informative predictors (those named X1–X5)?


KNN:

Given in the question, the result of KNN RMSE is:

MARS:

Next we will take a look at MARS model

set.seed(101)  
marsGrid <- expand.grid(degree =1:2, nprune=seq(2,14,by=2))
marsModel <- train(x = trainingData$x, y = trainingData$y, method='earth', tuneGrid = marsGrid, trControl = trainControl(method = "cv"))
marsModel
## 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.282319  0.3209299  3.647872
##   1        4      3.252892  0.6177937  2.596347
##   1        6      2.422492  0.7949329  1.951867
##   1        8      1.818816  0.8843736  1.443801
##   1       10      1.710585  0.8942351  1.320219
##   1       12      1.713885  0.8938793  1.318051
##   1       14      1.719069  0.8935452  1.327885
##   2        2      4.282319  0.3209299  3.647872
##   2        4      3.235833  0.6141738  2.589953
##   2        6      2.612814  0.7594481  2.101367
##   2        8      2.018077  0.8545566  1.578292
##   2       10      1.606696  0.9113584  1.281839
##   2       12      1.286895  0.9434766  1.071257
##   2       14      1.253988  0.9469622  1.038891
## 
## 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.
#Variable Importance
varImp(marsModel)
## earth variable importance
## 
##     Overall
## X4   100.00
## X2    83.05
## X1    67.99
## X5    55.45
## X3    43.90
## X10    0.00
## X6     0.00
## X9     0.00
## X7     0.00
## X8     0.00
marsPred <- predict (marsModel, testData$x)
postResample(pred = marsPred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 1.3021741 0.9319866 1.0256858

Neural Network:

nnetPred <- predict(nnetModel, newdata = testData$x)
pr <- postResample(pred = nnetPred, obs = testData$y)

SVM:

set.seed(102)
svmModel <- train(x = trainingData$x, y = trainingData$y, method='svmRadial', tuneLength = 14, trControl = trainControl(method = "cv"))
svmModel$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.0592686035661848 
## 
## Number of Support Vectors : 165 
## 
## Objective Function Value : -92.0455 
## Training error : 0.010848
svmPred <- predict(svmModel, newdata = testData$x)
postResample(pred = svmPred, obs = testData$y)
##      RMSE  Rsquared       MAE 
## 1.9501784 0.8481132 1.5150040

MARS has the most accurate result among the 4 models here with best RMSE value of 1.3021741



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.

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

  2. 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?

  3. 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?


set.seed(1234)
registerDoParallel(cores=3)
chem_imput <- missForest(ChemicalManufacturingProcess, ntree = 100)$ximp
##   missForest iteration 1 in progress...done!
##   missForest iteration 2 in progress...done!
##   missForest iteration 3 in progress...done!
##   missForest iteration 4 in progress...done!
ds <- list(org.data.set = chem_imput)

trans_ds <- ds$org.data.set %>% 
              mutate(BiologicalMaterial04.boxcoxtrans = BoxCox(BiologicalMaterial04, -0.99992424816297),
                   ManufacturingProcess01.boxcoxtrans = BoxCox(ManufacturingProcess01, 1.99995900720725),
                   ManufacturingProcess02.boxcoxtrans = BoxCox(ManufacturingProcess02, 1.99995900720725),
                   ManufacturingProcess03.boxcoxtrans = BoxCox(ManufacturingProcess03, -0.99992424816297),
                   ManufacturingProcess06.boxcoxtrans = BoxCox(ManufacturingProcess06, -0.99992424816297),
                   ManufacturingProcess07.boxcoxtrans = BoxCox(ManufacturingProcess07,-0.99992424816297),
                   ManufacturingProcess08.boxcoxtrans = BoxCox(ManufacturingProcess08, 1.99992424816297),
                   ManufacturingProcess12.boxcoxtrans = BoxCox(ManufacturingProcess12, 0.0000410225926326142),
                   ManufacturingProcess16.boxcoxtrans = BoxCox(ManufacturingProcess16, 1.99995900720725),
                   ManufacturingProcess18.boxcoxtrans = BoxCox(ManufacturingProcess18, 1.99995900720725))

data.set <- c(ds, list(trans.data.set = trans_ds))  %>% 
            c(., list(train.test.split = floor(0.75 * nrow(chem_imput)))) %>% 
            c(., train.index = list(sample(seq_len(nrow(chem_imput)), size = .$train.test.split))) %>%
            c(., train = list(as.data.frame(trans_ds[.$train.index,])), test = list(as.data.frame(trans_ds[-.$train.index,])))

a.

nnetFit <- nnet(data.set$train %>% dplyr::select(-Yield), data.set$train$Yield, size = 5, decay = .01, maxit = 500, trace = FALSE)
nnetPred <- predict(nnetFit, data.set$test %>% dplyr::select(-Yield)) %>% 
  cbind(.,data.set$test$Yield) %>% 
  as.data.frame()
colnames(nnetPred) <- c("predicted", "actual")
rmse(nnetPred$actual, nnetPred$predicted)
## [1] 39.27299
marsFit <- earth(data.set$train %>% dplyr::select(-Yield), data.set$train$Yield)
marsPred <- predict(marsFit, data.set$test %>% dplyr::select(-Yield)) %>% 
  cbind(.,data.set$test$Yield) %>% 
  as.data.frame()
colnames(marsPred) <- c("predicted", "actual")
rmse(marsPred$actual, marsPred$predicted)
## [1] 1.20368

b.

dt <- setDT(varImp(marsFit), keep.rownames = TRUE)[]
options(scipen=99); dt[order(-Overall)]
##                                     rn    Overall
##  1:             ManufacturingProcess32 100.000000
##  2:             ManufacturingProcess09  59.796136
##  3:             ManufacturingProcess13  33.003558
##  4:               BiologicalMaterial03  15.563278
##  5:             ManufacturingProcess33  14.981677
##  6:             ManufacturingProcess28  12.625159
##  7:             ManufacturingProcess45  11.930599
##  8:               BiologicalMaterial02   9.980304
##  9:               BiologicalMaterial01   0.000000
## 10:               BiologicalMaterial04   0.000000
## 11:               BiologicalMaterial05   0.000000
## 12:               BiologicalMaterial06   0.000000
## 13:               BiologicalMaterial07   0.000000
## 14:               BiologicalMaterial08   0.000000
## 15:               BiologicalMaterial09   0.000000
## 16:               BiologicalMaterial10   0.000000
## 17:               BiologicalMaterial11   0.000000
## 18:               BiologicalMaterial12   0.000000
## 19:             ManufacturingProcess01   0.000000
## 20:             ManufacturingProcess02   0.000000
## 21:             ManufacturingProcess03   0.000000
## 22:             ManufacturingProcess04   0.000000
## 23:             ManufacturingProcess05   0.000000
## 24:             ManufacturingProcess06   0.000000
## 25:             ManufacturingProcess07   0.000000
## 26:             ManufacturingProcess08   0.000000
## 27:             ManufacturingProcess10   0.000000
## 28:             ManufacturingProcess11   0.000000
## 29:             ManufacturingProcess12   0.000000
## 30:             ManufacturingProcess14   0.000000
## 31:             ManufacturingProcess15   0.000000
## 32:             ManufacturingProcess16   0.000000
## 33:             ManufacturingProcess17   0.000000
## 34:             ManufacturingProcess18   0.000000
## 35:             ManufacturingProcess19   0.000000
## 36:             ManufacturingProcess20   0.000000
## 37:             ManufacturingProcess21   0.000000
## 38:             ManufacturingProcess22   0.000000
## 39:             ManufacturingProcess23   0.000000
## 40:             ManufacturingProcess24   0.000000
## 41:             ManufacturingProcess25   0.000000
## 42:             ManufacturingProcess26   0.000000
## 43:             ManufacturingProcess27   0.000000
## 44:             ManufacturingProcess29   0.000000
## 45:             ManufacturingProcess30   0.000000
## 46:             ManufacturingProcess31   0.000000
## 47:             ManufacturingProcess34   0.000000
## 48:             ManufacturingProcess35   0.000000
## 49:             ManufacturingProcess36   0.000000
## 50:             ManufacturingProcess37   0.000000
## 51:             ManufacturingProcess38   0.000000
## 52:             ManufacturingProcess39   0.000000
## 53:             ManufacturingProcess40   0.000000
## 54:             ManufacturingProcess41   0.000000
## 55:             ManufacturingProcess42   0.000000
## 56:             ManufacturingProcess43   0.000000
## 57:             ManufacturingProcess44   0.000000
## 58:   BiologicalMaterial04.boxcoxtrans   0.000000
## 59: ManufacturingProcess01.boxcoxtrans   0.000000
## 60: ManufacturingProcess02.boxcoxtrans   0.000000
## 61: ManufacturingProcess03.boxcoxtrans   0.000000
## 62: ManufacturingProcess06.boxcoxtrans   0.000000
## 63: ManufacturingProcess07.boxcoxtrans   0.000000
## 64: ManufacturingProcess08.boxcoxtrans   0.000000
## 65: ManufacturingProcess12.boxcoxtrans   0.000000
## 66: ManufacturingProcess16.boxcoxtrans   0.000000
## 67: ManufacturingProcess18.boxcoxtrans   0.000000
##                                     rn    Overall

c.

subset.data.set <- data.set$trans.data.set %>% 
              dplyr::select(ManufacturingProcess32, ManufacturingProcess09, ManufacturingProcess13, 
                            ManufacturingProcess33, BiologicalMaterial02, BiologicalMaterial03, 
                            ManufacturingProcess28, ManufacturingProcess43, ManufacturingProcess04, 
                            BiologicalMaterial05, Yield)

correlations <- cor(subset.data.set)
corrplot::corrplot(correlations, order = "FPC", diag = TRUE)