Revision a53065a09c3fce65a63e137deb5bccb6162e6cff authored by Matthias Templ on 18 November 2020, 20:10:02 UTC, committed by cran-robot on 18 November 2020, 20:10:02 UTC
1 parent 9ae1e67
Raw File
impAll.R
#' Replacement of rounded zeros and missing values.
#' 
#' Parametric replacement of rounded zeros and missing values for compositional
#' data using classical and robust methods based on ilr coordinates with
#' special choice of balances. Values under detection limit should be saved
#' with the negative value of the detection limit (per variable). Missing
#' values should be coded as NA.
#' 
#' This is a wrapper function that calls \emph{impRZilr()} for the replacement
#' of zeros and \emph{impCoda} for the imputation of missing values
#' sequentially. The detection limit is automatically derived form negative
#' numbers in the data set.
#' 
#' @param x data frame
#' @return The imputed data set.
#' @note This function is mainly used by the compositionsGUI.
#' @seealso \code{\link{impCoda}}, \code{\link{impRZilr}}
#' @export
#' @references Hron, K., Templ, M., Filzmoser, P. (2010) Imputation of
#' missing values for compositional data using classical and robust methods,
#' \emph{Computational Statistics and Data Analysis}, 54 (12),
#' 3095-3107.
#' 
#' Martin-Fernandez, J.A., Hron, K., Templ, M., Filzmoser, P.,
#' Palarea-Albaladejo, J. (2012) Model-based replacement of rounded zeros in
#' compositional data: Classical and robust approaches, \emph{Computational
#' Statistics}, 56 (2012), 2688 - 2704.
#' @keywords manip
#' @examples
#' 
#' ## see the compositionsGUI
#' 
impAll <-
function(x) {
	maxLimits = apply(x, 2, min, na.rm = TRUE)
	maxLimits = -maxLimits
	maxLimits[maxLimits < 0] = 0

	if(any(is.na(x))) {
		temp <- x
		for(i in 1:length(maxLimits)) {
			temp[na.omit(temp[i]) < 0, i] = maxLimits[i] * 2 / 3
		}

		res <- adjust(impCoda(temp))
		isna = is.na(x)
		x[isna] = res$xImp[isna]
	}
	if(any(x < 0)) {
		temp <- x
		temp[temp < 0] = 0

		res <- impRZilr(temp, dl=maxLimits)
		x = res$xImp
	}

	return(x)
}

back to top