#-------------------------------------------------- # Zero replacement for the logratio method # "martin" : Martin-Fernandez et al.(2000, 2003) # "fry" : Fry et al.(2000) # "aitchison": Aitchison (1986, 2003) # # January 23, 2006 # Developed by Hiroyoshi Arai #-------------------------------------------------- zero.replace <- function(x, delta, total=100, method=c("martin", "fry", "aitchison")) { if (any(x < 0)) { stop("Data contains negative value(s).") } if ((delta < 0) || (total <= 0)) { stop("Invalid delta and/or total.") } method <- match.arg(method) x[is.na(x)] <- 0 x <- x/apply(x, 1, sum)*total z <- apply((x == 0), 1, sum) d <- ncol(x) if ((method == "martin") && !any(z*delta >= total)) { x <- (1-z*delta/total)*(x != 0)*x x[x == 0] <- delta } else { if ((method == "fry") && !any(z*delta*(z+1)*(d-z)/d^2 >= total)) { x <- (1-z*delta*(z+1)*(d-z)/d^2/total)*(x != 0)*x x <- x+delta*(z+1)*(d-z)/d^2*(x == 0) } else { if ((method == "aitchison") && !any((x-delta*(z+1)*z/d^2*(x != 0))[x != 0] <= 0)) { x <- x-delta*(z+1)*z/d^2*(x != 0) x <- x+delta*(z+1)*(d-z)/d^2*(x == 0) } else { stop("Invalid delta.") } } } return(x) }