The study used in this tutorial has been previously published by Stevens et al. (2018), and the deconvolved and annotated data file deposited at the Metabolights data repository. The data can be accessed directly via its study ID: MTBLS136. This workflow requires data to be formatted as a Microsoft Excel file, using the Tidy Data framework (i.e. each column is a variable, and row is an observation). As such, the Excel file contains a Data Sheet and Peak Sheet. The Data Sheet contains all the metabolite concentrations and metadata associated with each observation (requiring the inclusion of the columns: Idx, SampleID, and Class). The Peak Sheet contains all the metadata pertaining to each measured metabolite (requiring the inclusion of the columns: Idx, Name, and Label). Please inspect the Excel file MTBLS136.xlsx used in this workflow before proceeding.
This is a serum LC-MS dataset consisting of 949 named metabolites. The primary outcome for this paper was estrogen-only (E; n=332) vs. estrogen plus progestin (E+P; n=337) vs. non-users of post-menopausal hormone therapy regimes (Control; n=667). For the purpose of this study, we compare only the E vs. E+P in a binary discriminant analysis.
This Jupyter Notebook implements the complete workflow for creating, optimising, and evaluating a principal component logistic regression (PCLR) model. PCLR is a two-stage algorithm combining principal component analysis (PCA) and logistic regression, where the first N principal component scores act as the independent variables of the logistic regression, and the binary classification is the dependent variable. The value of N is chosen by the user. PCA was implemented using PCA and Logistic Regression from scikit-learn.
Please refer to the 'cimcb' package documentation for further details regarding this specific implementation: https://cimcb.github.io/cimcbn_components
: number of principal components projected into the logistic regression (default = 2)numpy
, pandas
, and cimcb
).
DataTable
and PeakTable
.DataTable
to include only those observations needed for the binary comparison and create a new table: DataTable2
. We define one column of the data table to be the "outcome" variable Outcomes
, and convert the class labels in this column to a binary outcome vector Y
, where 1
is the positive outcome, and 0
the negative outcome (eg. case=1 & control=0). A new variable peaklist
is created to hold the names (M1...Mn) of the metabolites to be used in the discriminant analysis. To create an independent dataset to evaluate, scikit-learn module's train_test_split()
function is used. The data is split into 2/3rd training (DataTrain
and YTrain
), and 1/3rd test (DataTest
and YTest
). The metabolite data corresponding to peaklist
is extracted from DataTrain
and placed in a matrix XTrain
. The XTrain
matrix is log-transformed and auto-scaled, with missing values imputed using k-nearest neighbours (k=3). Then the metabolite data corresponding to peaklist
is extracted from DataTest
and placed in a matrix XTest
. The XTest
matrix is log-transformed and auto-scaled (using mu and sigma from XTrain
), with missing values imputed using k-nearest neighbours (k=3).
cb.cross_val.KFold()
to carry out 5-fold cross-validation of a set of PCLR models configured with different numbers of principal components (1 to 30). This helper function is generally applicable, and the values being passed to it are:
cb.model.PCLR
.XTknn
, and binary outcome vector, Y
.param_dict
, describing key:value pairs where the key is a parameter that is passed to the model, and the value is a list of values to be passed to that parameter.folds
, and the number of Monte Carlo repetitions of the k-fold CV, n_mc
.cv.run()
followed by cv.plot(metric='r2q2')
are run the results are displayed as 2 plots of $R^2$ and $Q^2$ statistics: (a) the difference ($R^2 - Q^2$) vs. $Q^2$, and (b) absolute values of both $R^2$ and $Q^2$ against the number of components. If the function cv.plot(metric='auc')
is run the predictive ability of the model is presented as measures of the area under the ROC curve, $AUC(full)$ & $AUC(cv)$, as a nonparametric alternative to $R^2$ & $Q^2$. These multiple plots are used to aid in selecting the optimal number of components.cb.model.PCLR()
to building a PCLR model using the optimal hyperparameter values determined in step 4. The model is trained on the training dataset, XTrainKnn
, and tested on the independent test dataset, XTestKnn
. Next, the trained model's .evaluate()
method is used to visualise model performance for both the training and independent test dataset using: a violin plot showing the distributions of negative and positive responses as violin and box-whisker plots; a probability density function plot for each response type, and a ROC curve that displays the curve for the training dataset (green) and test dataset (yellow).
cb.bootstrap.Per()
with 100 boostrapped models. This generates a population of 100 model predictions for both the training set (in-bag prediction - IB) and the holdout test set (out-of-bag - OOB) from the full dataset, with the metabolite matrix, XBootKnn
, and binary outcome vector, Y
. These predictions are visualised with a box-violin and probability density function plot for the aggregate model. The ROC curve displays the curve for the training dataset (green) and test dataset (yellow) from section 5 with 95% confidence intervals (light green band = IB & light yellow band = OOB).
import numpy as np
import pandas as pd
import cimcb as cb
from sklearn.model_selection import train_test_split
print('All packages successfully loaded')
home = 'data/'
file = 'MTBLS136.xlsx'
DataTable,PeakTable = cb.utils.load_dataXL(home + file, DataSheet='Data', PeakSheet='Peak')
# Clean PeakTable and Extract PeakList
PercMiss = PeakTable['Perc_missing']
PeakTableClean = PeakTable[(PercMiss < 20)]
PeakList = PeakTableClean['Name']
# Select Subset of Data
DataTable2 = DataTable[(DataTable.Class == 1) | (DataTable.Class == 0)]
# Create a Binary Y Vector
Outcomes = DataTable2['Class']
Y = Outcomes.values
# Split Data into Train (2/3) and Test (1/3)
DataTrain, DataTest, YTrain, YTest = train_test_split(DataTable2, Y, test_size=1/3, stratify=Y, random_state=85)
# Extract Train Data
XTrain = DataTrain[PeakList]
XTrainLog = np.log(XTrain)
XTrainScale, mu, sigma = cb.utils.scale(XTrainLog, method='auto', return_mu_sigma=True)
XTrainKnn = cb.utils.knnimpute(XTrainScale, k=3)
# Extract Test Data
XTest = DataTest[PeakList]
XTestLog = np.log(XTest)
XTestScale = cb.utils.scale(XTestLog, method='auto', mu=mu, sigma=sigma)
XTestKnn = cb.utils.knnimpute(XTestScale, k=3)
# Parameter Dictionary
#param_dict = {'n_components': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]}
param_dict = {'n_components': [20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,200]}
# Initialise
cv = cb.cross_val.KFold(model=cb.model.PCLR,
X=XTrainKnn,
Y=YTrain,
param_dict=param_dict,
folds=5,
n_mc=10)
# Run and Plot
cv.run()
cv.plot(metric='auc')
cv.plot(metric='r2q2')
# Build Model
model = cb.model.PCLR(n_components=70)
YPredTrain = model.train(XTrainKnn, YTrain)
YPredTest = model.test(XTestKnn)
# Put YTrain and YPredTrain in a List
EvalTrain = [YTrain, YPredTrain]
# Put YTest and YPrestTest in a List
EvalTest = [YTest, YPredTest]
# Evaluate Model (include Test Dataset)
model.evaluate(testset=EvalTest)
# Extract X Data
XBoot = DataTable2[PeakList]
XBootLog = np.log(XBoot)
XBootScale = cb.utils.scale(XBootLog, method='auto')
XBootKnn = cb.utils.knnimpute(XBootScale, k=3)
YPredBoot = model.train(XBootKnn, Y)
# Build Boostrap Models
bootmodel = cb.bootstrap.Per(model, bootnum=100)
bootmodel.run()
# Boostrap Evaluate Model (include Test Dataset)
bootmodel.evaluate(trainset=EvalTrain, testset=EvalTest)
home = 'results/'
file = 'PCLR_MTBL136.xlsx'
bootmodel.save_results(home + file)