{ "cells": [ { "cell_type": "markdown", "metadata": { "toc-hr-collapsed": false }, "source": [ "
The study used in this tutorial has been previously published by Chan et al. (2016), and the deconvolved and annotated data file deposited at the Metabolomics Workbench data repository (Study ID: ST001047). The data can be accessed directly via its project DOI: 10.21228/M8B10B. 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 ST001047.xlsx used in this workflow before proceeding.
\n", "\n", "This is a urine NMR data set consisting of 149 named metabolites. The primary outcome for this paper was the urine was Gastric Cancer (GC; n=43) v Benign Tumor (BN; n=40) v Healthy Control (HE; n=40). For the purposes of this publication we compare only the GC vs HE samples in a binary discrimiant analysis.
\n", "\n", "\n", "This Jupyter Notebook implements the complete workflow for creating, optimising, and evaluating a random forest (RF) model. RF was implemented using Random Forest Classifier from scikit-learn.
\n", " \n", " Please refer to the 'cimcb' package documentation for further details regarding this specific implementation: https://cimcb.github.io/cimcbmax_depth
: the maximum depth allowed for each tree (default = None)min_samples_leaf
: the minimum number of samples required at each leaf node after a split in a tree. This value can either be an integer or a fraction of the total number of samples (default = 1)n_estimators
: the number of trees in the forest (default = 100)max_features
: the number of features considered at each split in a tree (default = square-root(n_features))criterion
: the function used to measure the quality of the split in a tree. This is 'gini' for Gini impurity or 'entropy' for information gain (default = 'gini')min_samples_split
: the minimum number of samples required for a split in a tree. This value can either be an integer or a fraction of the total number of samples (default = 2)max_leaf_nodes
: the maximum number of leaf nodes in a tree (default = None)Preliminary analysis indicated that varying hyperparameters n_estimators
, max_features
, min_samples_split
, and max_leaf_nodes
had little impact on performance (low sensitivity) for the metabolomics data sets used in this study, thus they were kept constant at their default values. Therefore, hyperparameter optimisation was reduced to a grid search across depth = [1,2,3,4,5,6,7,8,9,10]
and min_samples_leaf = [0.01,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5]
.
numpy
, pandas
, and cimcb
).\n",
"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).\n",
" cb.cross_val.KFold()
to carry out 5-fold cross-validation of a set of RF models configured with different numbers of maximum depths (1 to 10) and minimum sample leaf as a fraction (0 to 0.5). This helper function is generally applicable, and the values being passed to it are: \n",
" cb.model.RF
.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 predictive ability of the multiple models across the hyperparameter grid search (sample leaf
vs. max depth
) are displayed in the form of heatmaps representing the parametric performance values $R^2$, $Q^2$ and $|R^2 - Q^2|$. These heatmaps are interactively linked to a scatter plot of $|R^2 - Q^2|$ vs. $Q^2$ and line plots of $R^2$ & $Q^2$ vs sample leaf
and max depth
. If the function cv.plot(metric='auc')
is run the predictive ability of the models 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 hyperparameter values.cb.model.RF()
to building a RF 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).\n",
" 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).\n",
"