diff --git a/R/getVariogramModel.R b/R/getVariogramModel.R index 19075bb..7fc0860 100644 --- a/R/getVariogramModel.R +++ b/R/getVariogramModel.R @@ -100,8 +100,10 @@ #' pairs in the upper quartile of the lags minus that of pairs in the lower #' quartile, and its p-value compares it with the same statistic after the #' sampling times are permuted within each subject, which leaves the -#' dissimilarities unchanged. It uses the pairs at all lags, and is computed -#' for each level of \code{split.by}. +#' dissimilarities unchanged. When the times can be permuted within subjects +#' in at most a million ways, every permutation is used and the p-value is +#' exact; otherwise \code{permutations} random permutations are used. It uses +#' the pairs at all lags, and is computed for each level of \code{split.by}. #' #' \strong{Technical replicates.} The nugget contains measurement noise and #' change faster than the shortest lag. When some samples were sequenced @@ -910,6 +912,10 @@ setMethod("getVariogramModel", signature = c(x = "SummarizedExperiment"), stats::weighted.mean(p$d[hi], wh) - stats::weighted.mean(p$d[lo], wl) } obs <- stat(p$h) + exact <- .temporal_structure_exact(p, time, q, obs) + if( !is.null(exact) ){ + return(list(statistic = obs, p.value = exact)) + } # The samples of the pairs, ordered by subject id <- c(p$k, p$l) keep <- !duplicated(id) @@ -925,6 +931,60 @@ setMethod("getVariogramModel", signature = c(x = "SummarizedExperiment"), return(list(statistic = obs, p.value = p_value)) } +# The exact p-value of the test for temporal structure, from every +# permutation of the sampling times within subjects; NULL when there are more +# than max.perm permutations +.temporal_structure_exact <- function(pairs, time, q, obs, max.perm = 1e6){ + parts <- lapply(split(seq_len(nrow(pairs)), pairs$subject), function(i){ + ids <- sort(unique(c(pairs$k[i], pairs$l[i]))) + list(t = time[ids], k = match(pairs$k[i], ids), + l = match(pairs$l[i], ids), d = pairs$d[i]) + }) + n <- vapply(parts, function(x) length(x$t), integer(1L)) + if( prod(factorial(n)) > max.perm ){ + return(NULL) + } + # Lags of each subject's pairs under each permutation of its times + lags <- lapply(parts, function(x){ + pm <- .permutations(length(x$t)) + tk <- matrix(x$t[pm[, x$k, drop = FALSE]], nrow = nrow(pm)) + tl <- matrix(x$t[pm[, x$l, drop = FALSE]], nrow = nrow(pm)) + abs(tl - tk) + }) + idx <- as.matrix(expand.grid(lapply(lags, function(m) seq_len(nrow(m))))) + # Subject-weighted means of the pairs in the lower and upper quartile of + # the observed lags, for every combination of permutations + num_lo <- num_hi <- den_lo <- den_hi <- 0 + for( j in seq_along(parts) ){ + h <- lags[[j]][idx[, j], , drop = FALSE] + d <- parts[[j]]$d + lo <- h <= q[[1L]] + hi <- h >= q[[2L]] + n_lo <- rowSums(lo) + n_hi <- rowSums(hi) + num_lo <- num_lo + ifelse(n_lo > 0, drop(lo %*% d) / pmax(n_lo, 1), 0) + num_hi <- num_hi + ifelse(n_hi > 0, drop(hi %*% d) / pmax(n_hi, 1), 0) + den_lo <- den_lo + (n_lo > 0) + den_hi <- den_hi + (n_hi > 0) + } + st <- num_hi / den_hi - num_lo / den_lo + st <- st[is.finite(st)] + res <- mean(st >= obs - 1e-12 * max(1, abs(obs))) + return(res) +} + +# All permutations of 1, ..., n, one per row +.permutations <- function(n){ + if( n == 1L ){ + return(matrix(1L, 1L, 1L)) + } + sub <- .permutations(n - 1L) + res <- do.call(rbind, lapply(seq_len(n), function(i){ + cbind(i, matrix(setdiff(seq_len(n), i)[sub], nrow = nrow(sub))) + })) + return(res) +} + # Ten-fold cross-validation over subjects. Single pairs are too noisy to # separate forms, so the held-out subjects' variogram is binned at quantiles # of the lags and compared with the fitted variogram by the mean squared diff --git a/man/getVariogramModel.Rd b/man/getVariogramModel.Rd index e87cee0..71cdd50 100644 --- a/man/getVariogramModel.Rd +++ b/man/getVariogramModel.Rd @@ -230,8 +230,10 @@ lag at all: its estimate is the subject-weighted mean dissimilarity of pairs in the upper quartile of the lags minus that of pairs in the lower quartile, and its p-value compares it with the same statistic after the sampling times are permuted within each subject, which leaves the -dissimilarities unchanged. It uses the pairs at all lags, and is computed -for each level of \code{split.by}. +dissimilarities unchanged. When the times can be permuted within subjects +in at most a million ways, every permutation is used and the p-value is +exact; otherwise \code{permutations} random permutations are used. It uses +the pairs at all lags, and is computed for each level of \code{split.by}. \strong{Technical replicates.} The nugget contains measurement noise and change faster than the shortest lag. When some samples were sequenced diff --git a/tests/testthat/test-getVariogramModel.R b/tests/testthat/test-getVariogramModel.R index 4ebccc3..cc3b9eb 100644 --- a/tests/testthat/test-getVariogramModel.R +++ b/tests/testthat/test-getVariogramModel.R @@ -247,3 +247,38 @@ test_that("Technical replicates split the nugget", { expect_error(getVariogramModel(tse, time.col = "time", group = "subject", assay.type = "x", replicate.by = "x")) }) + +test_that("The test for temporal structure is exact for small designs", { + # Four subjects with three samples each: 6^4 permutations of the times + set.seed(2) + time <- rep(c(0, 3, 9), 4) + runif(12) + subject <- rep(paste0("s", 1:4), each = 3) + ij <- do.call(rbind, lapply(split(seq_along(time), subject), function(i){ + t(combn(i, 2)) + })) + x <- ave(rnorm(12), subject, FUN = cumsum) + pairs <- data.frame(subject = subject[ij[, 1]], + h = abs(time[ij[, 2]] - time[ij[, 1]]), + mid = (time[ij[, 1]] + time[ij[, 2]]) / 2, + d = (x[ij[, 1]] - x[ij[, 2]])^2, k = ij[, 1], l = ij[, 2]) + res <- miaTime:::.temporal_structure(pairs, time, 19) + # Every permutation, one at a time + q <- quantile(pairs$h, c(0.25, 0.75)) + stat <- function(h){ + lo <- h <= q[[1]] + hi <- h >= q[[2]] + wl <- 1 / as.numeric(table(pairs$subject[lo])[pairs$subject[lo]]) + wh <- 1 / as.numeric(table(pairs$subject[hi])[pairs$subject[hi]]) + weighted.mean(pairs$d[hi], wh) - weighted.mean(pairs$d[lo], wl) + } + pm <- miaTime:::.permutations(3) + expect_identical(nrow(unique(pm)), 6L) + grid <- expand.grid(rep(list(1:6), 4)) + st <- apply(grid, 1, function(r){ + tp <- unlist(lapply(1:4, function(j) time[(3 * j - 2):(3 * j)][ + pm[r[[j]], ]])) + stat(abs(tp[pairs$l] - tp[pairs$k])) + }) + expect_equal(res$statistic, stat(pairs$h)) + expect_equal(res$p.value, mean(st >= stat(pairs$h) - 1e-12)) +})