tests/testthat/test-RISE.R

testRISE <- function(n.iter) {
  if(requireNamespace("GraphRankTest", quietly = TRUE) & requireNamespace("rmvnorm", quietly = TRUE)) {
    for(i in 1:n.iter) {
      set.seed(i)
      X1 <- mvtnorm::rmvnorm(100, sigma = matrix(0.2, 10, 10) + diag(0.8, 10, 10), 
                             mean = runif(10, -2, 2))
      X2 <- mvtnorm::rmvnorm(100, mean = runif(10, -2, 2), 
                             sigma = matrix(0.5, 10, 10) + diag(0.5, 10, 10))
      dists <- -stats::dist(rbind(X1, X2))
      set.seed(i)
      res.GraphRankTest <- GraphRankTest::RISE(S = dists, sample1ID = 1:nrow(X1), 
                                               sample2ID = (nrow(X1)+1):(nrow(X1)+nrow(X2)), 
                                               perm = 10)
      res.RISE.appr <- DataSimilarity::RISE(X1, as.data.frame(X2), seed = i)
      res.RISE.perm <- DataSimilarity::RISE(X1, as.data.frame(X2), n.perm = 10, seed = i)
      testthat::test_that("output type", {
        # check length and names of output
        testthat::expect_length(res.RISE.appr, 6)
        testthat::expect_named(res.RISE.appr, c("statistic", "p.value", "parameter", 
                                                "alternative", "method", "data.name"))
        testthat::expect_length(res.RISE.perm, 6)
        testthat::expect_named(res.RISE.perm, c("statistic", "p.value", "parameter", 
                                                "alternative", "method", "data.name"))
        # check p values in [0,1]
        testthat::expect_lte(res.RISE.appr$p.value, 1)
        testthat::expect_gte(res.RISE.appr$p.value, 0)
        testthat::expect_lte(res.RISE.perm$p.value, 1)
        testthat::expect_gte(res.RISE.perm$p.value, 0)
        
        # statistic is not NA
        testthat::expect_false(is.na(res.RISE.appr$statistic))
        testthat::expect_false(is.na(res.RISE.perm$statistic))
        # output should be htest
        testthat::expect_s3_class(res.RISE.appr, "htest")
        testthat::expect_s3_class(res.RISE.perm, "htest")
      })
      
      testthat::test_that("output values", {
        # check test statistic values
        testthat::expect_equal(res.RISE.appr$statistic, res.GraphRankTest$test.statistic, 
                               check.attributes = FALSE)
        testthat::expect_equal(res.RISE.perm$statistic, res.GraphRankTest$test.statistic, 
                               check.attributes = FALSE)
        # check test p values
        testthat::expect_equal(res.RISE.appr$p.value, res.GraphRankTest$pval.approx, 
                               check.attributes = FALSE)
        testthat::expect_equal(res.RISE.perm$p.value, res.GraphRankTest$pval.perm, 
                               check.attributes = FALSE)
      })
      
      res.RISE.appr.1 <- DataSimilarity::RISE(X1[, 1, drop = FALSE], as.data.frame(X2)[, 1, drop = FALSE],
                                              seed = i, graph.fun = MST)
      res.RISE.perm.1 <- DataSimilarity::RISE(X1[, 1, drop = FALSE], as.data.frame(X2)[, 1, drop = FALSE], 
                                              n.perm = 10, seed = i, graph.fun = MST)
      testthat::test_that("output type", {
        # check length and names of output
        testthat::expect_length(res.RISE.appr.1, 6)
        testthat::expect_named(res.RISE.appr.1, c("statistic", "p.value", "estimate", 
                                                  "alternative", "method", "data.name"))
        testthat::expect_length(res.RISE.perm.1, 6)
        testthat::expect_named(res.RISE.perm.1, c("statistic", "p.value", "estimate", 
                                                  "alternative", "method", "data.name"))
        # check p values in [0,1]
        testthat::expect_lte(res.RISE.appr.1$p.value, 1)
        testthat::expect_gte(res.RISE.appr.1$p.value, 0)
        testthat::expect_lte(res.RISE.perm.1$p.value, 1)
        testthat::expect_gte(res.RISE.perm.1$p.value, 0)
        
        # statistic is not NA
        testthat::expect_false(is.na(res.RISE.appr.1$statistic))
        testthat::expect_false(is.na(res.RISE.perm.1$statistic))
        # output should be htest
        testthat::expect_s3_class(res.RISE.appr.1, "htest")
        testthat::expect_s3_class(res.RISE.perm.1, "htest")
      })
      
      testthat::test_that("NA action", {
        set.seed(i)
        X1.NA <- matrix(rnorm(1000), ncol = 10)
        X2.NA <- matrix(rnorm(1000, mean = 0.5), ncol = 10)
        X1.NA[sample(length(X1.NA), 0.05 * length(X1.NA))] <- NA
        X2.NA[sample(length(X2.NA), 0.01 * length(X2.NA))] <- NA
        
        testthat::expect_no_error(suppressWarnings(DataSimilarity::RISE(X1.NA, X2.NA, 
                                                                       n.perm = 0, seed = i)))
        testthat::expect_warning(DataSimilarity::RISE(X1.NA, X2.NA, n.perm = 0, seed = i))
        testthat::expect_false(suppressWarnings(is.na(DataSimilarity::RISE(X1.NA, X2.NA, 
                                                                          n.perm = 0, seed = i)$statistic)))
      }) 
    }
  }
}

################################################################################
set.seed(0305)
testRISE(1)

Try the DataSimilarity package in your browser

Any scripts or data that you put into this service are public.

DataSimilarity documentation built on May 15, 2026, 9:07 a.m.