Package {easyCODA}


Type: Package
Version: 0.50
Date: 2026-11-23
Title: Compositional Data Analysis in Practice
Author: Michael Greenacre [aut, cre]
Maintainer: Michael Greenacre <michael.greenacre@upf.edu>
Description: Univariate and multivariate methods for compositional data analysis, based on logratios. The package implements the approach in the book Compositional Data Analysis in Practice by Michael Greenacre (2018, 2026), where accent is given to simple pairwise logratios. Selection can be made of logratios that account for a maximum percentage of logratio variance or maximize prediction accuracy of a response variable. Various multivariate analyses of logratios are included in the package.
License: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]
Imports: ca (≥ 0.7), vegan (≥ 2.3), ellipse (≥ 0.4.1), boot, stats, graphics, grDevices, utils
LazyData: true
ByteCompile: true
URL: https://github.com/michaelgreenacre/easyCODA, https://github.com/michaelgreenacre/CODAinPractice
BugReports: https://github.com/michaelgreenacre/easyCODA/issues
NeedsCompilation: no
Packaged: 2026-11-23 20:36:34 UTC; U16368
Depends: R (≥ 3.5.0)
Repository: CRAN
Date/Publication: 2026-11-23 23:50:02 UTC

Amalgamation clustering of the parts of a compositional data matrix

Description

This function clusters the parts of a compositional data matrix, using amalgamation of the parts at each step.

Usage

ACLUST(data, weight = FALSE, close = TRUE)

Arguments

data

Compositional data matrix, with the parts as columns

weight

FALSE (default), for unweighted analysis, TRUE for weighting using part averages of closed compositions, or a user-supplied vector of column weights

close

TRUE (default) will close the rows of data prior to clustering, FALSE leaves data as it is

Details

The function ACLUST performs amalgamation hierarchical clustering on the parts (columns) of a given compositional data matrix, as proposed by Greenacre (2019). At each step of the clustering two clusters are amalgamated that give the least loss of explained logratio variance.

Value

An object, compatible with a hclust object, which describes the tree produced by the clustering process on the n objects. The object is a list with components:

merge

an n-1 by 2 matrix. Row i of merge describes the merging of clusters at step i of the clustering. If an element j in the row is negative, then observation -j was merged at this stage. If j is positive then the merge was with the cluster formed at the (earlier) stage j of the algorithm. Thus negative entries in merge indicate agglomerations of singletons, and positive entries indicate agglomerations of non-singletons.

height

a set of n-1 real values (non-decreasing for ultrametric trees). The clustering height: that is, the value of the criterion associated with the clustering method for the particular agglomeration.

order

a vector giving the permutation of the original observations suitable for plotting, in the sense that a cluster plot using this ordering and matrix merge will not have crossings of the branches

labels

a vector of column labels, the column names of data

Author(s)

Michael Greenacre

References

Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC.
Greenacre, M. (2020), Amalgamations are valid in compositional data analysis, can be used in agglomerative clustering, and their logratios have an inverse transformation. Applied Computing and Geosciences, 5: 100017. Open access.

See Also

hclust, WARD,CLR, LR.VAR, CLOSE

Examples

data(RomanCups)

# amalgamation clustering    (weighted parts)
cups.aclust <- ACLUST(RomanCups, weight=TRUE)
plot(cups.aclust)

# reproducing Figure 2(b) of Greenacre (2019) (unweighted parts)
# dataset Aar is in the compositions package
# aar is a subset of Aar
# code given here within the '\dontrun' environment since external package 'compositions' required
## Not run: 
  library(compositions)
  data(Aar)
  aar <- Aar[,c(3:12)]
  aar.aclust <- ACLUST(aar)
# the maximum height is the total variance
# convert to percents of variance NOT explained
  aar.aclust$height <- 100 * aar.aclust$height / max(aar.aclust$height)
  plot(aar.aclust, main="Parts of Unexplained Variance", ylab="Variance (percent)")

## End(Not run)

Additive logratios

Description

Computation of additive logratios (ALRs) with respect to a specified part.

Usage

ALR(data, denom=ncol(data), weight=FALSE, stats=FALSE)

Arguments

data

A compositional data frame or matrix

denom

Number of part used in the denominator

weight

Logical indicating if varying weights are returned (default:FALSE unweighted, i.e. equal weights ared returned). If TRUE, weights proportional to compositional averages are returned. Alternatively a set of positive weights can be specified.

stats

Logical indicating if means, variances and total variance of the ALRs are returned (default:FALSE)

Details

The function ALR computes a set of additive logratios (ALRs) with respect to a specified part (by default, the last part).

Value

LR

The additive logratios (ALRs)

LR.wt

The weights assigned to the ALRs

denom

The index of the denominator used in the computation of the ALRs

part.names

The part names in the data, i.e. column names

part.wt

The part weights

means

The means of the ALRs (only returned if stats = TRUE)

vars

The variances of the ALRs (only returned if stats = TRUE)

totvar

The total variance of the ALRs (only returned if stats = TRUE)

Author(s)

Michael Greenacre

References

Aitchison, J. (1986), The Statistical Analysis of Compositional Data, Chapman & Hall.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.

See Also

invALR, PLR, CLR, invCLR, LR.VAR

Examples

data(Vegetables)
ALR(Vegetables, denom=2)

Compositional bar plot

Description

Horizontal bar plot of compositional data given as percentages

Usage

BAR(data, cols=rainbow(ncol(data)), col.names=colnames(data),
    row.names=rownames(data), order.column=NA, eps=0.5, main="", ylab="",
    ylim=c(0,nrow(data)), xlim=c(0,100), cex=1, truncate=NA)

Arguments

data

Compositional data matrix or data frame with samples in rows, parts in columns. Note that percentage data are assumed.

cols

Colours of points for each part, default rainbow

col.names

Part names, if modified

row.names

Sample names, if modified

order.column

By default parts are taken in order of columns, but can be re-ordered using this option

eps

Small space between bars, can be modified

main

Heading

ylab

Vertical axis label

ylim

Vertical axis limits (default is the number of rows in data)

xlim

Horizontal axis limits (default c(0,100))

cex

Character size scaling factor for labels

truncate

Truncate part (column) names to this number of characters for legend

Details

The function BAR makes a BAR plot for a compositional data matrix in percentages, where the parts are in the columns of a matrix or data frame.

Author(s)

Michael Greenacre

References

Greenacre, M. (2016), Data reporting and visualization in ecology, Polar Biology: 39, 2189-2205.

See Also

DOT

Examples

# Vegetables data set: order samples by carbohydrates 
data(Vegetables)
BAR(Vegetables, order.column=2)

Correspondence analysis

Description

Computation of correspondence analysis on a table of nonnegative data.

Usage

CA(data, nd = 2, suprow = NA, supcol = NA)

Arguments

data

A data frame or matrix of nonnegative data (no negative values)

nd

Number of dimensions for summary solution if not 2 (default)

suprow

Indices of rows that are supplementary points

supcol

Indices of columns that are supplementary points

Details

The function CA is a simple wrapper for the ca function in the ca package (Nenadic and Greenacre, 2007), for compatibility within the easyCODA package.

Supplementary rows and columns can be declared (also known as passive points) – these do not contribute to the solution but are positioned on the solution axes.

The function borrows the structure and functions of the ca package, which is required, and produces a ca object, and the same print, summary and plot methods can be used, as for a ca object. It additionally exports the principal coordinates of both the rows and columns, not presently exported in the ca package.

Value

sv

Singular values

nd

Number of dimensions in solution results

rownames

Row names

rowmass

Row weights

rowdist

Row logratio distances to centroid

rowinertia

Row inertias

rowcoord

Row standard coordinates

rowpcoord

Row principal coordinates

rowsup

Indices of row supplementary points

colnames

Column names

colmass

Column weights

coldist

Column logratio distances to centroid

colinertia

Column inertias

colcoord

Column standard coordinates

colpcoord

Column principal coordinates

N

The compositional data table

Author(s)

Michael Greenacre

References

Nenadic, O. and Greenacre, M. (2007). Correspondence analysis in R, with two- and three-dimensional graphics: The ca package. Journal of Statistical Software, 20 (3), https://www.jstatsoft.org/v20/i03/

See Also

PLOT, plot.ca, summary.ca, print.ca

Examples

# CA of the Roman cups data (symmetric map). ca package is required.
data(RomanCups)
require(ca)
PLOT(CA(RomanCups))
# CA of the Roman cups data (contribution biplot)
PLOT(CA(RomanCups), map="contribution", rescale=0.2)

Bivariate confidence and data ellipses

Description

Draws confidence and data ellipses in bivariate scatterplots

Usage

CIplot_biv(x, y, group, wt=rep(1/length(x),length(x)), 
           varnames=c("x","y"), groupnames=sort(unique(group)), 
           groupcols=rainbow(length(unique(group))), 
           shownames=TRUE, xlim=c(NA,NA), ylim=c(NA,NA), 
           lty=1, lwd=1, add=FALSE, alpha=0.95, ellipse=0, 
           shade=FALSE, alpha.f=0.2, frac=0.01, cex=1) 

Arguments

x

x-variable (horizontal) of scatterplot

y

y-variable (vertical) of scatterplot

group

Grouping variable (must be provided)

wt

Set of weights on the cases (operates when ellipse=1)

varnames

Vector of two labels for the axes (default is x and y)

groupnames

Vector of labels for the groups (default is unique values in group, sorted in alphanumeric order)

groupcols

Vector of colours for the groups

shownames

Whether to show group names at group centroids or not (default is TRUE)

xlim

Possible new x-limits for plot

ylim

Possible new y-limits for plot

lty

Line type for the ellipses (default is 1)

lwd

Line width for the ellipses (default is 1)

add

=TRUE if ellipses/intervals are added to existing plot (default is FALSE)

alpha

Confidence level of ellipses (default is 0.95)

ellipse

Type of ellipse (see Details below; default is 0 for normal-based ellipses)

shade

=TRUE for ellipse shading (default=FALSE)

alpha.f

Shading fraction (default is 0.2)

frac

Proportional part defining the width of the bars at the edges of confidence intervals (for ellipse=3 and 4)

cex

Character expansion factor for group names

Details

The function CIplot_biv makes various types of confidence and data ellipses, according to option ellipse. Set ellipse=-1 for regular data-covering ellipses. Set ellipse=0 (default) for normal-theory confidence ellipses. Setellipse=1 for bootstrap confidence ellipses. The option ellipse=2 for the delta method is not implemented yet. Set ellipse=3 for normal-theory confidence error bars lined up with axes. Set ellipse=4 for bootstrap confidence error bars along axes. The package ellipse is required.

Author(s)

Michael Greenacre

References

Greenacre, M. (2016), Data reporting and visualization in ecology, Polar Biology, 39:2189-2205.

See Also

DOT, BAR, CIplot_uni

Examples

# Generate some bivariate normal data in three groups with different means
# Means (1,0), (0,1) and (0,0)
means  <- matrix(c(1,0,0,1,0,0), ncol=3)
data   <- matrix(nrow=300, ncol=2)
groups <- sample(rep(c(1,2,3), 100))
for(i in 1:300) data[i,] <- rnorm(c(1,1), mean=means[,groups[i]])
# Plot confidence ellipses with shading
require(ellipse)
CIplot_biv(data[,1], data[,2], group=groups, shade=TRUE)

Univariate confidence plots

Description

Draws univariate confidence plots of single variables

Usage

CIplot_uni(data, group=NA, names=colnames(data), group.order=NA, 
           cols=rainbow(ncol(data)), ylim=c(NA,NA), cex=1, 
           pch=rep(21, ncol(data)), fg=rainbow(ncol(data)), bg="white", 
           shade=FALSE, alpha.f=0.2, horizon=FALSE, nperm=1000, 
           conf=0.5, CONF=0.95, thin=1, end=1, main="", ylab="") 

Arguments

data

matrix with data in columns (if not a matrix, then it is a vector with corresponding grouping variable group)

group

Grouping variable (if data is a vector)

names

labels for variables; if data is a matrix then by default the column names of data

group.order

group names in order preferred in plot

cols

Vector of colours for different variables (or groups)

ylim

Possible new y-limits for plot (after seeing first attempt at plotting))

cex

Expansion factor for symbols

pch

alternative set of symbols for samples, otherwise small filled circles (pch=21)

fg

colours for symbols

bg

colours for possible fill colour

shade

=TRUE for ellipse shading (default=FALSE)

alpha.f

shading factor, by default=0.2 (should be between 0 and 1)

horizon

TRUE if horizontal gray dashed lines required at "nice" y-values, not implemented yet

nperm

number of replicates for bootstrap sampling (default is 1000)

conf

confidence level for box, default 50% (0.5)

CONF

confidence level for whiskers, default 95% (0.95)

thin

by default=1, adjust for fatter or thinner boxes

end

by default=1, adjust for wider or narrower ends of whiskers

main

title for figure

ylab

y-axis label

Details

The function CIplot_uni plots univariate confidene intervals, called confidence plots, using bootstrapping. It is mainly for plotting several confidence plots alongside one another. These could be based on different variables in the columns of a matrix or groups of variables defined on a single vector. The package boot is required.

Author(s)

Michael Greenacre

References

Greenacre, M. (2016), Data reporting and visualization in ecology, Polar Biology, 39:2189-2205.

See Also

DOT, BAR, CIplot_biv

Examples

# Generate two normal variables in three groups with different means
# Means (1,0), (0,1) and (0,0)
means  <- matrix(c(1,0,0,1,0,0), ncol=3)
data   <- matrix(nrow=300, ncol=2)
groups <- sample(rep(c(1,2,3), 100))
for(i in 1:300) data[i,] <- rnorm(c(1,1), mean=means[,groups[i]])
# Confidence plots of first and second variable, with shading
require(boot)
CIplot_uni(data[,1], group=groups, shade=TRUE, ylim=c(-0.5,1.5), main="First variable")
CIplot_uni(data[,2], group=groups, shade=TRUE, ylim=c(-0.5,1.5), main="Second variable")

Closure of rows of compositional data matrix

Description

This function closes (or normalizes) the rows of a compositional data matrix, resulting in rows summing to 1.

Usage

CLOSE(x)

Arguments

x

Compositional data matrix.

Details

Compositional data carry relative information. It is sometimes required to close the data so that each row of observations sums to 1. The function CLOSE performs the closure.

Value

The closed compositional data matrix.

Author(s)

Michael Greenacre

References

Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC.

Examples

# column means of original data set and closed data
data(RomanCups)
apply(RomanCups, 2, mean)
cups <- CLOSE(RomanCups)
apply(cups, 2, mean)

Centred logratios

Description

Computation of centred logratios (CLRs).

Usage

CLR(data, weight = FALSE, stats = FALSE)

Arguments

data

A compositional data frame or matrix

weight

Logical indicating if varying weights are returned(default:FALSE, for unweighted (i.e. equal weights)). If TRUE, weights eequal to compositional means are returned. Alternatively a set of positive weights can be specified.

stats

Logical indicating if means, variances and total variance of the ALRs are returned (default:FALSE)

Details

The function CLR computes the set of centred logratios (CLRs).

Value

LR

The centred logratios (CLRs)

LR.wt

The weights assigned to the CLRs

part.names

The part names in the data, i.e. column names

part.wt

The part weights

means

The means of the ALRs (only returned if stats = TRUE)

vars

The variances of the ALRs (only returned if stats = TRUE)

totvar

The total variance of the ALRs (only returned if stats = TRUE)

Author(s)

Michael Greenacre

References

Aitchison, J. (1986), The Statistical Analysis of Compositional Data, Chapman & Hall.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.

See Also

invCLR, ALR, invALR, PLR, LR.VAR

Examples

data(Vegetables)
CLR(Vegetables, stats=TRUE)

Concordance coefficient

Description

Computation of Lin's concordance coefficient, with optional rescaling.

Usage

CONCORD(x1, x2, scaled = FALSE)

Arguments

x1, x2

two vectors of same length

scaled

if FALSE (default), no rescaling of one of the vectors, giving the regular concordance coefficient; if TRUE, one of the vectors is rescaled to fit the other one, giving a scaled concordance coefficient

Details

The function CONCORD computes the agreement between two vectors, measured by the concordance coefficent; alternatively, it measures the scaled concordance coefficient for agreement up to a scaling factor.

Value

the concordance coefficient

Author(s)

Michael Greenacre

References

Lin, L. I.-K. (1989). A concordance correlation coefficient to evaluate reproducibility. Biometrics 45: 255-268.

See Also

SUBCO

Examples

# measure how close the Kimberlite compositions are to subcompositional coherence 
# using Spearman correlations and a subcomposition of 2/3 of the parts      
data(Kimberlite)
kim <- CLOSE(Kimberlite[,3:24])
set.seed(1)
select <- sample(1:22, 14)
kim.sub <- CLOSE(kim[,select])
kim.cor <- as.dist(cor(kim, method="spearman")[select,select])
kim.sub.cor <- as.dist(cor(kim.sub, method="spearman"))
CONCORD(kim.cor,kim.sub.cor)

Dot plot

Description

Simple dot plot of original data

Usage

DOT(data, cols=NA, names=NA, group=NA, pch=NA, horizon=FALSE, jitter=1, 
     xscale=NA, xscalefac=1, yaxis=TRUE, shownames=TRUE, main="", ylab="", 
     xlim=c(NA,NA), ylim=c(NA, NA), cex=1)

Arguments

data

Matrix or data frame with data groups in columns; alternatively, a single vector but then groups (if any) have to specified with the group option

cols

Colours of points for each sample, default rainbow

names

Labels for variables, by default the column names of data, or group names

group

Group codes to split the data vector into separate plots

pch

Point character

horizon

TRUE if horizontal gray dashed lines required at "nice" y-values (default FALSE, not implemented yet)

jitter

1 by default, increase or decrease slightly for more jitter

xscale

User-supplied positions of points on horizontal axis

xscalefac

1 by default, rescale the positions on horizontal axis

yaxis

TRUE by default, FALSE to suppress and optionally add afterwards

shownames

TRUE by default; FALSE to not show group names and add them externally

main

Heading

ylab

Vertical axis label

xlim

Horizontal axis limits

ylim

Vertical axis limits

cex

Character size adjustment for labels

Details

The function DOT makes a dot plot for specified groups of points, which can be in columns of a matrix or data frame, or in a single vector with group codes specified separately.

Author(s)

Michael Greenacre

References

Greenacre, M. (2016), Data reporting and visualization in ecology, Polar Biology, 39:2189-2205.

See Also

BAR, CIplot_uni

Examples

data(Kimberlite)
kim <- CLOSE(Kimberlite[,3:23])
# Dot plot of two columns, iron (Fe) and magnesium (Mg)
DOT(kim[,c(4,5)])
# Dot plot of one column, iron (Fe), across the five phases
kim_phase <- Kimberlite[,2]
DOT(kim[,"Fe"], group=kim_phase, ylim=c(0.08, 0.12), main="Iron (Fe)")

Dummy variable (indicator) coding

Description

Convert categorical variable to dummy (0/1) coding

Usage

DUMMY(x, catnames=NA)

Arguments

x

Variable (vector) of categorical data to be coded

catnames

Category names

Details

The function DUMMY takes a categorical variable and converts it to a set of dummy variables (zeros and ones), where the ones indicate the corresponding category. There are as many columns in the result as there are unique categories in the input vector.

Author(s)

Michael Greenacre

Examples

# Indicator (dummy) coding of sex in FishMorphology data set
data(FishMorphology)
sex   <- FishMorphology[,1]
sex.Z <- DUMMY(sex, catnames=c("F","M")) 

Find the best ALR transformation

Description

Search over every possible reference part to choose an optimal ALR transformation.

Usage

FINDALR(data, weight=FALSE)

Arguments

data

Compositional data matrix, with the parts as columns

weight

FALSE (default) for equally weighted parts, TRUE when weights are in data list object, or a vector of user-defined part weights

Details

The function FINDALR considers every possible set of additive logratio (ALR) transformations, by trying each of the references. For each set the closeness to isometry is measured by the Procrustes correlation. In addition, the variance of the log-transformed reference is also computed. The reference with highest Procrustes correlation and the reference with the lowest variance of its log-transform are identified.

Value

An object which describes the tree produced by the clustering process on the n objects. The object is a list with components:

totvar

Total logratio variance

procrust.cor

The Procrustes correlations of the ALRs using each reference

procrust.max

The value of the highest Procrustes correlation

procrust.ref

The reference corresponding to the highest correlation

var.log

Variances of the log-transformed references

var.min

The value of the lowest variance

var.ref

The reference corresponding to the lowest variance

Author(s)

Michael Greenacre

References

Greenacre, M., Martinez-Alvaro, M. and Blasco, A. (2021), Compositional data analysis of microbiome and any-omics datasets: a validation of the additive logratio transformation, Frontiers in Microbiology 12: 2625
Gower, J. and Dijksterhuis, G.B. (2004), Procrustes Problems. Oxford University Press
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC

See Also

ALR

Examples

# For the Kimberlite data, first close (normalize) 
# then loop over the 21 possible references
# But note that for the default option weight=FALSE 
# closing the data is not necessary
data(Kimberlite)
kim <- CLOSE(Kimberlite[,4:24])
kim.BestAlr <- FINDALR(kim)
summary(kim.BestAlr, top=10)

Dataset: FishMorphology

Description

This data set consists of the sex, habitat, mass and then 26 morphometric measurements on 75 fish (Arctic charr)

Usage

data(FishMorphology)

Format

Data frame containing the 75 x 29 matrix. Column 1 contains sex (1=female, 2=male). Column 2 contains habitat (1=litoral, 2=pelagic). Column 3 contains the mass in grams. Columns 4 to 29 contain the 26 morphometric measurements.

Source

Greenacre, M and Primicerio, R (2010) Multivariate Analysis of Ecological Data. BBVA Foundation, Bilbao. Free download at www.multivariatestatistics.org


Dataset: Kimberlite

Description

This data set consists of the cation compositions of 21 elements in 126 samples of diamond-bearing volcanic rock.

Usage

data(Kimberlite)

Format

Data frame containing a 126 x 23 matrix, where the first two columns are the sample ID number and the names of the five volcanic phases, respectively, and the remaining columns the 21 minerals. Notice that this is a subset of the full data set of 270 samples analysed by Grunsky, Greenacre and Kjarsgaard (2024).

Source

Grunsky, E., Greenacre, M. and Kjarsgaard, B. (2024) GeoCoDA: Recognizing and validating structural processes in geochemical data. A workflow on compositional data analysis in lithogeochemistry. Applied Computing and Geosciences 22: 100149


Total logratio variance

Description

Computation of total (weighted)logratio variance.

Usage

LR.VAR(LRdata, row.wt = NA, weight=FALSE, vars=FALSE)

Arguments

LRdata

Matrix of logratios, either a vector or preferably the logratio object resulting from one of the functions ALR, CLR or PLR

row.wt

Optional set of row weights (default: equal weights)

weight

Logical indicating if parts are weighted or not (default: FALSE for unweighted (i.e., equal weights)). If TRUE, parts are weighted by compositional means. Alternatively a set of positive weights can be specified.

vars

If TRUE, output individual variances as well (default: FALSE)

Details

The function LR.VAR computes the sum of the logratio variances provided as input, using the weights in the logratio data object.

Value

LRtotvar

The total logratio variance

LRvars

(optional, if vars=TRUE, the individual logratio variances composing the total)

Author(s)

Michael Greenacre

References

Aitchison, J. (1986), The Statistical Analysis of Compositional Data, Chapman & Hall.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.

See Also

PLR, ALR, CLR

Examples

data(RomanCups)
# These give identical total weighted logratio variances
LR.VAR(CLR(RomanCups, weight=TRUE))
LR.VAR(PLR(RomanCups, weight=TRUE))
# Summing over all sets of ALRs gives twice the total variance
totvar <- 0
for(j in 1:ncol(RomanCups)) 
    totvar <- totvar + suppressMessages(LR.VAR(ALR(RomanCups, denom=j, weight=TRUE)))
totvar/2

Logratio analysis

Description

Computation of unweighted or weighted logratio analysis of a samples-by-parts compositional data table.

Usage

LRA(data, nd = 2, weight = FALSE, suprow = NA, row.wt = NA, amalg = NA, supamalg = FALSE)

Arguments

data

A data frame or matrix of compositional data, with no zero values

nd

Number of dimensions for summary solution if not 2 (default)

weight

FALSE (default) for no differential part weighting (i.e., equal weights), TRUE for weights equal to compositional part means, or a user-provided vector of part weights

suprow

Indices of rows that are supplementary points

row.wt

Optional user-defined set of positive weights for the rows (samples) (default: equal weights)

amalg

Optional list of amalgamated parts

supamalg

FALSE (default) when amalgamations are active and their subparts supplementary, TRUE when amalgamations are supplementary and their parts active

Details

The function LRA computes a logratio analysis of a table of compositional data based on the singular value decomposition. By default, the unweighted logratio analysis (Aitchison & Greenacre 2002) is performed.. When weight=TRUE the weighted log-ratio analysis is computed (Greenacre & Lewi 2009). User-specified weights can also be supplied, for the rows and/or the columns. Usually row weights are not specified, and are equal unless intentional weighting of the samples is desired. Column weights (if weight = TRUE) are the part means of the true compositional table, thus summing to 1. User-specified part weights can be provided using the same weight option.

Supplementary rows can be declared (also known as passive points) – these do not contribute to the solution but are positioned on the solution axes.

Amalgamations can be defined and can either replace their constituent parts (default) or be declared supplementary using the supamalg option: supamalg = FALSE (default), = TRUE if all declared amalgamations are supplementary.

The function produces a LRA object, and can be plotted using PLOT.

Value

sv

Singular values

nd

Number of dimensions in solution results

rownames

Row names

rowmass

Row weights

rowdist

Row logratio distances to centroid

rowinertia

Row inertias

rowcoord

Row standard coordinates

rowpcoord

Row principal coordinates

rowsup

Indices of row supplementary points

colnames

Column names

colmass

Column weights

coldist

Column logratio distances to centroid

colinertia

Column inertias

colcoord

Column standard coordinates

colpcoord

Column principal coordinates

N

The compositional data table

Author(s)

Michael Greenacre

References

Aitchison, J. and Greenacre, M. (2002), Biplots of compositional data, Applied Statistics 51, 375-392.
Greenacre, M. and Lewi, P.J. (2009), Distributional equivalence and subcompositional coherence in the analysis of compositional data, contingency tables and ratio scale measurements. Journal of Classification 26, 29-54.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.
Greenacre, M. (2020), Amalgamations are valid in compositional data analysis, can be used in agglomerative clustering, and their logratios have an inverse transformation. Applied Computing and Geosciences 5, 100017.

See Also

PLOT

Examples

# (weighted) LRA of the RomanCups data set, showing default symmetric map
data(RomanCups)
# unweighted LRA of the RomanCups data set, showing default symmetric map
PLOT(LRA(RomanCups))
# the solution is completely different when weighted
PLOT(LRA(RomanCups, weight=TRUE))

Principal component analysis

Description

Computation of unweighted or weighted principal component analysis of a matrix of interval-scale data (e.g., a matrix of logratios).

Usage

PCA(data, nd = 2, weight = FALSE, row.wt = NA, suprow = NA, msg = TRUE)

Arguments

data

A matrix of interval-scale data, or logratio object from functions ALR, CLR or LR

nd

Number of dimensions for summary solution if not 2 (default)

weight

FALSE (default) for no differential part weighting (i.e., equal weights), TRUE for weights equal to compositional part means, or a user-provided vector of part weights

row.wt

Optional user-defined set of positive weights for the rows (samples) (default: equal weights)

suprow

Indices of rows that are supplementary points

msg

TRUE (default) for printing a message to users to remind them that the new default is weight=FALSE

Details

The function PCA computes an unstandardized principal component analysis, based on the singular value decomposition, of a matrix of interval-scale data, usually a matrix of logratios in the context of this package (but it can be used for general data as well). Thr unweighted option weight = FALSE is the default, weights can be specified, either the part means using the weight=TRUE option a set of weights provided by the user.

User-specified weights can also be supplied for the rows. Usually row weights are not specified, and are equal unless intentional weighting of the samples is desired.

Supplementary rows can be declared (also known as passive points) – these do not contribute to the solution but are positioned on the solution axes.

Value

sv

Singular values

nd

Dimenson of the solution

rownames

Row names

rowmass

Row weights

rowdist

Row logratio distances to centroid

rowinertia

Row variances

rowcoord

Row standard coordinates

rowpcoord

Row principal coordinates

rowsup

Indices of row supplementary points

colnames

Column names

colmass

Column weights

coldist

Column logratio distances to centroid

colinertia

Column variances

colcoord

Column standard coordinates

colpcoord

Column principal coordinates

N

The data table

Author(s)

Michael Greenacre

References

Aitchison, J. and Greenacre, M. (2002), Biplots of compositional data, Applied Statistics 51, 375-392.
Greenacre, M. (2010), Biplots in Practice, BBVA Foundation, Bilbao. Free download from www.multivariatestatistics.org Greenacre, M., Groenen, P.J.F., Hastie, T., Iiodice d'Enza, A., Markos, A. and Tuzilhina, E. (2022), Principal component analysis, Nature Reeviews Methods Primer 2, 101.)

See Also

PLOT

Examples

# compute pairwise logratios of Vegetables data set
data("Vegetables")
veg.LR <- PLR(Vegetables)
# unweighted PCA biplot of the results
veg.pca <- PCA(veg.LR$LR)
PLOT(veg.pca, map="asymmetric")

Plot the results of a logratio analysis, principal component analysis or correspondence analysis

Description

Various maps and biplots of the results of the functions LRA, PCA or CA. Groups of rows can be optionally defined with colours and/or symbols.

Usage

PLOT(obj, classobj, map="symmetric", rescale=1, dim=c(1,2), axes.inv = c(1,1), main="", 
     most=FALSE, arrows=TRUE, cols=c("blue","red"), colarrows = "pink", cexs=c(0.8,0.8), 
     fonts=c(2,4), group=NA, groupnames=NA, groupcols=NA, grouppchs=NA, legendpos=NA, 
     ellipse=NA, ellipse.sup=FALSE, conf=0.95, labelrows=TRUE)

Arguments

obj

An LRA, PCA or CA object created using the respective functions

classobj

"LRA", "PCA" or "CA", or the class is picked up from the object itself

map

Choice of scaling of rows and columns: "symmetric" (default), "asymmetric" or "contribution"

rescale

A rescaling factor applied to column coordinates (default is 1 for no rescaling)

dim

Dimensions selected for horizontal and vertical axes of the plot (default is c(1,2))

axes.inv

Option for reversing directions of horizontal and vertical axes (default is c(1,1) for no reversing, change one or both to -1 for reversing)

main

Title for plot

most

Whether to limit the plotted variables to those with more than average contributions (default is FALSE)

arrows

To plot biplot arrows or not (default is TRUE)

cols

Colours for row and column labels (default is c("blue","red"))

colarrows

Colour for arrows in asymmetric and contribution biplots (default is "pink")

cexs

Character expansion factors for row and column labels (default is c(0.8,0.8))

fonts

Fonts for row and column labels (default is c(2,4))

group

Grouping variable for samples (rows); a numeric variable as long as number of rows

groupnames

Group names

groupcols

Group colours

grouppchs

Group symbols

legendpos

Position of legend: "topleft","topright","bottomleft","bottomright", default NA

ellipse

Set to optional type of ellipse (only if rows are grouped) – see CIplot_biv documentation

ellipse.sup

A logical flag indicating whether confidence ellipses should be calculated and drawn exclusively on the supplementary row points (default is FALSE).

conf

Confidence level for optional confidence ellipses (defaul=0.95)

labelrows

Use row labels (default=TRUE); if FALSE then use proposed symbols

Details

The function PLOT makes a scatterplot of the results of a logratio analysis (computed using function LRA), or a principal component analysis (computed using function PCA), or a correspondence analysis (computed using function CA). It includes the following options: colouring groups of row (sample) points, with optional symbols; adding a legend; and confidence ellipses using the function CIplot_biv for the two-dimensional group means By default, dimensions 1 and 2 are plotted on the horizontal and vertical axes.

By default, the symmetric scaling is used, where both rows and columns are in principal coordinates and have the same amount of weighted variance along the two dimensions. The other options are the asymmetric option, when columns are in standard coordinates, and the contribution option, when columns are in contribution coordinates. In cases where the row and column displays occupy widely different extents, the column coordinates can be rescaled using the rescale option.

Author(s)

Michael Greenacre

References

Greenacre, M. (2013), Contribution biplots, Journal of Computational and Graphical Statistics, 22, 107-122. Greenacre, M. (2016), Data reporting and visualization in ecology, Polar Biology, 39, 2189-2205.

See Also

CIplot_biv

Examples

# perform LRA on the fish data and define four sex-habitat groups for colouring
data(FishMorphology)
sex     <- FishMorphology[,1]
habitat <- FishMorphology[,2]
fish   <- as.matrix(FishMorphology[,4:29])
# Convert to compositional data matrix
fish   <- CLOSE(fish)
# Compute logarithm of mass and interaction of sex (F/M) and habitat (L/P) categories
sexhab  <- 2*(sex-1)+habitat
sexhab.names <- c("FL","FP","ML","MP")
rownames(fish) <- sexhab.names[sexhab]
# Correspondence analysis
fish.ca <- CA(fish)
PLOT(fish.ca, map="contribution", rescale=0.1, axes.inv=c(1,1), cexs=c(0.6,0.8),
     group=sexhab, groupcols=c("forestgreen","forestgreen","chocolate","chocolate"),
     groupnames=sexhab.names, ellipse=TRUE)

Plot the results of a redundancy analysis

Description

Various maps and biplots/triplots of the results of a redundancy analysis using function RDA.

Usage

PLOT.RDA(obj, map="symmetric", indcat=NA, rescale=1, dim=c(1,2), axes.inv=c(1,1),
         main="", arrows=TRUE, most=FALSE, type="wa", cols=c("blue","red","forestgreen"),
         colarrows=c("pink","lightgreen"), cexs=c(0.8,0.8,0.8), fonts=c(2,4,4), group=NA,
         groupnames=NA, groupcols=NA, grouppchs=NA, legendpos=NA, ellipse=NA, conf=0.95,
         labelrows=TRUE)

Arguments

obj

An RDA object created using function RDA

map

Choice of scaling of rows and columns: "symmetric" (default), "asymmetric" or "contribution"

indcat

A vector indicating which of the covariates are dummy (or fuzzy) variables

rescale

A rescaling factor applied to column coordinates(default is 1 for no rescaling). If rescale is a vector with two values, the first applies to the column coordinates and the second to the covariate coordinates.

dim

Dimensions selected for horizontal and vertical axes of the plot (default is c(1,2))

axes.inv

Option for reversing directions of horizontal and vertical axes (default is c(1,1) for no reversing, change one or both to -1 for reversing)

main

Title for plot

arrows

To plot biplot arrows or not (default is TRUE)

most

Whether to limit the plotted variables to those with more than average contributions (default is FALSE)

type

Type of scaling of the row points, "wa" (default) or "lc"

cols

Colours for row and column and covariate labels (default is c("blue","red","forestgreen"))

colarrows

Colour for arrows in asymmetric or contribution biplots, for columns and covariates (default is c("pink","lightgreen"))

cexs

Vector of character expansion factors for row and column and covariate labels (default is c(0.8,0.8,0.8))

fonts

Vector of font styles for row and column and covariate labels (default is c(2,4,4))

group

Optional grouping variable for assigning rows to groups

groupnames

Optional labels for the groups

groupcols

Optional colours for the groups

grouppchs

Optional symbols for the groups

legendpos

Optional position of legend (when there are groups))

ellipse

Optional confidence ellipses for group means

conf

Confidence level for optional confidence ellipses (defaul=0.95)

labelrows

Use row labels (default=TRUE); if FALSE then use proposed symbols

Details

The function PLOT.RDA makes a scatterplot of the results of a redundancy analysis (computed using function RDA), with various options for scaling the results and changing the direction of the axes. By default, dimensions 1 and 2 are plotted on the horizontal and vertical axes, and it is assumed that row points refer to samples and columns to compositional parts. Covariates are plotted according to their regression coefficients with the RDA dimensions, and if they contain dummy (or fuzzy) variables these are indicated by the option indcat, and hence plotted as centroids not arrows.

By default, the symmetric scaling is used, where both rows and columns are in principal coordinates and have the same amount of weighted variance along the two dimensions. The other options are the asymmetric option, when columns are in standard coordinates, and the contribution option, when columns are in contribution coordinates. In cases where the row and column displays as well as the covariate positions occupy widely different extents, the column and covariate coordinates can be rescaled using the rescale option.

Groups of points can be distinguished according to group, with row labels or symbols in different colours. Confidence ellipses for the group means can also be shown.

Author(s)

Michael Greenacre

See Also

RDA, CIplot_biv

Examples

# see an example of the use of PLOT.RDA in the example of the RDA function

All pairwise logratios

Description

Computation of all pairwise logratios.

Usage

PLR(data, ordering=1:ncol(data), weight=FALSE, stats=FALSE)

Arguments

data

A compositional data frame or matrix

ordering

A permutation of the columns (default: the original ordering)

weight

Logical indicating if varying weights are returned (default:FALSE unweighted, i.e. equal weights ared returned). If TRUE, weights proportional to compositional averages are returned. Alternatively a set of positive weights can be specified.

stats

Logical indicating if means, variances and total variance of the ALRs are returned (default:FALSE)

Details

The function PLR computes the complete set of pairwise logratios, in the order [1,2], [1,3], [2,3], [1,4], [2,4], [3,4], etc.

Value

LR

The pairwise logratios as columns of a data matrix

LR.wt

The weights assigned to the respective logratios

part.names

The part names in the data, i.e. column names

part.wt

The part weights

means

The means of the PLRs (only returned if stats = TRUE)

vars

The variances of the ALRs (only returned if stats = TRUE)

totvar

The total variance of the ALRs (only returned if stats = TRUE

Author(s)

Michael Greenacre

References

Aitchison, J. (1986), The Statistical Analysis of Compositional Data, Chapman & Hall.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.

See Also

ALR, invALR, CLR, invCLR, LR.VAR

Examples

data(Vegetables)
PLR(Vegetables)

Redundancy analysis

Description

Computation of unweighted or weighted redundancy analysis of a samples-by-parts compositional data table, given a set of covariates.

Usage

RDA(data, cov=NA, nd = NA, weight = FALSE, suprow = NA, row.wt = NA) 

Arguments

data

A data frame or matrix of interval-scale data, e.g. logratios (which are preferably in a list object with weights)

cov

List of covariates for constraining solution

nd

Number of dimensions for summary output, by default the number of constraining dimensions

weight

FALSE (default) for no differential part weighting (i.e., equal weights), TRUE for weights equal to compositional part means, or a user-provided vector of part weights

suprow

Indices of rows that are supplementary (passive) points (NOTE: this option is not implemented in this version)

row.wt

Optional user-defined set of positive weights for the rows (samples) (default: equal weights)

Details

The function RDA computes a redundancy analysis of a matrix of interval-scaled data, constrained by a matrix of covariates, using the singular value decomposition. The unweighted logratio analysis, with weight=FALSE, is the default. If weight = TRUE (the default) it is assumed that the weights are included in the data object, which comes from one of the logratio functions. User-specified part weights can be provided using the same weight option.

Usually row weights are not specified, they are equal unless intentional weighting of the samples is desired. Supplementary rows can be declared (also known as passive points) – these do not contribute to the solution but are positioned on the solution axes.

Value

sv

Singular values

nd

Number of dimensions in the solution output

rownames

Row names

rowmass

Row weights

rowdist

Row distances to centroid

rowinertia

Row variances

rowcoord

Row standard coordinates

rowpcoord

Row principal coordinates

rowsup

Indices of row supplementary points

colnames

Column names

colmass

Column weights

coldist

Column logratio distances to centroid

colinertia

Column variances

colcoord

Column standard coordinates

colpcoord

Column principal coordinates

covcoord

Regression coordinates of constraining variables

covnames

Names of constraining variables

N

The data table (usually logratios in this package)

cov

The table of covariates

Author(s)

Michael Greenacre

References

Van den Wollenbergh, A. (1977), Redundancy analysis. An alternative to canonical correlation analysis, Psychometrika 42, 207-219.
Greenacre, M. (2013), Contribution biplots, Journal of Computational and Graphical Statistics 22, 107-122.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.

See Also

PLOT.RDA, CLR, PLR, ALR, DUMMY

Examples

# Data set FishMorphology has sex, habitat and mass in first columns, 
# then morphometric data in remaining 26 columns
data(FishMorphology)
sex     <- FishMorphology[,1]
habitat <- FishMorphology[,2]
mass    <- FishMorphology[,3]
fish    <- as.matrix(FishMorphology[,4:29])
# Convert to compositional data matrix
fish    <- CLOSE(fish)
# Compute logarithm of mass and interaction of sex (F/M) and habitat (L/P) categories
logmass <- log(mass)
sexhab  <- 2*(sex-1)+habitat
sexhab.names <- c("FL","FP","ML","MP")
rownames(fish) <- sexhab.names[sexhab]
# Create dummy variables for sexhab and create matrix of covariates
sexhab.Z <- DUMMY(sexhab, catnames=sexhab.names)
vars     <- cbind(logmass, sexhab.Z)
# Perform RDA on centred logratios constrined by vars
fish.rda <- RDA(CLR(fish), cov=vars)
# Plot results (see Appendix C of book for more oprtions)
PLOT.RDA(fish.rda, map="asymmetric", rescale=c(0.02,0.005), group=sexhab, indcat=2:5, 
         groupcols=rainbow(4, start=0.1, end=0.8), grouppchs=rep(19, 4), 
         groupnames=sexhab.names, labelrows=FALSE, ellipse=1, legendpos="topright")	

Dataset: RomanCups

Description

This data set consists of the compositions of 11 oxides in 47 Roman cups found at an archaeological site in eastern England. Compositions are expressed as percentages.

Usage

data(RomanCups)

Format

Data frame containing the 47 x 11 matrix.

Source

Baxter MJ, Beardah CC, Cool HEM and Jackson CM (2005) Compositional data analysis of some alkaline glasses. Mathematical Geology 37: 183-196.


Amalgamation (summed) logratio

Description

Computation of a single amalgamation (summed) logratio

Usage

SLR(data, numer=NA, denom=NA, weight=FALSE)

Arguments

data

A compositional data frame or matrix

numer

Vector of parts in the numerator

denom

Vector of parts in the denominator

weight

Logical indicating if a varying weight is returned (default:TRUE). If FALSE, a weight based on equally-weighted parts is returned. Alternatively a positive weight can be specified.

Details

The function SLR computes a single amalgamation logratio based on the specified numerator and denominator parts that define the two summations in the ratio.

Value

LR

The amalgamation (summed)) logratio (SLR)

LR.wt

The weight assigned to the SLR

Author(s)

Michael Greenacre

References

Aitchison, J. (1986), The Statistical Analysis of Compositional Data, Chapman & Hall.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.
Greenacre, M. (2020), Amalgamations are valid in compositional data analysis, can be used in agglomerative clustering, and their logratios have an inverse transformation. Applied Computing and Geosciences, 5: 100017. Open access.

See Also

ALR, CLR, PLR

Examples

data(Vegetables)
SLR(Vegetables, numer=2, denom=c(1,3))

Stepwise selection of logratios

Description

Stepwise selection of pairwise logratios that explain maximum variance in a target matrix.

Usage

STEP(data, datatarget=data, previous=NA, previous.wt=NA, weight=FALSE, 
     random=FALSE, nsteps=min(ncol(data), ncol(datatarget))-1, top=1)

Arguments

data

A data frame or matrix of compositional data on which pairwise logratios are computed

datatarget

A matrix of interval-scale data, with as many rows as data, which serves as the target matrix whose variance is to be explained (by default it is the same matrix as data, in which case total logratio variance is to be explained)

previous

A vector or matrix of variables to be forced in before logratios are sought

previous.wt

Possible weights of the variable(s) forced in before logratios are sought (if not specified, weights of 1 are assumed)

weight

Logical indicating if parts are weighted or not (default: FALSE for unweighted (i.e., equal weights)). If TRUE, parts are weighted by compositional means. Alternatively a set of positive weights can be specified.

random

TRUE if a random selection is made of tied logratios; FALSE (default) if logratio that maximizes Procrustes correlation is chosen

nsteps

Number of steps to take (by default, one less than the number of columns of data and of datatarget, whichever is smaller)

top

Number of top variance-explaining logratios returned after last step (by default, 1, i.e. the best)

Details

The function STEP sequentially computes the logratios in a data matrix (usually compositional) that best explain the variance in a second matrix, called the target matrix. By default, the target matrix is the same matrix, in which case the logratios that best explain the logratio variance in the same matrix are computed. In this case, weights for the data matrix are assumed by default, proportional to part means of the compositional data matrix. For the unweighted logratio variance, specify the option weight=FALSE. User-specified weights on the columns of the data matrix (usually compositional parts) can be provided using the same weight option.

If the target matrix is a different matrix, it is the logratio variance of that matrix that is to be explained. An option for the target matrix to be any response matrix will be in the next release.

If nsteps > 1 and top=1 the results are in the form of an optimal set of logratios that sequentially add maximum explained variance at each step. If top>1 then at the last step the ordered list of top variance-explaining logratios is returned, which allows users to make an alternative choice of the logratio based on substantive knowledge. Hence, if nsteps=1 and top=10, for example, the procedure will move only one step, but list the top 10 logratios for that step. If top=1 then all results with extension .top related to the top ratios are omitted because they are already given.

The package vegan is required.

Value

names

Names of maximizing ratios in stepwise process

ratios

Indices of ratios

logratios

Matrix of logratios

R2max

Sequence of maximum cumulative explained variances

pro.cor

Corresponding sequence of Procrustes correlations

names.top

Names of "top" ratios at last step

ratios.top

Indices of "top" ratios

logratios.top

Matrix of "top" logratios

R2.top

Sequence of "top" cumulative explained variances (in descending order)

pro.cor.top

Corresponding sequence of "top" Procrustes correlations

totvar

Total logratio variance of target matrix

Author(s)

Michael Greenacre

References

Van den Wollenbergh, A. (1977), Redundancy analysis. An alternative to canonical correlation analysis, Psychometrika 42, 207-219.
Greenacre, M. (2018), Variable selection in compositional data analysis using pairwise logratios, Mathematical Geosciences, DOI: 10.1007/s11004-018-9754-x.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC

See Also

CLR, PLR, ALR

Examples

# Stepwise selection of ratios for RomanCups data set
data(RomanCups)
require(vegan)
set.seed(2026)
STEP(RomanCups, random=TRUE)
# Select best ratio in one step, but output "top 5"
STEP(RomanCups, nsteps=1, top=5)

Stepwise selection of pairwise logratios for generalized linear modelling

Description

Three different algorithms for selecting pairwise logratios that best explain/predict a response variable, which could be continuous, binary or count

Usage

STEPR(data, y, method = 1, family = "gaussian", nsteps = ncol(data)-1, 
      top = 1, previous = NA, criterion = "Bonferroni", alpha = 0.05, 
      previousparts=NA, denom=NA)

Arguments

data

A data frame or matrix of compositional data on which the pairwise logratios will be constructed and selected

y

The response variable: a numeric variable for regression (default)), a binary factor for logistic regression or a numeric count for Poisson regression

method

The selection method: 1 (unrestricted selection of logratios), 2 (restricted to non-overlapping parts), 3 (additive logratios)

family

The distribution used in the generalized linear model family: "gaussian" (default, for multiple regression), "binomial" (for logistic regression of binary response), or "poisson" (for Poisson regression)

nsteps

The maximum number of steps taken, by default one less than the number of parts

top

When one step is taken (nsteps=1), the ordered list of top logratios with the highest improvements in the likelihood function, for selection based on domain knowledge

previous

For specifying variable(s) to be included before stepwise selection takes place; these can be non-compositional variables and/or specific pairwise logratios computed in previous runs of STEPR or by hand; the matrix (or vector for a single variable) of values must be supplied

criterion

Criterion for stopping the stepwise selection: "Bonferroni" (default), "AIC", "BIC", or NA for no stopping until maximum specified or permissible logratios entered

alpha

Overall significance level (default is 0.05)

previousparts

(For method 2) The sequence numbers of the logratios, if any, forced in using the previous option

denom

(For method 3) The sequence number of the part used in denominator; for use when additive logratios are forced in using previous option or to select a set of additive logratios with specific reference from the start

Details

The function STEPR performs stepwise selection of pairwise logratios with the objective of explaining/predicting a response variable, in the framework of generalized linear modelling where the response can be numeric continuous (regression analysis), or a binary factor (logistic regression), or a numeric count (Poisson regression). The corresponding family option has to be indicated if the regression is logistic or Poisson. The different method options for the stepwise selection are method = 1 (unrestricted selection of logratios, any logratios can be selected irrespective of the previous ones), method = 2 (restricted to non-overlapping parts, each part participates at most in one logratio, so that parts in previously selected logratios are excluded in subsequent steps; logratio effects can be interpreted as under orthogonality), method = 3 (additive logratios, only logratios with the same denominator as the first selected logratio are c onsidered; the result is an additive logratio transformation on a subcomposition) Three alternative stopping criteria can be specified, otherwise the procedure executes as many steps as the value of nsteps. These are (in increasing strictness), "AIC", "BIC" and "Bonferroni" (the default).

Value

rationames

Names of the selected logratios

ratios

The sequence numbers of the selected parts in each ratio

logratios

Matrix of selected logratios

logLik

The -2*log-likelihood sequence for the steps

deviance

The deviance sequence for the steps

AIC

The AIC sequence for the steps

BIC

The BIC sequence for the steps

Bonferroni

The Bonferroni sequence for the steps

null.deviance

The null deviance for the regression

(Notice that for logLik, AIC, BIC and Bonferroni, the values for one more step are given, so that the stopping point can be confirmed.)

And the following if top > 1:

ratios.top

The top ratios and the sequence numbers of their parts

logratios.top

The matrix of top logratios

logLik.top

The set of top -2*log-likelihoods

deviance.top

The set of top deviances

AIC.top

The set of top AICs

BIC.top

The set of top BICs

Bonferroni.top

The set of top Bonferronis

Author(s)

Michael Greenacre

References

Coenders, G. and Greenacre, M. (2021), Three approaches to supervised learning for compositional data with pairwise logratios. aRxiv preprint. URL:https://arxiv.org/abs/2111.08953
Coenders, G. and Pawlowsky-Glahn, V. (2020), On interpretations of tests and effect sizes in regression models with a compositional predictor. SORT, 44:201-220
Greenacre, M. (2021), Compositional data analysis, Annual Review of Statistics and its Application, 8: 271-299

See Also

ALR, STEP, glm

Examples

# For the fish morphometric data, first close (normalize, although not necessary) 
# then loop over the 26*25/2 = 325 possible logratios stepwise
data(FishMorphology)
habitat <- FishMorphology[,2]
fish    <- CLOSE(FishMorphology[,4:29])
# predict habitat binary classification from morphometric ratios
fish.step1 <- STEPR(fish, as.factor(habitat), method=1, family="binomial")
# [1] "Criterion increases when 3-th ratio enters"
fish.step1$names
# [1] "Bac/Hg" "Hw/Jl"
# perform logistic regression with selected logratios
fish.glm   <- glm(as.factor(habitat) ~ fish.step1$logratios, family="binomial")
summary(fish.glm)
fish.pred1  <- predict(fish.glm)
table(fish.pred1>0.5, habitat)
#     habitat
#        1  2
# FALSE 56 11
# TRUE   3  5
# (Thus 61/75 correct predictions)
#
# force the sex variable in at the first step before selecting logratios
# and using more strict Bonferroni default
sex <- FishMorphology[,1] 
fish.step2 <- STEPR(fish, as.factor(habitat), method=1, previous=sex, family="binomial")
# [1] "Criterion increases when 3-th ratio enters"
fish.step2$names
# [1] "Bac/Hg" "Hw/Jl"
# perform logistic regression with sex and selected logratios
fish.glm   <- glm(as.factor(habitat) ~ sex + fish.step2$logratios, family="binomial")
summary(fish.glm)
# (sex not significant)
#
# check the top 10 ratios at Step 1 to allow domain knowledge to operate
fish.step3 <- STEPR(fish, as.factor(habitat), method=1, nsteps=1, top=5, family="binomial")
cbind(fish.step3$ratios.top, fish.step3$BIC.top)
#        row col         
# Bac/Hg   8  19 67.93744
# Bp/Hg    7  19 69.87134
# Jl/Hg    6  19 70.31554
# Jw/Bp    5   7 71.53671
# Jw/Jl    5   6 71.57122
# Suppose 5th in list, Jw/Jl (Jaw width/Jaw length), preferred at the first step
fish.step4 <- STEPR(fish, as.factor(habitat), method=1, 
                    previous=fish.step3$logratios.top[,5], family="binomial")
# [1] "Criterion increases when 2-th ratio enters"
fish.step4$names
# [1] "Bac/Hg"
# So after Jw/Jl forced in only Bac/Hg enters, the best one originally

Assessing subcompositional coherence

Description

Computation of several measures comparing results on subcompositions with the corresponding results in a composition.

Usage

SUBCO(data, nsub=100, propsub=1/3, weighted=TRUE, power=NA, chi=FALSE,                
      procrustes=TRUE, distances=TRUE, correlations=TRUE, covariances=TRUE, seed=123)

Arguments

data

A data frame or matrix of compositional data, which can contain zeros.

nsub

Number of random subcompositions for measuring subcomositional coherence

propsub

Proportion of parts in the random subcompositions (for measuring subcomositional coherence (default=1/3)

weighted

Whether the subcompositions are selected by weighted or unweighted random sampling (default is weighted=TRUE)

power

OPtional power transfomr of the compositional data.

chi

Optional chi-square standardization of the compositional data

procrustes

Computes Procrustes correlation between compositional and subcompositional part geoetries

distances

Computes concordance coefficient between compositional and subcompositional part distances

correlations

Computes concordance coefficient between compositional and subcompositional part correaltions

covariances

Computes concordance coefficient between compositional and subcompositional part covariances

seed

Value of seed for reproducibility of the random sampling

Details

The function SUBCO takes a compositional data set, including the zeros, and compares results on random subcompositions with the corresponding results on the original composition. Various measures of subcompositional coherence are computed in the results, and the user can select between Procrustes correlations, and concordance measures for distances, correlations and covariances, for the subcompositional parts and the corresponding parts in the composition. The results can be summarized using summary of the SUBCO object.

Value

procrustes.pro

Procrustes correlation between subcompositional and compositional part geometries

nrocrustes.chi

Same as procrusts.pro but for compositions that have been chi-square standardized

concord.distance.pro

Concordances between distances on subcompositional parts and those in the composition

concord.distance.chi

Same as concord.distance.pro but for chi-square standardized data

concord.cor

Concordances between subcompositional part correaltions and the corresponding compositional ones

concord.cov.pro

Concordances between subcompositional part covariances and the corresponding compositional ones

concord.cov.chi

Same as concord.cov.pro but for chi-square standardized data

Author(s)

Michael Greenacre

References

Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.
Lin, L. I.-K. (1989). A concordance correlation coefficient to evaluate reproducibility. Biometrics 45: 255-268.
Martinez-Alvara, M., Greenacre, M. and Blasco, A. (2026) Omics data in relative abundances are almost subcompositionally coherent. Frontiers in Microbiology 17: 1809364. )

See Also

CONCORD

Examples

# measure how close the Kimberlite compositions are to subcompositional coherence 
# this example has been trimmed to meet CRAN's limit on example execution time      
data(Kimberlite)
kim <- Kimberlite[,3:24]
kim.subco <- SUBCO(kim, procrustes=FALSE, distances=FALSE, covariances=FALSE)
summary(kim.subco)

Variance of a vector of observations, dividing by n rather than n-1

Description

This function computes the usual variance but divides by n, not by n-1.

Usage

VAR(x)

Arguments

x

Vector of values for which variance is computed

Details

To think of each of n observations weighted by 1/n this function VAR computes squared deviations from the mean and averages them. Thus, the sum of squared deviations is divided by n rather than by n-1, as for the unbiased estimate of the variance.

Value

The value of this modified variance, often called the population variance.

Author(s)

Michael Greenacre

References

Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC.

See Also

LR.VAR, CLOSE

Examples

data(RomanCups)
cups <- CLOSE(RomanCups)

# variances using base R function var
apply(cups, 2, var)

# variances using easyCODA function VAR
apply(cups, 2, VAR)

Dataset: Vegetables

Description

This data set consists of the protein, carbohydrate and fat compositions of 10 different vegetables. Compositions are expressed as percentages.

Usage

data(Vegetables)

Format

Data frame containing the 10 x 3 matrix.

Source

US Department of Agriculture, https://ndb.nal.usda.gov/ndb/nutrients/index


Ward clustering of a compositional data matrix

Description

This function clusters the rows (or the columns, if the matrix is transformed) of a compositional data matrix, using weighted Ward clustering of the logratios.

Usage

WARD(LRdata, weight=FALSE, row.wt=NA)

Arguments

LRdata

Matrix of logratios, either a vector or preferably the logratio object resulting from one of the functions ALR, CLR, or PLR (usually CLRs will be used))

weight

FALSE (default) for no differential part weighting (i.e., equal weights), TRUE for weights equal to compositional part means, or a user-provided vector of part weights

row.wt

Optional set of row weights (default is equal weights when row.wt=NA)

Details

The function WARD performs a weighted WARD hierarchical clustering on the rows of an input set of logratios, usually CLR-transformed. (This would be equivalent to performing the clustering on all pairwise logratios). The default option weight=FALSE assigns equal weights to the parts. The option weight=TRUE implies that column weights are provided, either in the input list object, or as a vector of user-specified weights using the same weight option. Following the standard R convention of hclust(..., method = "ward.D2"), the heights returned in this object represent the unsquared geometric distances (square roots). To reconstruct or verify the total log-ratio variance decomposition from the tree, you must sum the squares of the heights: sum(tree$height^2) -- see the examples below.

Value

An object which describes the tree produced by the clustering process on the n objects. The object is a list with components:

merge

an n-1 by 2 matrix. Row i of merge describes the merging of clusters at step i of the clustering. If an element j in the row is negative, then observation -j was merged at this stage. If j is positive then the merge was with the cluster formed at the (earlier) stage j of the algorithm. Thus negative entries in merge indicate agglomerations of singletons, and positive entries indicate agglomerations of non-singletons.

height

a set of n-1 real values (non-decreasing for ultrametric trees). The clustering height: that is, the value of the criterion associated with the clustering method for the particular agglomeration.

order

a vector giving the permutation of the original observations suitable for plotting, in the sense that a cluster plot using this ordering and matrix merge will not have crossings of the branches

Author(s)

Michael Greenacre

References

Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions Chapman & Hall / CRC.

See Also

hclust, CLR, LR.VAR

Examples

# clustering steps for unweighted and weighted logratios
# for both row- and column-clustering

data(Vegetables)

### WARD -----------------------------------------------------------------------
# clustering steps for unweighted and weighted logratios
# for both row- and column-clustering
data(RomanCups)
cups <- CLOSE(RomanCups)

# unweighted logratios: clustering samples
cups.uclr  <- CLR(cups)
cups.uward <- WARD(cups.uclr)   # weight=FALSE not needed here
plot(cups.uward)
# add up the heights of the nodes
sum(cups.uward$height^2)
# [1] 0.02100676
# check against the total logratio variance
LR.VAR(cups.uclr)
# [1] 0.02100676

# unweighted logratios: clustering parts
tcups <- t(cups)
tcups.uclr  <- CLR(tcups)
tcups.uward <- WARD(tcups.uclr)  
plot(tcups.uward, labels=colnames(cups))
sum(tcups.uward$height^2)
# [1] 0.02100676
LR.VAR(tcups.uclr)
# [1] 0.02100676

# weighted logratios: clustering samples
cups.clr <- CLR(cups, weight=TRUE)
cups.ward <- WARD(cups.clr, weight=TRUE)
plot(cups.ward)
sum(cups.ward$height^2)
# [1] 0.002339335
LR.VAR(cups.clr, weight=TRUE)
# [1] 0.002339335

# weighted logratios: clustering parts
# the default weight=FALSE now gives equal weights
# to the samples (columns of tcups)
tcups.clr  <- CLR(CLOSE(tcups))
tcups.ward <- WARD(tcups.clr, row.wt=colMeans(cups))
plot(tcups.ward, labels=colnames(cups))
sum(tcups.ward$height^2)
# [1] 0.002339335
LR.VAR(tcups.clr, row.wt=colMeans(cups))
# [1] 0.002339335

The chiPower transformation

Description

Transformation of compositional data with power transformation and chi-square normalization.

Usage

chiPower(X, close=TRUE, power=1, chi=TRUE, BoxCox=TRUE, CLR=TRUE)

Arguments

X

the compositional data matrix

close

close the data after powering; default is TRUE

power

power of the transformation; default is 1 (no powering)

chi

apply chi-square normalization; default is TRUE

BoxCox

apply Box-Cox style of transformation; default is TRUE

CLR

translate columns so that convergence is to CLR transformation; default is TRUE

Details

The function chiPower generates the chiPower transformation to new variables as alternatives to logratios, with the advantage that these new variables converge to centred logratios as the power decreases (for positive data). This transformation is convenient for data with zeros, but then the power needs to be monitored.

Value

new chiPower-transformed variables

Author(s)

Michael Greenacre

References

Greenacre, M. (2024). The chiPower transformation: a valid alternative to logratio transformations in compositional data analysis. Advances in Data Analysis and Classification, https://doi.org/10.1007/s11634-024-00600-x

See Also

CLR

Examples

# transform a compositional data set to chiPower variables   
data(Kimberlite)
kim <- CLOSE(Kimberlite[,3:24])
kim.chiP <- chiPower(kim)

Compositional Data Analysis in Practice

Description

Univariate and multivariate methods for compositional data analysis, based on logratios. The package implements the approach in the book Compositional Data Analysis in Practice by Michael Greenacre (2018, 2026), where accent is given to simple pairwise logratios. Selection can be made of logratios that account for a maximum percentage of logratio variance or maximize prediction accuracy of a response variable. Various multivariate analyses of logratios are included in the package.

Details

The DESCRIPTION file:

Package: easyCODA
Type: Package
Version: 0.50
Date: 2026-11-23
Title: Compositional Data Analysis in Practice
Authors@R: person(given = "Michael", family = "Greenacre", role = c("aut", "cre"), email = "michael.greenacre@upf.edu")
Author: Michael Greenacre [aut, cre]
Maintainer: Michael Greenacre <michael.greenacre@upf.edu>
Description: Univariate and multivariate methods for compositional data analysis, based on logratios. The package implements the approach in the book Compositional Data Analysis in Practice by Michael Greenacre (2018, 2026), where accent is given to simple pairwise logratios. Selection can be made of logratios that account for a maximum percentage of logratio variance or maximize prediction accuracy of a response variable. Various multivariate analyses of logratios are included in the package.
License: GPL (>= 2)
Imports: ca (>= 0.7), vegan (>= 2.3), ellipse (>= 0.4.1), boot, stats, graphics, grDevices, utils
LazyData: true
ByteCompile: true
URL: https://github.com/michaelgreenacre/easyCODA, https://github.com/michaelgreenacre/CODAinPractice
BugReports: https://github.com/michaelgreenacre/easyCODA/issues
NeedsCompilation: no

Index of help topics:

ACLUST                  Amalgamation clustering of the parts of a
                        compositional data matrix
ALR                     Additive logratios
BAR                     Compositional bar plot
CA                      Correspondence analysis
CIplot_biv              Bivariate confidence and data ellipses
CIplot_uni              Univariate confidence plots
CLOSE                   Closure of rows of compositional data matrix
CLR                     Centred logratios
CONCORD                 Concordance coefficient
DOT                     Dot plot
DUMMY                   Dummy variable (indicator) coding
FINDALR                 Find the best ALR transformation
FishMorphology          Dataset: FishMorphology
Kimberlite              Dataset: Kimberlite
LR.VAR                  Total logratio variance
LRA                     Logratio analysis
PCA                     Principal component analysis
PLOT                    Plot the results of a logratio analysis,
                        principal component analysis or correspondence
                        analysis
PLOT.RDA                Plot the results of a redundancy analysis
PLR                     All pairwise logratios
RDA                     Redundancy analysis
RomanCups               Dataset: RomanCups
SLR                     Amalgamation (summed) logratio
STEP                    Stepwise selection of logratios
STEPR                   Stepwise selection of pairwise logratios for
                        generalized linear modelling
SUBCO                   Assessing subcompositional coherence
VAR                     Variance of a vector of observations, dividing
                        by n rather than n-1
Vegetables              Dataset: Vegetables
WARD                    Ward clustering of a compositional data matrix
chiPower                The chiPower transformation
easyCODApackage         Compositional Data Analysis in Practice
invALR                  Inverse of additive logratios
invCLR                  Inverse of centred logratios
invSLR                  Inverse of full set of amalgamation balances

Author(s)

Michael Greenacre [aut, cre]

Maintainer: Michael Greenacre <michael.greenacre@upf.edu>

References

Greenacre, Michael (2018, 2026) Compositional Data Analysis in Practice, 1st and 2nd editions. Chapman & Hall / CRC Press

See Also

ca

Examples

# Roman cups glass compositions
# Notice that from version 0.50 of easyCODA onwards, coinciding with the 
# 2nd edition of COmpositional Data Analysis in Practice, the function default 
# is now weight = FALSE for unweighted analysis (i.e., equally weighted parts) 
data(RomanCups)
# unweighted logratio analysis
cups.ulra <- LRA(RomanCups)
PLOT(cups.ulra)
# weighted logratio analysis
cups.wlra <- LRA(RomanCups, weight=TRUE)
PLOT(cups.wlra)

Internal S3 Methods

Description

Internal S3 methods for summarizing and printing objects.


Inverse of additive logratios

Description

Given additive logratios (ALRs) with respect to a specified part, compute the inverse (i.e. original parts)

Usage

invALR(ALRdata, part.names=NULL, denom=NA)

Arguments

ALRdata

A matrix of additive logratios (ALRs) with respect to a specified part)

part.names

Part names in the reconstructed compositional data matrix

denom

The index of the denominator used in the computation of the ALRs (default: last part))

Details

The function invALR computes the original parts, given the additive logratios (ALRs)

Value

parts

The reconstructed parts (they add up to 1)

Author(s)

Michael Greenacre

References

Aitchison, J. (1986), The Statistical Analysis of Compositional Data, Chapman & Hall.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.

See Also

ALR, PLR, CLR, invCLR, LR.VAR

Examples

data(Vegetables)
# compute additive logratios with respect to second part
veg.ALR <- ALR(Vegetables, denom=2)$LR
# recover original parts (to get same order, specify the denominator used originally)
invALR(veg.ALR, part.names=colnames(Vegetables), denom=2)

Inverse of centred logratios

Description

Given centred logratios (CLRs), compute the inverse (i.e., recover the original parts)

Usage

invCLR(CLRdata, part.names=NULL)

Arguments

CLRdata

A matrix of centred logratios

part.names

Part names in the reconstructed compositional data matrix

Details

The function invCLR computes the original parts, given the centred logratios (CLRs)

Value

parts

The reconstructed parts (they add up to 1)

Author(s)

Michael Greenacre

References

Aitchison, J. (1986), The Statistical Analysis of Compositional Data, Chapman & Hall.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.

See Also

CLR, ALR, invALR, LR.VAR

Examples

data(Vegetables)
# compute centred logratios
veg.CLR <- CLR(Vegetables)$LR
# invert back to original parts (parts closed to sum to 1)
invALR(veg.CLR)

Inverse of full set of amalgamation balances

Description

Given a full set of amalgamation (or summation) balances (SLRs), compute the inverse (i.e. recover the original parts)

Usage

invSLR(SLRdata, part.names=NULL, ratio.names=NULL)

Arguments

SLRdata

A matrix of amalgamation logratios, one less column than the number of parts

part.names

Part names in the reconstructed compositional data matrix

ratio.names

Definition of the amalgamation logratios

Details

The function invSLR computes the original parts, given the amalgamation logratios (CLRs). The amalgamation logratios are specified in ratio.names in the format num/den where num and den are the numerator and denominator amalgamations respectively. An amalgamation is specified as "p1&p2&...", where p1, p2, etc. are the parts summed in the amalgamation. For example, an SLR of the ratio MnO/(CaO+P2O5) would be names as "MnO/CaO&P2O5". If the SLRs were created using gunction SLR, then the ratio.names are in their names – see example below.

Value

parts

The reconstructed parts (they add up to 1)

Author(s)

Michael Greenacre

References

Aitchison, J. (1986), The Statistical Analysis of Compositional Data, Chapman & Hall.
Greenacre, M. (2020), Amalgamations are valid in compositional data analysis, can be used in agglomerative clustering, and their logratios have an inverse transformation. Applied Computing and Geosciences, 5: 100017. Open access.
Greenacre, M. (2018, 2026), Compositional Data Analysis in Practice, 1st and 2nd editions, Chapman & Hall / CRC Press.

See Also

SLR, LR.VAR

Examples

data(Vegetables)
# compute two SLRs, logs of Carbohydrate / (Protein+Fat)), and Protein/Fat
veg.SLR <- cbind(SLR(Vegetables, numer=2, denom=c(1,3))$LR, SLR(Vegetables, numer=1, denom=3)$LR)
# invert back to original parts (parts closed to sum to 1)
invSLR(veg.SLR, part.names=colnames(Vegetables), ratio.names=colnames(veg.SLR))

mirror server hosted at Truenetwork, Russian Federation.