Authors: Marvin N. Wright, Lukas Burk, Pegah Golchian, Jan Kapar, Niklas Koenen, Sophie Hanna Langbein
Organizations: Leibniz Institute for Prevention Research and Epidemiology - BIPS, Bremen, Germany · University of Bremen, Germany · University of Copenhagen, Denmark
In the age of digital epidemiology, epidemiologists are faced by an increasing amount of data of growing complexity and dimensionality. Machine learning is a set of powerful tools that can help to analyze such enormous amounts of data. This chapter lays the methodological foundations for successfully applying machine learning in epidemiology. It covers the principles of supervised and unsupervised learning and discusses the most important machine learning methods. Strategies for model evaluation and hyperparameter optimization are developed and interpretable machine learning is introduced. All these theoretical parts are accompanied by code examples in R, where an example dataset on heart disease is used throughout the chapter.
Figures & tables
Figure 1: (a) Decision tree of the heart disease dataset using only the features ST_depression and serum_cholesterol . (b) The corresponding partition plot. The points are the instances/patients, and the shaded areas denote the model predictions.
Figure 2: A decision tree fitted on the heart disease dataset, visualized with the rpart.plot package. It predicts whether heart disease is present or absent in a patient, based on the features in the dataset, e.g., the results of a thallium stress test ( thal ). Each node contains the following information: 1) absent/present prediction, 2) proportion of patients with present heart disease, 3) size of the node as percentage of total sample size. The splitting criteria are denoted below the nodes. If the criteria apply, we follow the left path, otherwise the right path.
Figure 3: A visual representation of the cross-validation results of the cost-complexity pruning. The x -axis shows the complexity parameter cp . At the top, the number of leaf nodes corresponding to each complexity parameter is denoted. The y -axis represents a prediction error measure, based on cross-validation. The lowest error is reached at cp of 0.031. Figure created with the rpart package and slightly modified.
Figure 4: Model architecture of a sequential neural network with seven dense layers, generating predictions y^ from the input x . On the right, the model’s fourth layer is magnified, mapping the previous layer’s output x(4) to the next layer’s input x(5) through an affine transformation followed by a non-linear function, denoted as σ4 .
Figure 5: (a) The graphs of typical activation functions: hyperbolic tangent (green), rectified linear unit (ReLU) (orange), and logistic function (blue) used for probability outcomes. (b) Illustration of the gradient descent technique for learning the optimum θ^ by iteratively updating the current parameter θt for −η∇JD(θt) units based on the negated tangent’s slope at θt on the loss function and the learning rate η .
Figure 6: A confusion matrix showing the categorization of true and predicted class labels into true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN). Common statistics derived from these values are the positive predictive value (PPV) and the negative predictive value (NPV), the accuracy (ACC) and its complement, the classification error (CE), as well as the true positive rate (TPR) and true negative rate (TNR).
Figure 7: ROC curve (a), precision-recall curve (b), and threshold curve (c) of a random forest classifier applied to the heart disease task as generated by mlr3 . Depending on the task, each visualization can provide valuable insight into the model at hand and provide necessary context to the model evaluation process.
Figure 8: The bias-variance trade-off characterizes the common phenomenon of the prediction error decreasing with higher model complexity up to the point at which it begins to increase again. This is based on the fact that low model complexity typically implies a high bias, whereas high complexity tends to imply higher variance.
Figure 9: 3-fold cross-validation splits the data into three disjoint sets. In each iteration, two thirds of the data serve as training set while the remaining fold serves as the test set for performance estimation. The results of the folds are averaged for a final performance estimate. Reprinted with permission from Bischl et al. (2024, Chap.3) .
Figure 10: Illustration of (a) grid search and (b) random search for tuning two hyperparameters. The hyperparameters lambda and max_depth are tuned within the search space lambda ∈(0,1) and max_depth ∈(0,20) . The dots with black outline are the respective ( max_depth , lambda ) combinations for which the xgboost model is evaluated in every iteration of the (a) grid search or (b) random search algorithm. In (a), combinations are selected on an equally spaced grid within the borders of the search space. In (b), the combinations are selected randomly within the borders of the search space. A high cross-validation classification accuracy (classif.acc) is represented by yellow color coding, darker colors indicate points with lower accuracy.
Figure 11: Illustration of the process of nested resampling. The larger blocks signify the use of 3-fold CV for evaluating the models in the outer resampling process, while the smaller blocks depict 4-fold CV for the inner resampling used in hyperparameter optimization. The training sets are represented by light blue blocks, whereas the test sets are shown by dark blue blocks. The thin blue arrows point to each step of the outer resampling process, in which a different portion of the data is used as a test set. Reprinted with permission from Bischl et al. (2024, Chap.4) .
Figure 12: Overview of the most popular post-hoc interpretable machine learning methods.
Figure 13: Illustration of the local model-agnostic IML methods ICE and Shapley values on the heart disease dataset: In (a), the left plot shows the ICE lines for the feature resting_blood_pressure and all test instances, while the right one displays the centered ICE plots at a blood pressure of 94. Additionally, the red highlighted line represents the first patient in the test data. In (b), the Shapley values of all features for the first patient are depicted.
Figure 14: Illustration of the global model-agnostic IML methods PFI, PDP and ALE plots. All three methods are calculated on a test dataset based on a random forest classifier trained on the heart disease dataset. Plot (a) highlights the ordered relative PFI computed using the classification error (CE). The PDPs in (b) show how age (left plot) and thal (right plot) affect the average model predictions while holding all other features constant. In (c) ALE plots describe how changes in age (left plot) and thal (right plot) affect the average model predictions while taking into account the effects of other features.
Figure 15: Basic approach of feature attribution methods.
Figure 16: Visual comparison of different methods for generative modeling. Action-counteraction pairs are colored in green and blue, respectively, latent space representations in red.
normal, ST-T wave abnormality 3 3 3 T wave inversions and/or ST elevation or depression of >0.05 mV , showing probable or definite left ventricular hypertrophy by Estes’ criteria
Table 1: Description and values of the features in the heart disease dataset. Divided into categorical (above) and numerical (below) features.
School of Computer Science, University of Guelph, Ontario, Canada · St. Joseph’s Healthcare Hamilton, Hamilton, Ontario, Canada · The Research Institute of St. Joe’s Hamilton, Hamilton, Ontario, Canada +1