R Compositions Package Overview
R Compositions Package Overview
Abstract
compositions is a package for the the analysis of (e.g. chemical) com-
positions. Compositions are typically vectors of positive (or non negative)
numbers, where often the sum is either to a constant like 100%, in case of full
compositions, or meaningless, in case of subcompositions where meaningless
parts have been removed from the full composition. The package and this doc-
ument can be retrieved from [Link]
1 License
This document is distributed together with the package composition under the
GNU public license version 2.0 or newer. Please cite the package and/or this doc-
ument when you are using it for publications.
"rplus": The total amount is meaningful and data is analyzed in real (non
relative) geometry.
This approach is mainly equivalent to analyze the data as is with classical
multivariate methods. This approach is inappropriate for many examples of
1
datasets of amounts due many reasons including heteroskedastisity, strong
skewness and external or articial multiplicative errors on the whole dataset.
"rcomp": The total amount is meaningless or the individual amounts are part
of a whole (in equal units) and the data should be analyzed in real (non rela-
tive) geometry.
This class represents the classical view of compositions as a part of the math-
ematical simplex (the set of vectors of nonnegative numbers summing to 1).
Also a widely used approach, it has some traps and can lead to wrong in-
terpretations [Chayes(1960)] [Aitchison(1986)] and has to be used with great
care.
"acomp": The total amount is meaningless or the individual amounts are part
of a whole (in equal units) and the data should be analyzed in a relative ge-
ometry.
This class is based on the logistic approach of compositional data introduced
by John Aitchison [Aitchison(1982)] [Aitchison(1986)] [Aitchison(1997)] that
has greatly evolved in the past years [Aitchison(2002)] [Aitchison and Greenacre(2002)]
[Barceló-Vidal et al.(2001)Barceló-Vidal, Martín-Fernández, and Pawlowsky-Glahn]
[Buccianti et al.(1999)Buccianti, Pawlowsky-Glahn, Barceló-Vidal, and Jarauta-Bragulat]
[Egozcue, Pawlowsky-Glahn, Mateu-Figueras, and Barceló-Vidal(2003)Egozcue and others]
[Pawlowsky-Glahn and Egozcue(2001)] [Pawlowsky-Glahn and Egozcue(2002)]
[von Eynatten et al.(2002)von Eynatten, Pawlowsky-Glahn, and Egozcue]. This
approach can be seen as the modern approach to compositional data analysis.
However under some circumstances the approach has been criticized in favor of
the more classical "rcomp" approach [Rehder and Zier(2002)] [Shurtz(2003)].
For a deeper understanding of the acomp approach the reader is referred to in
[Barceló-Vidal et al.(2001)Barceló-Vidal, Martín-Fernández, and Pawlowsky-Glahn].
"aplus": The total amount is meaningful and the data should be analyzed in
relative geometry.
This approach evolved from mixing the ideas of compositional data analysis by
John Aitchison in the view of [Pawlowsky-Glahn(2003)] with the assumption
of a meaningful total. It is quite near to a simple log-transform approach,
which is quite common for geochemical data. However we try to stay more
consistent in the concept and try to allow to analyze the original data in a log
geometry rather than just log transformed, to keep the relation to the original
measurements.
An auxiliary class "rmult" is used to model simple vector valued data in a classical
fashion. It is mainly used internally although in theory it provides a nice interface
to multivariate data analysis.
> library(compositions)
2
cor cov dist var
%*%
> data(SimulatedAmounts)
> comps <- acomp([Link]) # View data as compositions
> plot(comps) # produces Ternary diagrams
> amounts <- aplus([Link]) # View data as amounts
> plot(amounts) # Produces Scatterplotmatrix in log-scale
Depending on the type of the data assigned by the constructors acomp, aplus,
rcomp, rplus, rmult a dierent plot function called [Link] will be in-
voked an plots the data in a fashion most feasible for the given datatype. This
principle used used all over the package.
> mean(comps)
Cu Zn Pb
0.08918175 0.23949922 0.67131903
attr(,"class")
[1] "acomp"
> mean(amounts)
Cu Zn Pb
3.018042 8.105008 22.718430
attr(,"class")
[1] "aplus"
> dat <- comps
To keep a maximum of similarity we can apply the same instructions to a dataset
of a dierent type to perform a similar task with methods applicable to the other
data type. Thus you should try afterwards the same instructions with
4 Statistical Graphics
4.1 Ternary diagrams
The rst steps in a data analysis should always be plots. The classical plot for
compositional data is the ternary diagram. This package also contains advanced
treatment for high dimensional compositions.
3
> plot(dat)
A ternary diagram has 3 not perpendicular axes. Each corner of a ternary diagram
is associated one part of the composition. The location of a point in a ternary
diagram has two main interpretations. Any composition on a line parallel to the
axis opposite to a corner has the same portion of that component. The portion
corresponds to the relative distance to the line to the axis on the distance of the
corner to the axis. A second interpretation that all points on a straight line through
one of the corners have equal relative portions of the two remaining components.
This portion is given by the relative portions represented by the point, where the
line crosses the opposite axis.
Several informations can be added to ternary diagrams:
plot(acomp(sa.lognormals5))
plot(acomp(sa.lognormals5),margin="rcomp")
plot(acomp(sa.lognormals5),margin="Cu")
barplot(dat)
barplot(acomp(dat[1:10,]))# a subset only
barplot(mean(dat)) # the mean only
barplot(dat-mean(dat)) # relative changes against the mean
pie(mean(dat)) # Only one composition at a time can be drawn
The piechart is not part of the package.
4.3 Boxplots
An basic principle in compositions is, that while the individual quantities are in-
uenced by everything, relative portions of two components are meaningful and
relatively easy to interpret, since the eect of the other components, which could
eventually extrude the two parts, is removed. The boxplot function shows a matrix
displaying this relative amounts of all pairs in the dataset.
4
5 Descriptive Statistics
Various descriptive statistics can be easily computed:
5
totals command.
> totals(acomp(amounts))
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[39] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
> totals(aplus(amounts))
[1] 89.86653 51.27057 54.28445 18.31066 31.19352 21.82243 39.43808
...
The constructor acomp closes the data to 1 (or the amount given by the total
command line option) and thus totals can not be retrieved afterwards. The total
sum can be reconstituted by the optional total= parameter of the constructors:
> acomp(dat5,c("Pb","Cd","Co"))
Pb Cd Co
[1,] 0.9630809 1.939768e-02 1.752142e-02
...
> acomp(dat5,1:3)
Cu Zn Pb
[1,] 0.427087826 0.421878850 0.15103332
...
6
The parts selected can be given either by names or column numbers. Both methods
can not be mixed. It is a major property of the acomp approach to be consistent
with taking subcompositions.
Another approach is that marginal compositions taking some interesting com-
ponents and the rest. The various approaches dier in how to make a rest. The
approach of taking just the sum of the rest is consistent with rcomp-approach and
computed by
> rcompmargin(dat5,c("Cd","Cu"))
Cd Cu +
[1,] 3.024494e-03 0.424629320 0.5723462
...
This approach often leads badly readable ternary diagrams since the rest is often
nearly everything. A sophisticated approach is that of taking the geometric mean
of the rest, which is consistent with the acomp-approach:
> acompmargin(dat5,c("Cd","Cu"))
Cd Cu *
[1,] 6.258330e-03 0.8786497 0.11509202
...
This approach as been proposed by Vera Pawlowsky-Glahn (as cited in the help
in this package). You can distinguish the margins, when selected implicitly in plot
functions by the symbol + or * to name them.
The most advanced concept is that of grouping parts together and to represent
each group by some mean amount. The conceptual approach of seeing the groups
of parts just as components of the original material for themself leads to grouping
by adding the parts in the groups. This approach is consistent with the rcomp
approach and computed by
> dat5
Cu Zn Pb Cd Co
[1,] 0.424629320 0.419450330 0.15016391 3.024494e-03 2.731947e-03
...
> groupparts(rcomp(dat5),Cparts=c("Cu","Cd","Co"),Zparts=c("Zn"),Pparts=c("Pb"))
Cparts Zparts Pparts
[1,] 0.430385760 0.419450330 0.15016391
...
An aggregation approach more consistent with the relative geometry of acomp is
that of taking geometric means instead of sums:
> groupparts(dat5,Cparts=c("Cu","Cd","Co"),Zparts=c("Zn"),Pparts=c("Pb"))
Cparts Zparts Pparts
[1,] 0.0259834679 0.717242525 0.25677401
...
This approach seams to change everything and to be dicult to understand . How-
ever it is linearly consistent with taking subcompositions and changing units and so
on and can not lead to false conclusion because the sequence of data treatment. The
approach is simplication of the approach in [Egozcue, J.J. and V. Pawlowsky-Glahn (2005)],
which proposes a reweighting of the geometric means to achieve isometry of the
transformation. However full isometry can not be achieved when things are seen
as compositions afterwards. The groupparts function also exists for the classes
rplus using sums and aplus using compositions.
7
6.3 Transformations
All the underlying spaces of the four classes can be mapped into a classical coordi-
nate based vectorspace by some transformations. The package provides all the trans-
formations dened for the Aitchison simplex alr (additive log ration)[Aitchison(1986)],
clr (centered log ratio)[Aitchison(1986)] and ilr (isometric log ratio) [Egozcue, Pawlowsky-Glahn, Mateu-Figueras,
The concept of transformations is discussed in detail in [Pawlowsky-Glahn(2003)]
and further in [Pawlowsky-Glahn and Mateu-Figueras(2005)].
> dat
Cu Zn Pb
[1,] 0.097971136 0.391326782 0.51070208
...
attr(,"class")
[1] "acomp"
> clr(dat)
Cu Zn Pb
[1,] -1.011994527 0.3728755437 0.639118984
...
attr(,"class")
[1] "rmult"
> [Link](clr(dat))
Cu Zn Pb
[1,] 0.097971136 0.391326782 0.51070208
...attr(,"class")
[1] "acomp"
> ilr(dat)
[,1] [,2]
[1,] -1.239435107 -0.188262542
...
attr(,"class")
[1] "rmult"
> [Link](ilr(dat)) # No rownames
8
transform is given by the generic functions cdt (centered default transform) and
the isometric (bijective, isometric) transform is given by idt (isometric default
transform):
> cdt(dat) # clr, cpt, ilt, iit for acomp, rcomp, aplus, rplus
Cu Zn Pb
[1,] -1.011994527 0.3728755437 0.639118984
...
attr(,"class")
[1] "rmult"
> idt(dat) # ilr, ipt, ilt, iit for acomp, rcomp, aplus, rplus
[,1] [,2]
[1,] -1.239435107 -0.188262542
...
attr(,"class")
[1] "rmult"
6.4 Operations
The neutral element of the space (i.e. the 0) is given by rep(1/D,D). For a deeper
understanding of these space structures of the class acomp the reader is referred to
[Barceló-Vidal et al.(2001)Barceló-Vidal, Martín-Fernández, and Pawlowsky-Glahn].
For the amounts in relative scale (class aplus) the vector space structure is
given by similar operations without closing the data to 1:
9
> acomp(c(1,2,3)) + 3* acomp(c(10,1,1))
[1] 0.995024876 0.001990050 0.002985075
attr(,"class")
[1] "acomp"
to datasets as a whole operating with an individual object or a whole dataset of the
same size again (like R typical parallel operation of vectors)
(dat - mean(dat))/msd(dat)
to be meaning full, which is just an isotropic scaling. Furthermore the vector spaces
are equipped with a scalar product and a norm [Barceló-Vidal et al.(2001)Barceló-Vidal, Martín-Fernández, and Paw
which according to the original denition of %*% in R are computed by the
%*% operator:
> acomp(c(1,2,1))%*%acomp(c(1/2,1,2))
[1] 0
For datasets the operator does not behave like matrix multiplication but like a
componentwise scalar product:
10
7 Multivariate Methods
The central idea of the package following the coordinate approach of [Pawlowsky-Glahn(2003)]
and [Pawlowsky-Glahn and Mateu-Figueras(2005)] is to transform the data by one
of transforms into a classical multivariate dataset, to apply classical multivariate
statistics and to back transform or interpreted the results afterwards in the original
space.
The package augments the standard principle component analysis with specic in-
terpretations in the given scale.
Standard deviations:
Comp.1 Comp.2
1.3604382 0.4460269
Comp.1 Comp.2
SS loadings 1.000 1.000
11
Proportion Var 0.333 0.333
Cumulative Var 0.333 0.667
> plot(pc) # screeplot
> plot(pc,type="variance") # other screeplot
> plot(pc,type="biplot") # biplot
> plot(pc,type="loadings") # loadings as compositions
> plot(pc,type="relative") # loadings of log-ratios
> ? [Link] # help
A detailed course in interpretation of the results goes far beyond the scope of this
software introduction. Not all possibilities have been discussed in literature until
now. However references are [Aitchison and Greenacre(2002)], [Pawlowsky-Glahn and Egozcue(2001)],
[Pawlowsky-Glahn(2003)], [Pawlowsky-Glahn and Mateu-Figueras(2005)].
The package does not contain any special routine for cluster analysis, however due
to its generic distance computation typical hclust usage is done in the selected
geometry and automatically consistent with the selected approach:
hc <- hclust(dist(dat,method="euclidean"),linkage="avarage")
# The other distance types '"euclidean"', '"maximum"', '"manhattan"',
# '"canberra"', and '"minkowski"' are also meaningfull here.
plot(hc) # [Link] showing the dendrogramm
plot(dat,col=cutree(hc,4),pch=20) # show 4 clusters in colors
This cluster analysis is automatically based on a meaningful distance computed
with the specied method in the cdt (see help) transform. At this time to compute
a kmeans-clustering should be done manually in Euclidean coordinates (which is
are explained in the help topic ilr) :
library(MASS)
library(mda)
# split a dataset into training and validation part:
selection <- sample(nrow([Link]),floor(nrow([Link])*0.7))
trainset <- acomp([Link][selection,])
traingroups <- [Link][selection]
testset <- acomp([Link][-selection,])
testgroups <- [Link][-selection]
12
predict(discr,newdata=idt(acomp(testset,c("clay","sand","gravel"))))
table(testgroups,predict(discr,newdata=idt(acomp(testset,c("clay","sand","gravel"))))$class)
barplot([Link](t(discr$scaling))) # Visualise the discrimination functions
plot(acomp([Link]),col=predict(discr,idt(acomp([Link],c("clay","sand","gravel"))))$class,p
plot(acomp([Link]),col=[Link],add=T)
Linear models can use any of the given scales as regressors or as response. However
we decided not to introduce special routines for that since one retains much more
exibility by using standard methods in conjunction with transformations. How-
ever this means that the user has to be aware of backtransforming. In case of a
compositional response this could like
Call:
lm(formula = ilr(y) ~ X, data = [Link](X = x))
Coefficients:
[,1] [,2]
(Intercept) -2.00642 -0.07032
XMiddle 1.83599 0.88136
XUpper 0.47219 -1.09672
> summary(manova(mylm))
Df Pillai approx F num Df den Df Pr(>F)
X 2 1.260 48.505 4 114 < 2.2e-16 ***
Residuals 57
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
> [Link](coefficients(mylm))
[,1] [,2] [,3]
(Intercept) 0.04102279 0.4556668 0.50331037
13
xMiddle 0.79781245 0.1570356 0.04515198
xUpper 0.40384157 0.1042933 0.49186510
attr(,"class")
[1] "acomp"
> plot([Link](resid(mylm)),col=x)
> plot(y,col=x)
> plot( [Link](predict(mylm)),add=T,pch=20,col=x)
> ellipses
> [Link](predict(mylm,newdata=[Link](X=factor(levels(x)))))
[,1] [,2] [,3]
1 0.04102279 0.4556668 0.5033104
2 0.25768467 0.5633886 0.1789267
3 0.05315796 0.1524882 0.7943539
attr(,"class")
[1] "acomp"
>
Similarly we can introduce the composition as regressors
Call:
lm(formula = y ~ idt(X), data = list(y = y, X = x))
Coefficients:
(Intercept) idt(X)1 idt(X)2 idt(X)3
2.791 -1.721 2.154 -1.609
> anova(mylm)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
idt(X) 3 759.74 253.25 18.034 2.595e-08 ***
Residuals 56 786.41 14.04
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
> plot(predict(mylm),resid(mylm))
> predict(mylm,newdata=list(X=acomp(x[1:3,])))
A combination of all this is also possible:
Coefficients:
Cu Zn
(Intercept) 1.97369 4.46492
kMiddle -0.24318 -1.75248
14
kUpper -0.76350 -2.21039
idt(X)1 -0.02325 -0.01573
idt(X)2 -0.55759 -0.57721
> plot([Link](predict(mylm)),add=T,col=k,pch=20)
> summary(manova(mylm))
8 Conclusions
Without conclusion, but opening to further steps like barycentric coordinates (endpointCoordinates)
or simulation ([Link],[Link],[Link], [Link]) I ask the
reader to make his own experiences with compositional data analysis.
References
[Aitchison(1982)] Aitchison, J., 1982. The statistical analysis of compositional data
(with discussion). Journal of the Royal Statistical Society, Series B (Statistical
Methodology) 44 (2), 139177.
[Aitchison and Greenacre(2002)] Aitchison, J., Greenacre, M., 2002. Biplots for
compositional data. Applied Statistics 51 (4), 375392.
15
a geochemical case study. In: Lippard, S. J., Næss, A., Sinding-Larsen,
R. (Eds.), Proceedings of IAMG'99 The fth annual conference of the
International Association for Mathematical Geology. Vol. I and II. Tapir,
Trondheim (N), pp. 139144.
16
[Rehder and Zier(2002)] Rehder, S. and Zier, U. (2002), Some remarks about trans-
formations. In: Bayer, Burger, Skala (Eds.), Proceedings of IAMG'02 The
eight annual conference of the International Association for Mathematical Ge-
ology. Vol. I and II. Berlin (D), pp. 423428.
[Shurtz(2003)] Shurtz, Robert F., 2003. Compositional geometry and mass conser-
vation. Mathematical Geology 35 (8), 972937.
17