https://github.com/cran/robCompositions
Raw File
Tip revision: 325444901c2227c36bea9ac4bd62ffcee2b6bc95 authored by Matthias Templ on 06 May 2015, 17:22:25 UTC
version 1.9.1
Tip revision: 3254449
addLR.R
#' Additive log-ratio transformation
#' 
#' The additive log-ratio transformation moves D-part compositional data from
#' the simplex into a (D-1)-dimensional real space.
#' 
#' The compositional parts are divided by the rationing part before the
#' logarithm is taken.
#' 
#' @param x D-part compositional data
#' @param ivar Rationing part
#' @return A list of class \dQuote{alr} which includes the following content:
#' \item{x.alr}{the transformed data} \item{varx}{the rationing variable}
#' \item{ivar}{the index of the rationing variable, indicating the column
#' number of the rationing variable in the data matrix \emph{x}}
#' \item{cnames}{the column names of \emph{x}} The additional information such
#' as \emph{cnames} or \emph{ivar} is usefull when a back-transformation is
#' applied on the \sQuote{same} data set.
#' @author Matthias Templ
#' @seealso \code{\link{addLRinv}}, \code{\link{isomLR}}
#' @references Aitchison, J. (1986) \emph{The Statistical Analysis of
#' Compositional Data} Monographs on Statistics and Applied Probability.
#' Chapman \& Hall Ltd., London (UK). 416p.
#' @keywords manip
#' @examples
#' 
#' data(arcticLake)
#' x <- arcticLake
#' x.alr <- addLR(x, 2)
#' y <- addLRinv(x.alr)
#' ## This exactly fulfills:
#' addLRinv(addLR(x, 3))
#' data(expenditures)
#' x <- expenditures
#' y <- addLRinv(addLR(x, 5))
#' head(x)
#' head(y)
#' ## --> absolute values are preserved as well.
#' 
#' ## preserve only the ratios:
#' addLRinv(x.alr, ivar=2, useClassInfo=FALSE)
#' 
#' 
addLR <- function (x, ivar=ncol(x)){

	if(dim(x)[2] < 2) stop("data must be of dimension greater equal 2")
	x.alr <- log(x/x[, ivar])
	res <- list(x.alr=x.alr[,-ivar], 
			varx=x[,ivar], ivar=ivar, cnames=colnames(x))
	class(res) <- "alr"
	return(res)
}
back to top