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
36 changes: 36 additions & 0 deletions R/getVariogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
#' permutation test is not available, as levels are not assigned to
#' subjects.
#'
#' With \code{subset.by}, only pairs whose earlier sample is \code{TRUE} in
#' a logical column of \code{colData} are used, for example pairs starting
#' from a given community type. Subjects are then compared on those pairs,
#' and a sample-level state is compared within them.
#'
#' \strong{Developing communities.} When change at a given lag depends on
#' the time since a common origin (age in infant cohorts, time since an
#' intervention), groups whose samples were taken at different ages differ
Expand Down Expand Up @@ -174,6 +179,10 @@
#' sample-level state. \eqn{D(L)} is estimated for each of its levels, and
#' each level is compared with the first. (Default: \code{NULL})
#'
#' @param subset.by \code{NULL} or \code{Character scalar}. Specifies a name
#' of a logical column of \code{colData}; only pairs whose earlier sample is
#' \code{TRUE} are used. See details. (Default: \code{NULL})
#'
#' @param time.point \code{NULL}, \code{numeric vector} or \code{"median"}. The
#' time at which \eqn{D(L)} is estimated, for communities whose change
#' depends on time since a common origin; one value, or one per lag. See
Expand Down Expand Up @@ -277,6 +286,7 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"),
method = "bray",
time.lag = NULL,
split.by = NULL,
subset.by = NULL,
time.point = NULL,
window = 2,
squared = FALSE,
Expand Down Expand Up @@ -304,6 +314,12 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"),
temp <- .check_input(
split.by, list(NULL, "character scalar"), colnames(colData(x)))
.check_split_by_name(split.by, .VARIOGRAM_COLUMNS)
temp <- .check_input(
subset.by, list(NULL, "character scalar"), colnames(colData(x)))
if( !is.null(subset.by) && !is.logical(x[[subset.by]]) ){
stop("'subset.by' must specify a logical column of colData(x).",
call. = FALSE)
}
if( !(is.null(time.point) || identical(time.point, "median") ||
(is.numeric(time.point) && !anyNA(time.point))) ){
stop("'time.point' must be NULL, numeric or \"median\".",
Expand Down Expand Up @@ -336,6 +352,9 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"),
pairs <- .get_within_subject_pairs(
x, time.col, group, assay.type, method, split.by, squared,
bounded, args)
if( !is.null(subset.by) ){
pairs <- .subset_pairs(pairs, x, time.col, subset.by)
}
if( is.null(time.lag) ){
time.lag <- .default_lags(pairs)
}
Expand Down Expand Up @@ -441,6 +460,23 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"),
return(pairs)
}

# The pairs whose earlier sample is TRUE in the logical column subset.by
.subset_pairs <- function(pairs, x, time.col, subset.by){
samples <- attr(pairs, "samples")
pair_level <- attr(pairs, "pair.level")
keep <- x[[subset.by]][samples]
time <- x[[time.col]][samples]
first <- ifelse(time[pairs$k] <= time[pairs$l], pairs$k, pairs$l)
pairs <- pairs[keep[first] %in% TRUE, , drop = FALSE]
if( nrow(pairs) == 0L ){
stop("No pair starts from a sample with 'subset.by' TRUE.",
call. = FALSE)
}
attr(pairs, "samples") <- samples
attr(pairs, "pair.level") <- pair_level
return(pairs)
}

# Squared, or on the scale -log(1 - d) for bounded dissimilarities
.dissimilarity_scale <- function(d, squared, bounded){
if( squared ){
Expand Down
10 changes: 10 additions & 0 deletions man/getVariogram.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/test-getVariogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,26 @@ test_that("Pairs are classified by a sample-level state", {
args$split.by <- "type"
expect_error(do.call(getVariogram, args), "output column")
})

test_that("Pairs are restricted to those from samples with subset.by TRUE", {
data(hitchip1006, package = "miaTime")
tse <- transformAssay(hitchip1006, method = "hellinger")
tse$early <- tse$time < 3
args <- list(tse, time.col = "time", group = "subject",
assay.type = "hellinger", method = "euclidean", squared = TRUE,
time.lag = c(1, 3))
res <- do.call(getVariogram, c(args, subset.by = "early"))
# The same as the local estimate on the pairs from early samples
pairs <- miaTime:::.get_within_subject_pairs(tse, "time", "subject",
"hellinger", "euclidean", NULL, TRUE, FALSE, list())
first <- pmin(pairs$mid - pairs$h / 2)
ref <- miaTime:::.local_variogram(pairs[first < 3, ], c(1, 3), 2, 0.95,
c(NA, NA))
expect_equal(res$estimate, ref$estimate)
expect_equal(res$n.pairs, ref$n.pairs)
expect_error(do.call(getVariogram, c(args, subset.by = "time")),
"logical")
tse$none <- FALSE
args[[1]] <- tse
expect_error(do.call(getVariogram, c(args, subset.by = "none")))
})