library(AppliedPredictiveModeling)
library(caret)
library(tidyverse)
library(corrplot)Developing a model to predict permeability (see Sect. 1.4) could save significant resources for a pharmaceutical company, while at the same time more rapidly identifying molecules that have a sufficient permeability to become a drug:
Start R and use these commands to load the data:
library(AppliedPredictiveModeling)
data(permeability)The matrix fingerprints contains the 1,107 binary molecular predictors for the 165 compounds, while permeability contains permeability response.
The fingerprint predictors indicate the presence or absence of substructures of a molecule and are often sparse meaning that relatively few of the molecules contain each substructure. Filter out the predictors that have low frequencies using the nearZeroVar function from the caret package. How many predictors are left for modeling?
There were 1,107 predictors and now there are only 388 predictors left for modeling.
dim(fingerprints)## [1] 165 1107
fingerprints <- fingerprints[, -nearZeroVar(fingerprints)]
dim(fingerprints)## [1] 165 388
Split the data into a training and a test set, pre-process the data, and tune a PLS model. How many latent variables are optimal and what is the corresponding resampled estimate of \(R^2\)?
Ans: The PLS model is optimal with 2 latent variables. The corresponding \(R^2\) estimate is 0.496.
set.seed(145)
train_test_split <- createDataPartition(permeability, p = 0.7, list = FALSE)
train.x <- fingerprints[train_test_split,]
train.y <- permeability[train_test_split,]
test.x <- fingerprints[-train_test_split,]
test.y <- permeability[-train_test_split,]
plsTune_model <- train(train.x, train.y,
method = "pls",
metric = "Rsquared",
tuneLength = 20,
trControl = trainControl(method = "cv", number = 10),
preProc = c("center", "scale"))
plsTune_model## Partial Least Squares
##
## 117 samples
## 388 predictors
##
## Pre-processing: centered (388), scaled (388)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 105, 106, 105, 106, 105, 105, ...
## Resampling results across tuning parameters:
##
## ncomp RMSE Rsquared MAE
## 1 13.62026 0.3285374 10.652011
## 2 12.44941 0.4621046 9.102099
## 3 12.45109 0.4629837 9.764434
## 4 12.44551 0.4727658 10.050409
## 5 12.28097 0.4968722 9.638072
## 6 12.34801 0.5020549 9.708019
## 7 12.10582 0.5257396 9.640292
## 8 11.90026 0.5358451 9.122373
## 9 12.14730 0.5278046 9.110471
## 10 12.60989 0.5211377 9.454211
## 11 12.82672 0.5098361 9.619314
## 12 12.72982 0.5278461 9.688480
## 13 12.97442 0.5207150 9.900039
## 14 13.56583 0.4900933 10.295606
## 15 14.15032 0.4675832 10.748427
## 16 14.52839 0.4583899 10.979932
## 17 14.87386 0.4533527 11.144903
## 18 15.56560 0.4259195 11.718703
## 19 15.83802 0.4180429 11.816698
## 20 16.24258 0.4129796 12.029895
##
## Rsquared was used to select the optimal model using the largest value.
## The final value used for the model was ncomp = 8.
ggplot(plsTune_model) +
xlab("Number of Predictors")Predict the response for the test set. What is the test set estimate of \(R^2\)?
Ans: The estimated \(R^2\) for the test set is 0.45 which is slightly worse than the training set.
pls.predictions <- predict(plsTune_model, newdata = test.x)
postResample(pred = pls.predictions, obs = test.y)## RMSE Rsquared MAE
## 10.9958387 0.4520939 8.4675095
Try building other models discussed in this chapter. Do any have better predictive performance?
Ans: Of all the models discussed in this chapter, none of them outperformed the PLS model. The closest model to the PLS model, in terms of predictive performance, is the pcr model with an \(R^2\) of 0.444.
pcrTune <- train(train.x, train.y,
method = "pcr",
metric = "Rsquared",
tuneLength = 10,
trControl = trainControl("cv", number = 10),
preProc=c('center', 'scale')
)
ridgeGrid <- data.frame(.lambda = seq(0.001, .1, length = 10))
ridgeTune <- train(train.x, train.y,
method = "ridge",
metric = "Rsquared",
tuneGrid = ridgeGrid,
tuneLength = 10,
trControl = trainControl(method = "cv", number = 10),
preProc = c("center", "scale"))
enetGrid <- expand.grid(.lambda = c(0.001, 0.01, .1),
.fraction = seq(.05, 1, length = 10))
enetTune <- train(train.x, train.y,
method = "enet",
metric = "Rsquared",
tuneGrid = enetGrid,
tuneLength = 10,
trControl = trainControl(method = "cv", number = 10),
preProc = c("center", "scale"))pcr.predictions <- predict(pcrTune, newdata = test.x)
pcr.model <- postResample(pred = pcr.predictions, obs = test.y)
ridge.predictions <- predict(ridgeTune, newdata = test.x)
ridge.model <- postResample(pred = ridge.predictions, obs = test.y)
enet.predictions <- predict(enetTune, newdata = test.x)
enet.model <- postResample(pred = enet.predictions, obs = test.y)
pls.predictions <- predict(plsTune_model, newdata = test.x)
pls.model <-postResample(pred = pls.predictions, obs = test.y)
rbind(pcr.model, ridge.model, enet.model, pls.model)## RMSE Rsquared MAE
## pcr.model 10.35943 0.4442586 7.984956
## ridge.model 12.22714 0.4285471 8.882826
## enet.model 12.55376 0.3868604 8.945535
## pls.model 10.99584 0.4520939 8.467509
Would you recommend any of your models to replace the permeability laboratory experiment?
I would not recommend the permeability laboratory experiment to be replaced with any of the models created above. The Rsquared values show that at best, only about 45% of the variability is being explained by the model. Other models at best only explain about 44% of the variability. There is no enough evidence to recommend replacement of the experiment.
A chemical manufacturing process for a pharmaceutical product was discussed in Sect. 1.4. In this problem, the objective is to understand the relationship between biological measurements of the raw materials (predictors), measurements of the manufacturing process (predictors), and the response of product yield. Biological predictors cannot be changed but can be used to assess the quality of the raw material before processing. On the other hand, manufacturing process predictors can be changed in the manufacturing process. Improving product yield by 1% will boost revenue by approximately one hundred thousand dollars per batch:
Start R and use these commands to load the data:
library(AppliedPredictiveModeling)
data("ChemicalManufacturingProcess")The matrix process Predictors contains the 57 predictors (12 describing the input biological material and 45 describing the process predictors) for the 176 manufacturing runs. yield contains the percent yield for each run.
A small percentage of cells in the predictor set contain missing values. Use an imputation function to fill in these missing values (e.g., see Sect. 3.8).
imputed.knn <- preProcess(ChemicalManufacturingProcess,
method = "knnImpute",
k = sqrt(nrow(ChemicalManufacturingProcess))
)
imputed.data <- predict(imputed.knn, ChemicalManufacturingProcess)Split the data into a training and a test set, pre-process the data, and tune a model of your choice from this chapter. What is the optimal value of the performance metric?
I removed near zero variables to Pre-process the data. I then created a 70% - 30% train test split. A ridge regression model was created to model the data. The optimal value of the performance metric are provided below. This model achieved the highest R squared and a relatively low RMSE.
near_zero <- nearZeroVar(imputed.data)
imputed.data <- imputed.data[, -near_zero]
set.seed(135)
train_test_split <- createDataPartition(permeability, p = 0.7, list = FALSE)
train.data <- imputed.data[train_test_split,]
test.data <- imputed.data[-train_test_split,]
ridgeGrid <- data.frame(.lambda = seq(0.001, 1.1, length = 20))
ridgeTune <- train(Yield~., train.data,
method = "ridge",
metric = "Rsquared",
tuneGrid = ridgeGrid,
tuneLength = 20,
trControl = trainControl(method = "cv", number = 10),
preProc = c("center", "scale"))
ridgeTune## Ridge Regression
##
## 117 samples
## 56 predictor
##
## Pre-processing: centered (56), scaled (56)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 105, 105, 105, 105, 105, 105, ...
## Resampling results across tuning parameters:
##
## lambda RMSE Rsquared MAE
## 0.00100000 0.8836701 0.4892562 0.6879355
## 0.05884211 0.7460628 0.5524848 0.6088823
## 0.11668421 0.7307948 0.5638120 0.6036631
## 0.17452632 0.7259840 0.5716728 0.6029863
## 0.23236842 0.7263915 0.5774559 0.6057676
## 0.29021053 0.7302007 0.5818294 0.6097413
## 0.34805263 0.7365252 0.5851904 0.6152280
## 0.40589474 0.7448464 0.5877962 0.6214372
## 0.46373684 0.7548211 0.5898226 0.6279914
## 0.52157895 0.7662006 0.5913951 0.6350538
## 0.57942105 0.7787918 0.5926058 0.6433680
## 0.63726316 0.7924373 0.5935243 0.6525863
## 0.69510526 0.8070039 0.5942039 0.6623523
## 0.75294737 0.8223761 0.5946866 0.6733343
## 0.81078947 0.8384521 0.5950056 0.6854019
## 0.86863158 0.8551419 0.5951876 0.6977637
## 0.92647368 0.8723648 0.5952543 0.7101275
## 0.98431579 0.8900489 0.5952236 0.7224672
## 1.04215789 0.9081297 0.5951103 0.7355509
## 1.10000000 0.9265497 0.5949267 0.7488982
##
## Rsquared was used to select the optimal model using the largest value.
## The final value used for the model was lambda = 0.9264737.
ridgeTune$results[15,]## lambda RMSE Rsquared MAE RMSESD RsquaredSD MAESD
## 15 0.8107895 0.8384521 0.5950056 0.6854019 0.1650476 0.1426706 0.1213477
Predict the response for the test set.What is the value of the performance metric and how does this compare with the resampled performance metric on the training set?
The test set results are a bit worse than the training set which is kind of expected. The model performance seems to be similar on both training and testing, indicating that the model has not been overfit or underfit.
ridge.predictions <- predict(ridgeTune, newdata = test.data)
ridge.model <- postResample(pred = ridge.predictions, obs = test.data$Yield)
ridge.model## RMSE Rsquared MAE
## 4.77862468 0.05342368 2.47201478
Which predictors are most important in the model you have trained? Do either the biological or process predictors dominate the list?
Below is a list of the top 20 variables from the model. The top 5 variables are a combination of biological and process variables. The top 20 contains 11 process variables and 9 biological variables. It appears that there is a fairly equal split of importance between the two variable types.
varImp(ridgeTune)## loess r-squared variable importance
##
## only 20 most important variables shown (out of 56)
##
## Overall
## ManufacturingProcess32 100.00
## ManufacturingProcess13 98.52
## ManufacturingProcess17 92.39
## ManufacturingProcess09 78.45
## BiologicalMaterial03 69.51
## BiologicalMaterial06 68.69
## ManufacturingProcess36 62.91
## ManufacturingProcess06 60.53
## BiologicalMaterial12 58.78
## BiologicalMaterial02 56.90
## ManufacturingProcess31 47.68
## ManufacturingProcess11 44.77
## BiologicalMaterial09 39.63
## BiologicalMaterial11 39.58
## ManufacturingProcess30 39.38
## ManufacturingProcess33 37.25
## ManufacturingProcess18 36.68
## ManufacturingProcess29 35.23
## BiologicalMaterial04 35.18
## ManufacturingProcess12 33.38
Explore the relationships between each of the top predictors and the response. How could this information be helpful in improving yield in future runs of the manufacturing process?
Majority of the important variables have a slight correlation with the target variable, Yield. There are a few of biological material and manufacturing processes that are correlated with each other. The relationships between the response variable and the other predictor variables can be leveraged into maximizing variables that are positively correlated with Yield and other predictors and reduce variables that have a negative relationship with respect to other independent variables.
important.vars <- imputed.data %>%
select("Yield", "ManufacturingProcess32", "BiologicalMaterial06", "ManufacturingProcess13",
"BiologicalMaterial12", "BiologicalMaterial03", "ManufacturingProcess17", "ManufacturingProcess36",
"ManufacturingProcess09", "ManufacturingProcess06", "ManufacturingProcess31")
cor.matrix <- cor(important.vars)
corrplot(corr = cor.matrix, tl.col = 'black', type = 'lower', diag = FALSE)