## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(transitiontrees)
has_tna  <- requireNamespace("tna",       quietly = TRUE)
has_nest <- requireNamespace("Nestimate", quietly = TRUE)

## ----data---------------------------------------------------------------------
data(engagement)
class(engagement)
dim(engagement)

## ----wide---------------------------------------------------------------------
tree_wide <- context_tree(engagement, max_depth = 2L, min_count = 5L)
tree_wide$alphabet
n_nodes(tree_wide)

## ----tna, eval = has_tna------------------------------------------------------
library(tna)
model_tna <- tna(engagement)
class(model_tna)

tree_tna <- context_tree(model_tna, max_depth = 2L, min_count = 5L)
tree_tna

## ----nestimate, eval = has_nest-----------------------------------------------
library(Nestimate)
model_nest <- build_tna(engagement)
class(model_nest)

tree_nest <- context_tree(model_nest, max_depth = 2L, min_count = 5L)
tree_nest

## ----agree, eval = has_tna && has_nest----------------------------------------
identical(tree_wide$nodes, tree_tna$nodes)
identical(tree_tna$nodes, tree_nest$nodes)

data.frame(
  route   = c("wide", "tna", "Nestimate"),
  n_nodes = c(n_nodes(tree_wide), n_nodes(tree_tna), n_nodes(tree_nest)),
  nobs    = c(model_fit(tree_wide)$nobs, model_fit(tree_tna)$nobs,
              model_fit(tree_nest)$nobs))

## ----pure-graph, eval = has_nest, error = TRUE--------------------------------
try({
graph_only <- build_tna(engagement)
graph_only$data <- NULL          # strip the stored sequences
context_tree(graph_only)         # -> informative error, not a fabricated fit
})

