In multi-label classification, each observation can be associated with several labels at once. CCNR fits a classifier chain network, in which every label receives the predictions of the labels before it in the chain, and the parameters of all labels are estimated jointly. This vignette walks through a complete analysis: fitting a model, choosing the label order, selecting the tuning parameters by cross-validation, evaluating predictions on held-out data, and inspecting the estimated label interdependencies. The method itself is described in Touw and Van de Velden (2025).
The amphibians data set records the presence of seven
amphibian species at 189 water reservoirs in Poland. The site
characteristics are summarized by 25 principal components, which serve
as the explanatory variables.
library(CCNR)
data(amphibians)
X <- amphibians$X
Y <- amphibians$Y
dim(X)
#> [1] 189 25
round(colMeans(Y), 2)
#> GreenFrog BrownFrog Toad FireToad TreeFrog Newt CrestNewt
#> 0.57 0.78 0.66 0.31 0.38 0.31 0.11The species differ considerably in prevalence. The great crested newt
(CrestNewt) is present at only 11% of the sites.
To evaluate the final model on data it has not seen, 140 sites are used for training and the remaining 49 are set aside.
ccn takes the explanatory variables x, the
binary label matrix y, and two tuning parameters:
q, which controls how the losses of the labels are
aggregated, and the ridge penalty lambda.
model <- ccn(X_train, Y_train, q = 1.5, lambda = 0.5)
model
#> Classifier Chain Network
#> ------------------------
#> Labels (L) : 7
#> Predictors (m): 25
#> Observations : 140
#> Parameters : 203
#>
#> Hyperparameters:
#> q : 1.5
#> lambda : 0.5
#>
#> Training loss : 0.49864
#> Labels : GreenFrog, BrownFrog, Toad, FireToad, TreeFrog, Newt, CrestNewt
#> Label order : 1 2 3 4 5 6 7summary prints the estimated intercepts b,
the coefficients W of the explanatory variables, and the
coefficients C of the label predictions. The coefficients
of the principal components are of little interest here, so
W is left out.
summary(model, which = c("b", "C"))
#> Classifier Chain Network - Summary
#> ----------------------------------
#> Labels (L) : 7
#> Predictors (m): 25
#> Observations : 140
#> Parameters : 203
#>
#> Hyperparameters:
#> q : 1.5
#> lambda : 0.5
#>
#> Training loss : 0.49864
#> Label order : 1 2 3 4 5 6 7
#> Deterministic starts : 1
#> Random starts : 0
#>
#> Intercepts (b):
#> GreenFrog BrownFrog Toad FireToad TreeFrog Newt CrestNewt
#> 0.3445 1.1565 0.4914 -1.0156 -0.6863 -1.1394 -2.3949
#>
#> Coefficients of the label predictions (C):
#> GreenFrog BrownFrog Toad FireToad TreeFrog Newt
#> BrownFrog 0.1112
#> Toad 0.1245 0.0215
#> FireToad 0.1840 0.0930 0.0278
#> TreeFrog 0.0969 0.1152 0.0745 0.0549
#> Newt 0.1550 0.0760 0.1085 0.0814 0.0699
#> CrestNewt 0.1355 0.0647 0.0958 0.0306 0.0585 0.0601The same estimates are returned as a list by coef. The
methods fitted and residuals are also
available.
The order of the labels in the chain determines which labels can
inform which. By default, the column order of y is used.
entropy_label_order derives an order from the labels
themselves.
colnames(Y_train)[entropy_label_order(Y_train, method = "cebcc1")]
#> [1] "CrestNewt" "BrownFrog" "FireToad" "Toad" "GreenFrog" "TreeFrog"
#> [7] "Newt"The label_order argument of ccn accepts
either such a permutation or a function of x and
y that returns one. A function is evaluated on the data
that are actually used for fitting, which matters during
cross-validation: the order is then recomputed on every set of training
folds.
With rare labels, a random split into folds can leave a label without
any positive examples in some fold.
multilabel_stratified_folds assigns the observations to
folds such that the proportion of every label stays close to its
proportion in the full data.
fold_id <- multilabel_stratified_folds(Y_train, folds = 5, seed = 1)
round(t(sapply(1:5, function(k) colMeans(Y_train[fold_id == k, ]))), 2)
#> GreenFrog BrownFrog Toad FireToad TreeFrog Newt CrestNewt
#> [1,] 0.57 0.79 0.68 0.29 0.39 0.29 0.07
#> [2,] 0.57 0.79 0.68 0.32 0.39 0.29 0.07
#> [3,] 0.57 0.79 0.64 0.29 0.36 0.29 0.11
#> [4,] 0.57 0.75 0.64 0.29 0.36 0.29 0.11
#> [5,] 0.61 0.79 0.64 0.29 0.39 0.32 0.11ccn_cv evaluates every combination of the candidate
values for q, lambda, and
label_order. Candidate label orders are given as a named
list, in which NULL stands for the column order of
y. By default, combinations are scored with the Hamming
accuracy.
cv <- ccn_cv(X_train, Y_train, q = c(1, 2), lambda = c(0.1, 0.5, 1),
label_order = list(default = NULL, cebcc1 = cebcc1_order),
fold_assignments = fold_id)
cv$best_params
#> q lambda label_order
#> 5 1 1 defaultThe selected model, refitted on all training data, is available as
cv$best_model. The methods predict,
coef, fitted, and residuals can
also be applied to cv directly.
predict returns predicted labels by default and
predicted probabilities for type = "response". Labels are
obtained by comparing the probabilities with a threshold of 0.5. A
vector of thresholds, one per label, can be supplied instead. Setting
each threshold to the prevalence of the label in the training data helps
to detect rare labels.
Y_prob <- predict(cv, newdata = X_test, type = "response")
Y_class <- predict(cv, newdata = X_test)
Y_class_prev <- predict(cv, newdata = X_test, threshold = colMeans(Y_train))The package provides scoring functions for multi-label predictions. Those based on predicted labels depend on the threshold, while the log-likelihood and the mean area under the ROC curve use the probabilities.
predictions <- list(threshold_0.5 = Y_class, prevalence = Y_class_prev)
round(sapply(predictions, function(p) c(
hamming = ccn_hamming_accuracy(Y_test, p),
exact_match = ccn_exact_match(Y_test, p),
micro_f1 = ccn_micro_f1(Y_test, p),
macro_f1 = ccn_macro_f1(Y_test, p))), 3)
#> threshold_0.5 prevalence
#> hamming 0.682 0.647
#> exact_match 0.041 0.061
#> micro_f1 0.623 0.621
#> macro_f1 0.462 0.597
ccn_log_likelihood(Y_test, Y_prob)
#> [1] -0.5744917
ccn_mean_auc(Y_test, Y_prob)
#> [1] 0.6989606Any of these, or a function of your own, can be passed to the
scoring argument of ccn_cv. A scoring function
receives the arguments y_true, y_class, and
y_prob and returns a single number, where larger is
better.
The matrix C contains the estimated effect of the
prediction for one label on the prediction for another. Rows correspond
to the predicted label and columns to the labels that precede it in the
chain, so the matrix is lower triangular.
round(coef(cv)$C, 3)
#> GreenFrog BrownFrog Toad FireToad TreeFrog Newt CrestNewt
#> GreenFrog NA NA NA NA NA NA NA
#> BrownFrog 0.084 NA NA NA NA NA NA
#> Toad 0.119 0.027 NA NA NA NA NA
#> FireToad 0.121 0.069 0.039 NA NA NA NA
#> TreeFrog 0.075 0.085 0.080 0.054 NA NA NA
#> Newt 0.137 0.091 0.086 0.072 0.064 NA NA
#> CrestNewt 0.104 0.052 0.083 0.044 0.069 0.074 NAA positive entry means that a higher predicted presence of the
earlier species increases the predicted presence of the later one, after
accounting for the site characteristics. Because C is
penalized together with W, the size of the entries depends
on lambda.
The help pages of the functions used here contain further options and
examples. generate_dataset creates a small synthetic data
set that is convenient for experimenting with the package.
Touw, D. J. W. and Van de Velden, M. (2025). Classifier chain networks for multi-label classification. Expert Systems with Applications, 286, 128048. https://doi.org/10.1016/j.eswa.2025.128048