The k-NN algorithm for compositional data: a revised approach with and without zero values present

In compositional data, an observation is a vector with non-negative components which sum to a constant, typically 1. Data of this type arise in many areas, such as geology, archaeology, biology, economics and political science among others. The goal of this paper is to extend the taxicab metric and a newly suggested metric for compositional data by employing a power transformation. Both metrics are to be used in the k-nearest neighbours algorithm regardless of the presence of zeros. Examples with real data are exhibited.


Introduction
Compositional data are non-negative multivariate data and each vector sums to the same constant, usually 1 for convenience.Compositional data are met in many disciplines, including geology (Aitchison, 1982), economics (Fry et al., 2000), archaeology (Baxter et al., 2005) and political sciences (Rodriques and Lima, 2009).Their sample space is called simplex S d and in mathematical terms is where D denotes the number of components and d = D − 1.
Ever since Aitchison (1982) suggested the use of the log-ratio transformation for compositional data, most of the analyses of such data have been implemented using this transformation.Aitchison (2003) implemented linear discriminant analysis for compositional data using the log-ratio transformation.Over the years though, researchers have suggested alternative ways for supervised classification of compositional data, see for example Gallo (2010) and Neocleous et al. (2011).
An important issue in compositional data is the presence of zeros, which cause problems for the logarithmic transformation.The issue of zero values in some components is not addressed in most papers, but see Neocleous et al. (2011) for an example of discrimination in the presence of zeros.Alternatively, one could use alternative models (see for example Scealy andWelsh, 2011a andStewart andField, 2011) or replace the zero values by making parametric assumptions (Martin et al., 2012).
In this paper we suggest the use of a recently developed metric, for classification of compositional data, when the k-nearest neighbours (k-NN) algorithm is implemented.It is a metric for probability distributions (Endres and Schindelin, 2003;Österreicher and Vajda, 2003) which can be adopted to compositional data as well, since each vector sums to 1.The second metric we suggest is the Manhattan metric, a scaled version of which has already been used for compositional data analysis (Miller, 2002).We will extend both of these metrics by applying a power transformation.We will see that both of these metrics handle zeros naturally and hence they can be used regardless of them being present in some components.This is a very attractive feature of these metrics in contrast to the Aitchisonian metric suggested by Aitchison (2003) which is not applicable when zeros are present in the data.Examples using real data are used to illustrate the performance of these metrics.
Section 2 describes the two metrics, how they can be extended and also presents graphically their loci of points equidistant from the centre of the simplex.Section 3 shows the k-NN algorithm for compositional data and Section 4 contains examples using real data.Finally, section 5 concludes this paper.

Metrics for compositional data
We will present three metrics for compositional data ,two of which have already been examined.But first we will show the power transformation.Aitchison (2003) defined the power transformation to be (1) The value of α will be determined by the estimated accuracy of the k-NN algorithm.

The ES-OV α metric for compositional data
We advocate that as a measure of the distance between two compositions we can use the square root of the Jensen-Shannon divergence where x, w ∈ S d .Endres and Schindelin (2003) and Österreicher and Vajda (2003) proved, independently, that (2) satisfies the triangular identity and thus it is a metric.For this reason we will refer to it as the ES-OV metric.
We will use the power transformation (1) to define a more general metric termed ES-OV α metric

The taxicab α metric for compositional data
The taxicab metric is also known as L 1 (or Manhattan) metric and is defined as We will again employ the power transformation (1) to define a more general metric which we will term the TC α metric 2.3 The Aitchisonian metric for compositional data Aitchison (2003) suggested the Euclidean metric applied to the log-ratio transformed data as a measure of distance between compositions where g (z) = D i=1 z 1/D i stands for the geometric mean.

Some comments
The power transformed compositional vectors still sum to 1 and thus the ES-OV α (3) is still a metric.It becomes clear that when α = 1 we end up with the ES-OV metric (2).If on the other hand α = 0, then the distance is zero, since the compositional vectors become equal to the centre of the simplex.An advantage of the ES-OV α metric (3) over the Aitchisonian metric ( 6) is that the the first one is defined even when zero values are present.In this case the Aitchisonian metric (6) becomes degenerate and thus cannot be used.We have to note that we need to scale the data so that they sum to 1 in the case of the ES-OV metric, but this is not a requirement of the taxicab metric.
Alternative metrics could be used as well, such as 1. the Hellinger metric (Owen, 2001) 2. or the angular metric if we treat compositional data as directional data (for more information about this approach see Stephens (1982) and Scealy andWelsh (2011, 2014)) Aitchison (1992) argued that a simplicial metric should satisfy certain properties.These properties include 1. Scale invariance.The requirement here is that the measure used to define the distance between two compositional data vectors should be scale invariant, in the sense that it makes no difference whether the compositions are represented by proportions or percentages.
2. Subcompositional dominance.To explain this we consider two compositional data vectors and we select sub-vectors from each consisting of the same components.Subcompositional dominance means that the distance between the sub-vectors is always less than or equal to the distance between the original compositional vectors.
3. Perturbation invariance.The requirement here is that the distance between compositional vectors x and w should be the same as distance between x ⊕ 0 p and w ⊕ 0 p, where the operator ⊕ 0 means element-wise multiplication and then division by the sum so that the resulting vectors belong to S d and p is any vector (not necessarily compositional) with positive components.
If all of the above metrics satisfy or not these thee properties should not be a problem.Take for example subcompositional dominance.If someone has a compositional dataset, there has to be a good reason why he would choose to discard some components and form a sub-composition.And even if he does, all the metrics are still applicable.
The message this paper tries to convey is that if someone uses a well defined metric (or even a dissimilarity measure) in order to perform classification he should be fine with that.When dealing with data lying on the Euclidean space, one can use dissimilarity measures as well to perform clustering or discrimination.The question of interest is how can we discriminate the observed groups of points as adequately as possible.

Loci of points equidistant from the centre of the simplex
Figure 1 shows the effect of the power transformation (1) on the data.As expected, the data come closer to the barycentre of the triangle as α tends to zero.The data used and plotted on Figure 1 are the Arctic lake data (Aitchison, 2003).Figures 2 and 3 show the plots of loci of points of the ES-OV α metric (3) and of the TC α metric (5) for different values of α and Figure 4 shows the contour plots of the Aitchisonian metric (6).In all cases, the plots of loci of points refer to the distance from the barycentre of the simplex.The loci of points seen on Figure 2 have similar shape regardless of the value of α.This is not true for the loci in Figure 3, which change as the value of α changes.3 Supervised classification for compositional data using the k-NN algorithm The goal of this paper is to perform supervised classification of compositional data using the k-NN algorithm.For this reason we will use the ES-OV α (3) and TC α (5) metrics and compare their performance and suitability with the Aitchisonian metric metric (6).
The k-NN algorithm is a non-parametric supervised learning technique which is computationally heavier than quadratic and linear discriminant analysis but easier to implement as it relies solely on metrics between points.
Similarly to other supervised classification techniques it requires some parameter tuning.
The two parameters associated with it in our case are the power parameter α and the number of nearest neighbours k.We describe the steps of the k-NN for compositional data in our case.
1. Separate the data into the training and the test dataset.
2. Choose a value of k, the number of nearest neighbours.
3. Classify the test data using either the ES-OV α (3), the TC α (5) for a range of values of α and each time calculate the percentage of correct classification.We can of course use the Aitchisonian metric (6) instead of the ES-OV α (3) or the TC α metric (5).In this case we have to choose the number of nearest neighbours only, since no power transformation is involved.We could of course use any other metric defined in R d .In this case we would have to apply the additive log-ratio transformation (Aitchison, 2003) to the data.The issue in that case though would be the presence of zeros in the data.
In the next section we will see two examples using real data and see the performance of the algorithm when each of the two metrics is used.

Examples using real data
We will now see the performance of the k-NN algorithm using the ES-OV α metric (3), the TC α metric and the Aitchisonian metric (6) with real data.

Example 1. Hydrochemical data with no zero values
The first dataset comes from hydrochemistry.A hydrochemical data set (Otero et al., 2005) contains measurements on 14 elements.the data were gathered within a period of 2 years from 31 stations located along the rivers and main tributaries of the Llobregat river, one of the medium rivers in northeastern Spain.Each of these elements is measured approximately once each month during these 2 years.There are 4 tributaries of interest, Anoia (143 measurements), Cardener (95 measurements), Upper Llobregat (135 measurements) and Lower Llobregat (112 measurements).Thus, there are 485 across all 4 tributaries.This dataset contains no zero values, so all three metrics are applicable.The size of the training sample was equal to 434 and thus the test sample consisted of 51 observations, which were sampled using stratified random sampling each time to ensure that observations from all tributaries are selected every time.Figure 5 shows the heat plot of the estimated percentage for different values of k and α.
If α = 0.5 and k = 2 the estimated percentage percentage of correct classification is equal to 92.78% and when α = 1 and k = 3 the estimated percentage is 89.88% when the ES-OV α    3) with α = 0.5 and α = 1 respectively.The green and the blue lines are based on the TC α metric (5) with α = 0.35 and α = 1 respectively.The turquoise line is the Aitchisonian metric (6).metric (3) was applied.When the TC α metric ( 5) is applied the results are similar, with α = 0.35 and k = 2 the estimated percentage of correct classification is 93.77% and when α = 1 and k = 2, the estimated percentage of correct classification is 86.55%.This is an example where a value of α other than 1 leads to better results.The change in the percentage might seem small, but if we take into account the total sample size, we will see that the 3% of 485 observations is 14 observations and it is not a small number.The Aitchisonian metric on the other hand did not do that well.The maximum estimated percentage was equal to 85.46% when k = 2.
More information (including the specificities and sensitivities for each tributary averaged over all 200 replications) regarding the classification results is presented in Table 1 below.A general conclusion about the mean sensitivities and specificities is that the lower sensitivities are observed when the estimated percentage of correct classification is lower and they have also larger standard errors.The mean specificities on the other hand are in general high and are less affected by the estimated percentage of correct classification.1: Classification results for the hydrochemical data.The number inside the parentheses indicates the standard error of the percentages.

ES-OV
In addition we calculated the ROC curves for each of the three metrics.In order to do this we performed a 1-fold cross validation.That is, we removed an observation and then using the parameters α and k which are given in Table 1 (since they produced the best results) we classified it.This procedure was repeated for all observations.Thus, we ended up with the predicted membership values for all observations based on the 3 metrics.This allowed us to draw the ROC curves for each tributary when all 3 metrics were used.The results are presented in Figure 7.
We can see that for all tributaries the ROC curves of the ES-OV α metric (3) and the TC α metric (5) are similar, whereas the ROC curve of the Aitchisonian metric ( 6) is always the lowest.Example 2. Forensic glass data with zero values

Specificity
In the second example we will use the forensic glass dataset which has 214 observations from 6 different categories of glass with 8 chemical elements, in percentage form.The categories which occur are containers (13 observations), vehicle headlamps (29 observations), tableware (9 observations), vehicle window glass (17 observations), window float glass (70 observations) and window non-float glass (76 observations).This dataset contains a large number of zeros as well, thus excluding LRA from being applied here.The data are available from the UC Irvine Machine Learning Repository.An interesting feature of this dataset is that it contains many zero values.This means that the Aitchisonian metric ( 6) is not to be used.The ES-OV α and the TC α metrics on the other hand are not affected by the presence of zeros, since 0 log 0 = 0.In this example the sample size of the test data was equal to 30, hence we used 184 compositional vectors to train the k-NN algorithm.Again, the test data were chosen via stratified random sampling to avoid having categories not been selected in the test sample.Figure 8 shows the estimated percentage as a function of k and α using both metrics.This is a simpler case to draw conclusions, since the best results are obtained when α = 1 and k = 2 for both metrics, thus the ES-OV (2) and the TC (4) metrics should be used, with the estimated percentage of correct classification being 71.45% and 73.35% respectively.Table 2 presents analytical information of the classification results.Estimates of the sensitivities and of the specificities for each category of glass are also given.
The mean sensitivities of ES-OV α metric (3) for Tableware and Vehicle window are low and the same is true for the Vehicle window when TC α (5) is used.We observed that many times, Tableware and Vehicle window were being wrongly classified as Vehicle float.A possible reason for this could be the small sample size of Tableware (this type of glass had the minimum number of observations).A chemist or a forensic scientist could perhaps give a possible answer to this (if that is the case of these types of glass being of similar structure).
The ROC curves for each glass category (based on 1-fold cross validation) using both metrics are presented in Figure 9.We cannot say that one metric does better than the other always.For some glass categories, the two ROC curves are similar and for some others one seems a bit better than the other.

Conclusions
We suggested the use of a recently developed metric (2), for supervised classification when the k-NN algorithm is implemented.We also added a free parameter to the metric with the intention of improving the classification results.This free parameter was used to generalize the taxicab metric as well.The examples showed that both the ES-OV α (3) and the taxicab α (5) metric can be used for supervised clustering of compositional data, but can also be used in other scenarios as well.
An advantage of both metrics over the Aitchisonian metric ( 6) is that they handle zeros

4.
Repeat steps 2 − 3 for a different value of k. 5. Repeat steps 1 − 4 B (in our case B = 200) times and for each α and k and estimate the percentage of correct classification by averaging over all B times.

Figure 5 :
Figure 5: The estimated percentage of correct classification for the hydrochemical data as a function of k, the nearest neighbours and of α using the (a) ES-OV α metric (3) and (b) TC α (5).

Figure 6 :
Figure 6: The estimated percentage of correct classification as a function of k.The black and the red lines are based on the ES-OV α metric (3) with α = 0.5 and α = 1 respectively.The green and the blue lines are based on the TC α metric (5) with α = 0.35 and α = 1 respectively.The turquoise line is the Aitchisonian metric (6).

Figure 7 :
Figure 7: ROC curves for all tributaries using the three metrics For ES-OV α we used α = 0.35 and k = 2 and for TC α we used α = 0.5 and k = 2.For Each plot corresponds to one of the four tributaries (a) Anoia, (b) Cardener, (c) Upper Llobregat and (d) Upper Llobregat.

Figure 8 :
Figure 8: The estimated percentage of correct classification for the forensic glass data as a function of k, the nearest neighbours and of α using the (a) ES-OV α metric (3) and (b) TC α (5).