SMM: An R Package for Estimation and Simulation of Discrete-time semi-Markov Models

Semi-Markov models, independently introduced by Lévy (1954), Smith (1955) and Takacs (1954), are a generalization of the well-known Markov models. For semi-Markov models, sojourn times can be arbitrarily distributed, while sojourn times of Markov models are constrained to be exponentially distributed (in continuous time) or geometrically distributed (in discrete time). The aim of this paper is to present the R package SMM , devoted to the simulation and estimation of discrete-time multi-state semi-Markov and Markov models. For the semi-Markov case we have considered: parametric and non-parametric estimation; with and without censoring at the beginning and/or at the end of sample paths; one or several independent sample paths. Several discrete-time distributions are considered for the parametric estimation of sojourn time distributions of semi-Markov chains: Uniform, Geometric, Poisson, Discrete Weibull and Binomial Negative.


Introduction
Semi-Markov models, independently introduced by Lévy (1954), Smith (1955) and Takacs (1954), are a generalization of the well-known Markov models.For semi-Markov models, sojourn times can be arbitrarily distributed, while sojourn times of Markov models are constrained to be exponentially distributed (in continuous time) or geometrically distributed (in discrete time).For this reason, semi-Markov processes are more general and more adapted for applications than the Markov processes.
Note that the semi-Markov theory is developed mainly in a continuous-time setting, while utterly less works address the discrete-time case.We refer the reader to Limnios and Oprisan (2001) for continuous-time framework and to Barbu and Limnios (2008) and references therein for discrete-time framework.The R package SMM that we present in this paper is developed in discrete time.Note that undertaking works also in discrete time (modeling stochastic tools, associated estimation procedures, corresponding software, etc.) is an important issue for several reasons.In our opinion, the most relevant of these reasons is that the time scale is intrinsically discrete in several applications.For instance, in DNA studies, when modeling a nucleotide or protein sequence by means of a stochastic process, the "time" of that process is in fact the position along the sequence, so it is discrete.Other examples can be found in some reliability/survival analysis applications where the time represents the number of cycles of a system or the counting of days/hours/etc.We can argue further for the importance of developing works also in discrete time, in parallel to their analogous ones developed in continuous time.For instance, we can mention the simplicity of computations in discrete time, the fact that a discrete-time stochastic process does not explode, the potential use of discrete processes after the discretization of continuous ones, etc.Few R packages have been developed to handle semi-Markov models or hidden semi-Markov models.For semi-Markov models we have the recent semiMarkov R package (Król and Saint-Pierre, 2015) that performs maximum likelihood estimation for parametric continuous-time semi-Markov processes, where the distribution can be chosen between Exponential, Weibull or exponentiated Weibull.That package computes associated hazard rates; covariates can also be taken into account through the Cox proportional hazard model.Two R packages are also dedicated to hidden semi-Markov models, implementing estimation and prediction methods: the hsmm R package (Bulla et al., 2010) and the mhsmm R package (O'Connell and Højsgaard, 2011).
Note that there is no R package developed for discrete-time multi-state semi-Markov models.Thus the purpose of this paper is to present an R package that we have developed, called SMM, which performs parametric and non-parametric estimation and simulation for multi-state discrete-time semi-Markov processes.For the parametric estimation, several discrete distributions are considered The R Journal Vol.10/2, December 2018 ISSN 2073-4859 for the sojourn times: Uniform, Geometric, Poisson, Discrete Weibull and Negative Binomial.The non-parametric estimation concerns the sojourn time distributions, where no assumptions are made on the shape of distributions.Moreover, the estimation can be done on the basis of one or several trajectories, with or without censoring.The aim of this paper is to describe the different possibilities of this package.To summarize, the package SMM that we present deals with different problems: • Parametric estimation for sojourn time distributions (Uniform, Geometric, Poisson, Discrete Weibull and Negative Binomial) or non-parametric estimation; • One or several sample paths; • Four different types of sojourn times: a general one depending on the current state and on the next state to be visited, one depending only on the next state, one depending only on the current state, and one depending neither on the current state nor on the next state; • Four different types of censoring: censoring at the beginning of sample paths, censoring at the end of sample paths, censoring at the beginning and at the end of sample paths or no censoring at all.
Let us make some remarks about these points.
First, concerning the censoring, the simplest situation is the one when all the sojourn times are completely observed (non censored).A more complicated and realistic framework is when the last sojourn time is not completely observed, thus being right censored; in most practical situations this case occurs.An analogous situation is when the first sojourn time is not completely observed, thus being also right censored.In practice, this last case occurs when one does not know the beginning of a phenomenon modeled by a semi-Markov chain.Note that this censoring at the beginning of the sample path is a right censoring (not a left censoring); indeed, when the first sojourn time is censored as in our paper, the available information is that the real sojourn time (that is not observed) is greater than this censored observed time.Thus we are clearly in a right censoring framework, although this happens at the beginning (i.e., left) of the trajectory, which could seem confusing.The most complete framework is when both the first and the last sojourn times are right censored.
Second, when considering estimation starting from several independent sample paths of a semi-Markov chain, it is assumed that all the trajectories are censored in the same way; note that this is not a real constraint, but we imposed this condition only in order to avoid useless technical notations that would make the comprehension more difficult.
Third, note that it is important for the four types of models (of sojourn times) to be considered separately because: (i) in practical situations, one model could be more adapted than some other; (ii) different models will yield specific parameter estimators, so it is important to study them separately.
The paper is organized as follows.The next section Semi-Markov models describes the semi-Markov models used in this package.Section The SMM package illustrates the different functions of the SMM package.We end the paper by presenting some concluding remarks on this R package.

Semi-Markov models
Let us consider a random system with finite state space E = {1, . . ., s}, s < ∞.Let (Ω, A, P) be a probability space and assume that the time evolution of the system is governed by a stochastic process Y = (Y k ) k∈N * , defined on (Ω, A, P) with values in E; that is to say that Y k gives the state of the system at time k.Let T = (T m ) m∈N * , defined on (Ω, A, P) with values in N, be the successive time points when state changes in (Y k ) k∈N * occur (the jump times) and let also J = (J m ) m∈N * , defined on (Ω, A, P) with values in E, be the successively visited states at these time points.The relation between the process Y and the process J of the successively visited states is given by Y k = J N(k) , or, equivalently, In this paper we consider four different semi-Markov models corresponding to the following four types of sojourn times.
• Sojourn times depending on the current state and on the next state: • Sojourn times depending only on the current state: The R Journal Vol.10/2, December 2018 ISSN 2073-4859 • Sojourn times depending only on the next state to be visited: • Sojourn times depending neither on the current state nor on the next state: Note that the sojourn times of the type ) are particular cases of the general type f ij (•).Nonetheless, in some specific applications, particular cases can be important because they are adapted to the phenomenon under study; that is the reason why we investigate these cases separately.
General case: sojourn times of the type f ij (.) Definition 1 (semi-Markov chain SMC and Markov renewal chain MRC).If we have (1) All along this paper we assume that the MRC or SMC are homogeneous with respect to the time in the sense that Equation (1) is independent of m.
Note that if (J, T) is a MRC, then it can be proved that J = (J m ) m∈N * is a Markov chain with state space E, called the embedded Markov chain of the MRC (J, T) (or of the SMC Y).
Definition 2. For a semi-Markov chain we define: (2) Clearly, a semi-Markov chain is uniquely determined a.s.by an initial distribution (µ i ) i∈E and a semi-Markov kernel (q ij (k)) i,j∈E,k∈N or, equivalently, by an initial distribution (µ i ) i∈E , a Markov transition matrix (p ij ) i,j∈E and conditional sojourn time distributions Other assumptions we make are: (i) We do not allow transitions to the same state, i.e., p ii = 0 for all i ∈ E, or equivalently q ii (k) = 0, for all i ∈ E, k ∈ N; (ii) We assume that there are not instantaneous transitions, that is q ij (0) ≡ 0 or equivalently f ij (0) ≡ 0 for all i, j ∈ E; note that this implies that T is a strictly increasing sequence.
For the conditional sojourn time distributions, one can consider the associated cumulative distribution function defined by For any distribution function F(•) we can consider the associated survival/reliability function defined by The R Journal Vol.10/2, December 2018 ISSN 2073-4859 Consequently we have Particular cases: sojourn times of the type f i. (.), f .j(.) and f (.) We have considered up to here general semi-Markov models with the semi-Markov kernel of the type given in (2).Particular types of this semi-Markov model can be taken into account, by considering particular cases of holding time distributions f ij (k), where these distributions depend only on the current state i, or only on the next state j, or neither on i nor on j.For each case, let us define the semi-Markov kernel and the distribution function associated to the sojourn time distribution.
• Sojourn times depending only on the current state: • Sojourn times depending only on the next state: • Sojourn times depending neither on the current state nor on the next state: We also denote the associated survival/reliability functions respectively by

The SMM package
The SMM R package is mainly devoted to the simulation and estimation of discrete-time semi-Markov models in different cases by the two following functions: simulSM() for the simulation of sequences from a semi-Markov model: -One or several trajectories -Discrete Weibull of type I with the density f (x) = q (x−1) β − q x β for x = 1, 2, . . ., n with n > 0, q is the first parameter, 0 < q < 1, and β is the second parameter, β > 0; 0, 1, . . ., n with n > 0, Γ is the Gamma function, α is the parameter of overdispersion, α > 0, and µ is the mean, µ ∈ R. In order to avoid any confusion, note also that the expressions of the different densities considered in this package are also provided in the corresponding manual.
The SMM R package is also devoted to the simulation and estimation of discrete-time Markov models by the two following functions: simulMk() for the simulation of sequences from a kth order Markov model; estimMk() for the estimation of the parameters of the model.
All the different possibilities of the package are illustrated in Figure 1.

Parametric simulation: according to classical distributions
In this part, we will consider the simulation according to classical distributions.
Parameters: This simulation is carried out by the function simulSM().The different parameters of the function are: • E: Vector of state space of length S • NbSeq: Number of simulated sequences The R Journal Vol.10/2, December 2018 ISSN 2073-4859 • lengthSeq: Vector containing the lengths of each simulated sequence • TypeSojournTime: Type of sojourn time; it can be "fij", "fi", "fj" or "f" according to the four cases previously discussed • init: Vector of initial distribution of length S • Ptrans: Matrix of transition probabilities of the embedded Markov chain J = (J m ) m of size S × S • distr: Sojourn time distributions: is a matrix of distributions of size S × S if TypeSojournTime is equal to "fij", is a vector of distributions of size S if TypeSojournTime is equal to "fi" or "fj", where the distributions to be used can be one of "uniform", "geom", "pois", "weibull" or "nbinom".
• param: Parameters of sojourn time distributions: is an array of parameters of size S × S × 2 if TypeSojournTime is equal to "fij" is a matrix of parameters of size S × 2 if TypeSojournTime is equal to "fi" or "fj" is a vector of parameters if TypeSojournTime is equal to "f" • cens.beg:Type of censoring at the beginning of sample paths; 1 (if the first sojourn time is censored) or 0 (if not).All the sequences must be censored in the same way.
• cens.end:Type of censoring at the end of sample paths; 1 (if the last sojourn time is censored) or 0 (if not).All the sequences must be censored in the same way.
• File.out:Name of fasta file for saving the sequences; if File.out = NULL, no file is created.
A fasta file is a simple text file containing sequences only described by one description line beginning by a ">": an example is given in Figure 2.
> sequence 1 aaacgtacgagtcgatcgatcgactcgatcgtacgtacggt > sequence 2 acgtattacgatgctagctaggttggggactgcatgcatgaatgagcgatc The R commands below generate three sequences of size 1000, 10000 and 2000 respectively with the finite state space E = {a, c, g, t}, where the sojourn times depend on the current state and on the next state.
Second, note also that the parameters of the distributions are given in the following way: for example, f 13 (•) is Geometric distribution with parameter 0.4, while f 14 (•) is Negative Binomial with parameters 4 and 2. In other words, the parameters of f 13 (•) are given in the vector param.array[1,3,] that is equal to (0.4, 0) and the parameters of f 14 (•) are given in the vector param.array[1,4,] that is equal to (4, 2); that means that if a distribution has only 1 parameter, the corresponding vector of parameters will have 0 on the second position.
Values: The function simulSM() returns a list of simulated sequences.These sequences can be saved in a fasta file by using the parameter File.out.

seq3[[1]][1:15]
[1] "g" "g" "g" "g" "c" "c" "c" "a" "a" "a" "c" "c" "c" "g" "g" Non-parametric simulation: according to distributions given by the user Now we will consider the simulation according to distributions given by the user.• TypeSojournTime: Type of sojourn time; it can be "fij", "fi", "fj" or "f" according to the four cases previously discussed • init: Vector of initial distribution of length S • Ptrans: Matrix of transition probabilities of the embedded Markov chain J = (J m ) m of size S × S • laws: Sojourn time distributions introduced by the user: is an array of size S × S × Kmax if TypeSojournTime is equal to "fij", is a matrix of size S × Kmax if TypeSojournTime is equal to "fi" or "fj", where Kmax is the maximum length for the sojourn times.
• cens.beg:Type of censoring at the beginning of sample paths; 1 (if the first sojourn time is censored) or 0 (if not).All the sequences must be censored in the same way.
• cens.end:Type of censoring at the end of sample paths; 1 (if the last sojourn time is censored) or 0 (if not).All the sequences must be censored in the same way.
• File.out:Name of fasta file for saving the sequences; if File.out = NULL, no file is created.
The R commands below generate three sequences of size 1000, 10000 and 2000 respectively with the finite state space E = {a, c, g, t}, where the sojourn times depend only on the next state.

Values:
The function simulSM() returns a list of simulated sequences.These sequences can be saved in a fasta file by using the parameter File.out.

Estimation of semi-Markov models
In this subsection we explain and illustrate the estimation of a semi-Markov model in the parametric and non-parametric cases.

Parametric estimation of semi-Markov models
We will consider the distributions where the dimension of parameters set m ij is known; no assumptions are made on p ij ij .From the data, we want to estimate p ij and θ ij , i, j ∈ E. Let us assume that we have at our disposal several independent sample paths of a semi-Markov chain, say L, each of them of length n l , l = 1, . . ., L, censored at the beginning and at the end of the trajectory, i.e., y l 1 , y l 2 , . . ., y l n l , The R Journal Vol.10/2, December 2018 ISSN 2073-4859 or, equivalently, j l 0 , k l 0 , j l 1 , k l 1 , j l 2 , k l 2 , . . ., j l t l , k l t l , j l t l +1 , k l t l +1 with ∑ t l +1 i=0 k l i = n l , where j l 0 , . . ., j l t l +1 are the successive distinct visited states, k l 0 is the first sojourn time, assumed to be right censored, k l t l +1 is the last sojourn time, assumed also to be right censored, while k l 1 , . . ., k l t l are the other successive sojourn times, assumed to be complete (observed, non censored).
To estimate the parameters of model, we use the maximum likelihood estimation: where we introduced the following counting processes >k} , where N l (n l ) = max{m ∈ N | T l m ≤ n l } is the counting process of jump number in [1; n l ] of the trajectory l.

Note that:
-N ij (L, n 1:L ) represents the number of transitions from state i to state j along the L sample paths; -N ij (k; L, n 1:L ) represents the number of transitions from state i to state j along the L sample paths, with sojourn time of length k in state i; represents the number of trajectories starting in state i, with a next transition to state j and censored sojourn time in state i greater than k; -N e i• (k; L) represents the number of trajectories ending in state i with a censored sojourn time in state i greater than k.
Note also that in the expression (6) of the log-likelihood, the first two terms correspond to the transition probabilities and the observed (non censored) sojourn times, the third term is the contribution to the likelihood of the first sojourn times, assumed to be right censored, while the last term is the contribution to the likelihood of the last sojourn times, assumed to be right censored.
Up to here we presented the estimation for the general case, taking into account the censoring at the beginning and at the end and the sojourn times depending on the current state and on the next state.Thus the maximization problem ( 6) is written with the sojourn times depending on the current state and on the next state (the general model of the type The R Journal Vol.10/2, December 2018 ISSN 2073-4859 different cases of sojourn times are written and coded in the package.Note also that different types of censoring are also written and coded in the package.

Parameters:
The estimation is carried out by the function estimSM().The different parameters of the function are: • file: Path of the fasta file which contains the sequences from which to estimate • seq: List of the sequence(s) from which to estimate • E: Vector of state space of length S • TypeSojournTime: Type of sojourn time; it can be "fij", "fi", "fj" or "f" according to the four cases previously discussed • distr: Sojourn time distributions: is a matrix of distributions of size S × S if TypeSojournTime is equal to "fij", is a vector of distributions of size S if TypeSojournTime is equal to "fi" or "fj", where the distributions to be used can be one of "uniform", "geom", "pois", "weibull" or "nbinom".
• cens.beg:Type of censoring at the beginning of sample paths; 1 (if the first sojourn time is censored) or 0 (if not).All the sequences must be censored in the same way.
• cens.end:Type of censoring at the end of sample paths; 1 (if the last sojourn time is censored) or 0 (if not).All the sequences must be censored in the same way.
Note that the sequences from which we estimate can be given either as an R list (seq argument) or as a file in fasta format (file argument).

Values:
The function estimSM() returns a list containing: • init: Vector of size S with estimated initial probabilities of the semi-Markov chain estSeq3$init [1] 0.0000000 0.0000000 0.3333333 0.6666667  ,3,] is the vector containing the parameters of the distribution f 13 (•).
• param: Parameters of sojourn time distributions: is an array of parameters of size S × S × 2 if TypeSojournTime is equal to "fij" is a matrix of parameters of size S × 2 if TypeSojournTime is equal to "fi" or "fj" is a vector of parameters if TypeSojournTime is equal to "f" • laws: Sojourn time distributions introduced by the user: is an array of size S × S × Kmax if TypeSojournTime is equal to "fij", -is a matrix of size S × Kmax if TypeSojournTime is equal to "fi" or "fj", The R Journal Vol.10/2, December 2018 ISSN 2073-4859 • distr: Sojourn time distributions: is a matrix of distributions of size S × S if TypeSojournTime is equal to "fij", is a vector of distributions of size S if TypeSojournTime is equal to "fi" or "fj", -is a distribution if TypeSojournTime is equal to "f", where the distributions to be used can be one of "uniform", "geom", "pois", "weibull" or "nbinom".
• param: Parameters of sojourn time distributions: is an array of parameters of size S × S × 2 if TypeSojournTime is equal to "fij" is a matrix of parameters of size S × 2 if TypeSojournTime is equal to "fi" or "fj" is a vector of parameters if TypeSojournTime is equal to "f" • laws: Sojourn time distributions introduced by the user: is an array of size S × S × Kmax if TypeSojournTime is equal to "fij", is a matrix of size S × Kmax if TypeSojournTime is equal to "fi" or "fj", is a vector of length Kmax if TypeSojournTime is equal to "f", where Kmax is the maximum length for the sojourn times.

733
Values: The function BIC_SM() returns a list with the value of BIC for each sequence.

The Markov case
In the SMM R package, we have also implemented the estimation and the simulation of discrete-time multi-state Markov models.As in the semi-Markov case, other functions are available, enabling to estimate the initial distribution, to compute the log-likelihood and also the AIC and BIC of a Markov model.
To summarize, the importance and interest of the R package SMM that we have developed come from: -considering versatile tools, namely discrete-time multi-state semi-Markov processes, that are of use in a variety of applied fields, like survival analysis, biology, reliability, DNA analysis, insurance and finance, earthquake modeling, meteorology studies, etc.An example of application can be the description of DNA sequences by using a 2-state process to distinguish between coding and noncoding regions in DNA sequences.It is well known that the length of such regions generally does not follow a Geometric distribution.Thus semi-Markov chains can represent adapted tools for this type of modeling.
-implementing parametric and non-parametric estimation/simulation; -considering several censoring schemes, that important in various applications; -taking into account one or several independent sample paths; -considering different types of semi-Markov kernels: either of the general type q ij (k) = p ij f ij (k) with the holding time distributions f ij (k) depending on the current state and on the next state to be visited, or with the holding time distributions depending only on the current state, q ij (k) := p ij f i• (k), or with the holding time distributions depending only on the next state to be visited, q ij (k) := p ij f •j (k), or with the holding time distributions depending neither on the current, nor on the future state, q ij (k) := p ij f (k).As already mentioned, it is important that these four types of models be considered separately.
In conclusion, the R package SMM that we have developed deals with an important and versatile tool, useful for researchers, practitioners and engineers in various fields.

Figure 1 :
Figure 1: Schema of the SMM package.

Figure 2 :
Figure 2: Example of a fasta file.

Parameters:
This simulation is carried out by the function simulSM().The different parameters of the function are: • E: Vector of state space of length S • NbSeq: Number of simulated sequences • lengthSeq: Vector containing the lengths of each simulated sequence txt") ## computation of the log-likelihood LoglikelihoodSM(seq = seq5000, E = E, mu = rep(1/4,4), Ptrans = Pij, distr = "pois", param = 2The function likelihoodSM() returns a list containing: • L: List with the value of the likelihood for each sequence • Kmax: Maximal sojourn time We also consider model selection criteria in order to evaluate and choose among candidate models; the Akaike information criterion (AIC) and the Bayesian information criterion (BIC) are considered.AIC_SM(): computation of the AIC AIC(M) = −2 log L + 2M, where L is the log-likelihood, M is the number of parameters involved in the model Parameters: • E: Vector of state space of length S • seq: List of the sequence(s) from which to estimate • mu: Vector of initial distribution of length S • Ptrans: Matrix of transition probabilities of the embedded Markov chain J = (J m ) m of size S × S [1] 0.2205882 0.2205882 0.2058824 0.3529412 LoglikelihoodMk(): Computation of the log-likelihood Parameters: • mu: Initial distribution • Ptrans: Probability transition matrix • k: Order of the Markov chain ## state space E = c("a","c","g","t") S = length(E) ## creation of transition matrix p = matrix(rep(1/4,S*S),ncol=4) ## for the reproducibility of the results set.seed(6) ## simulation of two sequences of length 20 and 50 respectively seq.markov2 = simulMk(E = E, nbSeq = 2, lengthSeq = c(20,50), The R Journal Vol.10/2, December 2018 ISSN 2073-4859 models, where the sojourn time is only modelled by a Geometric distribution (in discrete time) or an Exponential distribution (in continuous time).The SMM package offers a variety of conditional sojourn time distributions (Poisson, Uniform, Negative Binomial, Geometric and Discrete Weibull).
Four different types of censoring mechanisms: censoring at the beginning of sample paths, censoring at the end, censoring at the beginning and at the end, no censoring -Four different types of sojourn times: depending on the current state and on the next state, depending only on the current state, depending only on the next state, depending neither on the current state nor on the next state estimSM() for the estimation of model parameters: Four different types of sojourn times: depending on the current state and on the next state, depending only on the current state, depending only on the next state, depending neither on the current state nor on the next state As the Negative Binomial distribution and the Discrete Weibull distribution can have several different parameterizations, note that we have considered the following ones in our package: -According to classical distributions for the sojourn times (Uniform, Geometric, Poisson, Discrete Weibull and Negative Binomial) or according to distributions given by the user --One or several trajectories -Parametric (Uniform, Geometric, Poisson, Discrete Weibull and Negative Binomial) or non-parametric distributions for the sojourn times -Four different types of censoring mechanisms: censoring at the beginning of sample paths, censoring at the end, censoring at the beginning and at the end, no censoring The R Journal Vol.10/2, December 2018 ISSN 2073-4859 - Matrix of size S × S with estimated transition probabilities of the embedded Markov chain J = (J m ) m Array with estimated parameters of the sojourn time distributions • Ptrans:• param: