tests/testthat/test-discrete-matrix-detection.R

test_that("MDgof recognizes and standardizes discrete matrices", {
  grid <- expand.grid(x=0:1, y=0:2)
  counts <- c(11, 13, 17, 19, 23, 29)

  standard <- cbind(x=grid$x, y=grid$y, counts=counts)
  expect_true(is_discrete_data(standard))
  expect_false(prepare_gof_input(standard)$Continuous)

  # Standard names are honored even when columns are rearranged.
  named_reordered <- standard[, c("counts", "y", "x")]
  out <- prepare_gof_input(named_reordered)
  expect_false(out$Continuous)
  expect_equal(colnames(out$x), c("x", "y", "counts"))
  expect_equal(out$x[, "x"], standard[, "x"])
  expect_equal(out$x[, "y"], standard[, "y"])
  expect_equal(out$x[, "counts"], standard[, "counts"])

  # With arbitrary names, infer the count column and preserve the relative
  # order of the two support columns.
  arbitrary <- cbind(first=grid$x, n=counts, second=grid$y)
  out2 <- prepare_gof_input(arbitrary)
  expect_false(out2$Continuous)
  expect_equal(out2$x[, 1], grid$x)
  expect_equal(out2$x[, 2], grid$y)
  expect_equal(out2$x[, 3], counts)

  # Data frames follow the same path.
  out3 <- prepare_gof_input(as.data.frame(arbitrary))
  expect_false(out3$Continuous)
  expect_equal(out3$x, out2$x)
})

test_that("discrete detection rejects matrices that do not have the count-table format", {
  grid <- expand.grid(x=0:1, y=0:2)

  noninteger_counts <- cbind(grid$x, grid$y, c(1, 2, 3, 4, 5, 6.5))
  expect_false(is_discrete_data(noninteger_counts))

  negative_counts <- cbind(grid$x, grid$y, c(1, 2, 3, 4, 5, -1))
  expect_false(is_discrete_data(negative_counts))

  incomplete_grid <- cbind(grid$x[-1], grid$y[-1], 1:5)
  expect_false(is_discrete_data(incomplete_grid))
})

test_that("gof_test accepts a discrete matrix whose count column is not third", {
  pnull <- function(z) {
    if(!is.matrix(z)) z <- rbind(z)
    apply(z, 1, function(v) pbinom(v[1], 1, 0.5) * pbinom(v[2], 2, 0.5))
  }

  rnull <- function() {
    grid <- expand.grid(x=0:1, y=0:2)
    counts <- c(11, 13, 17, 19, 23, 29)
    cbind(first=grid$x, n=counts, second=grid$y)
  }

  ans <- gof_test(rnull(), pnull, rnull, B=0)
  expect_s3_class(ans, "MDgof_test")
  expect_identical(ans$metadata$data.type, "discrete")
})

Try the MDgof package in your browser

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

MDgof documentation built on Sept. 23, 2026, 5:08 p.m.