R/get.Niuj.R

Defines functions get.Niuj

Documented in get.Niuj

#' Counting process \ifelse{html}{\out{N<sub>iuj</sub>}}{N_iuj} of the couple (state, backward recurrence time)
#'
#' @description Counts, in a list of sequences, the transitions of the Markov
#'   chain \eqn{(Z_k, U_k)_{k \in \mathbb{N}}}{(Z_k, U_k), k in N}, where
#'   \eqn{(Z_k)_{k \in \mathbb{N}}}{(Z_k), k in N} is the semi-Markov chain and
#'   \eqn{(U_k)_{k \in \mathbb{N}}}{(U_k), k in N} its backward recurrence time
#'   (time already spent in the current state):
#'   \deqn{N_{iuj} = \sum_{k \ge 1} \mathbf{1}_{\{Z_{k - 1} = i,\ U_{k - 1} = u,\ Z_k = j\}}.}{N_iuj = sum_{k >= 1} 1{Z_{k-1} = i, U_{k-1} = u, Z_k = j}.}
#'   The couple \eqn{(Z_k, U_k)_{k \in \mathbb{N}}}{(Z_k, U_k), k in N} is a
#'   Markov chain (Barbu and Limnios, 2008, Section 6.2.1, Equation (6.5) and
#'   Proposition 6.1, p. 137). These counts are the sufficient statistics of
#'   the exact maximum likelihood estimator of a nonparametric semi-Markov
#'   chain censored at the end of the sequences (Trevezas and Limnios, 2011).
#'
#' @details Notation (see [smmR-package] for the complete description of the
#'   models): \eqn{(Z_k)_{k \in \mathbb{N}}}{(Z_k), k in N} is the observed
#'   semi-Markov chain and \eqn{(U_k)_{k \in \mathbb{N}}}{(U_k), k in N} its
#'   backward recurrence time, \eqn{U_k = k - S_{N(k)}}{U_k = k - S_N(k)}.
#'
#'   \eqn{U_k = 0} when the chain has just entered a new state at time
#'   \eqn{k}; if \eqn{Z_{k - 1} = i} and \eqn{U_{k - 1} = u}, then either
#'   \eqn{Z_k = i} and \eqn{U_k = u + 1} (the sojourn in \eqn{i} goes on) or
#'   \eqn{Z_k = j \neq i}{Z_k = j != i} and \eqn{U_k = 0} (jump to \eqn{j}).
#'   The diagonal counts \eqn{N_{iui}}{N_iui} are therefore the numbers of
#'   times the sojourn in \eqn{i} goes on after \eqn{u + 1} time units.
#'
#'   The states are taken in their order of first appearance in `sequences`
#'   (`unique(unlist(sequences))`), as in [get.processes].
#'
#' @param sequences A list of vectors (of characters or numbers) representing
#'   the observed sequences of states.
#'
#' @return A numeric array of dimension \eqn{(s, k_{max}, s)}{(s, kmax, s)},
#'   where \eqn{s} is the number of distinct observed states and
#'   \eqn{k_{max}}{kmax} the length of the longest observed sojourn: the
#'   element `[i, u + 1, j]` is \eqn{N_{iuj}}{N_iuj}, for
#'   \eqn{u = 0, \dots, k_{max} - 1}{u = 0, ..., kmax - 1}. The dimensions are
#'   named with the states and the values of \eqn{u}.
#'
#' @references
#' V. S. Barbu, N. Limnios. (2008). Semi-Markov Chains and Hidden Semi-Markov
#' Models Toward Applications - Their Use in Reliability and DNA Analysis.
#' New York: Lecture Notes in Statistics, vol. 191, Springer.
#'
#' S. Trevezas, N. Limnios. (2011). Exact MLE and asymptotic properties for
#' nonparametric semi-Markov models. Journal of Nonparametric Statistics,
#' 23(3), 719-739.
#'
#' @seealso [get.processes], [fitsmm]
#'
#' @examples
#' seqs <- list(c("c", "c", "a", "a", "a", "g"), c("a", "g", "g"))
#' N <- get.Niuj(seqs)
#' dim(N)      # states "c", "a", "g" (order of appearance), kmax = 3
#'
#' # Transitions from state "a" after u = 0, 1, 2 time units
#' N["a", , ]  # rows: u = 0, 1, 2; columns: next state "c", "a", "g"
#'
#' @export
#'
get.Niuj <- function(sequences) {
 
  #############################
  # Checking parameters sequences
  #############################
  
  if (!(is.list(sequences) && all(sapply(sequences, function(x) is.vector(x) && (is.character(x) || is.numeric(x)))))) {
    stop("'sequences' should be a list of vectors")
  }
  
  states <- unique(unlist(sequences))
  processes <- processesSemiMarkov(sequences = sequences, states = states, verbose = FALSE)
  
  Ym <- lapply(processes$Ym, function(x) x - 1)
  Um <- processes$Um
  s <- processes$s
  kmax <- processes$kmax
  
  Niuj <- getCountingNiuj(Ym, Um, s, kmax)
  dimnames(Niuj) <- list(states, 0:(kmax - 1), states)
  
  return(Niuj)
   
}

Try the smmR package in your browser

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

smmR documentation built on Nov. 28, 2026, 1:06 a.m.