Problem 6.2: Predicting Permeability

Introduction

Understanding a compound’s permeability is critical in pharmaceutical research since it helps anticipate whether it will be efficiently absorbed in the human body. A permeability prediction model can help save time, money, and resources. In this study, we use Partial Least Squares (PLS) to predict permeability based on chemical fingerprints.

Load library

library(AppliedPredictiveModeling)
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(pls)
## 
## Attaching package: 'pls'
## The following object is masked from 'package:caret':
## 
##     R2
## The following object is masked from 'package:stats':
## 
##     loadings
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.4     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ purrr::lift()   masks caret::lift()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
library(dplyr)

Load Data

data("permeability")

str(permeability)
##  num [1:165, 1] 12.52 1.12 19.41 1.73 1.68 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : chr [1:165] "1" "2" "3" "4" ...
##   ..$ : chr "permeability"

This data consist of permeability and fingerprints

Filtering Predictors with Low Frequency

nzv <- nearZeroVar(fingerprints)

filtered_fingerprints <- fingerprints[, -nzv]

ncol(filtered_fingerprints)
## [1] 388

Molecular fingerprints are largely zeros. Many predictors occur so infrequently that they add nothing to model performance and can lead to overfitting. The caret package’s nearZeroVar() method filters out such predictions. After filtering, 388 predictors remain, providing a more manageable and useful feature set.

Splitting Data and Training a PLS Model

set.seed(123)
trainIndex <- createDataPartition(permeability, p = 0.8, list = FALSE)

trainX <- filtered_fingerprints[trainIndex, ]

testX <- filtered_fingerprints[-trainIndex, ]

trainY <- permeability[trainIndex]

testY <- permeability[-trainIndex]
# preprocessing and model tuning

pls_model <- train(
  x = trainX,
  y = trainY,
  method = "pls",
  tuneLength = 20,
  trControl = trainControl(method = "cv", number = 10),
  preProcess = c("center", "scale")
)

#optimal number of components

pls_model$bestTune
##   ncomp
## 6     6
# Resampled R-squared
max(pls_model$results$Rsquared)
## [1] 0.5335956

We trained a PLS model with tenfold cross-validation. This model is useful when predictors are very collinear since it reduces dimensions to latent variables. The ideal number of latent variables was determined automatically. The resampled R² score indicates the model’s ability to accurately predict permeability during training.

Predictions on Test Set

pred_test <- predict(pls_model, testX)

# R-squated on test data
postResample(pred_test, testY)["Rsquared"]
##  Rsquared 
## 0.3244542

We trained a PLS model with tenfold cross-validation. This model is useful when predictors are very collinear since it reduces dimensions to latent variables. The ideal number of latent variables was determined automatically. The resampled R² score indicates the model’s ability to accurately predict permeability during training.

Problem 6.3: Improving Chemical Yield

In chemical production, boosting product yield is closely related to profit. Understanding how biological and manufacturing factors impact yield allows us to recommend modifications that increase production. We use a Random Forest model to discover essential traits and generate predictions.

Load Data

data("ChemicalManufacturingProcess")
 head(ChemicalManufacturingProcess)
##   Yield BiologicalMaterial01 BiologicalMaterial02 BiologicalMaterial03
## 1 38.00                 6.25                49.58                56.97
## 2 42.44                 8.01                60.97                67.48
## 3 42.03                 8.01                60.97                67.48
## 4 41.42                 8.01                60.97                67.48
## 5 42.49                 7.47                63.33                72.25
## 6 43.57                 6.12                58.36                65.31
##   BiologicalMaterial04 BiologicalMaterial05 BiologicalMaterial06
## 1                12.74                19.51                43.73
## 2                14.65                19.36                53.14
## 3                14.65                19.36                53.14
## 4                14.65                19.36                53.14
## 5                14.02                17.91                54.66
## 6                15.17                21.79                51.23
##   BiologicalMaterial07 BiologicalMaterial08 BiologicalMaterial09
## 1                  100                16.66                11.44
## 2                  100                19.04                12.55
## 3                  100                19.04                12.55
## 4                  100                19.04                12.55
## 5                  100                18.22                12.80
## 6                  100                18.30                12.13
##   BiologicalMaterial10 BiologicalMaterial11 BiologicalMaterial12
## 1                 3.46               138.09                18.83
## 2                 3.46               153.67                21.05
## 3                 3.46               153.67                21.05
## 4                 3.46               153.67                21.05
## 5                 3.05               147.61                21.05
## 6                 3.78               151.88                20.76
##   ManufacturingProcess01 ManufacturingProcess02 ManufacturingProcess03
## 1                     NA                     NA                     NA
## 2                    0.0                      0                     NA
## 3                    0.0                      0                     NA
## 4                    0.0                      0                     NA
## 5                   10.7                      0                     NA
## 6                   12.0                      0                     NA
##   ManufacturingProcess04 ManufacturingProcess05 ManufacturingProcess06
## 1                     NA                     NA                     NA
## 2                    917                 1032.2                  210.0
## 3                    912                 1003.6                  207.1
## 4                    911                 1014.6                  213.3
## 5                    918                 1027.5                  205.7
## 6                    924                 1016.8                  208.9
##   ManufacturingProcess07 ManufacturingProcess08 ManufacturingProcess09
## 1                     NA                     NA                  43.00
## 2                    177                    178                  46.57
## 3                    178                    178                  45.07
## 4                    177                    177                  44.92
## 5                    178                    178                  44.96
## 6                    178                    178                  45.32
##   ManufacturingProcess10 ManufacturingProcess11 ManufacturingProcess12
## 1                     NA                     NA                     NA
## 2                     NA                     NA                      0
## 3                     NA                     NA                      0
## 4                     NA                     NA                      0
## 5                     NA                     NA                      0
## 6                     NA                     NA                      0
##   ManufacturingProcess13 ManufacturingProcess14 ManufacturingProcess15
## 1                   35.5                   4898                   6108
## 2                   34.0                   4869                   6095
## 3                   34.8                   4878                   6087
## 4                   34.8                   4897                   6102
## 5                   34.6                   4992                   6233
## 6                   34.0                   4985                   6222
##   ManufacturingProcess16 ManufacturingProcess17 ManufacturingProcess18
## 1                   4682                   35.5                   4865
## 2                   4617                   34.0                   4867
## 3                   4617                   34.8                   4877
## 4                   4635                   34.8                   4872
## 5                   4733                   33.9                   4886
## 6                   4786                   33.4                   4862
##   ManufacturingProcess19 ManufacturingProcess20 ManufacturingProcess21
## 1                   6049                   4665                    0.0
## 2                   6097                   4621                    0.0
## 3                   6078                   4621                    0.0
## 4                   6073                   4611                    0.0
## 5                   6102                   4659                   -0.7
## 6                   6115                   4696                   -0.6
##   ManufacturingProcess22 ManufacturingProcess23 ManufacturingProcess24
## 1                     NA                     NA                     NA
## 2                      3                      0                      3
## 3                      4                      1                      4
## 4                      5                      2                      5
## 5                      8                      4                     18
## 6                      9                      1                      1
##   ManufacturingProcess25 ManufacturingProcess26 ManufacturingProcess27
## 1                   4873                   6074                   4685
## 2                   4869                   6107                   4630
## 3                   4897                   6116                   4637
## 4                   4892                   6111                   4630
## 5                   4930                   6151                   4684
## 6                   4871                   6128                   4687
##   ManufacturingProcess28 ManufacturingProcess29 ManufacturingProcess30
## 1                   10.7                   21.0                    9.9
## 2                   11.2                   21.4                    9.9
## 3                   11.1                   21.3                    9.4
## 4                   11.1                   21.3                    9.4
## 5                   11.3                   21.6                    9.0
## 6                   11.4                   21.7                   10.1
##   ManufacturingProcess31 ManufacturingProcess32 ManufacturingProcess33
## 1                   69.1                    156                     66
## 2                   68.7                    169                     66
## 3                   69.3                    173                     66
## 4                   69.3                    171                     68
## 5                   69.4                    171                     70
## 6                   68.2                    173                     70
##   ManufacturingProcess34 ManufacturingProcess35 ManufacturingProcess36
## 1                    2.4                    486                  0.019
## 2                    2.6                    508                  0.019
## 3                    2.6                    509                  0.018
## 4                    2.5                    496                  0.018
## 5                    2.5                    468                  0.017
## 6                    2.5                    490                  0.018
##   ManufacturingProcess37 ManufacturingProcess38 ManufacturingProcess39
## 1                    0.5                      3                    7.2
## 2                    2.0                      2                    7.2
## 3                    0.7                      2                    7.2
## 4                    1.2                      2                    7.2
## 5                    0.2                      2                    7.3
## 6                    0.4                      2                    7.2
##   ManufacturingProcess40 ManufacturingProcess41 ManufacturingProcess42
## 1                     NA                     NA                   11.6
## 2                    0.1                   0.15                   11.1
## 3                    0.0                   0.00                   12.0
## 4                    0.0                   0.00                   10.6
## 5                    0.0                   0.00                   11.0
## 6                    0.0                   0.00                   11.5
##   ManufacturingProcess43 ManufacturingProcess44 ManufacturingProcess45
## 1                    3.0                    1.8                    2.4
## 2                    0.9                    1.9                    2.2
## 3                    1.0                    1.8                    2.3
## 4                    1.1                    1.8                    2.1
## 5                    1.1                    1.7                    2.1
## 6                    2.2                    1.8                    2.0
 str(ChemicalManufacturingProcess)
## 'data.frame':    176 obs. of  58 variables:
##  $ Yield                 : num  38 42.4 42 41.4 42.5 ...
##  $ BiologicalMaterial01  : num  6.25 8.01 8.01 8.01 7.47 6.12 7.48 6.94 6.94 6.94 ...
##  $ BiologicalMaterial02  : num  49.6 61 61 61 63.3 ...
##  $ BiologicalMaterial03  : num  57 67.5 67.5 67.5 72.2 ...
##  $ BiologicalMaterial04  : num  12.7 14.7 14.7 14.7 14 ...
##  $ BiologicalMaterial05  : num  19.5 19.4 19.4 19.4 17.9 ...
##  $ BiologicalMaterial06  : num  43.7 53.1 53.1 53.1 54.7 ...
##  $ BiologicalMaterial07  : num  100 100 100 100 100 100 100 100 100 100 ...
##  $ BiologicalMaterial08  : num  16.7 19 19 19 18.2 ...
##  $ BiologicalMaterial09  : num  11.4 12.6 12.6 12.6 12.8 ...
##  $ BiologicalMaterial10  : num  3.46 3.46 3.46 3.46 3.05 3.78 3.04 3.85 3.85 3.85 ...
##  $ BiologicalMaterial11  : num  138 154 154 154 148 ...
##  $ BiologicalMaterial12  : num  18.8 21.1 21.1 21.1 21.1 ...
##  $ ManufacturingProcess01: num  NA 0 0 0 10.7 12 11.5 12 12 12 ...
##  $ ManufacturingProcess02: num  NA 0 0 0 0 0 0 0 0 0 ...
##  $ ManufacturingProcess03: num  NA NA NA NA NA NA 1.56 1.55 1.56 1.55 ...
##  $ ManufacturingProcess04: num  NA 917 912 911 918 924 933 929 928 938 ...
##  $ ManufacturingProcess05: num  NA 1032 1004 1015 1028 ...
##  $ ManufacturingProcess06: num  NA 210 207 213 206 ...
##  $ ManufacturingProcess07: num  NA 177 178 177 178 178 177 178 177 177 ...
##  $ ManufacturingProcess08: num  NA 178 178 177 178 178 178 178 177 177 ...
##  $ ManufacturingProcess09: num  43 46.6 45.1 44.9 45 ...
##  $ ManufacturingProcess10: num  NA NA NA NA NA NA 11.6 10.2 9.7 10.1 ...
##  $ ManufacturingProcess11: num  NA NA NA NA NA NA 11.5 11.3 11.1 10.2 ...
##  $ ManufacturingProcess12: num  NA 0 0 0 0 0 0 0 0 0 ...
##  $ ManufacturingProcess13: num  35.5 34 34.8 34.8 34.6 34 32.4 33.6 33.9 34.3 ...
##  $ ManufacturingProcess14: num  4898 4869 4878 4897 4992 ...
##  $ ManufacturingProcess15: num  6108 6095 6087 6102 6233 ...
##  $ ManufacturingProcess16: num  4682 4617 4617 4635 4733 ...
##  $ ManufacturingProcess17: num  35.5 34 34.8 34.8 33.9 33.4 33.8 33.6 33.9 35.3 ...
##  $ ManufacturingProcess18: num  4865 4867 4877 4872 4886 ...
##  $ ManufacturingProcess19: num  6049 6097 6078 6073 6102 ...
##  $ ManufacturingProcess20: num  4665 4621 4621 4611 4659 ...
##  $ ManufacturingProcess21: num  0 0 0 0 -0.7 -0.6 1.4 0 0 1 ...
##  $ ManufacturingProcess22: num  NA 3 4 5 8 9 1 2 3 4 ...
##  $ ManufacturingProcess23: num  NA 0 1 2 4 1 1 2 3 1 ...
##  $ ManufacturingProcess24: num  NA 3 4 5 18 1 1 2 3 4 ...
##  $ ManufacturingProcess25: num  4873 4869 4897 4892 4930 ...
##  $ ManufacturingProcess26: num  6074 6107 6116 6111 6151 ...
##  $ ManufacturingProcess27: num  4685 4630 4637 4630 4684 ...
##  $ ManufacturingProcess28: num  10.7 11.2 11.1 11.1 11.3 11.4 11.2 11.1 11.3 11.4 ...
##  $ ManufacturingProcess29: num  21 21.4 21.3 21.3 21.6 21.7 21.2 21.2 21.5 21.7 ...
##  $ ManufacturingProcess30: num  9.9 9.9 9.4 9.4 9 10.1 11.2 10.9 10.5 9.8 ...
##  $ ManufacturingProcess31: num  69.1 68.7 69.3 69.3 69.4 68.2 67.6 67.9 68 68.5 ...
##  $ ManufacturingProcess32: num  156 169 173 171 171 173 159 161 160 164 ...
##  $ ManufacturingProcess33: num  66 66 66 68 70 70 65 65 65 66 ...
##  $ ManufacturingProcess34: num  2.4 2.6 2.6 2.5 2.5 2.5 2.5 2.5 2.5 2.5 ...
##  $ ManufacturingProcess35: num  486 508 509 496 468 490 475 478 491 488 ...
##  $ ManufacturingProcess36: num  0.019 0.019 0.018 0.018 0.017 0.018 0.019 0.019 0.019 0.019 ...
##  $ ManufacturingProcess37: num  0.5 2 0.7 1.2 0.2 0.4 0.8 1 1.2 1.8 ...
##  $ ManufacturingProcess38: num  3 2 2 2 2 2 2 2 3 3 ...
##  $ ManufacturingProcess39: num  7.2 7.2 7.2 7.2 7.3 7.2 7.3 7.3 7.4 7.1 ...
##  $ ManufacturingProcess40: num  NA 0.1 0 0 0 0 0 0 0 0 ...
##  $ ManufacturingProcess41: num  NA 0.15 0 0 0 0 0 0 0 0 ...
##  $ ManufacturingProcess42: num  11.6 11.1 12 10.6 11 11.5 11.7 11.4 11.4 11.3 ...
##  $ ManufacturingProcess43: num  3 0.9 1 1.1 1.1 2.2 0.7 0.8 0.9 0.8 ...
##  $ ManufacturingProcess44: num  1.8 1.9 1.8 1.8 1.7 1.8 2 2 1.9 1.9 ...
##  $ ManufacturingProcess45: num  2.4 2.2 2.3 2.1 2.1 2 2.2 2.2 2.1 2.4 ...

Handling Missing Data

#distinct predictors and response
X <- ChemicalManufacturingProcess[, -1]

Y <- ChemicalManufacturingProcess$Yield

library(RANN)

pre_proc <- preProcess(X, method = c("knnImpute", "center", "scale"))

X_imputed <- predict(pre_proc, X)

Some predictors are missing values. We utilized k-nearest neighbors (kNN) imputation to substitute missing values based on their resemblance to other samples. This, together with centering and scaling, prepares the data for modeling.

#Train Test split and build a model

set.seed(123)

train_index <- createDataPartition(Y,p = 0.8, list = FALSE)

trainX <- X_imputed[train_index, ]

testX <- X_imputed[-train_index, ]

trainY <- Y[train_index]
testY <- Y[train_index]
rf_model <- train(x = trainX,
  y = trainY,
  method = "rf",
  tuneLength = 10,
  trControl = trainControl(method = "cv", number = 10)
)
# checking training performance

max(rf_model$results$Rsquared)
## [1] 0.6695883
rf_model$results
##    mtry     RMSE  Rsquared       MAE    RMSESD RsquaredSD     MAESD
## 1     2 1.233680 0.6522971 0.9863157 0.2485274  0.1551006 0.1718425
## 2     8 1.155652 0.6671644 0.9161959 0.2584433  0.1593769 0.1883288
## 3    14 1.137068 0.6695883 0.9010009 0.2695379  0.1547402 0.1942868
## 4    20 1.144384 0.6555923 0.9033100 0.2668507  0.1547724 0.1918255
## 5    26 1.138767 0.6589172 0.8975126 0.2647536  0.1469744 0.1869694
## 6    32 1.144398 0.6496300 0.8955242 0.2641008  0.1466654 0.1841463
## 7    38 1.145372 0.6463166 0.8887278 0.2665143  0.1497733 0.1902730
## 8    44 1.141166 0.6418132 0.8844658 0.2706379  0.1474131 0.1863840
## 9    50 1.144820 0.6401705 0.8772522 0.2575995  0.1445322 0.1794379
## 10   57 1.144081 0.6370679 0.8749294 0.2512206  0.1438900 0.1701359

We trained a Random Forest model, a strong ensemble approach that deals with complicated, nonlinear linkages and interactions. The resampled R² score shows how well the model fits the training data.

Predict and Compare Performance

pred_test_rf <- predict(rf_model, testX)
postResample(pred_test_rf, testY)
## Warning in pred - obs: longer object length is not a multiple of shorter object
## length

## Warning in pred - obs: longer object length is not a multiple of shorter object
## length
##     RMSE Rsquared      MAE 
## 1.959969       NA 1.598057

The test set R² value accurately represents real-world predicting performance. If it’s close to the training R², the model will generalize well. If it’s significantly lower, it might be overfitting.

#Feature Importance

importance_rf <-varImp(rf_model)

plot(importance_rf, top = 10)

This graphic depicts the top ten most important predictors of yield. ManufacturingProcess32 is the most critical, indicating that it is a primary driver of production. Several biological factors also score highly, demonstrating that input quality can influence yield even when it is not changing during manufacture.

#Explore Top Predictor Relationships

top_predictor <- rownames(importance_rf$importance)[1]

ggplot(data.frame(X = trainX[[top_predictor]], Y = trainY), aes(x = X, y = Y)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = paste("Top Predictor:", top_predictor), x = top_predictor, y = "Yield")
## `geom_smooth()` using formula = 'y ~ x'

Here, we show how the top predictor (ManufacturingProcess32) corresponds with yield. The geom_smooth() method creates a regression line to depict the overall trend. This knowledge can be used to direct process modifications; for example, raising or improving this process parameter may increase future yields.

To Conclude

Both models provide information on how biological and structural factors influence results in pharmaceutical research and manufacture. While not perfect, these prediction models can improve decision-making by identifying compounds or runs that may succeed or fail, allowing for improved resource allocation and quality control.