tests/testthat/test-structures.R

test_that("all seven structures produce valid generators", {
  for (p in ms_structures()) {
    Q <- ms_generator(p)
    expect_true(is.matrix(Q))
    expect_equal(nrow(Q), ncol(Q))
    expect_equal(unname(rowSums(Q)), rep(0, nrow(Q)), tolerance = 1e-12)
    off <- Q; diag(off) <- 0
    expect_true(all(off >= 0))
    expect_silent(ms_validate_Q(Q))
  }
})

test_that("reference intensities match the documented values", {
  expect_equal(ms_generator("two_state")[1, 2], 0.20)
  Q3 <- ms_generator("illness_death_3state")
  expect_equal(c(Q3[1, 2], Q3[1, 3], Q3[2, 3]), c(0.20, 0.10, 0.40))
  expect_equal(sum(ms_allowed("complex_hybrid")), 6L)
  expect_equal(sum(ms_allowed("illness_death_4state")), 5L)
})

test_that("absorbing states have zero diagonal", {
  Q <- ms_generator("illness_death_3state")
  expect_equal(Q[3, 3], 0)
  Q <- ms_generator("competing_risks")
  expect_equal(diag(Q)[2:3], c(`Cause 1` = 0, `Cause 2` = 0))
})

test_that("invalid generators are rejected", {
  expect_error(ms_validate_Q(matrix(1:6, 2, 3)), "square")
  expect_error(ms_validate_Q(rbind(c(0, -0.1), c(0, 0))), "non-negative")
  expect_error(ms_validate_Q(matrix(0, 3, 3)), "no permitted transitions")
  expect_warning(ms_validate_Q(rbind(c(0, 0.2, 0), c(0, 0, 0), c(0, 0, 0))),
                 "unreachable")
  expect_error(ms_generator("not_a_process"), "Unknown process")
})

test_that("user Q is checked against the declared structure", {
  bad <- ms_generator("illness_death_3state")
  bad[2, 1] <- 0.1                       # recovery, forbidden by the structure
  expect_error(sim_mspdata("illness_death_3state", n = 5, t = 5, Q = bad),
               "forbids")
})

Try the modMStates package in your browser

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

modMStates documentation built on Sept. 3, 2026, 5:10 p.m.