diff --git a/R/getVariogram.R b/R/getVariogram.R index 71f0af7..86b115f 100644 --- a/R/getVariogram.R +++ b/R/getVariogram.R @@ -86,6 +86,13 @@ #' first (\code{\link[mia:rarefyAssay]{rarefyAssay()}}) when depth differs #' between samples or groups. #' +#' Dissimilarities bounded by 1, such as Bray–Curtis and Jaccard, approach +#' their bound as communities share fewer features, so \eqn{D(L)} flattens +#' near 1 whatever the dynamics. With \code{bounded = TRUE} they are analysed +#' on the scale \eqn{-\log(1 - d)}, which is unbounded and close to \eqn{d} +#' for small dissimilarities. Pairs without shared features (\eqn{d = 1}) are +#' set to \eqn{d = 1 - 10^{-9}}. +#' #' @references #' Diggle P.J. (1988) An approach to the analysis of repeated measurements. #' Biometrics 44, 959–971. doi: 10.2307/2531727 @@ -150,6 +157,10 @@ #' @param squared \code{Logical scalar}. Whether the dissimilarities are #' squared. (Default: \code{FALSE}) #' +#' @param bounded \code{Logical scalar}. Whether the dissimilarities are +#' bounded by 1 and analysed on the scale \eqn{-\log(1 - d)}. See details. +#' (Default: \code{FALSE}) +#' #' @param conf.level \code{Numeric scalar}. Confidence level of the #' intervals. (Default: \code{0.95}) #' @@ -237,6 +248,7 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), time.point = NULL, window = 2, squared = FALSE, + bounded = FALSE, conf.level = 0.95, permutations = 0, ...){ @@ -274,6 +286,7 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), stop("'window' must be greater than 1.", call. = FALSE) } temp <- .check_input(squared, list("logical scalar")) + temp <- .check_input(bounded, list("logical scalar")) temp <- .check_input(conf.level, list("numeric scalar")) if( conf.level <= 0 || conf.level >= 1 ){ stop("'conf.level' must be between 0 and 1.", call. = FALSE) @@ -286,7 +299,8 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), ########################### INPUT CHECK END ############################ args <- list(...)[!names(list(...)) %in% c("altexp")] pairs <- .get_within_subject_pairs( - x, time.col, group, assay.type, method, split.by, squared, args) + x, time.col, group, assay.type, method, split.by, squared, + bounded, args) if( is.null(time.lag) ){ time.lag <- .default_lags(pairs) } @@ -303,7 +317,8 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), # All pairs of samples of the same subject, with their lag, midpoint time and # dissimilarity. .get_within_subject_pairs <- function( - x, time.col, group, assay.type, method, split.by, squared, args){ + x, time.col, group, assay.type, method, split.by, squared, bounded, + args){ time <- x[[time.col]] subject <- x[[group]] keep <- !is.na(time) & !is.na(subject) @@ -355,6 +370,13 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), if( squared ){ pairs$d <- pairs$d^2 } + if( bounded ){ + if( any(pairs$d < 0 | pairs$d > 1, na.rm = TRUE) ){ + stop("'bounded = TRUE' needs dissimilarities between 0 and 1.", + call. = FALSE) + } + pairs$d <- -log1p(-pmin(pairs$d, 1 - 1e-9)) + } if( !is.null(split.by) ){ pairs$level <- level[match(pairs$subject, subject)] } diff --git a/man/getVariogram.Rd b/man/getVariogram.Rd index 042b1ed..c127f01 100644 --- a/man/getVariogram.Rd +++ b/man/getVariogram.Rd @@ -24,6 +24,7 @@ addVariogram(x, ...) time.point = NULL, window = 2, squared = FALSE, + bounded = FALSE, conf.level = 0.95, permutations = 0, ... @@ -75,6 +76,10 @@ details. (Default: \code{NULL})} \item{squared}{\code{Logical scalar}. Whether the dissimilarities are squared. (Default: \code{FALSE})} +\item{bounded}{\code{Logical scalar}. Whether the dissimilarities are +bounded by 1 and analysed on the scale \eqn{-\log(1 - d)}. See details. +(Default: \code{FALSE})} + \item{conf.level}{\code{Numeric scalar}. Confidence level of the intervals. (Default: \code{0.95})} @@ -186,6 +191,13 @@ additively, so the noise does not change the shape of the variogram, only its level. Dissimilarities depend on sequencing depth; rarefy the counts first (\code{\link[mia:rarefyAssay]{rarefyAssay()}}) when depth differs between samples or groups. + +Dissimilarities bounded by 1, such as Bray–Curtis and Jaccard, approach +their bound as communities share fewer features, so \eqn{D(L)} flattens +near 1 whatever the dynamics. With \code{bounded = TRUE} they are analysed +on the scale \eqn{-\log(1 - d)}, which is unbounded and close to \eqn{d} +for small dissimilarities. Pairs without shared features (\eqn{d = 1}) are +set to \eqn{d = 1 - 10^{-9}}. } \examples{ library(miaTime) diff --git a/tests/testthat/test-getVariogram.R b/tests/testthat/test-getVariogram.R index 5a06289..3b52a76 100644 --- a/tests/testthat/test-getVariogram.R +++ b/tests/testthat/test-getVariogram.R @@ -169,3 +169,24 @@ test_that("Permutation p-values of comparisons", { res2 <- do.call(getVariogram, c(args, permutations = 19)) expect_equal(res$p.value.permutation, res2$p.value.permutation) }) + +test_that("Bounded dissimilarities are analysed on the -log(1 - d) scale", { + data(hitchip1006, package = "miaTime") + tse <- transformAssay(hitchip1006, method = "relabundance") + args <- list(tse, time.col = "time", group = "subject", + assay.type = "relabundance", method = "bray", time.lag = c(1, 3)) + pairs <- miaTime:::.get_within_subject_pairs(tse, "time", "subject", + "relabundance", "bray", NULL, FALSE, FALSE, list()) + pairs$d <- -log(1 - pairs$d) + ref <- miaTime:::.local_variogram(pairs, c(1, 3), 2, 0.95, c(NA, NA)) + res <- do.call(getVariogram, c(args, bounded = TRUE)) + expect_equal(res$estimate, ref$estimate) + expect_true(any(res$supported)) + expect_true(all(res$estimate > do.call(getVariogram, args)$estimate, + na.rm = TRUE)) + # Dissimilarities above 1 are not bounded + expect_error(getVariogram(tse, time.col = "time", group = "subject", + assay.type = "counts", method = "euclidean", time.lag = 1, + bounded = TRUE)) + expect_error(do.call(getVariogram, c(args, bounded = NA))) +})