This chapter introduces the rtemis interface for supervised learning: the form of the input data, the configuration objects, train(), the fitted model object, and prediction on new cases. It then reproduces the pattern of Figure 4.2 on real data, with training and test error diverging as the model class grows.
5.1 Input data
train() takes a data.table or data.frame in which each row is a case, the outcome is the last column, and every other column is a feature. Features are numeric or factors. The type of the outcome selects the task: a numeric outcome defines regression, and a factor defines classification.
The diabetes data have a numeric outcome, progression, in the last column:
Each algorithm is configured by a setup_*() function, whose arguments are the algorithm’s hyperparameters. available_supervised() lists the algorithms:
available_supervised()
BART: Bayesian Additive Regression Trees (stochtree)
CART: CART decision tree (rpart)
ConditionalSuperLearner: Conditional SuperLearner: an oracle routes each case to one of a library of experts
GAM: GAM (generalized additive model)
GLM: GLM (generalized linear model)
GLMNET: Elastic net (glmnet)
GLMTree: Model-Based Recursive Partitioning: a tree with a GLM in each leaf
HAL: Highly Adaptive Lasso (hal9001)
Isotonic: Isotonic regression
MonotonicHAL: Monotonic Highly Adaptive Lasso (hal9001)
KNN: k-Nearest Neighbors (kknn)
LightCART: Single LightGBM tree (CART mode)
LightGBM: LightGBM gradient boosting
LightRF: LightGBM random forest
LightRuleFit: LightRuleFit (LightGBM rules + GLMNET)
LINAD: Linear Additive Tree
LINADForest: Bagged ensemble of Linear Additive Trees
MARS: Multivariate Adaptive Regression Splines (earth)
MLP: Multilayer perceptron (torch)
ModalityStacking: ModalityStacking: stacked ensemble with one learner per feature group
NNLS: Non-negative least squares (nnls)
Ranger: Ranger random forest
SuperLearner: SuperLearner: cross-validated stacked ensemble
LinearSVM: SVM with linear kernel (e1071)
RadialSVM: SVM with radial kernel (e1071)
SPLS: Sparse Partial Least Squares
TabNet: TabNet neural network
A configuration is an object that can be printed and inspected before training. setup_GLM() configures a generalized linear model, which is a linear regression for a numeric outcome and a logistic regression for a binary outcome (Chapter 14):
setup_GLM()
<GLMHyperparameters> hyperparameters:
ifw: <lgc> FALSE
tunable_hyperparameters: <int> 1
fixed_hyperparameters: <int> 0
tuned: <int> -1
resampled: <int> 0
n_workers: <int> 1
No search values defined for tunable hyperparameters.
setup_SerialExecution() configures how computations run, here on a single process, and sets the random seed that makes the run reproducible.
5.3 Training and testing on a single split
The simplest held-out design divides the cases once into a training set and a test set. resample() with setup_StratSub() draws a random subsample of the cases for training, stratified on the outcome so that its distribution is similar in both sets (Chapter 10). With n_resamples = 1L and train_p = 0.75, it returns one set of training indices containing 75% of the cases:
train() fits the model on the training set and, when dat_test is given, evaluates it on the test set. While it runs, train() reports each step with a timestamp and the name of the function that produced the message:
2026-10-11 12:40:27 Checking data is ready for training... ✔ [check_supervised]
2026-10-11 12:40:27 ▶ [train]2026-10-11 12:40:27 Training set: 331 cases x 10 features. [summarize_supervised]2026-10-11 12:40:27 Test set: 111 cases x 10 features. [summarize_supervised]2026-10-11 12:40:27 //CPU | serial | 1 worker: algorithm 1 thread, tuning 1, outer resampling 1 [msg_resources]2026-10-11 12:40:27 Shared memory (auto): not sharing training data (26.2 kB) -- this run transfers nothing to workers. [report_shared_memory]2026-10-11 12:40:27 Training GLM Regression... [train]2026-10-11 12:40:27 Checking data is ready for training... ✔ [check_supervised]
The printed summary reports regression metrics on the training and test sets: mean absolute error (MAE), mean squared error (MSE), its square root (RMSE), and \(R^2\), the proportion of the outcome’s variance explained (Chapter 8). Subsequent chapters hide the progress messages and show the summary.
5.4 The fitted model
train() returns an object of class Regression or Classification. Its properties are accessed with @:
plot_true_pred() plots observed against predicted outcomes for the training and test sets:
plot_true_pred(mod_glm)
5.5 Classification
A factor outcome defines classification. For a binary outcome, the second factor level is the positive class. In the heart failure data, death has levels "No" and "Yes", so "Yes" is the positive class:
With a threshold of 0.5, the model predicts death for 3 of the 14 test patients who died within 60 days. The threshold is a decision, separate from the model, and the probabilities carry more information than any single threshold. Chapter 8 separates the evaluation of probabilities from the evaluation of class predictions, and Chapter 40 discusses the choice of threshold.
5.6 Overfitting with a growing model class
A linear model with the ten diabetes features has 11 coefficients. Adding all products of pairs of features, the two-way interactions, enlarges the model class to linear functions of 55 terms; adding all products of three features enlarges it to 175 terms. model.matrix() constructs these terms, and the same training and test cases are used for each model:
Training \(R^2\) rises from 0.529 to 0.753 as terms are added. Test \(R^2\) falls from 0.478 to -0.393. \(R^2\) is computed as one minus the ratio of the model’s squared error to the squared error of a constant prediction equal to the mean of the test outcomes, so a negative value means that the model predicts the test cases less accurately than that constant. With 175 terms estimated from 331 training cases, the model fits noise that does not recur in new cases, the pattern of Figure 4.2. plot_true_pred() shows the discrepancy for the three-way interaction model:
plot_true_pred(mods_order[[3]])
A model class can be large and still generalize, if its fitting procedure controls complexity. Chapter 18 fits the same expanded models with penalties on the size of the coefficients.