--- title: "iterate-matrix-example" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{iterate-matrix-example} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r include = FALSE} knitr::opts_chunk$set(comment = "#>", eval = greta:::check_tf_version("message")) ``` ```{r setup} library(greta.dynamics) greta_sitrep() ``` ```{r} #| label: simulate # simulate from a probabilistic 4-stage transition matrix model k <- 4 # component variables # survival probability for all stages survival <- uniform(0, 1, dim = k) # conditional (on survival) probability of staying in a stage stasis <- c(uniform(0, 1, dim = k - 1), 1) # marginal probability of staying/progressing stay <- survival * stasis progress <- (survival * (1 - stay))[1:(k - 1)] # recruitment rate for the largest two stages recruit <- exponential(c(3, 5)) ``` ```{r} #| label: combine-matrix # combine into a matrix: tmat <- zeros(k, k) diag(tmat) <- stay progress_idx <- row(tmat) - col(tmat) == 1 tmat[progress_idx] <- progress tmat[1, k - (1:0)] <- recruit ``` ```{r} #| label: show-iterations-growth-rate # analyse this to get the intrinsic growth rate and stable state iterations <- iterate_matrix(tmat) iterations$lambda iterations$stable_distribution iterations$all_states ``` ```{r} #| label: transition-matrices # Can also do this simultaneously for a collection of transition matrices k <- 2 n <- 10 survival <- uniform(0, 1, dim = c(n, k)) stasis <- cbind(uniform(0, 1, dim = n), rep(1, n)) stay <- survival * stasis progress <- (survival * (1 - stasis))[, 1] recruit_rate <- 1 / seq(0.1, 5, length.out = n) recruit <- exponential(recruit_rate, dim = n) tmats <- zeros(10, 2, 2) tmats[, 1, 1] <- stasis[, 1] tmats[, 2, 2] <- stasis[, 2] tmats[, 2, 1] <- progress tmats[, 1, 2] <- recruit ``` ```{r} #| label: show-iterations iterations <- iterate_matrix(tmats) iterations$lambda iterations$stable_distribution dim(iterations$all_states) iterations$all_states[, , 1] iterations$all_states[, , 100] ```