An inner Markov loop implemented using RcppArmadillo
ArmaMarkovLoop(sim, cohort, transition, duration)
sim | Matrix with the same number of rows as the duration of the model and the same number of columns as the number of states in the model. |
---|---|
cohort | A vector containing the initial state. |
transition | A transition matrix, see |
duration | Numeric, how many long to run the model for. |
A matrix of the same size as the inputted sim matrix.
transition <- matrix(rnorm(4), 2, 2) sim <- matrix(NA, 100, 2) cohort <- c(1, 0) duration <- 100 # Reference R implementation sim_r <- markov_loop(sim, cohort, transition, duration) # RcppArmadillo implementation sim_rcppArma <- ArmaMarkovLoop(sim, cohort, transition, duration) # Check results are within tolerances all.equal(sim_r, sim_rcppArma)#> [1] TRUE# Benchmark library(microbenchmark) microbenchmark(markov_loop(sim, cohort, transition, duration), ArmaMarkovLoop(sim, cohort, transition, duration), times = 1000)#> Unit: microseconds #> expr min lq mean #> markov_loop(sim, cohort, transition, duration) 82.705 90.0675 111.054580 #> ArmaMarkovLoop(sim, cohort, transition, duration) 4.707 5.8930 7.477264 #> median uq max neval #> 101.8255 128.666 3565.076 1000 #> 6.6350 8.538 50.838 1000