Nothing
#' Processes associated with a list of sequences
#'
#' @description Computes, for a list of sequences, the processes of the
#' Markov renewal representation of a semi-Markov chain (successively
#' visited states, jump times, sojourn times, backward recurrence times) and
#' the counting processes used by the estimators of [fitsmm].
#'
#' @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, \eqn{(J_n)_{n \in \mathbb{N}}}{(J_n), n in N} the visited
#' states, \eqn{(X_n)_{n \in \mathbb{N}^*}}{(X_n), n in N*} the sojourn times
#' and \eqn{(U_k)_{k \in \mathbb{N}}}{(U_k), k in N} the backward recurrence
#' times.
#'
#' For each sequence \eqn{Z_1, \dots, Z_n}{Z_1, ..., Z_n} (time is
#' counted from 1 to \eqn{n}, as the indices of the R vector):
#' * `Jm` gives the successively visited states \eqn{J_0, J_1, \dots}{J_0, J_1, ...}
#' (embedded Markov chain \eqn{(J_n)_{n \in \mathbb{N}}}{(J_n), n in N});
#' * `Xm` gives the corresponding sojourn times \eqn{X_1, X_2, \dots}{X_1, X_2, ...}
#' (\eqn{(X_n)_{n \in \mathbb{N}^*}}{(X_n), n in N*}); the last one is
#' censored by the end of the sequence;
#' * `Tm` gives the times when a new state is entered, from the second
#' visited state on, followed by \eqn{n + 1};
#' * `Um` gives the backward recurrence times
#' \eqn{(U_k)_{k \in \mathbb{N}}}{(U_k), k in N}, i.e. the number of time
#' units already spent in the current state (0 at each jump; Barbu and
#' Limnios, 2008, Equation (6.5), p. 137).
#'
#' The states are taken in their order of first appearance in `sequences`
#' (`unique(unlist(sequences))`), not in alphabetical order.
#'
#' @param sequences A list of vectors (of characters or numbers) representing
#' the observed sequences of states.
#'
#' @return A list with the following components:
#' \item{s}{Number of distinct observed states;}
#' \item{states}{Vector of the observed states, in their order of first
#' appearance (length \eqn{s});}
#' \item{M}{Total length (sum of the length of each sequence);}
#' \item{L}{Number of sequences;}
#' \item{kmax}{Length of the longest sojourn (censored sojourns included);}
#' \item{Ym}{The list `sequences` itself;}
#' \item{Jm}{List of the successively visited states of each sequence;}
#' \item{Tm}{List of the jump times of each sequence (see Details);}
#' \item{Xm}{List of the sojourn times of each sequence;}
#' \item{Um}{List of the backward recurrence times of each sequence;}
#' \item{counting}{List of counting processes, with the states coded by
#' their index in `states` and the sojourn times
#' \eqn{k = 1, \dots, k_{max}}{k = 1, ..., kmax}: `Nijk` (array of
#' dimension \eqn{(s, s, k_{max})}{(s, s, kmax)}, number of transitions
#' \eqn{i \to j}{i -> j} after a complete sojourn of length \eqn{k} in
#' \eqn{i}), its margins `Nij`, `Nik`, `Njk`, `Ni`, `Nj`, `Nk` and its
#' total `N`; `Nbijk` and its margins `Nbik`, `Nbjk`, `Nbk` (first sojourn
#' of each sequence, used for the censoring at the beginning); `Neik`,
#' `Nek` (last sojourn of each sequence, censored at the end); `Nebik`,
#' `Nebk` (sums of the previous two); `Nstarti` (number of sequences
#' starting in each state).}
#'
#' @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.
#'
#' @seealso [get.Niuj], [fitsmm]
#'
#' @examples
#' seqs <- list(c("c", "c", "a", "a", "a", "g"), c("a", "g", "g"))
#' p <- get.processes(seqs)
#' p$states # "c" "a" "g"
#' p$Jm[[1]] # "c" "a" "g"
#' p$Xm[[1]] # 2 3 1
#' p$Tm[[1]] # 3 6 7
#' p$Um[[1]] # 0 1 0 1 2 0
#' p$counting$Nij
#'
#' @export
#'
get.processes <- 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)
processes$Ym <- sequences
processes$Jm <- lapply(processes$Jm, function(x) states[x])
class(processes) <- NULL
return(processes)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.