Overview
coda.base provides a compact and efficient toolkit for compositional data analysis using log-ratio coordinates. It supports the complete workflow from compositions to coordinates and back, including:
- transforming compositions with
coordinates()andcomposition(); - constructing ALR, CLR, ILR, principal-component and balance bases;
- applying closure, perturbation and powering;
- computing Aitchison and other compositional distances;
- replacing rounded zeros and missing values; and
- finding principal balances with exact, constrained or tabu-search methods.
Core numerical operations are implemented in C++ for performance.
Installation
Install the released version from CRAN:
install.packages("coda.base")Install the development version from GitHub:
# install.packages("pak")
pak::pak("mcomas/coda.base")Usage
Transform a composition into isometric log-ratio coordinates and reconstruct the original composition:
library(coda.base)
x <- c(a = 0.2, b = 0.3, c = 0.5)
B <- ilr_basis(x)
z <- coordinates(x, B)
z
#> ilr1 ilr2
#> -0.2867071 -0.5826178
composition(z, B)
#> a b c
#> 0.2 0.3 0.5The package also provides the standard operations of the Aitchison geometry:
x <- c(a = 2, b = 3, c = 5)
y <- c(a = 1, b = 4, c = 5)
closure(x)
#> a b c
#> 0.2 0.3 0.5
perturbation(closure(x), closure(y))
#> [1] 0.05128205 0.30769231 0.64102564
dist_coda(rbind(x, y))
#> x
#> y 0.7130311Principal balances provide interpretable coordinates that seek to capture the variation in a compositional data set:
votes <- parliament2017[-1]
B_pb <- pb_basis(votes, method = "constrained")
head(coordinates(votes, B_pb), 3)
#> pb1 pb2 pb3 pb4 pb5 pb6 pb7
#> 1 -1.497066 0.8542554 -1.638509 0.1469780 -1.552474 -1.331674 0.5706805
#> 2 -1.347042 0.7209798 -1.531162 0.2267969 -1.790182 -1.415831 0.5581286
#> 3 -1.462949 1.0246594 -1.743697 0.1235819 -1.330296 -1.198655 0.4131829Getting help
If you find a bug, please open an issue with a minimal reproducible example.
Citation
If you use coda.base in research, retrieve the recommended citation with:
citation("coda.base")