## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>",
                      fig.width = 7, fig.height = 4.5)
library(transitiontrees)
set.seed(1)

## ----data---------------------------------------------------------------------
data(group_regulation_long)
nrow(group_regulation_long)
head(group_regulation_long)
sort(table(group_regulation_long$Action), decreasing = TRUE)

## ----fit----------------------------------------------------------------------
tree <- context_tree(group_regulation_long,
                     actor = "Actor", time = "Time", action = "Action",
                     max_depth = 3L, min_count = 10L)
tree

## ----inspect------------------------------------------------------------------
summary(tree)
model_fit(tree)

## ----common-------------------------------------------------------------------
common_pathways(tree, top = 8)      # the highways

## ----divergent----------------------------------------------------------------
divergent_pathways(tree, top = 8)   # where adding history changes the prediction most

## ----sharp--------------------------------------------------------------------
sharp_pathways(tree, top = 8)       # the most peaked next-action predictions

## ----dependence---------------------------------------------------------------
tree_dependence(tree, sort_by = "entropy_drop", top = 8)

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

## ----holdout------------------------------------------------------------------
model_fit(pruned)$perplexity                       # in-sample (optimistic)

tg <- tune_tree(group_regulation_long,
               actor = "Actor", time = "Time", action = "Action",
               max_depth = 1L:3L, min_count = 10L, folds = 5L, seed = 1L)
attr(tg, "best")                                   # cross-validated winner

## ----scores-------------------------------------------------------------------
wide <- prepare_input(group_regulation_long,
                     actor = "Actor", time = "Time", action = "Action")
mine_sequences(pruned, newdata = wide, which = "surprising", n = 5L)
score_positions(pruned, newdata = wide, worst = 5L)

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

## ----bootstrap-summary--------------------------------------------------------
head(summary(boot), 10)

## ----bootstrap-plot, fig.height = 5.5-----------------------------------------
plot(boot)

## ----groups-------------------------------------------------------------------
grp <- context_tree(group_regulation_long,
                    actor = "Actor", time = "Time", action = "Action",
                    group = "Achiever", max_depth = 2L, min_count = 10L)
cmp <- compare_groups(grp, iter = 199L, seed = 1L)
cmp$omnibus

## ----groups-plot, fig.height = 5----------------------------------------------
plot_difference(grp, depth = 1L)

