PNA
¹H qNMR and Chemometrics of Heirloom Apple Juices and Ciders · PNA-WP-CIDER v3.0

13. Chemometrics: Classification

13.1 Questions, labels and validation

Supervised classification answers a different question from the preceding section: not whether groups exist, but whether a property assigned to the ciders beforehand can be recognized from the spectrum. Nine properties were examined, and they are of three kinds. Four are defined by the composition itself: whether a cider is dry or not (below or above 18 g/L of sugar), its dryness class on the four-level scale, its dominant acid, and the presence of sucrose at 2 g/L or more. These labels were derived from the same spectra that serve as predictors, so that a successful classification proves only that the bucket table contains the information that the targeted analysis extracted from it. Three labels are external to the measurement: the market segment, as a contrast between mass-market and regional craft ciders and as a three-way division that adds the traditional European ciders; the producer, for the four producers represented by eight or more ciders; and the declaration of an adjunct such as fruit, spice, hops or barrel aging. The ninth label, the measurement campaign, is not a property of the cider at all and was included as a control.

The validation was designed to prevent the two errors that most often inflate reported accuracies.204,220,223,240 Hyperparameters were chosen in an inner cross-validation, so that the performance estimate is not biased by their selection, and the folds were formed by producer, so that a model is never tested on ciders of a producer from which it has learned. For the producer label itself the folds were formed by product, which keeps the lots of one product together. Performance is expressed as balanced accuracy, the mean of the recalls of the classes, for which chance corresponds to 0.5 with two classes, 0.33 with three and 0.25 with four. Because the best of six classifiers is reported for each label, the significance was assessed by a permutation test that repeats the entire procedure, including the tuning of all six classifiers and the selection of the best, on data whose labels had been shuffled among the producers. When k of the 100 permutations reach the observed value, the p-value is (k + 1)/101, so that its smallest attainable value is 0.01.

Supervised classification of the ciders from optimized buckets
Figure 43. Supervised classification of the ciders from optimized buckets. (a) Freeze-dried tubes and (b) native tubes: balanced accuracy of six classifiers for nine labels in nested cross-validation grouped by producer (by product for the producer label). The vertical bar marks chance; the gray band extends from chance to the 95th percentile of the best-of-six balanced accuracy obtained with permuted labels. (c) to (f) Confusion matrices (percent of each true class, rows) for the dryness class with a random forest and with linear discriminant analysis, for the three market segments and for the four producers.

Table 18. Classification of the ciders from optimized buckets: best balanced accuracy among six classifiers in nested, grouped cross-validation (five repetitions), and the permutation test of the best-of-six statistic (one repetition; 100 permutations of the labels among the cross-validation groups).

LabelTubenClassesChanceBest balanced accuracy (model)Statistic testedPermuted: mean95th percentilep
Dry / not dry (18 g/L)freeze-dried8720.500.96 (PLS-DA)0.960.570.680.01
native9620.500.98 (PLS-DA)0.970.560.710.01
Four dryness classesfreeze-dried8740.250.88 (RF)0.880.290.400.01
native9640.250.85 (RF)0.810.280.360.01
Dominant acidfreeze-dried8720.500.98 (RF)0.970.580.720.01
native9620.500.98 (SVM-linear)0.970.570.680.01
Sucrose ≥ 2 g/Lfreeze-dried8720.500.80 (RF)0.820.560.720.01
native9620.500.93 (SVM-linear)0.920.560.740.01
Mass-market / regional craftfreeze-dried7320.500.84 (SVM-RBF)0.830.570.720.01
native8220.500.79 (SVM-RBF)0.820.590.730.01
Mass-market / regional craft, 2013–2015 onlyfreeze-dried4120.500.89 (SVM-linear)0.890.570.740.01
native4720.500.91 (SVM-RBF)0.910.560.720.01
Three market segmentsfreeze-dried8030.330.59 (PLS-DA)0.570.390.520.03
native8930.330.49 (LDA)0.530.400.560.08
Producer (four producers)freeze-dried3640.250.78 (SVM-linear)0.840.290.410.01
native4040.250.80 (PLS-DA)0.810.290.430.01
Declared adjunctfreeze-dried8720.500.66 (RF)0.660.560.710.13
native9620.500.57 (PLS-DA)0.580.570.700.43
Measurement campaign (2013–2015 / later)freeze-dried8720.500.60 (RF)0.610.570.710.29
native9620.500.78 (RF)0.790.580.730.02

The statistic tested is the highest balanced accuracy among the six classifiers in a single repetition of the nested cross-validation, which is the quantity that the permutation null reproduces; it differs slightly from the five-repetition mean of the best model. p = 0.01 is the smallest value attainable with 100 permutations.

13.2 Labels defined by the composition

Before the individual results are considered, the null distributions of Table 18 deserve a remark, because they calibrate everything that follows. For a label with two classes, the best of six classifiers reaches a balanced accuracy of 0.56 to 0.59 on average when the labels carry no information at all, and 0.68 to 0.74 in one of twenty such trials. Chance is therefore not 0.5 for the quantity that is usually reported, the performance of the best model tried, and with samples of this number no two-class result below about 0.72 can be told apart from the outcome of choosing among models.203 The gray bands of Figure 43 mark this limit.

The two-class labels that follow from the composition are recognized almost without error. Whether a cider contains less or more than 18 g/L of sugar is predicted with a balanced accuracy of 0.96 from the freeze-dried and 0.98 from the native buckets, and the dominant acid with 0.98 from either. Every classifier except the nearest-neighbor method exceeds 0.9 for both labels in both tubes; the nearest-neighbor method is distinctly weaker (0.86 to 0.93). The presence of sucrose is recognized less reliably from the freeze-dried buckets (0.80) than from the native ones (0.93). At least part of this difference has a simple cause. In five of the 21 ciders that carry the positive label, sucrose had been partly inverted in the acidified freeze-dried sample and the reported value was taken from the native tube (Section 5.7); for three of them, the lots of Stormalong Red Skies at Night, the freeze-dried spectrum shows less than the limit of 2 g/L, so that the label no longer describes the spectrum from which it is to be predicted.

The four dryness classes are more instructive, because here the classifiers differ widely (Figure 43c, d). The random forest reaches 0.88 and 0.85, linear discriminant analysis 0.83 and 0.77, and PLS discriminant analysis only 0.56 and 0.53, little more than half of what it achieves for the two-class version of the same question. The explanation lies in the nature of the label and not in the data. The classes are consecutive intervals of one quantity, the sugar content. A decision tree divides its variables at thresholds and is the natural model for such a label, whereas PLS discriminant analysis fits one linear response to each class and assigns the class with the largest response;220 the response of an interior class, which is low on both sides of an interval and high within it, cannot be approximated by a linear function, and the interior classes are masked by their neighbors. All errors of the random forest are confusions of adjacent classes, and the semi-dry class, with twelve ciders the smallest, is the least well recognized (67 %).

A better model than any of the six is available without further effort. When the limits of the classes are applied to the sugar content predicted by the PLS regression of Section 14, in cross-validation, one of the 96 ciders is assigned to a wrong class from the native buckets and one of 87 from the freeze-dried buckets, a balanced accuracy of 0.98 in both cases. An ordered class that is defined by limits on a measurable quantity should be predicted by regression of that quantity. The present data give no support for the common practice of applying discriminant analysis to labels of this kind.

These results are to be read for what they are. The four labels are functions of concentrations that were themselves determined from the spectra, and a model that reproduces them shows that the bucket table retains the information. The same accuracies are obtained from the table of fitted concentrations (dry or not dry 0.97 and 0.97, dominant acid 0.98 and 0.98), as they must be. The practical content of the result is this: a cider can be assigned to its dryness class and its acid type from a bucket table alone, without line fitting and without a library of reference spectra.

13.3 Labels external to the measurement

The labels of the second kind are the ones on which a classification can produce new information, and the results are correspondingly less uniform. Mass-market and regional craft ciders are distinguished with a balanced accuracy of 0.84 from the freeze-dried and 0.79 from the native buckets. The statistic tested by permutation, 0.83 and 0.82, lies above the 95th percentile of the permuted labels (0.72 and 0.73) and was not reached by any of the 100 permutations (p = 0.01). Four of the classifiers agree within a few hundredths; the random forest fails (0.58 and 0.54), because with 19 ciders against 63 it assigns nearly everything to the larger class. The distinction has an evident chemical basis (Section 8): the mass-market ciders of this study contain twice as much residual sugar in the median as the craft ciders, ten of the nineteen are sweetened mainly with sucrose, only one has undergone malolactic conversion, and their content of succinic acid is about half that of the craft ciders.

An objection must be met before this result is accepted. Eighteen of the nineteen mass-market ciders were measured in the first campaign, whereas the craft ciders are spread over all four, and a model could in principle have learned the campaign instead of the product. The classification was therefore repeated with the ciders of the first campaign alone, 18 mass-market against 23 (freeze-dried) or 29 (native) craft ciders. The balanced accuracy was no lower, 0.89 and 0.91, again with p = 0.01. The distinction is thus recognized within a single campaign and cannot be an effect of the measurement. Since the folds were formed by producer, each of the eight mass-market brands was judged by a model that had not seen it, which speaks for a compositional character common to these products. The statement is nevertheless one about nineteen products of eight brands, nearly all of them purchased before 2016, and not about the category as it stands today.

The extension to three classes by the addition of the traditional European ciders was not successful. The balanced accuracy is 0.59 from the freeze-dried buckets (p = 0.03) and 0.49 from the native ones (p = 0.08), and the European ciders themselves are recognized in about one case in ten; they are assigned to the craft or the mass-market class in nearly equal proportions (Figure 43e). Seven ciders of six producers from three countries do not form a class in the dimensions that dominate the spectra. They range from the completely dry, lactate-dominant ciders of Etienne Dupont and Sarasola to the sweetened, malate-dominant English ciders, one of which contains 15 g/L of sucrose. The property that the targeted analysis found to be common to them, the high content of polyphenols (Section 8.6), occupies a few buckets of low intensity to which Pareto scaling gives little weight. The failure says more about the smallness and heterogeneity of the class than about the method.

The four producers represented by eight or more ciders, Angry Orchard, Downeast, Graft and Stormalong, are recognized with a balanced accuracy of 0.78 and 0.80 against a chance level of 0.25 (p = 0.01; Figure 43f). Each has a house style that is plain from the composition: sweetening with sucrose, unfiltered ciders with the sugars of the juice, sour ciders with lactic and acetic acids and 1,3-propanediol, and dry to semi-sweet, malate-dominant ciders of heirloom apples. It must be added that producer and campaign are almost completely confounded in this group. All fourteen Stormalong ciders were measured in 2020, the Graft ciders in 2017 and after 2022, and seven of the eight Angry Orchard ciders before 2016. For the native tubes, which carry a signature of the campaign (Section 13.4), it cannot be decided how much of the recognition is due to the cider; for the freeze-dried tubes, in which no such signature was detectable, the compositional reading is the more probable. In either case a model of four producers has no use beyond these four, since a cider of any other producer would be assigned to one of them.

The declaration of an adjunct is not recognized. The best balanced accuracies, 0.66 and 0.57, lie within the range obtained with permuted labels (p = 0.13 and 0.43). This label also shows how much the design of a permutation test matters. When the labels are permuted freely among the samples instead of among the producers, the result for the freeze-dried tubes appears significant (p = 0.02), because the null distribution becomes narrower (95th percentile 0.64 instead of 0.71). Adjuncts are a habit of producers: only 14 of the 36 producers are represented in both classes, and a free permutation destroys this association, which the grouped cross-validation respects. It tests against a hypothesis that is too easy to reject. The grouped permutation is the appropriate one, and by it nothing has been shown. The outcome is not surprising. The label covers fruit, hops, spices, honey and barrel aging, which have no spectral feature in common, and several of them contribute nothing that a ¹H spectrum at 300 MHz would register. Individual adjuncts are detected reliably by their own markers, citric and quinic acids for berries and citrus fruit and sorbitol for pear (Sections 8.4 and 8.5), and a targeted marker is the proper tool for this question.

13.4 The measurement campaign as a control

The campaign in which a cider was measured is not a property of the cider. A classifier that predicts it has found either a difference between the products that happened to be measured in different years or a difference in the measurement itself, and in the second case every other model built on the same data is open to doubt. The label was therefore included as a control, in the form of a contrast between the first campaign and the three later ones. From the freeze-dried buckets the campaign is not predicted: the best balanced accuracy is 0.60 (p = 0.29). From the native buckets it is. The random forest reaches 0.78, and the value of 0.79 obtained in the repetition used for the permutation test was reached by one of the 100 permutations (p = 0.02). PLS discriminant analysis reaches 0.66, and the remaining classifiers do not exceed 0.53.

The origin of this signal was located by removing and isolating parts of the spectrum (Figure 44). When the 24 buckets between the excluded water region and the excluded region of the standard, from 5.12 to 6.05 ppm, are removed from the native table, the random forest falls to 0.63 and PLS discriminant analysis to 0.52. Conversely, the fifteen buckets between 5.45 and 6.05 ppm alone, a range that excludes the anomeric signals of sucrose and glucose, predict the campaign with 0.79, 0.74 and 0.71 (random forest, support vector machine, PLS discriminant analysis), as well as the whole table and now with every classifier. Hardly any constituent of cider resonates in this range. The only assigned signal is the H-2 doublet of sorbic acid, and when the test is restricted to the 72 ciders in which no sorbic acid was found the result is unchanged (0.80, 0.73 and 0.71). What differs is the level of the region itself. Its median is 14 mmol of protons per liter per ppm in these ciders of the first campaign, with an interquartile range from 0.3 to 34, against 4 to 7 in the later campaigns; in the freeze-dried tubes, whose residual water signal is small, the quartiles lie between −0.3 and 1.9 in every campaign.

Location of the campaign signature
Figure 44. Location of the campaign signature. Balanced accuracy of three classifiers for the measurement campaign and for three properties of the ciders, from all optimized buckets, from the table without the buckets between 5.12 and 6.05 ppm, and from the fifteen buckets between 5.45 and 6.05 ppm alone; (a) native tubes, (b) freeze-dried tubes. Bars extend from chance to the best of the three classifiers.

The signature is therefore the flank of the residual water signal. Its height and shape depend on the efficiency of the water suppression, on the homogeneity of the field, and on the spectral width and filter settings, which differed between the campaigns, as in part did the spectrometer (Table 8). It is an instrumental feature, and it is the same broad contribution that appeared in the third principal component of the native tubes (Section 12.1). A random forest finds it readily because a single bucket with a characteristic level suffices for a tree, while methods that weigh all buckets together are dominated by the far larger variance of ethanol and sugar.

The consequences are limited. The chemical labels do not depend on the region: without it, dry and not dry are distinguished with 0.98, the dominant acid with 0.97 and the two market segments with 0.80, the same values as with it, and the contrast of the market segments has been shown above to hold within one campaign. In the freeze-dried tubes the same fifteen buckets do carry chemical information, since they predict dryness with 0.85, presumably through the wings of the anomeric signals of the concentrated sugars and through sorbic acid, which accompanies residual sugar; there the region is harmless and should be kept. For native tubes it is recommended that the range between the water signal and 6.05 ppm be excluded from bucket tables intended for classification, at the cost of the olefinic signals of sorbic acid, whose methyl doublet remains, and that the prediction of the campaign, or of whatever variable describes the history of the measurement, be made a routine control. A model built on native spectra is verified anew before it is applied to data from another instrument or another set of acquisition parameters.