tests/testthat/helper-models.R

# Small models used by the tests, and exact computations by enumeration of all
# the sequences X_0, ..., X_m (independent of the recursions of the package).

ab <- c("a", "b")

model_ab1 <- function(length = 8) {
  dmm(ab, init = c(0.3, 0.7),
      matrices = list(matrix(c(0.9, 0.1, 0.4, 0.6), 2, byrow = TRUE),
                      matrix(c(0.2, 0.8, 0.7, 0.3), 2, byrow = TRUE)),
      order = 1, degree = 1, length = length)
}

model_ab2 <- function(length = 8) {
  # rows: aa, ab, ba, bb
  dmm(ab, init = c(0.1, 0.2, 0.3, 0.4),
      matrices = list(matrix(c(0.9, 0.1, 0.5, 0.5, 0.3, 0.7, 0.6, 0.4), 4, byrow = TRUE),
                      matrix(c(0.2, 0.8, 0.6, 0.4, 0.8, 0.2, 0.1, 0.9), 4, byrow = TRUE)),
      order = 2, degree = 1, length = length)
}

# all the sequences of m + 1 states (one per row) and their probabilities,
# computed directly from the definition of the model
all_paths <- function(x, m) {
  paths <- as.matrix(expand.grid(rep(list(x$states), m + 1), stringsAsFactors = FALSE))
  k <- x$order
  prob <- apply(paths, 1, function(p) {
    pr <- x$init[[paste(p[seq_len(k)], collapse = "")]]
    for (t in k:m) {
      P <- getTransitionMatrix(x, t)
      pr <- pr * P[paste(p[(t - k + 1):t], collapse = ""), p[t + 1]]
    }
    pr
  })
  list(paths = paths, prob = prob)
}

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.