# Mahalanobis distances and ecological niche modelling: correcting a chi-squared probability error

- Published
- Accepted
- Received

- Academic Editor
- Jason Blackburn

- Subject Areas
- Biogeography, Ecology, Statistics
- Keywords
- Species distribution modelling, Ecological niche modelling, Mahalanobis distance, Habitat suitability modelling, Resource selection functions, Virtual ecology, Presence-only, Multivarite normal distribution, Chi-squared distribution, Probability

- Copyright
- © 2019 Etherington
- Licence
- This is an open access article distributed under the terms of the Creative Commons Attribution License, which permits unrestricted use, distribution, reproduction and adaptation in any medium and for any purpose provided that it is properly attributed. For attribution, the original author(s), title, publication source (PeerJ) and either DOI or URL of the article must be cited.

- Cite this article
- 2019. Mahalanobis distances and ecological niche modelling: correcting a chi-squared probability error. PeerJ 7:e6678 https://doi.org/10.7717/peerj.6678

## Abstract

The Mahalanobis distance is a statistical technique that can be used to measure how distant a point is from the centre of a multivariate normal distribution. By measuring Mahalanobis distances in environmental space ecologists have also used the technique to model: ecological niches, habitat suitability, species distributions, and resource selection functions. Unfortunately, the original description of the Mahalanobis distance technique for ecological modelling contained an error describing how Mahalanobis distances could be converted into probabilities using a chi-squared distribution. This error has been repeated in the literature, and is present in popular modelling software. In the hope of correcting this error to maximise the potential application of the Mahalanobis distance technique within the ecological modelling community, I explain how Mahalanobis distances are calculated, and through a virtual ecology experiment demonstrate how to correctly produce probabilities and discuss the implications of the error for previous Mahalanobis distance studies.

## Introduction

The Mahalanobis distance (Mahalanobis, 1936) is a statistical technique that can be used to measure how distant a point is from the centre of a multivariate normal distribution. Consider a data matrix **A** with *m* rows of observations and *n* columns of measured variables.
$\mathbf{A}=\left[\begin{array}{ccccc}\hfill {x}_{11}\hfill & \hfill {x}_{12}\hfill & \hfill {x}_{13}\hfill & \hfill \dots \hfill & \hfill {x}_{1n}\hfill \\ \hfill {x}_{21}\hfill & \hfill {x}_{22}\hfill & \hfill {x}_{23}\hfill & \hfill \dots \hfill & \hfill {x}_{2n}\hfill \\ \hfill {x}_{31}\hfill & \hfill {x}_{32}\hfill & \hfill {x}_{33}\hfill & \hfill \dots \hfill & \hfill {x}_{3n}\hfill \\ \hfill \vdots \hfill & \hfill \vdots \hfill & \hfill \vdots \hfill & \hfill \ddots \hfill & \hfill \vdots \hfill \\ \hfill {x}_{m1}\hfill & \hfill {x}_{m2}\hfill & \hfill {x}_{m3}\hfill & \hfill \dots \hfill & \hfill {x}_{mn}\hfill \end{array}\right].$

The Mahalanobis distance *D*^{2} for each observation vector **x**_{m} = [*x*_{m1}, *x*_{m2}, *x*_{m3}, …, *x*_{mn}] is calculated as a function of an *n*-dimensional vector $\stackrel{\u0304}{\mathbf{x}}$ containing the means for each column of variables, and a variance–covariance matrix **S** of dimensions *n* × *n* that contains variances for each column along the main diagonal and pair-wise column covariances values elsewhere (Manly, 2005). (1)${D}^{2}\left({\mathbf{x}}_{m}\right)={\left({\mathbf{x}}_{m}-\stackrel{\u0304}{\mathbf{x}}\right)}^{\text{T}}{\mathbf{S}}^{-1}\left({\mathbf{x}}_{m}-\stackrel{\u0304}{\mathbf{x}}\right).$

When applied to *m* = 50 points in *n* = 2 dimensions, the calculated *D*^{2} values follow a characteristic elliptical pattern with *D*^{2} radiating out from the central location of the distribution (Fig. 1A).

While *D*^{2} can be calculated for any *n*-dimensions, the values of *D*^{2} are not comparable when *n* varies, as *D*^{2} increases as *n* increases (Figs. 2A–2C). However, the $\stackrel{\u0304}{\mathbf{x}}$ and **S**^{−1} used to calculate *D*^{2} (Eq. (1)) transform the values from each *n* into independent standard normal distributions, with $\stackrel{\u0304}{\mathbf{x}}$ centering and **S**^{−1} scaling and rotating each variable distribution. This means that *D*^{2} is essentially the sum of *n* independent standard normal variables, and as such follows a chi-squared distribution with degrees of freedom equal to the number of dimensions *n* (Manly, 2005).

For a chi-squared random variable with *n* degrees of freedom denoted as ${\chi}_{n}^{2}$, the probability density function *f* of ${\chi}_{n}^{2}$ when *x* ≥ 0 is (2)$f}_{{\chi}_{n}^{2}}\left(x\right)=\frac{1}{{2}^{n\u22152}\Gamma \left(n\u22152\right)}{e}^{-x\u22152}{x}^{\left(n\u22152\right)-1$

where Γ is a gamma function (as the chi- squared distribution is actually a special case of the gamma distribution), and the associated cumulative distribution function is ${F}_{{\chi}_{n}^{2}}\left(x\right)=P\left({\chi}_{n}^{2}\le x\right)$ (Johnson, Kotz & Balakrishnan, 1994). When the variables in **A** are normally distributed, the association between *D*^{2} and ${f}_{{\chi}_{n}^{2}}\left(x\right)$ can be clearly seen (Figs. 2A–2C).

By converting *D*^{2} into probabilities using ${F}_{{\chi}_{n}^{2}}\left(x\right)$ we can put *D*^{2} from any number of dimensions on a common 0–1 scale that indicates the probability $P\left({\chi}_{n}^{2}\le {D}^{2}\right)$ that a location has a *D*^{2} that is greater than that would be expected by chance (Figs. 2D–2F). By specifying a significance level *α* this process is commonly used as an outlier detection method as it is: parameter free, computational efficient, accounts for collinearity between variable dimensions, and is scale independent (Aggarwal, 2017). Returning to our earlier example, having transformed the values from *D*^{2} to $P\left({\chi}_{n}^{2}\le {D}^{2}\right)$ we can see that there are two points that are very likely to be outliers, and would be classified as outliers with *α* = 0.975 (Fig. 1B).

The potential of *D*^{2} for use in habitat modelling was first identified by Clark, Dunn & Smith (1993) and then discussed further in the context of niche modelling by Farber & Kadmon (2003)—which is how I will continue the discussion. The premise here is that given a data matrix **A** of *m* species observations for which various environmental variables *n* are measured, *D*^{2} can be used as a measure of niche suitability from an optimum location in environmental space. Having defined a niche in this way, by measuring the *D*^{2} for each location on a landscape a map of niche suitability can then be produced. The key advantages of using *D*^{2} over other methods are that the *D*^{2} method needs only presence information, and so does not require either absences or a background definition, and that independence of explanatory variables is not required (Clark, Dunn & Smith, 1993; Farber & Kadmon, 2003). Studies that have compared Mahalanobis distance to other modelling approaches have also shown that while the optimum method tends to vary by the species in question, the Mahalanobis distance approach performs well against a variety of other presence-only, presence-background, and presence-absence modelling approaches (Dettmers, Buehler & Bartlett, 2002; Johnson & Gillingham, 2005; Tsoar et al., 2007).

While introducing *D*^{2} into the ecological modelling domain Clark, Dunn & Smith (1993) also highlighted that *D*^{2} will follow a chi-squared distribution, and therefore the potential to convert the distances into probabilities. However, as ecological niche models aim to describe probability of belonging to the niche, rather than use the chi-squared cumulative distribution function ${F}_{{\chi}_{n}^{2}}\left(x\right)=P\left({\chi}_{n}^{2}\le x\right)$ that has 0 at the optimum, we use the inverse chi-squared cumulative distribution function ${F}_{{\chi}_{n}^{2}}^{-1}\left(x\right)=P\left({\chi}_{n}^{2}>x\right)$ that has 1 at the optimum. This means that the probabilities $P\left({\chi}_{n}^{2}>{D}^{2}\right)$ indicate locations with a *D*^{2} that is less than that would be expected by chance, and hence are more likely to be within the niche.

Unfortunately, when describing the use of a chi-squared distribution to convert *D*^{2} into probabilities, Clark, Dunn & Smith (1993) p.522 state that “Assuming multivariate normality, Mahalanobis distances are approximately distributed as Chi-square with *n* − 1 degrees of freedom, where *n* equals the number of habitat characters.”, but this is incorrect. *D*^{2} values follow a chi-squared distribution with degrees of freedom equal to *n* (Manly, 2005) as has already been clearly shown (Fig. 2). This error has been repeatedly described in the literature (Knick & Rotenberry, 1998; Farber & Kadmon, 2003; Hellgren et al., 2007), and has even permeated into software such as the R package adehabitat (Calenge, 2006).

To demonstrate this error and to examine its implications, I present an experiment based on a virtual ecology approach (Zurell et al., 2010)—which allows us to examine methodologies in a controlled system uncomplicated by the uncertainties of the real world!

## Materials and Methods

The virtual ecology experiment began by defining, and therefore knowing truthfully, the fundamental niche of the imaginary species *Mimbulus mimbletonia* (Rowling, 2003). This fundamental niche *N* was defined using a multivariate normal distribution describing the niche in relation to two environmental variables of temperature and rainfall. (3)${N}_{\left(x,y\right)}={e}^{-\frac{1}{2}{\left(\left(x,y\right)-\mu \right)}^{\text{T}}{\Sigma}^{-1}\left(\left(x,y\right)-\mu \right)},\text{where}\mu =\left[\begin{array}{cc}\hfill 25\hfill & \hfill 100\hfill \end{array}\right]\Sigma =\left[\begin{array}{cc}\hfill 9\hfill & \hfill 60\hfill \\ \hfill 60\hfill & \hfill 625\hfill \end{array}\right].$

A sample of observations was then created by randomly sampling the niche space ranging 15–35 °C of temperature and 0–200 mm of rainfall. At each randomly selected location the species was considered detected using a probability equal to the fundamental niche. This process was continued until a sample of 200 observations was generated.

The values of temperature and rainfall at each of the 200 sampled locations were then used to estimate the fundamental niche using Mahalanobis distances. The *D*^{2} was calculated for each of the sampled locations, and the resulting distribution of *D*^{2} was compared against a range of inverse cumulative chi-squared distribution functions based on differing degrees of freedom.

Finally, the *D*^{2} values for the samples were converted into probabilities of belonging to the fundamental niche, and these predictions were compared against the known fundamental niche values for the locations. Probabilities were calculated using chi-squared distributions with *n* and *n* − 1 degrees of freedom to examine any differences.

## Results

The fundamental niche defined using the multivariate normal distribution (Eq. (3)) produced an elliptically shape niche with positive correlation between rainfall and temperature (Fig. 3A). The random sampling resulted in a set of 200 samples that followed this elliptical niche pattern, with a greater concentration of samples towards the centre of the niche (Fig. 3B).

The *D*^{2} values calculated on the basis of these samples also followed an elliptical pattern (Fig. 3C), and when the calculated *D*^{2} values for each sample were plotted against the actual fundamental niche value for each sample, a trend that clearly follows the inverse cumulative chi-squared distribution when *n* = 2 can be seen (Fig. 3D). This is as we would expect as in this example the fundamental niche is based on the two environmental variables of temperature and rainfall.

When the *D*^{2} values were converted to probabilities using *n* = 2 degrees of freedom we see a near-perfect linear fit between the estimated niche suitability and the actual known niche suitability (Fig. 3E). This proves quite clearly that to get a truthful estimate of our known fundamental niche, the probabilities need to be based on an inverse cumulative chi-squared distribution with *n* degrees of freedom. In contrast, when the *D*^{2} values are converted to probabilities using *n* − 1 degrees of freedom we see a badly-fitting curvilinear relationship that underestimates niche suitability (Fig. 3F).

## Discussion

The results of the virtual ecology experiment (Fig. 3) clearly shows the erroneous under prediction of niche suitability when using a chi-squared distribution with *n* − 1 degrees of freedom. Fortunately most previous *D*^{2} studies simply rescaled the *D*^{2} into quantiles or ranks of increasing suitability (Knick & Dyer, 1997; Knick & Rotenberry, 1998; Johnson & Gillingham, 2005; Hellgren et al., 2007; Etherington et al., 2009) or binary classifications based on a threshold (Farber & Kadmon, 2003; Thatcher, Van Manen & Clark, 2006; Tsoar et al., 2007) and so are not affected by this problem. Also, although I could not find any examples of this, those studies that did create chi-square probabilities using *n* − 1 degrees of freedom, but then converted to categories based on quantiles or a predictive threshold would not have impacted the conclusions of the study as while the shape of the relationship becomes curved with *n* − 1, the trend is still one of monotonic increase, therefore the quantiles would be the same. However, those studies that have created chi-squared probabilities with *n* − 1 as some form of suitability index (Clark, Dunn & Smith, 1993) will have underestimated the niche suitability.

## Conclusion

Given that *D*^{2} values are unitless and unbounded and are not directly comparable for different dimensions, I would argue that anyone using the Mahalanobis distance method should present their results as chi-squared probabilities. This will put Mahalanobis distance models on a 0–1 scale that enables models based on differing numbers of *n* to be directly comparable, and is consistent with most other types of ecological niche models that also use a 0–1 scale. As such, it will be very important that the chi-square probabilities are calculated correctly and hopefully the methodological description and experimental evidence presented here will enable that to be achieved.

## Supplemental Information

### R code to reproduce figures

Each R script will generate one of the figures used as examples of Mahalanobis distance behaviour within the manuscript.