## Introduction

The comparative method is the basis for a range of analyses in ecology and evolutionary biology. The rationale for the approach is that as species evolve, traits adapt in response to changes in the biotic and abiotic environment, with the consequence that current distributions of trait values reflect the processes that shaped them in the past. Given the information on species’ traits, together with phylogenetic information, it should be possible to reconstruct the evolutionary history of a group.

The most common approach to comparative analysis revolves around a group of closely related statistical modelling approaches (e.g. Felsenstein 1985; Grafen 1989; Lynch 1991; Martins & Hansesn 1997; Pagel 1997; Garland, Midford & Ives 1999). In broad terms, these are equivalents of the linear modelling methods (GLM, regression, ANOVA, etc.) that are routinely used throughout biology, but accounting for the influence of phylogeny. The approaches rely on a common approach to modelling the interdependence between species resulting from common evolutionary history. In broad terms, the method relies on fitting multivariate normal distributions to describe the interdependence between species. In evolutionary terms, this can be justified by assuming that traits evolve according to a Brownian model of trait evolution (Felsenstein 1985; Harvey & Pagel 1991).

This approach to modelling trait variation is useful because it is so flexible: in addition to the variety of approaches that have been developed directly based on this model for trait variation, the method has been extended in various ways. Pagel (1997, 1999) outlined transformations that could be applied to a phylogeny and estimated as part of the model. These include parameters allowing for increases or decreases in the rate of evolution (κ and δ) or for variable levels of phylogenetic dependence (λ; Freckleton, Harvey & Pagel 2002). Hansen (1997) showed that phylogenetic constraint could be measured by a further transformation (α) that generates an Ornstein–Uhlenbeck model (Hansen, Pinaar & Orzack 2008). Thomas, Freckleton & Szekely (2006) outlined a transformation (θ) to allow for trait-dependent rates of evolution (O’Meara *et al.* 2006). It is possible to combine different models for nonindependence, and Freckleton & Jetz (2009) suggested how spatial and phylogenetic models could be used simultaneously.

One of the difficulties in applying these approaches is that one step in the analysis can be computationally demanding. As outlined in detail below, to apply the approach, a cophenetic matrix, **V**, is computed, which comprises the shared path lengths of all *n* species in the phylogeny. For *n* species, **V** has dimensions *n* × *n*, that is, its size grows as the square of the number of species in the analysis. This has two consequences for the computation of the PGLS model. First, the matrix has to be generated in the first place. This requires allocating enough memory to hold all of the entries of **V** and then initiating one traversal (i.e. successively visiting all nodes) of the phylogeny per pair of species sharing an ancestor to measure the shared path lengths. Second, **V** has to be inverted at one point in the analysis. This is a numerical step, and the computational overhead can be considerable in addition to the burden of computing **V**.

Figure 1 illustrates that these computational burdens can be large and increase nonlinearly with the size of the phylogeny. The time taken for the formation of **V** scales approximately to the power 2·5, with increasing the size of the phylogeny, whilst inverting the variance–covariance matrix scales to the power 2·87. The lower bound for the exponent of the time taken to invert **V** is probably 2, as there are *n*^{2} entries in a matrix for *n* species. However, the fastest current algorithm for matrix inversion has an exponent of 2·376 (Robinson 2005). Irrespective of processor power, this scaling sets an effective limit to the size of phylogeny that can be analysed using this method, probably of the order of 1 × 10^{5}. A further issue is that memory requirements also are demanding: a variance matrix for a phylogeny of 1 × 10^{5} species will require 1 × 10^{10} entries to be stored (requiring *c*. 80 gigabytes of memory for double data types). Although cophenetic matrices for phylogenies are frequently sparse (many entries are zero) and efficient methods tailored to sparse matrices could be brought to bear (e.g. Hadfield & Nakagawa 2010), there are undoubtedly considerable computational costs to be borne. These problems are not unique to the generalised least squares approach. For example, phylogenetic eigenvector regression (PVR; Diniz-Filho, de Sant Ana & Bini 1998) requires that a distance matrix is computed, from which eigenvectors are extracted. These calculations require approximately the same time and memory as computing, storing and inverting **V**.

Modern comparative analyses can require considerable numbers of computations. For example, estimating parameters modelling different modes of evolution (e.g. the transformations described above) requires that for each value of the parameter examined, **V** is calculated from the matrix obtained from the phylogeny and solved for each parameter value. Because the parameters have to be estimated iteratively, a large number of values may have to be explored. In analyses in which phylogenetic uncertainty is analysed, **V** has to be computed individually for each candidate phylogeny. In Bayesian MCMC analyses, this number might be in the order of millions. Finally, simulations require large numbers of iterations across wide ranges of parameters, and slow computation can limit the range of parameters that can be explored.

The problems of computational constraints in analyses of this sort have long been recognised. Felsenstein (1973) struggled with the problem of calculating the likelihood of a set of data on a tree with a given set of branch lengths. This likelihood depends on a matrix, **V,** but given the computational constraints at that time, direct inversion of **V** was computationally impractical for even moderately sized problems. To get around this, Felsenstein (1973) presented a method of calculating likelihoods that did not require **V** or its inverse. Although the link has not been greatly stressed (Felsenstein 2004; Freckleton & Harvey 2006; Freckleton & Jetz 2009; Thomas & Freckelton 2011), this approach is essentially the same as the method of contrasts, which is the most widely used comparative method (Felsenstein 1985).

Here, I outline how the approach suggested in Felsenstein (1973), which has been largely overlooked in the comparative literature, can be used to greatly enhance the speed of computation in comparative analysis. Most codes and packages that are currently available use the slower matrix inversion method (Freckleton & Harvey 2006; Freckleton & Jetz 2009; Thomas & Freckelton 2011). We first outline the method for calculating the maximum likelihood estimates of parameters of a single trait. I then show how this can be generalised to calculate the likelihood for arbitrary parameters. I finally illustrate how this can be extended to problems of correlated evolution and PGLS. R code is supplied to demonstrate the computationally efficient methods.