## Apart from error checking and very large number cases, the R implementation is simply
..clr1 <- function (w) {
ln <- log(w)
ln[-1L] - mean(ln)
}
## and its inverse
..clr1inv <- function(lp) {
p1 <- exp(c(-sum(lp), lp))
p1/sum(p1)
}
lp <- clr1( (1:3)/6 )
clr1inv(lp)
stopifnot(all.equal(clr1inv(lp), (1:3)/6))
for(n in 1:100) {
k <- 2 + rpois(1, 3) # #{components}
lp <- rnorm(k-1) # arbitrary unconstrained
## clr1() and clr1inv() are inverses :
stopifnot(all.equal(lp, clr1(clr1inv(lp))))
}
wM <- clr1inv(c(720,720,720))
w2 <- clr1inv(c(720,718,717))
stopifnot(is.finite(wM), all.equal(wM, c(0, 1/3, 1/3, 1/3))
, is.finite(w2), all.equal(w2, c(0, 0.84379473, 0.1141952, 0.042010066))
)
Run the code above in your browser using DataLab