## ----options, include = FALSE-----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7,
                      fig.height = 4.5, fig.align = "center", dpi = 96)
old_options <- options(digits = 3, width = 90)

## ----setup------------------------------------------------------------------------------
library(transitiontrees)
head(engagement, 3)

## ----fit--------------------------------------------------------------------------------
tree <- context_tree(engagement, max_depth = 3, min_count = 10)
tree

## ----frequencies------------------------------------------------------------------------
state_frequencies(tree)

## ----summary----------------------------------------------------------------------------
summary(tree)

## ----common-----------------------------------------------------------------------------
common_pathways(tree, top = 5)

## ----divergent--------------------------------------------------------------------------
divergent_pathways(tree, top = 6, min_count = 50)

## ----sharp------------------------------------------------------------------------------
sharp_pathways(tree, top = 5, min_count = 50)

## ----prune------------------------------------------------------------------------------
pruned <- prune_tree(tree, criterion = "G2", alpha = 0.05)
pruned

## ----plot-tree, fig.width = 9, fig.height = 5-------------------------------------------
plot(pruned)

## ----compare-pruning--------------------------------------------------------------------
compare_pruning(tree)

## ----fit-unpruned-----------------------------------------------------------------------
model_fit(tree)

## ----fit-pruned-------------------------------------------------------------------------
model_fit(pruned)

## ----predict----------------------------------------------------------------------------
histories <- list(c("Active", "Average"),
                  c("Average", "Disengaged"),
                  c("Disengaged", "Active", "Disengaged"))
predict(pruned, newdata = histories, tidy = TRUE)

## ----restore-options, include = FALSE-----------------------------------------
options(old_options)

