## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width  = 7,
  fig.height = 5
)

## ----transitions--------------------------------------------------------------
# Observed transitions (from -> to): n individuals
# a -> a : 18    j -> a : 2    p -> j : 1
# j -> j : 13    p -> p : 8    p -> m : 2  (died)
#
# No deaths observed in adults or juveniles — a classic holey matrix!
# raretrans handles this by placing prior weight on the dead fate.

## ----build_matrices-----------------------------------------------------------
library(raretrans)

stage_names <- c("proto", "juvenile", "adult")

# Transition matrix T (column = source stage, row = destination stage)
# Columns must be in the same order as stage_names
T_mat <- matrix(
  c(8,  0,  0,   # proto  -> proto, juvenile, adult
    1, 13,  0,   # juvenile -> proto, juvenile, adult
    0,  2, 18),  # adult  -> proto, juvenile, adult
  nrow = 3, ncol = 3, byrow = TRUE,
  dimnames = list(stage_names, stage_names)
)

# Fertility matrix F (no sexual reproduction observed this period)
F_mat <- matrix(0, nrow = 3, ncol = 3,
                dimnames = list(stage_names, stage_names))

TF <- list(T = T_mat, F = F_mat)

# Observed number of individuals at start of period (column sums of T + deaths)
N <- c(proto = 11, juvenile = 15, adult = 18)

T_mat

## ----cri----------------------------------------------------------------------
cri <- transition_CrI(TF, N, stage_names = stage_names)
cri

## ----plot_with_dead, fig.cap = "Posterior transition probabilities with 95% credible intervals. Note the non-zero dead fate for adults and juveniles despite zero observed deaths."----
plot_transition_CrI(cri,
                    title = "Lepanthes eltoroensis — population 231, year 6")

## ----plot_no_dead, fig.cap = "Posterior transition probabilities for survival transitions only."----
plot_transition_CrI(cri,
                    include_dead = FALSE,
                    title = "Lepanthes eltoroensis — survival transitions only")

