7.2
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: K-NN Model
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.565620 0.4887976 2.886629
## 7 3.422420 0.5300524 2.752964
## 9 3.368072 0.5536927 2.715310
## 11 3.323010 0.5779056 2.669375
## 13 3.275835 0.6030846 2.628663
## 15 3.261864 0.6163510 2.621192
## 17 3.261973 0.6267032 2.616956
## 19 3.286299 0.6281075 2.640585
## 21 3.280950 0.6390386 2.643807
## 23 3.292397 0.6440392 2.656080
##
## 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
set.seed(200)
marsGrid <- expand.grid(degree = 1:2, nprune = seq(2,14,by=2))
marsTune <- train(x = trainingData$x, y = trainingData$y, method = "earth",preProc=c("center", "scale"),tuneGrid= marsGrid)## Loading required package: earth
## Warning: package 'earth' was built under R version 3.4.4
## Loading required package: plotmo
## Warning: package 'plotmo' was built under R version 3.4.4
## Loading required package: plotrix
## Warning: package 'plotrix' was built under R version 3.4.4
## Loading required package: TeachingDemos
marsTune## Multivariate Adaptive Regression Spline
##
## 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:
##
## degree nprune RMSE Rsquared MAE
## 1 2 4.404370 0.2169801 3.602809
## 1 4 2.911612 0.6566780 2.332623
## 1 6 2.457276 0.7520496 1.936728
## 1 8 1.874996 0.8564515 1.454104
## 1 10 1.826010 0.8647165 1.403008
## 1 12 1.828526 0.8647554 1.409072
## 1 14 1.851762 0.8618349 1.418886
## 2 2 4.430822 0.2110990 3.615584
## 2 4 2.904182 0.6592858 2.313859
## 2 6 2.448257 0.7536608 1.934173
## 2 8 1.918478 0.8501920 1.497030
## 2 10 1.624494 0.8912320 1.291867
## 2 12 1.511115 0.9072610 1.187800
## 2 14 1.461783 0.9126680 1.145746
##
## 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, newdata=testData$x)
postResample(pred = marsPred, obs = testData$y)## RMSE Rsquared MAE
## 1.2779993 0.9338365 1.0147070
varImp(marsTune)## earth variable importance
##
## Overall
## X1 100.00
## X4 84.98
## X2 68.87
## X5 48.55
## X3 38.96
## X10 0.00
## X9 0.00
## X7 0.00
## X6 0.00
## X8 0.00
SVM model
set.seed(200)
svmModel = train(x=trainingData$x, y=trainingData$y, method="svmRadial", preProc=c("center", "scale"), tuneLength=20)
svmModel## 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.580633 0.7683393 2.009726
## 0.50 2.377466 0.7816479 1.842618
## 1.00 2.245113 0.7991788 1.740655
## 2.00 2.142750 0.8151393 1.660343
## 4.00 2.096519 0.8219403 1.623355
## 8.00 2.076956 0.8249904 1.606453
## 16.00 2.075921 0.8251733 1.605902
## 32.00 2.075921 0.8251733 1.605902
## 64.00 2.075921 0.8251733 1.605902
## 128.00 2.075921 0.8251733 1.605902
## 256.00 2.075921 0.8251733 1.605902
## 512.00 2.075921 0.8251733 1.605902
## 1024.00 2.075921 0.8251733 1.605902
## 2048.00 2.075921 0.8251733 1.605902
## 4096.00 2.075921 0.8251733 1.605902
## 8192.00 2.075921 0.8251733 1.605902
## 16384.00 2.075921 0.8251733 1.605902
## 32768.00 2.075921 0.8251733 1.605902
## 65536.00 2.075921 0.8251733 1.605902
## 131072.00 2.075921 0.8251733 1.605902
##
## Tuning parameter 'sigma' was held constant at a value of 0.06574662
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.06574662 and C = 16.
svmPred = predict(svmModel, newdata=testData$x)
postResample(pred=svmPred, obs=testData$y) ## RMSE Rsquared MAE
## 2.0804235 0.8246038 1.5805232
Which models appear to give the best performance? Does MARS select the informative predictors (those named X1–X5)?
The MARS model appears to give the best performance with the lowest RMSE(1.28!) The Mars Model does select the most informative predictors, with X1 as the most important and X3 as the least of the above predictors.
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?
(b) 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 tenpredictors from the optimal linear model?
(c) 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?
Here are 3 models used above on the new set of data, KNN, MARS, and SVM.
data("ChemicalManufacturingProcess")
predictors <- subset(ChemicalManufacturingProcess,select= -Yield)
yield <- subset(ChemicalManufacturingProcess,select="Yield")
samples = dim(predictors)[1]
features = dim(predictors)[2]
missing_rows = apply(predictors, 1, function(x) sum(is.na(x)))
for(x in 1:features ){
blanks = is.na(predictors[,x] )
predictors[blanks,x] = missing_rows[x]
}training_data <- createDataPartition(yield$Yield,
p = 0.75,
list = FALSE)
train_predictors <- predictors[training_data,]
train_yield <- yield[training_data,]
test_predictors <- predictors[-training_data,]
test_yield <- yield[-training_data,]SVM model
set.seed(200)
svmModel2 = train(x=train_predictors, y=train_yield, method="svmRadial", preProc=c("center", "scale"), tuneLength=20)## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.
svmModel2## Support Vector Machines with Radial Basis Function Kernel
##
## 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:
##
## C RMSE Rsquared MAE
## 0.25 1.494915 0.4140616 1.219381
## 0.50 1.421013 0.4539929 1.147392
## 1.00 1.362910 0.4966482 1.091817
## 2.00 1.320208 0.5277724 1.057811
## 4.00 1.300426 0.5397655 1.041855
## 8.00 1.292465 0.5440603 1.034480
## 16.00 1.293744 0.5433139 1.035557
## 32.00 1.293743 0.5433141 1.035556
## 64.00 1.293743 0.5433141 1.035556
## 128.00 1.293743 0.5433141 1.035556
## 256.00 1.293743 0.5433141 1.035556
## 512.00 1.293743 0.5433141 1.035556
## 1024.00 1.293743 0.5433141 1.035556
## 2048.00 1.293743 0.5433141 1.035556
## 4096.00 1.293743 0.5433141 1.035556
## 8192.00 1.293743 0.5433141 1.035556
## 16384.00 1.293743 0.5433141 1.035556
## 32768.00 1.293743 0.5433141 1.035556
## 65536.00 1.293743 0.5433141 1.035556
## 131072.00 1.293743 0.5433141 1.035556
##
## Tuning parameter 'sigma' was held constant at a value of 0.01870708
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were sigma = 0.01870708 and C = 8.
svmPred = predict(svmModel2, newdata=test_predictors)
postResample(pred=svmPred, obs=test_yield) ## RMSE Rsquared MAE
## 1.1684790 0.5935785 0.9448891
KNN Model
set.seed(200)
knnModel2 <- train(x=train_predictors, y=train_yield,method = "knn", preProc = c("center", "scale"),tuneLength = 10)## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
knnModel2## 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.480358 0.4015046 1.174378
## 7 1.466669 0.4111778 1.178026
## 9 1.497054 0.3914014 1.206893
## 11 1.501295 0.3863116 1.217342
## 13 1.502828 0.3913138 1.224524
## 15 1.508884 0.3951055 1.228886
## 17 1.508847 0.4000942 1.226208
## 19 1.518390 0.3964603 1.231253
## 21 1.520646 0.4006238 1.229189
## 23 1.527843 0.4007453 1.233656
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was k = 7.
knnPred = predict(knnModel2, newdata=test_predictors)
postResample(pred=knnPred, obs=test_yield) ## RMSE Rsquared MAE
## 1.4780310 0.3573608 1.1463961
MARS Model
set.seed(200)
marsGrid <- expand.grid(degree = 1:2, nprune = seq(2,14,by=2))
marsTune2 <- train(x=train_predictors, y=train_yield, method = "earth",preProc=c("center", "scale"),tuneGrid= marsGrid)## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: BiologicalMaterial07
marsTune2## Multivariate Adaptive Regression Spline
##
## 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:
##
## degree nprune RMSE Rsquared MAE
## 1 2 1.535256 0.3696160 1.220823
## 1 4 1.335812 0.5088211 1.077173
## 1 6 1.583862 0.4552183 1.144350
## 1 8 1.893228 0.4141461 1.220725
## 1 10 2.255281 0.3798139 1.303574
## 1 12 2.731288 0.3167524 1.444752
## 1 14 3.063569 0.2784495 1.538773
## 2 2 1.530000 0.3758679 1.210932
## 2 4 1.383032 0.4872566 1.109887
## 2 6 1.412701 0.4787681 1.130257
## 2 8 2.205019 0.4072149 1.304914
## 2 10 3.092319 0.3544644 1.490383
## 2 12 3.446455 0.3171085 1.574253
## 2 14 4.601815 0.3095075 1.796782
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were nprune = 4 and degree = 1.
marsPred = predict(marsTune2, newdata=test_predictors)
postResample(pred=marsPred, obs=test_yield) ## RMSE Rsquared MAE
## 1.2116078 0.5617009 0.9189991
varImp(marsTune2)## earth variable importance
##
## only 20 most important variables shown (out of 57)
##
## Overall
## ManufacturingProcess32 100.0
## ManufacturingProcess13 57.3
## BiologicalMaterial05 0.0
## ManufacturingProcess29 0.0
## BiologicalMaterial04 0.0
## ManufacturingProcess33 0.0
## ManufacturingProcess26 0.0
## BiologicalMaterial10 0.0
## ManufacturingProcess03 0.0
## BiologicalMaterial02 0.0
## ManufacturingProcess09 0.0
## ManufacturingProcess38 0.0
## ManufacturingProcess37 0.0
## BiologicalMaterial09 0.0
## BiologicalMaterial11 0.0
## BiologicalMaterial06 0.0
## ManufacturingProcess02 0.0
## ManufacturingProcess45 0.0
## ManufacturingProcess04 0.0
## BiologicalMaterial12 0.0
The model with the optimal resampling and test set performance is the SVM Model.
b.
impFeatures <- varImp(svmModel2)
impFeatures## loess r-squared variable importance
##
## only 20 most important variables shown (out of 57)
##
## Overall
## ManufacturingProcess32 100.00
## ManufacturingProcess13 82.03
## BiologicalMaterial06 77.49
## BiologicalMaterial02 70.17
## BiologicalMaterial12 69.03
## BiologicalMaterial03 66.21
## ManufacturingProcess17 62.74
## ManufacturingProcess09 54.27
## ManufacturingProcess29 53.35
## ManufacturingProcess31 51.92
## ManufacturingProcess06 46.90
## BiologicalMaterial04 46.86
## BiologicalMaterial11 46.27
## BiologicalMaterial01 42.44
## BiologicalMaterial08 39.78
## ManufacturingProcess11 36.00
## ManufacturingProcess10 33.31
## BiologicalMaterial09 32.26
## ManufacturingProcess12 29.59
## ManufacturingProcess30 27.38