Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions R/getVariogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
#'
Expand Down Expand Up @@ -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,
...){
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)]
}
Expand Down
12 changes: 12 additions & 0 deletions man/getVariogram.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/testthat/test-getVariogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})