From 9d237a6fd6d613b6bb9f86e18c86e442b8274e19 Mon Sep 17 00:00:00 2001 From: Daena Rys Date: Tue, 3 Nov 2026 11:45:08 +0200 Subject: [PATCH] Add relative change D(L)/B to getVariogram --- R/getVariogram.R | 66 +++++++++++++++++++++++++++--- man/getVariogram.Rd | 17 ++++++++ tests/testthat/test-getVariogram.R | 27 ++++++++++++ 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/R/getVariogram.R b/R/getVariogram.R index 86b115f..bac5a3a 100644 --- a/R/getVariogram.R +++ b/R/getVariogram.R @@ -93,6 +93,16 @@ #' for small dissimilarities. Pairs without shared features (\eqn{d = 1}) are #' set to \eqn{d = 1 - 10^{-9}}. #' +#' \strong{Relative change.} \eqn{D(L)} is on the scale of the +#' dissimilarity, so it cannot be compared between dissimilarities, studies +#' or body sites. With \code{relative = TRUE}, \eqn{D(L)} is also reported +#' relative to \eqn{B}, the mean dissimilarity between samples of different +#' subjects, where every pair of subjects counts equally: a value of 0.5 +#' means that over \eqn{L} a subject's community changes half as much as it +#' differs from another subject's. \eqn{B} is computed from the samples of +#' all subjects, so it is the same for every level of \code{split.by}, and +#' it is treated as fixed. +#' #' @references #' Diggle P.J. (1988) An approach to the analysis of repeated measurements. #' Biometrics 44, 959–971. doi: 10.2307/2531727 @@ -126,6 +136,8 @@ #' \item{supported}{Whether the design supports \eqn{D(L)} at this lag.} #' \item{time.point}{The time at which \eqn{D(L)} was estimated, when #' \code{time.point} is given.} +#' \item{relative}{When \code{relative = TRUE}: \eqn{D(L) / B}, for +#' estimates of \eqn{D(L)}.} #' } #' \code{addVariogram} returns \code{x} with this table in its #' \code{metadata}. @@ -161,6 +173,10 @@ #' bounded by 1 and analysed on the scale \eqn{-\log(1 - d)}. See details. #' (Default: \code{FALSE}) #' +#' @param relative \code{Logical scalar}. Whether \eqn{D(L)} is also +#' reported relative to the dissimilarity between subjects. See details. +#' (Default: \code{FALSE}) +#' #' @param conf.level \code{Numeric scalar}. Confidence level of the #' intervals. (Default: \code{0.95}) #' @@ -249,6 +265,7 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), window = 2, squared = FALSE, bounded = FALSE, + relative = FALSE, conf.level = 0.95, permutations = 0, ...){ @@ -287,6 +304,7 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), } temp <- .check_input(squared, list("logical scalar")) temp <- .check_input(bounded, list("logical scalar")) + temp <- .check_input(relative, 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) @@ -307,6 +325,13 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), res <- .variogram( pairs, time.lag, split.by, time.point, window, conf.level, permutations) + if( relative ){ + B <- .between_subject_level( + x, time.col, group, assay.type, method, split.by, squared, + bounded, args) + res$relative <- ifelse( + res$type == "dissimilarity", res$estimate / B, NA_real_) + } res <- DataFrame(res, check.names = FALSE) return(res) } @@ -367,20 +392,51 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), mid = (time[k] + time[l]) / 2, d = d[ut]) }) pairs <- do.call(rbind, unname(res)) + pairs$d <- .dissimilarity_scale(pairs$d, squared, bounded) + if( !is.null(split.by) ){ + pairs$level <- level[match(pairs$subject, subject)] + } + return(pairs) +} + +# Squared, or on the scale -log(1 - d) for bounded dissimilarities +.dissimilarity_scale <- function(d, squared, bounded){ if( squared ){ - pairs$d <- pairs$d^2 + d <- d^2 } if( bounded ){ - if( any(pairs$d < 0 | pairs$d > 1, na.rm = TRUE) ){ + if( any(d < 0 | 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)) + d <- -log1p(-pmin(d, 1 - 1e-9)) } + return(d) +} + +# Mean dissimilarity between samples of different subjects, every pair of +# subjects counting equally +.between_subject_level <- function( + x, time.col, group, assay.type, method, split.by, squared, bounded, + args){ + keep <- !is.na(x[[time.col]]) & !is.na(x[[group]]) if( !is.null(split.by) ){ - pairs$level <- level[match(pairs$subject, subject)] + keep <- keep & !is.na(x[[split.by]]) } - return(pairs) + x <- x[, keep] + subject <- as.character(x[[group]]) + if( length(unique(subject)) < 2L ){ + stop("'relative = TRUE' needs at least two subjects.", call. = FALSE) + } + d <- do.call(getDissimilarity, c( + list(x = t(assay(x, assay.type)), method = method), args)) + d <- .dissimilarity_scale(as.matrix(d), squared, bounded) + # Sums and numbers of sample pairs for every pair of subjects + sums <- rowsum(t(rowsum(d, subject)), subject) + n <- as.numeric(table(subject)[rownames(sums)]) + means <- sums / outer(n, n) + B <- mean(means[upper.tri(means)]) + return(B) } # The k-th smallest of the subjects' shortest lags and the k-th largest of diff --git a/man/getVariogram.Rd b/man/getVariogram.Rd index c127f01..58c369f 100644 --- a/man/getVariogram.Rd +++ b/man/getVariogram.Rd @@ -25,6 +25,7 @@ addVariogram(x, ...) window = 2, squared = FALSE, bounded = FALSE, + relative = FALSE, conf.level = 0.95, permutations = 0, ... @@ -80,6 +81,10 @@ squared. (Default: \code{FALSE})} bounded by 1 and analysed on the scale \eqn{-\log(1 - d)}. See details. (Default: \code{FALSE})} +\item{relative}{\code{Logical scalar}. Whether \eqn{D(L)} is also +reported relative to the dissimilarity between subjects. See details. +(Default: \code{FALSE})} + \item{conf.level}{\code{Numeric scalar}. Confidence level of the intervals. (Default: \code{0.95})} @@ -108,6 +113,8 @@ comparisons, the permutation p-value (see details).} \item{supported}{Whether the design supports \eqn{D(L)} at this lag.} \item{time.point}{The time at which \eqn{D(L)} was estimated, when \code{time.point} is given.} +\item{relative}{When \code{relative = TRUE}: \eqn{D(L) / B}, for +estimates of \eqn{D(L)}.} } \code{addVariogram} returns \code{x} with this table in its \code{metadata}. @@ -198,6 +205,16 @@ 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}}. + +\strong{Relative change.} \eqn{D(L)} is on the scale of the +dissimilarity, so it cannot be compared between dissimilarities, studies +or body sites. With \code{relative = TRUE}, \eqn{D(L)} is also reported +relative to \eqn{B}, the mean dissimilarity between samples of different +subjects, where every pair of subjects counts equally: a value of 0.5 +means that over \eqn{L} a subject's community changes half as much as it +differs from another subject's. \eqn{B} is computed from the samples of +all subjects, so it is the same for every level of \code{split.by}, and +it is treated as fixed. } \examples{ library(miaTime) diff --git a/tests/testthat/test-getVariogram.R b/tests/testthat/test-getVariogram.R index 3b52a76..ba55e91 100644 --- a/tests/testthat/test-getVariogram.R +++ b/tests/testthat/test-getVariogram.R @@ -190,3 +190,30 @@ test_that("Bounded dissimilarities are analysed on the -log(1 - d) scale", { bounded = TRUE)) expect_error(do.call(getVariogram, c(args, bounded = NA))) }) + +test_that("Relative change divides D(L) by the dissimilarity between subjects", { + data(hitchip1006, package = "miaTime") + tse <- transformAssay(hitchip1006, method = "relabundance") + # Subjects with two or more samples, and ten with one + n <- table(tse$subject) + tse <- tse[, tse$subject %in% c(names(n)[n > 1], names(n)[n == 1][1:10])] + args <- list(tse, time.col = "time", group = "subject", + assay.type = "relabundance", method = "bray", time.lag = c(1, 3), + split.by = "sex") + res <- do.call(getVariogram, c(args, relative = TRUE)) + # Every pair of subjects counts equally + d <- as.matrix(getDissimilarity(t(assay(tse, "relabundance")), + method = "bray")) + s <- tse$subject + sp <- combn(unique(s), 2) + B <- mean(apply(sp, 2, function(p) mean(d[s == p[1], s == p[2]]))) + est <- res$type == "dissimilarity" + expect_equal(res$relative[est], res$estimate[est] / B) + expect_true(all(is.na(res$relative[!est]))) + expect_false("relative" %in% names(do.call(getVariogram, args))) + # On the same scale as D(L) + res <- do.call(getVariogram, c(args, relative = TRUE, bounded = TRUE)) + B <- mean(apply(sp, 2, function(p) + mean(-log(1 - d[s == p[1], s == p[2]])))) + expect_equal(res$relative[est], res$estimate[est] / B) +})