transitiontrees: Transition Trajectories and Dynamics of Variable-Length Pathways or Sequences
Source:R/transitiontrees-package.R
transitiontrees-package.Rdtransitiontrees models how categorical processes unfold: how one
state follows another in learning sessions, collaborative work, care
pathways or any other ordered record of events. Its model is the
variable-order transition tree, known as a prediction suffix tree (Ron,
Singer & Tishby, 1996) or variable-length Markov chain
(Bühlmann & Wyner, 1999). A first-order Markov chain
predicts the next state from the current state alone, and a chain of
fixed order \(m\) from the last \(m\) states, with a number of
parameters that grows exponentially in \(m\). A transition tree lets
the length of the relevant history vary: it keeps a longer context only
where the data show that the extra history changes what comes next.
Each node of the tree is a pathway, a run of recent states written from
oldest to newest ("A -> B -> C"), with the distribution of the
state that followed it. The package treats these pathways as the unit of
analysis. It reports which pathways occur, which of them change the
prediction relative to a shorter history, how stable they are under
resampling, and whether they differ between groups, and it uses the
fitted tree to predict, score, simulate and impute sequences. All
results are returned as data frames with one row per pathway, sequence
or group, and all plots are ggplot2 objects.
Details
With the default floor smoothing, the fitted counts and probabilities match those of the PST package (Gabadinho & Ritschard, 2016). The smoothing schemes other than the floor, the pruning criteria, cross-validated tuning, the bootstrap and the permutation tests are this package's own.
Data input
context_tree() accepts wide sequence tables, lists of
sequences, long event logs (reshaped by prepare_input()),
and transition network models from tna (Saqr et al., 2025) and
Nestimate that carry their sequences. The bundled datasets are trajectories,
engagement, group_regulation_long and
ai_long.
Fitting and refining a tree
context_tree() fits a tree, or one tree per group.
prune_tree() removes contexts that do not change the
prediction, smooth_tree() re-estimates probabilities under
another smoothing scheme, and compare_pruning() and
compare_smoothing() tabulate the alternatives.
tune_tree() chooses depth, minimum count, smoothing and
pruning by cross-validated perplexity.
Reading pathways
tree_pathways() (also as.data.frame()) lists every
pathway; common_pathways(),
divergent_pathways() and sharp_pathways()
rank them by frequency, divergence from the shorter history, and
certainty of the next state. tree_dependence() adds
entropies, state_frequencies() gives the state counts,
and query_pathway(), pathway_exists(),
subtree() and mine_contexts() look up
individual pathways.
Fit, prediction and scoring
model_fit() returns log-likelihood, AIC, BIC and
perplexity, in-sample or on new data (see also
logLik.transitiontrees(), perplexity() and
n_nodes()). predict.transitiontrees()
predicts the next state, score_sequences(),
score_positions() and mine_sequences() score
observed sequences, generate_sequences() simulates new
ones, and impute_sequences() fills gaps.
Reliability and comparison
bootstrap_pathways() resamples sequences to assess the
stability of each pathway. compare_trees() and
tree_distance() compare two trees, and
compare_groups() tests group differences pathway by
pathway with permutation tests.
Visualization
plot.transitiontrees() draws the tree;
plot_pathways(), plot_divergence() and
plot_distributions() show pathways and their next-state
distributions; plot_pruning() shows the pruning decision
along one pathway; plot_predictive() shows predictive
performance on new data; plot_trajectories() draws the
forward tree of sequence beginnings; and
plot_difference() and
plot_pathway_resamples() show group differences and
bootstrap distributions.
References
Bühlmann, P., & Wyner, A. J. (1999). Variable length Markov chains. The Annals of Statistics, 27(2), 480-513. doi:10.1214/aos/1018031204
Gabadinho, A., & Ritschard, G. (2016). Analyzing state sequences with probabilistic suffix trees: The PST R package. Journal of Statistical Software, 72(3). doi:10.18637/jss.v072.i03
Ron, D., Singer, Y., & Tishby, N. (1996). The power of amnesia: Learning probabilistic automata with variable memory length. Machine Learning, 25(2-3), 117-149. doi:10.1023/A:1026490906255
Saqr, M., López-Pernas, S., Törmänen, T., Kaliisa, R., Misiejuk, K., & Tikka, S. (2025). Transition network analysis: A novel framework for modeling, visualizing, and identifying the temporal patterns of learners and learning processes. In Proceedings of the 15th International Learning Analytics and Knowledge Conference (LAK '25) (pp. 351-361). doi:10.1145/3706468.3706513
Author
Maintainer: Mohammed Saqr [email protected] (ORCID) [copyright holder]
Authors:
Mohammed Saqr [email protected] (ORCID) [copyright holder]
Sonsoles López-Pernas [email protected] (ORCID)