tests/testthat/test-vectorized.R

# The vectorized computations give the same values as the position by
# position definitions

test_that("stationary laws at all positions", {
  for (x in list(model_ab1(), model_ab2())) {
    all <- getStationaryLaw(x, all.pos = TRUE)
    for (p in c(1, 4, x$length))
      expect_equal(unname(all[p, ]), unname(as.vector(getStationaryLaw(x, p))))
    sd <- stationary_distributions(x, start = 1, end = x$length)[[1]]
    expect_equal(unname(sd[, ab]), unname(all))
  }
})

test_that("word probabilities of order 2 models", {
  x <- model_ab2(length = 10)
  st <- getStationaryLaw(x, 3, internal = TRUE)
  names(st) <- rownames(x$matrices$Pi0)
  expect_equal(unname(word_probability("aba", 3, x)),
               unname(st[["ab"]] * getTransitionMatrix(x, 5)["ab", "a"]))
  # word shorter than the order: words of length 2 beginning with the word
  expect_equal(unname(word_probability("b", 3, x)), unname(st[["ba"]] + st[["bb"]]))
  wp <- words_probabilities(c("aba", "b", "ab"), c(2, 6), x)[[1]]
  for (j in 1:3) for (p in 2:6)
    expect_equal(wp[p - 1, j + 1], unname(word_probability(c("aba", "b", "ab")[j], p, x)))
  expect_equal(word_probabilities("aba", c(2, 6), x)[[1]]$probability, wp[, 2])
})

test_that("simulation of an order 2 model", {
  set.seed(2)
  x <- model_ab2(length = 40000)
  s <- simulate(x, model_size = 40001)
  fit <- fitdmm(s, 2, 1, ab, init.estim = "freq")
  expect_equal(fit$matrices$Pi0, x$matrices$Pi0, tolerance = 0.06)
  expect_equal(fit$matrices$Pi1, x$matrices$Pi1, tolerance = 0.06)
})

Try the drimmR package in your browser

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

drimmR documentation built on Nov. 28, 2026, 1:08 a.m.