From 22389788da8b162b561d4185ce7258e668ce171b Mon Sep 17 00:00:00 2001 From: mitchelloharawild Date: Tue, 17 Sep 2024 00:00:44 +1000 Subject: [PATCH] Fix unit tests --- tests/testthat/test-apply.R | 12 ++++++------ tests/testthat/test-dist-gh.R | 2 +- tests/testthat/test-dist-multivariate-normal.R | 9 ++++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tests/testthat/test-apply.R b/tests/testthat/test-apply.R index 5dd379f..d7a6bd6 100644 --- a/tests/testthat/test-apply.R +++ b/tests/testthat/test-apply.R @@ -84,27 +84,27 @@ test_that("Recycling rules and output for applying multiple inputs over multiple dimnames(dist_named) <- c("a", "b") expect_equal( - quantile(dist, 0.5), + quantile(dist, 0.5, type = "marginal"), matrix(c(1,3,2,5), nrow = 2) ) expect_equal( - quantile(dist_named, 0.5), + quantile(dist_named, 0.5, type = "marginal"), matrix(c(1,3,2,5), nrow = 2, dimnames = list(NULL, c("a", "b"))) ) expect_equal( - quantile(dist, c(0.5, 0.9)), + quantile(dist, c(0.5, 0.9), type = "marginal"), list(matrix(c(1, 3.5631031310892, 2, 4.21971242404268), ncol = 2), matrix(c(3, 5.86563641722901, 5, 7.5631031310892), ncol = 2)) ) expect_equal( - quantile(dist_named, c(0.5, 0.9)), + quantile(dist_named, c(0.5, 0.9), type = "marginal"), list(matrix(c(1, 3.5631031310892, 2, 4.21971242404268), ncol = 2, dimnames = list(NULL, c("a", "b"))), matrix(c(3, 5.86563641722901, 5, 7.5631031310892), ncol = 2, dimnames = list(NULL, c("a", "b")))) ) expect_equal( - quantile(dist, c(0.5, 0.9, 0.95)), + quantile(dist, c(0.5, 0.9, 0.95), type = "marginal"), list( matrix(c(1, 3.5631031310892, 4.28970725390294, 2, 4.21971242404268, 4.84897005289389), ncol = 2), @@ -113,7 +113,7 @@ test_that("Recycling rules and output for applying multiple inputs over multiple ) ) expect_equal( - quantile(dist_named, c(0.5, 0.9, 0.95)), + quantile(dist_named, c(0.5, 0.9, 0.95), type = "marginal"), list( matrix(c(1, 3.5631031310892, 4.28970725390294, 2, 4.21971242404268, 4.84897005289389), ncol = 2, dimnames = list(NULL, c("a","b"))), diff --git a/tests/testthat/test-dist-gh.R b/tests/testthat/test-dist-gh.R index c288f02..4cc664f 100644 --- a/tests/testthat/test-dist-gh.R +++ b/tests/testthat/test-dist-gh.R @@ -11,7 +11,7 @@ test_that("g-and-h distribution", { expect_equal(format(dist), "gh(A = 0, B = 1, g = 0, h = 0.5)") # Require package installed - skip_if_not_installed("gh", "0.1.0") + skip_if_not_installed("gk", "0.1.0") # quantiles expect_equal(quantile(dist, 0.1), gk::qgh(0.1, A, B, g, h, c)) diff --git a/tests/testthat/test-dist-multivariate-normal.R b/tests/testthat/test-dist-multivariate-normal.R index 5597c4d..85bb730 100644 --- a/tests/testthat/test-dist-multivariate-normal.R +++ b/tests/testthat/test-dist-multivariate-normal.R @@ -15,13 +15,16 @@ test_that("Multivariate normal distribution", { # quantiles expect_equal( - quantile(dist, 0.1), + quantile(dist, 0.1, type = "marginal"), matrix(c(qnorm(0.1, mu[1], sqrt(sigma[1,1])), qnorm(0.1, mu[2], sqrt(sigma[2,2]))), nrow = 1, dimnames = list(NULL, c("a", "b"))) ) skip_if_not_installed("mvtnorm") - expect_equivalent(quantile(dist, 0.1, type = "equicoordinate"), mvtnorm::qmvnorm(0.1, mean = mu, sigma = sigma)$quantile) + expect_equivalent( + quantile(dist, 0.1, type = "equicoordinate"), + rep(mvtnorm::qmvnorm(0.1, mean = mu, sigma = sigma)$quantile, 2) + ) # pdf expect_equal(density(dist, cbind(1, 2)), mvtnorm::dmvnorm(c(1, 2), mean = mu, sigma = sigma)) @@ -29,7 +32,7 @@ test_that("Multivariate normal distribution", { # cdf expect_equivalent(cdf(dist, cbind(1, 2)), mvtnorm::pmvnorm(upper = c(1,2), mean = mu, sigma = sigma)) - expect_equivalent(cdf(dist, cbind(-3, 4)), mvtnorm::pmvnorm(c(-3, 4), mean = mu, sigma = sigma)) + expect_equivalent(cdf(dist, cbind(-3, 4)), mvtnorm::pmvnorm(upper = c(-3, 4), mean = mu, sigma = sigma)) # F(Finv(a)) ~= a # expect_equal(cdf(dist, list(as.numeric(quantile(dist, 0.53)))), 0.53, tolerance = 1e-3)