File: expmMC.R

package info (click to toggle)
r-cran-ctmcd 1.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,384 kB
  • sloc: cpp: 183; ansic: 19; makefile: 2
file content (25 lines) | stat: -rw-r--r-- 1,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
expmMC =
  function(gm, t, method="PadeRBS",order=8) {
    if (!requireNamespace("markovchain", quietly = TRUE)) {
      stop("markovchain is needed for this function to work. Please install it.",
           call. = FALSE)
    }
    if(!(class(gm) %in% c("gm","matrix","Matrix")))
      stop("Error! Expecting either a matrix or a gm object")
    if ( class(gm) %in% c("matrix","Matrix")) generator_matrix = gm else generator_matrix = as.matrix(gm[["par"]])
    transitionMatrix = expm(generator_matrix*t,method=method,order=order)
    out = list()
    attr(out,"states") = letters[1:nrow(transitionMatrix)]
    attr(out,"byrow") = TRUE
    rownames(transitionMatrix) = letters[1:nrow(transitionMatrix)]
    colnames(transitionMatrix) = letters[1:ncol(transitionMatrix)]
    attr(out,"transitionMatrix") = transitionMatrix
    attr(out,"name") = "Unnamed Markov chain"
    class(out) = "markovchain"
    attr(attr(out,"class"),"package") = "markovchain"
    out = asS4(out)
    return(out)
  }