1  Introduction

1.1 Why this book exists

Machine learning is a set of methods for building functions that map measurements to predictions, and a set of procedures for estimating how well those functions perform on cases that were not used to build them. Most introductions organize the subject by algorithm. This book organizes it by the order in which the decisions of an analysis depend on one another: the question determines the data and the outcome; the outcome determines how performance is measured; the measure of performance and the resampling design determine how algorithms are compared; and only then does the choice among algorithms become meaningful.

Each topic is presented twice. A concept chapter introduces the idea in plain language, with the mathematics needed to state it precisely, references to the corresponding chapters of Applied Math for Data Science (AMDS), the seminal papers, and an encyclopedic entry for further orientation. An applied chapter then carries out the same idea on real data, using the rtemis R package for modeling and rtemis.draw for visualization.

1.2 Questions

Data analyses answer three kinds of question (Hernán et al. 2019):

  • Descriptive questions ask what is the case in a population: the proportion of patients admitted for heart failure who die within 60 days, or the average difference in serum creatinine between survivors and non-survivors.
  • Predictive questions ask what the outcome will be for a new individual, given measurements available at the time of prediction: whether a patient admitted today will die within 60 days, given age, ejection fraction and laboratory values.
  • Causal questions ask what the outcome would be under an intervention: whether lowering serum creatinine would change a patient’s risk of death.

The three kinds use overlapping methods, often the same regression model, but they estimate different quantities, generalize in different ways, and are judged by different criteria. This book addresses predictive questions. Chapter 2 develops the distinction, and in particular the difference between generalizing from a sample to a population, which is the concern of descriptive and inferential statistics, and generalizing from a set of cases to new cases, which is the concern of predictive modeling.

1.3 A model answers a specific question

A predictive analysis begins with a written statement of the question. The statement specifies four elements:

  1. Outcome. What is predicted, defined so that its value is unambiguous for every case, including the time horizon over which it is observed.
  2. Population. The individuals for whom predictions are made, defined by inclusion criteria that can be applied to a new case.
  3. Features. The measurements used to make the prediction. Each feature must be available at the moment the prediction is made.
  4. Use. The decision or action the prediction informs, which determines which errors matter and therefore how performance is measured.

A trained model is an answer to exactly one such question. A change to any of the four elements defines a different question. A model that predicts death within 60 days does not predict death within one year; a model trained on patients with severe systolic dysfunction does not describe patients with preserved ejection fraction; a model that uses a measurement recorded after the prediction time cannot be applied at that time, whatever its accuracy on the training data. Writing the question down before analysis makes each of these constraints explicit, and it makes departures from them detectable.

The heart failure data used throughout this book illustrate the point. The published outcome variable records whether a patient died during follow-up, but follow-up periods range from 4 to 285 days. A patient recorded as alive after 12 days of follow-up and a patient recorded as alive after 280 days contribute the same outcome value to a model, although their outcomes refer to different periods. Chapter 7 restates the outcome with a fixed horizon, death within 60 days of baseline, which every case either has or has not experienced, and excludes the cases whose status at 60 days is unknown. The question used throughout the book is:

Among patients aged 40 years or older admitted with heart failure due to left ventricular systolic dysfunction (NYHA class III or IV), will the patient die within 60 days of baseline, given age, sex, comorbidities and laboratory measurements recorded at baseline?

rtemis records the question with the model: train() accepts a question argument, and the text is stored in the fitted object and reported with it.

1.4 Who it is for

The book is written for readers who analyze data and want to build and evaluate predictive models correctly: students in quantitative programs, researchers in the life and social sciences, and data scientists who use R. It assumes working knowledge of R and of introductory statistics. The mathematical background, including linear algebra, calculus, optimization, probability and estimation, is covered in AMDS, which each concept chapter cites at the point where it is needed.

1.5 rtemis and rtemis.draw

The applied chapters use two R packages.

rtemis provides a single interface to supervised learning, preprocessing, resampling, hyperparameter tuning, calibration, conformal prediction, explanation, dimensionality reduction and clustering. Every step of an analysis is configured by a setup_*() function that returns a typed, validated configuration object. train() combines a dataset with these configurations, fits the model, and assesses it:

mod <- train(
  dat,
  hyperparameters = setup_GLMNET(),
  outer_resampling_config = setup_KFold(n_resamples = 10L, seed = 2026L),
  execution_config = setup_SerialExecution(seed = 2026L),
  question = "..."
)

Configuration objects print their settings, so the complete specification of an analysis can be inspected before anything is computed:

setup_KFold(n_resamples = 10L, seed = 2026L)
<KFoldConfig>
 n_resamples: 10
stratify_var: NULL
strat_n_bins: 4
    id_strat: NULL
        seed: 2026

rtemis.draw produces interactive figures that follow the light or dark color scheme of the page. Fitted rtemis objects are plotted with their plot_*() methods, such as plot_true_pred(), plot_roc() and plot_varimp(), which render through rtemis.draw; other figures call rtemis.draw functions such as draw_scatter() and draw_histogram() directly.

Both packages are installed from R-universe:

install.packages(
  c("rtemis", "rtemis.draw"),
  repos = c("https://rtemis-org.r-universe.dev", "https://cloud.r-project.org")
)

rtemis calls established R packages for each algorithm, for example glmnet for elastic net regression and ranger for random forests, and reports any that are missing when a function needs them. Every chapter attaches the packages in the same order, rtemis and then rtemis.draw:

This book was rendered with rtemis 1.4.2 and rtemis.draw 0.5.4 on R version 4.6.1 (2026-06-24).

1.6 Data

Three public medical datasets recur across chapters, so that methods can be compared on the same question. Copies are stored in the data/ directory of the book’s repository, together with their sources and licenses.

Dataset Cases Outcome Use
Heart Failure Clinical Records (Ahmad et al. 2017; Chicco and Jurman 2020) 299 patients Death within 60 days of baseline (derived) Binary classification
Diabetes progression (Efron et al. 2004) 442 patients Disease progression one year after baseline Regression
Parkinson’s Telemonitoring (Tsanas et al. 2010) 5875 recordings from 42 patients Total UPDRS score Regression with repeated measures

The shared setup file _setup.R, sourced at the start of each chapter, defines the functions read_heart_failure(), read_diabetes() and read_parkinsons(). Chapter 7 derives each step of these functions.

1.7 How it is organized

Each topic forms a part of the book with two chapters: the concept and its application in rtemis. The topics are ordered so that each depends only on earlier ones.

  • Framing. Questions and generalization (Chapter 2); supervised learning as a problem of minimizing expected loss on new cases (Chapter 4).
  • Data. Reading, typing and inspecting data, and defining the outcome (Chapter 6).
  • Evaluation. Performance metrics (Chapter 8), resampling (Chapter 10), and preprocessing as part of the fitted model (Chapter 12). These precede every learning algorithm, so that each algorithm is assessed on held-out data from its first example.
  • Linear models and complexity. Generalized linear models (Chapter 14), the bias–variance tradeoff (Chapter 16), regularization (Chapter 18), and hyperparameter tuning (Chapter 20).
  • Flexible models, in order of increasing flexibility: additive models and basis expansions (Chapter 22), nearest neighbors (Chapter 24), support vector machines (Chapter 26), decision trees (Chapter 28), bagging and random forests (Chapter 30), boosting and Bayesian additive regression trees (Chapter 32), neural networks (Chapter 34), and stacking (Chapter 36).
  • From predictions to decisions. Interpretation (Chapter 38), class imbalance (Chapter 40), calibration (Chapter 42), and conformal prediction (Chapter 44).
  • Learning without an outcome. Dimensionality reduction
    1. and clustering (Chapter 48).
  • Practice. Reproducibility and reporting (Chapter 50).

Unsupervised learning appears last because it has no outcome and therefore uses none of the evaluation framework that the rest of the book builds.