## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>",
                      fig.width = 8, fig.height = 5, out.width = "100%")

## ----setup--------------------------------------------------------------------
library(transitiontrees)
data(trajectories)
set.seed(1)

tree   <- context_tree(trajectories, max_depth = 4L, min_count = 5L)
pruned <- prune_tree(tree, criterion = "G2", alpha = 0.05)

## ----smoothing-grid-----------------------------------------------------------
compare_smoothing(trajectories, max_depth = 4L, min_count = 5L)

## ----resmooth-----------------------------------------------------------------
compare_smoothing(pruned)

## ----pruning-grid-------------------------------------------------------------
compare_pruning(tree)

## ----tune---------------------------------------------------------------------
tg <- tune_tree(trajectories, max_depth = 1L:4L, folds = 5L, seed = 42L)
head(tg, 6)
attr(tg, "best")

## ----tune-plot, fig.height = 5------------------------------------------------
plot(tg)

## ----boot---------------------------------------------------------------------
boot <- bootstrap_pathways(pruned, iter = 100L, stat = "count",
                          seed = 1L, keep_resamples = TRUE)
boot

## ----boot-cis-----------------------------------------------------------------
head(summary(boot))

## ----boot-resamples, fig.height = 4.5-----------------------------------------
plot_pathway_resamples(boot, stat = "divergence", top = 6L)

## ----group-fit----------------------------------------------------------------
data(group_regulation_long)
grp <- prune_tree(context_tree(group_regulation_long,
                              actor = "Actor", time = "Time", action = "Action",
                              group = "Achiever", max_depth = 2L, min_count = 10L))
cmp <- compare_trees(grp, iter = 199L, seed = 1L)
cmp

## ----compare-plot, fig.height = 4.5-------------------------------------------
plot(cmp)

## ----query--------------------------------------------------------------------
query_pathway(pruned, c("Active", "Active"))               # full distribution
query_pathway(pruned, "Disengaged", next_state = "Disengaged")  # one cell
pathway_exists(pruned, "Active -> Disengaged")             # membership (no backoff)

## ----query-exact--------------------------------------------------------------
query_pathway(pruned, c("Active", "Average", "Active"), exact = TRUE)

## ----subtree------------------------------------------------------------------
sub <- subtree(pruned, "Active")   # its banner reads "subtree of: Active"
sub
head(tree_pathways(sub), 4)

## ----mine-contexts------------------------------------------------------------
mine_contexts(pruned, state = "Disengaged", min_prob = 0.5)

## ----mine-sequences-----------------------------------------------------------
mine_sequences(pruned, newdata = trajectories, which = "surprising", n = 5L)

## ----impute-------------------------------------------------------------------
gappy <- list(c("Active", "Active", NA, "Disengaged"),
              c("Average", NA, "Average"))
impute_sequences(pruned, gappy, method = "modal")

## ----generate-----------------------------------------------------------------
generate_sequences(pruned, n = 4L, length = 10L)
simulate(pruned, nsim = 4L, seed = 42L, length = 10L)

