| Title: | Transparent Analysis of Ordered Categorical Sequences |
|---|---|
| Description: | Provides transparent, reproducible, and auditable tools for validating, preparing, encoding, summarising, comparing, modelling, and diagnosing ordered categorical sequence data. Supports explicit preprocessing policies, contiguous motifs and bounded subsequences, consensus and group comparisons, edit and transition distances, clustering and stability diagnostics, transition networks, higher-order models, categorical, mixture, multichannel, and covariate hidden Markov models, longitudinal panel workflows, time-varying models, design-aware inference, analysis contracts and provenance audits, and guarded adapters to specialist sequence-analysis packages. |
| Authors: | Stefanos Balaskas [aut, cre] (ORCID: <https://orcid.org/0000-0003-2444-9796>) |
| Maintainer: | Stefanos Balaskas <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.1 |
| Built: | 2026-10-09 21:25:09 UTC |
| Source: | https://github.com/stefanosbalaskas/gp3sequences |
Convert sequence data to cSPADE transaction input
as_arules_sequences( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state" )as_arules_sequences( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state" )
data |
Long-format sequence data. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
An arules transactions object whose transaction information
contains positive integer sequenceID and eventID fields required by
arulesSequences::cspade().
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) if (requireNamespace("arules", quietly = TRUE) && requireNamespace("arulesSequences", quietly = TRUE)) { as_arules_sequences(sequences) }sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) if (requireNamespace("arules", quietly = TRUE) && requireNamespace("arulesSequences", quietly = TRUE)) { as_arules_sequences(sequences) }
Create GrpString-compatible event and string inputs
as_grpstring_data( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", alphabet = NULL )as_grpstring_data( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", alphabet = NULL )
data |
Long-format sequence data. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
alphabet |
Optional single-character symbols. When omitted, printable ASCII characters are assigned deterministically. |
A list of class gp3_grpstring_input containing a wide event data
frame, event-name vector, character vector, conversion key, and string vector.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) as_grpstring_data(sequences)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) as_grpstring_data(sequences)
Convert a transition network to an igraph object
as_igraph_transition_network(network, directed = TRUE)as_igraph_transition_network(network, directed = TRUE)
network |
A first-order transition network. |
directed |
Whether the resulting graph is directed. |
An igraph graph with edge attributes copied from the network.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) network <- create_transition_network(sequences) if (requireNamespace("igraph", quietly = TRUE)) { as_igraph_transition_network(network) }sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) network <- create_transition_network(sequences) if (requireNamespace("igraph", quietly = TRUE)) { as_igraph_transition_network(network) }
Convert sequence data to seqHMM observations
as_seqhmm_sequences( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", ... )as_seqhmm_sequences( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", ... )
data |
Long-format sequence data. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
... |
Additional arguments passed to |
A TraMineR stslist suitable for seqHMM::build_hmm().
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) if (requireNamespace("TraMineR", quietly = TRUE) && requireNamespace("seqHMM", quietly = TRUE)) { as_seqhmm_sequences(sequences) }sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) if (requireNamespace("TraMineR", quietly = TRUE) && requireNamespace("seqHMM", quietly = TRUE)) { as_seqhmm_sequences(sequences) }
Convert sequence data to a TraMineR state-sequence object
as_traminer_sequences( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", missing = NA, right = "DEL", ... )as_traminer_sequences( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", missing = NA, right = "DEL", ... )
data |
Long-format sequence data. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
missing |
Missing-state code passed to |
right |
Right-missing policy passed to |
... |
Additional arguments passed to |
A TraMineR stslist object with original sequence identifiers as
row names.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) if (requireNamespace("TraMineR", quietly = TRUE)) { as_traminer_sequences(sequences) }sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) if (requireNamespace("TraMineR", quietly = TRUE)) { as_traminer_sequences(sequences) }
Inspects supported gp3sequences objects for structural contract validity, recoverable provenance, identifiers, state levels, method settings, and randomness metadata. The audit does not reinterpret sequence structure as a psychological, diagnostic, cognitive, emotional, or causal construct.
audit_sequence_analysis(x, strict = FALSE, tolerance = 1e-08)audit_sequence_analysis(x, strict = FALSE, tolerance = 1e-08)
x |
A gp3sequences analysis result or supported structural object. |
strict |
Logical; if |
tolerance |
Numerical tolerance used for matrix/probability checks. |
An object of class gp3_sequence_analysis_audit containing
summary, issues, provenance, contract, and status.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3"), each = 3L), sequence_order = rep(1:3, times = 3L), state = c( "A", "B", "C", "A", "B", "B", "C", "B", "A" ) ) distance <- compute_sequence_distance( sequences, method = "levenshtein" ) audit_sequence_analysis(distance)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3"), each = 3L), sequence_order = rep(1:3, times = 3L), state = c( "A", "B", "C", "A", "B", "B", "C", "B", "A" ) ) distance <- compute_sequence_distance( sequences, method = "levenshtein" ) audit_sequence_analysis(distance)
Examines a long-format data frame against the neutral gp3sequences
sequence-data contract without modifying the input.
audit_sequence_data( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL )audit_sequence_data( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL )
data |
A data frame containing ordered state observations. |
sequence_id_col |
Name of the sequence identifier column. |
order_col |
Name of the numeric sequence-order column. |
state_col |
Name of the categorical state column. |
duration_col |
Optional name of a numeric duration column. |
metadata_cols |
Optional character vector naming columns that should remain constant within each sequence. |
expected_states |
Optional vector of known or permitted state values. |
The audit checks column mappings, empty inputs, missing identifiers, missing or non-numeric order values, duplicated positions, integer order gaps, unordered rows, missing states, consecutive repeated states, single-row sequences, invalid durations, inconsistent metadata, unexpected states, and unused factor levels.
The function reports structural properties only. It does not infer psychological, cognitive, emotional, or diagnostic states.
A data frame with one row per detected issue and the stable columns
sequence_id, row, column, issue_code, severity, value,
message, and action. Severity values are error, review, and
info.
sequences <- data.frame( id = rep(c("s1", "s2"), each = 3L), position = rep(1:3, times = 2L), state = c("home", "search", "product", "home", "category", "product") ) audit_sequence_data( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" )sequences <- data.frame( id = rep(c("s1", "s2"), each = 3L), position = rep(1:3, times = 2L), state = c("home", "search", "product", "home", "category", "product") ) audit_sequence_data( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" )
Uses repeated subsampling without replacement and records pairwise co-clustering agreement relative to the full-data solution.
bootstrap_sequence_clusters( distance, k, method = c("hierarchical", "pam", "clara"), n_boot = 100L, sample_fraction = 0.8, seed = 1L, linkage = "average", ... )bootstrap_sequence_clusters( distance, k, method = c("hierarchical", "pam", "clara"), n_boot = 100L, sample_fraction = 0.8, seed = 1L, linkage = "average", ... )
distance |
Sequence distance object. |
k |
Number of clusters. |
method |
Clustering method. |
n_boot |
Number of subsamples. |
sample_fraction |
Fraction of sequences sampled in each iteration. |
seed |
Reproducibility seed. |
linkage |
Hierarchical linkage. |
... |
Additional clustering arguments. |
An object of class gp3_sequence_cluster_bootstrap.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) bootstrap_sequence_clusters(distance, k = 2L, n_boot = 5L, seed = 1L)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) bootstrap_sequence_clusters(distance, k = 2L, n_boot = 5L, seed = 1L)
Resamples declared independent units within groups and returns a percentile interval for the mean difference. This interval does not by itself license causal interpretation.
bootstrap_sequence_group_difference( inference, n_boot = 999L, level = 0.95, seed = 1L )bootstrap_sequence_group_difference( inference, n_boot = 999L, level = 0.95, seed = 1L )
inference |
A result from |
n_boot |
Number of bootstrap samples. |
level |
Confidence level. |
seed |
Reproducibility seed. |
The inference object with a bootstrap component.
# See `test_sequence_group_difference()`.# See `test_sequence_group_difference()`.
Bootstrap transition-network edge weights
bootstrap_transition_network( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", n_boot = 100L, level = 0.95, seed = 1L, include_self = TRUE )bootstrap_transition_network( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", n_boot = 100L, level = 0.95, seed = 1L, include_self = TRUE )
data |
Long-format sequence data. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
n_boot |
Number of bootstrap samples of whole sequences. |
level |
Confidence level for percentile intervals. |
seed |
Reproducibility seed. |
include_self |
Include self-transitions. |
A data frame containing observed edge weights, bootstrap means, standard deviations, and percentile intervals.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) bootstrap_transition_network(sequences, n_boot = 5L, seed = 1L)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) bootstrap_transition_network(sequences, n_boot = 5L, seed = 1L)
Cluster sequences from a distance object
cluster_sequences( distance, k, method = c("hierarchical", "pam", "clara"), linkage = "average", seed = 1L, ... )cluster_sequences( distance, k, method = c("hierarchical", "pam", "clara"), linkage = "average", seed = 1L, ... )
distance |
A |
k |
Number of clusters. |
method |
|
linkage |
Hierarchical linkage method. |
seed |
Reproducibility seed for optional stochastic methods. |
... |
Additional arguments passed to the selected clustering function. |
A list of class gp3_sequence_clustering containing assignments,
model object, medoid identifiers where available, and settings.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) cluster_sequences(distance, k = 2L)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) cluster_sequences(distance, k = 2L)
Compares structural contracts and recoverable provenance from two analysis objects. This helper is intended for regression checks, sensitivity analyses, and reproducibility audits; it does not select a statistically or substantively "best" analysis.
compare_sequence_analysis_results( x, y, tolerance = 1e-08, compare_values = FALSE )compare_sequence_analysis_results( x, y, tolerance = 1e-08, compare_values = FALSE )
x, y
|
Two analysis objects. |
tolerance |
Numerical comparison tolerance. |
compare_values |
Logical; when |
An object of class gp3_sequence_analysis_comparison containing
per-field comparisons, both audits, optional whole-object comparison, and
an overall equality flag.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3"), each = 3L), sequence_order = rep(1:3, times = 3L), state = c( "A", "B", "C", "A", "B", "B", "C", "B", "A" ) ) d1 <- compute_sequence_distance( sequences, method = "levenshtein" ) d2 <- compute_sequence_distance( sequences, method = "levenshtein" ) compare_sequence_analysis_results(d1, d2)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3"), each = 3L), sequence_order = rep(1:3, times = 3L), state = c( "A", "B", "C", "A", "B", "B", "C", "B", "A" ) ) d1 <- compute_sequence_distance( sequences, method = "levenshtein" ) d2 <- compute_sequence_distance( sequences, method = "levenshtein" ) compare_sequence_analysis_results(d1, d2)
Produces descriptive state, transition and sequence-length comparisons. No hypothesis tests or causal interpretations are produced.
compare_sequence_groups( data, group_col, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", reference = NULL, metrics = c("state", "transition", "length"), include_self = TRUE, transition_separator = " -> ", zero_policy = c("missing", "infinite") )compare_sequence_groups( data, group_col, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", reference = NULL, metrics = c("state", "transition", "length"), include_self = TRUE, transition_separator = " -> ", zero_policy = c("missing", "infinite") )
data |
Long-format sequence data. |
group_col |
Grouping column that is constant within sequence. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
reference |
Optional reference group. When supplied, each other
group is reported as |
metrics |
Any of |
include_self |
Include self-transitions. |
transition_separator |
Separator used in reported transition labels. It must not occur inside an observed state label. |
zero_policy |
Ratio policy when the reference value is zero. |
A list of class gp3_sequence_group_comparison containing group
summaries and pairwise descriptive contrasts.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) compare_sequence_groups(sequences, group_col = "group")sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) compare_sequence_groups(sequences, group_col = "group")
Compare fitted sequence HMMs descriptively
compare_sequence_hmms(...)compare_sequence_hmms(...)
... |
Fitted HMM or mixture-HMM objects. |
A data frame containing log likelihood, AIC, BIC, parameter count, convergence status, and delta criteria. Criteria are descriptive and do not automatically select a substantive model.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) model_1 <- fit_sequence_hmm(sequences, 1L, max_iter = 5L) model_2 <- fit_sequence_hmm(sequences, 2L, max_iter = 5L) compare_sequence_hmms(one = model_1, two = model_2)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) model_1 <- fit_sequence_hmm(sequences, 1L, max_iter = 5L) model_2 <- fit_sequence_hmm(sequences, 2L, max_iter = 5L) compare_sequence_hmms(one = model_1, two = model_2)
Computes distance and simple structural changes between consecutive panel occasions. The output is descriptive and does not establish a causal or psychological change.
compare_sequence_panel_changes( panel, method = c("levenshtein", "lcs", "optimal_matching", "transition"), normalise = c("none", "max_length", "path_length"), indel_cost = 1, substitution_cost = 1, substitution_matrix = NULL, transition_smoothing = 0 )compare_sequence_panel_changes( panel, method = c("levenshtein", "lcs", "optimal_matching", "transition"), normalise = c("none", "max_length", "path_length"), indel_cost = 1, substitution_cost = 1, substitution_matrix = NULL, transition_smoothing = 0 )
panel |
A sequence panel. |
method |
Distance method supported by |
normalise |
Distance normalisation. |
indel_cost, substitution_cost
|
Costs for optimal matching. |
substitution_matrix |
Optional named substitution matrix. |
transition_smoothing |
Smoothing for transition-profile distance. |
A data frame of class gp3_sequence_panel_changes.
panel_data <- data.frame( participant_id = rep(c("p1", "p2"), each = 8L), occasion = rep(rep(c(1, 2), each = 4L), times = 2L), sequence_id = rep(c("a", "b", "c", "d"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "C", "C", "D", "D", "C", "B", "A", "D", "B", "B", "A") ) compare_sequence_panel_changes( prepare_sequence_panel(panel_data, "participant_id", "occasion"), method = "lcs" )panel_data <- data.frame( participant_id = rep(c("p1", "p2"), each = 8L), occasion = rep(rep(c(1, 2), each = 4L), times = 2L), sequence_id = rep(c("a", "b", "c", "d"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "C", "C", "D", "D", "C", "B", "A", "D", "B", "B", "A") ) compare_sequence_panel_changes( prepare_sequence_panel(panel_data, "participant_id", "occasion"), method = "lcs" )
Performs transparent contingency-table tests on sequence-level presence. Multiple testing is adjusted explicitly. Results are associational unless a randomized design independently justifies causal interpretation.
compare_sequence_subsequences( occurrences, group_col, test = c("auto", "chisq", "fisher"), p_adjust = "BH", min_sequence_count = 1L )compare_sequence_subsequences( occurrences, group_col, test = c("auto", "chisq", "fisher"), p_adjust = "BH", min_sequence_count = 1L )
occurrences |
A non-contiguous subsequence occurrence table. |
group_col |
Sequence-constant group column retained through
|
test |
|
p_adjust |
Multiple-testing adjustment method. |
min_sequence_count |
Minimum total sequence count per subsequence. |
A data frame of prevalence contrasts and adjusted p-values.
data <- data.frame( sequence_id = rep(paste0("s", 1:4), each = 4L), sequence_order = rep(1:4, 4L), state = c("A", "B", "C", "D", "A", "B", "D", "D", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L) ) occurrences <- extract_sequence_subsequences(data, metadata_cols = "group") compare_sequence_subsequences(occurrences, "group")data <- data.frame( sequence_id = rep(paste0("s", 1:4), each = 4L), sequence_order = rep(1:4, 4L), state = c("A", "B", "C", "D", "A", "B", "D", "D", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L) ) occurrences <- extract_sequence_subsequences(data, metadata_cols = "group") compare_sequence_subsequences(occurrences, "group")
Provides transparent base-R implementations of Levenshtein edit distance, longest-common-subsequence distance, optimal matching, and a first-order transition-profile distance.
compute_sequence_distance( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", method = c("levenshtein", "lcs", "optimal_matching", "transition"), indel_cost = 1, substitution_cost = 1, substitution_matrix = NULL, transition_smoothing = 0, normalise = c("none", "max_length", "path_length") )compute_sequence_distance( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", method = c("levenshtein", "lcs", "optimal_matching", "transition"), indel_cost = 1, substitution_cost = 1, substitution_matrix = NULL, transition_smoothing = 0, normalise = c("none", "max_length", "path_length") )
data |
Long-format sequence data or a prepared gp3sequences result. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
method |
Distance method. |
indel_cost |
Non-negative insertion/deletion cost used by
|
substitution_cost |
Non-negative default substitution cost used by
|
substitution_matrix |
Optional named square substitution-cost matrix for
|
transition_smoothing |
Non-negative smoothing for transition profiles. |
normalise |
One of |
A dist object with method and preprocessing metadata attached.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) compute_sequence_distance(sequences, method = "lcs")sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) compute_sequence_distance(sequences, method = "lcs")
Computes a descriptive consensus state at each observed sequence position. The function does not treat the consensus as a behavioural norm and does not infer psychological or causal meaning.
create_consensus_sequence( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", group_cols = NULL, weight_col = NULL, missing_state_policy = c("exclude", "state", "error"), missing_state_label = "<MISSING>", tie_method = c("first", "last", "missing", "all"), state_levels = NULL, min_support = 1L )create_consensus_sequence( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", group_cols = NULL, weight_col = NULL, missing_state_policy = c("exclude", "state", "error"), missing_state_label = "<MISSING>", tie_method = c("first", "last", "missing", "all"), state_levels = NULL, min_support = 1L )
data |
A long-format data frame, a prepared gp3sequences result, or a data frame containing canonical columns. |
sequence_id_col, order_col, state_col
|
Column names defining sequences. |
group_cols |
Optional columns defining independent consensus groups. |
weight_col |
Optional non-negative row-weight column. |
missing_state_policy |
One of |
missing_state_label |
Label used when missing states are retained. |
tie_method |
Deterministic tie policy: |
state_levels |
Optional preferred state ordering used for ties. |
min_support |
Minimum number of contributing sequences at a position. |
A data frame of class gp3_consensus_sequence containing group
columns, sequence position, consensus state, support counts and weights,
agreement proportion, tie count, tied states, and total group sequences.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) create_consensus_sequence(sequences, group_cols = "group")sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) create_consensus_sequence(sequences, group_cols = "group")
Combines multiple named cluster assignments through a co-association matrix
and applies hierarchical clustering to 1 - co-association.
create_sequence_cluster_ensemble(..., k, linkage = "average")create_sequence_cluster_ensemble(..., k, linkage = "average")
... |
Two or more |
k |
Number of consensus clusters. |
linkage |
Hierarchical linkage applied to the co-association distance. |
An object of class gp3_sequence_cluster_ensemble containing the
consensus assignments, co-association matrix, model, and source solutions.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) d1 <- compute_sequence_distance(sequences, method = "lcs") d2 <- compute_sequence_distance(sequences, method = "transition") create_sequence_cluster_ensemble(cluster_sequences(d1, 2L), cluster_sequences(d2, 2L), k = 2L)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) d1 <- compute_sequence_distance(sequences, method = "lcs") d2 <- compute_sequence_distance(sequences, method = "transition") create_sequence_cluster_ensemble(cluster_sequences(d1, 2L), cluster_sequences(d2, 2L), k = 2L)
Create a transition network from ordered sequences
create_transition_network( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", group_cols = NULL, order = 1L, include_self = TRUE, normalise = c("count", "from", "global"), smoothing = 0, context_separator = " > " )create_transition_network( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", group_cols = NULL, order = 1L, include_self = TRUE, normalise = c("count", "from", "global"), smoothing = 0, context_separator = " > " )
data |
Long-format sequence data or a prepared result. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
group_cols |
Optional grouping columns constant within sequence. |
order |
Markov order. |
include_self |
Include first-order self-transitions. |
normalise |
Edge weight scale: counts, conditional probabilities from each context, or global shares. |
smoothing |
Non-negative additive smoothing applied to observed edges. |
context_separator |
Separator used for higher-order contexts. |
A data frame of class gp3_transition_network containing context,
next state, counts, weights, sequence prevalence, and group columns.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) create_transition_network(sequences, normalise = "from")sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) create_transition_network(sequences, normalise = "from")
Records the unit of assignment and the design under which a sequence-group contrast will be evaluated. Causal interpretation is permitted only for an explicitly randomized design and still depends on the validity of the study implementation.
declare_sequence_comparison_design( group_col, unit_col, design = c("observational", "randomized", "paired_randomized"), pair_col = NULL, cluster_col = NULL )declare_sequence_comparison_design( group_col, unit_col, design = c("observational", "randomized", "paired_randomized"), pair_col = NULL, cluster_col = NULL )
group_col |
Group or treatment column. |
unit_col |
Independent assignment or analysis-unit column. |
design |
|
pair_col |
Pair or block column required for paired randomization. |
cluster_col |
Optional higher-level assignment cluster. |
An object of class gp3_sequence_comparison_design.
declare_sequence_comparison_design("group", "participant_id", design = "randomized")declare_sequence_comparison_design("group", "participant_id", design = "randomized")
Decode states from a covariate-dependent HMM
decode_covariate_sequence_states( model, data = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", method = c("viterbi", "posterior") )decode_covariate_sequence_states( model, data = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", method = c("viterbi", "posterior") )
model |
A fitted covariate HMM. |
data |
Optional new data. Training data are used when omitted. |
sequence_id_col, order_col, state_col
|
Core columns for new data. |
method |
|
A long decoded-state data frame.
# See `fit_covariate_sequence_hmm()`.# See `fit_covariate_sequence_hmm()`.
Decode latent states from a multichannel HMM
decode_multichannel_sequence_states( model, data = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", channel_cols = NULL, method = c("viterbi", "posterior") )decode_multichannel_sequence_states( model, data = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", channel_cols = NULL, method = c("viterbi", "posterior") )
model |
A fitted multichannel HMM. |
data |
Optional new long-format multichannel data. |
sequence_id_col, order_col
|
Core sequence columns for new data. |
channel_cols |
Channel columns for new data; defaults to training names. |
method |
|
A long data frame of decoded states and posterior probabilities.
# See `fit_multichannel_sequence_hmm()`.# See `fit_multichannel_sequence_hmm()`.
Decode hidden states from a fitted HMM
decode_sequence_states( model, data = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", method = c("viterbi", "posterior"), component = NULL )decode_sequence_states( model, data = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", method = c("viterbi", "posterior"), component = NULL )
model |
A fitted single HMM or HMM mixture. |
data |
Optional new long-format data. Training sequences are used when omitted. |
sequence_id_col, order_col, state_col
|
Sequence columns for new data. |
method |
|
component |
Mixture component to decode. When omitted for a mixture, each sequence uses its highest-responsibility component. |
A long data frame containing decoded latent states and posterior probabilities where available.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) model <- fit_sequence_hmm(sequences, 2L, max_iter = 5L) decode_sequence_states(model)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) model <- fit_sequence_hmm(sequences, 2L, max_iter = 5L) decode_sequence_states(model)
Detect descriptive transition communities
detect_transition_communities( network, method = c("label_propagation", "components"), max_iter = 100L, seed = 1L )detect_transition_communities( network, method = c("label_propagation", "components"), max_iter = 100L, seed = 1L )
network |
A first-order transition network. |
method |
|
max_iter |
Maximum label-propagation iterations. |
seed |
Reproducibility seed used only to rotate update order. |
A data frame with states and deterministic community labels.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) network <- create_transition_network(sequences) detect_transition_communities(network)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) network <- create_transition_network(sequences) detect_transition_communities(network)
Creates a deterministic dictionary and adds integer and labelled state codes to long-format sequence data.
encode_sequence_data( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL, state_levels = NULL, prefix = "S", width = NULL )encode_sequence_data( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL, state_levels = NULL, prefix = "S", width = NULL )
data |
A data frame containing ordered state observations. |
sequence_id_col |
Name of the sequence identifier column. |
order_col |
Name of the numeric sequence-order column. |
state_col |
Name of the categorical state column. |
duration_col |
Optional name of a numeric duration column. |
metadata_cols |
Optional character vector naming columns that should remain constant within each sequence. |
expected_states |
Optional vector of known or permitted state values. |
state_levels |
Optional atomic vector defining the complete state ordering. When omitted, factor levels are respected; otherwise observed state labels are sorted alphabetically. |
prefix |
Character prefix used for labelled codes. |
width |
Optional positive integer width for the numeric part of each labelled code. The default is determined from the dictionary size. |
The function does not reinterpret states. State codes are transparent identifiers derived from an explicit or deterministic state ordering.
A named list containing:
data: deterministically sorted canonical data with state_index and
state_code;
dictionary: state labels, integer indices, labelled codes, and an
observed-state indicator;
audit, status, and mapping from input validation;
settings: the resolved code prefix and width.
sequences <- data.frame( id = c("s1", "s1", "s2", "s2"), position = c(1, 2, 1, 2), state = c("home", "search", "home", "product") ) encoded <- encode_sequence_data( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) encoded$dictionary encoded$datasequences <- data.frame( id = c("s1", "s1", "s2", "s2"), position = c(1, 2, 1, 2), state = c("home", "search", "home", "product") ) encoded <- encode_sequence_data( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) encoded$dictionary encoded$data
Extract representative sequences from clusters
extract_representative_sequences( clustering, distance = NULL, n_per_cluster = 1L )extract_representative_sequences( clustering, distance = NULL, n_per_cluster = 1L )
clustering |
A clustering result. |
distance |
Optional distance when absent from |
n_per_cluster |
Number of representatives per cluster. |
A data frame with cluster, rank, representative ID, and mean within-cluster distance.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) fit <- cluster_sequences(distance, k = 2L) extract_representative_sequences(fit)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) fit <- cluster_sequences(distance, k = 2L) extract_representative_sequences(fit)
Enumerates contiguous state motifs from validated long-format sequence data.
extract_sequence_ngrams( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL, min_length = 2L, max_length = 3L, overlap = c("allow", "disallow"), separator = " > ", state_levels = NULL )extract_sequence_ngrams( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL, min_length = 2L, max_length = 3L, overlap = c("allow", "disallow"), separator = " > ", state_levels = NULL )
data |
A data frame containing ordered state observations. |
sequence_id_col |
Name of the sequence identifier column. |
order_col |
Name of the numeric sequence-order column. |
state_col |
Name of the categorical state column. |
duration_col |
Optional name of a numeric duration column. |
metadata_cols |
Optional character vector naming columns that should remain constant within each sequence. |
expected_states |
Optional vector of known or permitted state values. |
min_length |
Positive whole number giving the shortest motif length. |
max_length |
Positive whole number giving the longest motif length. |
overlap |
Character value specifying whether overlapping occurrences of
the same motif within the same sequence are |
separator |
Character value used only to display state labels in the
human-readable |
state_levels |
Optional atomic vector defining the complete state ordering. When omitted, factor levels are respected; otherwise observed labels are sorted deterministically. |
Motifs are contiguous windows only. No subsequence gaps, edit distances,
statistical tests, or substantive interpretations are introduced.
Consecutive repeated states are used exactly as supplied; any repeat
collapsing should be performed explicitly with prepare_sequence_data()
before extraction.
With overlap = "disallow", overlap is resolved independently within each
sequence-motif pair by a deterministic left-to-right greedy rule. Different
motifs and different motif lengths do not compete for positions.
The collision-resistant motif_id and motif_key columns are derived from
deterministic state codes. The display separator may therefore also occur
inside a state label without changing motif identity.
A named list containing:
occurrences: one row per retained contiguous motif occurrence;
motifs: the distinct motif dictionary;
sequences: sequence-level counts of candidate and retained occurrences;
state_dictionary: the deterministic state dictionary;
audit, status, and mapping from input validation;
settings: the resolved motif extraction settings.
sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "A", "A", "B", "A", "C") ) ngrams <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state", min_length = 2, max_length = 3, overlap = "allow" ) ngrams$occurrences ngrams$motifssequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "A", "A", "B", "A", "C") ) ngrams <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state", min_length = 2, max_length = 3, overlap = "allow" ) ngrams$occurrences ngrams$motifs
Enumerates ordered non-contiguous subsequences under explicit length, gap, span, and safety limits. The implementation is transparent and intended for modest sequence collections; specialist mining packages remain preferable for very large search spaces.
extract_sequence_subsequences( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", metadata_cols = NULL, min_length = 2L, max_length = 5L, max_gap = Inf, max_span = Inf, repeated_state_policy = c("preserve", "collapse"), separator = " > ", max_combinations_per_sequence = 100000L )extract_sequence_subsequences( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", metadata_cols = NULL, min_length = 2L, max_length = 5L, max_gap = Inf, max_span = Inf, repeated_state_policy = c("preserve", "collapse"), separator = " > ", max_combinations_per_sequence = 100000L )
data |
Long-format sequence data or a prepared result. |
sequence_id_col, order_col, state_col
|
Core sequence columns. |
metadata_cols |
Optional sequence-constant metadata retained as attributes. |
min_length, max_length
|
Minimum and maximum subsequence lengths. |
max_gap |
Maximum number of skipped positions between adjacent selected
states. Use |
max_span |
Maximum difference between the first and last selected
sequence positions. Use |
repeated_state_policy |
Preserve or collapse consecutive repeated states before mining. |
separator |
Separator used in stable motif labels. |
max_combinations_per_sequence |
Safety limit for the number of index combinations considered for any one sequence. |
A data frame of class gp3_sequence_subsequences, with one row per
qualifying occurrence.
sequences <- data.frame( sequence_id = rep(c("s1", "s2"), each = 5L), sequence_order = rep(1:5, times = 2L), state = c("A", "B", "C", "D", "E", "A", "C", "B", "D", "E") ) extract_sequence_subsequences(sequences, min_length = 2L, max_length = 3L, max_gap = 2L)sequences <- data.frame( sequence_id = rep(c("s1", "s2"), each = 5L), sequence_order = rep(1:5, times = 2L), state = c("A", "B", "C", "D", "E", "A", "C", "B", "D", "E") ) extract_sequence_subsequences(sequences, min_length = 2L, max_length = 3L, max_gap = 2L)
Applies transparent count, prevalence, length, and top-ranking filters to sequence motif summaries.
filter_sequence_motifs( x, min_occurrences = 1L, min_sequences = 1L, min_prevalence = 0, motif_lengths = NULL, top_n = NULL, rank_by = c("sequence_prevalence", "n_occurrences", "n_sequences"), ties = c("include", "first") )filter_sequence_motifs( x, min_occurrences = 1L, min_sequences = 1L, min_prevalence = 0, motif_lengths = NULL, top_n = NULL, rank_by = c("sequence_prevalence", "n_occurrences", "n_sequences"), ties = c("include", "first") )
x |
A motif extraction, motif summary, or filtered-motif object. |
min_occurrences |
Non-negative whole number giving the minimum total occurrence count. |
min_sequences |
Non-negative whole number giving the minimum number of sequences containing the motif. |
min_prevalence |
Minimum sequence prevalence between 0 and 1. |
motif_lengths |
Optional vector of positive whole-number motif lengths to retain. |
top_n |
Optional positive whole number giving the requested number of highest-ranked motifs. |
rank_by |
Metric used for deterministic top-ranking: one of
|
ties |
Character value controlling the top- |
Filtering is descriptive and deterministic. When ties = "first", ties are
resolved by sequence prevalence, occurrence count, sequence count, shorter
motif length, and finally the collision-resistant motif key.
A named list containing the filtered motifs table, the matching
sequence-level rows in by_sequence, sequence and state dictionaries,
validation metadata, filter settings, and counts before and after filtering.
sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "A", "A", "B", "A", "C") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) filtered <- filter_sequence_motifs( extracted, min_sequences = 2, top_n = 5, ties = "include" ) filtered$motifssequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "A", "A", "B", "A", "C") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) filtered <- filter_sequence_motifs( extracted, min_sequences = 2, top_n = 5, ties = "include" ) filtered$motifs
Filter non-contiguous subsequence summaries
filter_sequence_subsequences( summary, min_sequences = 1L, min_prevalence = 0, max_mean_gap = Inf, top_n = NULL, ties = c("include", "exclude") )filter_sequence_subsequences( summary, min_sequences = 1L, min_prevalence = 0, max_mean_gap = Inf, top_n = NULL, ties = c("include", "exclude") )
summary |
A data frame from |
min_sequences |
Minimum sequence count. |
min_prevalence |
Minimum sequence prevalence. |
max_mean_gap |
Optional maximum mean gap. |
top_n |
Optional number of rows retained after deterministic sorting. |
ties |
Include or exclude ties at the |
A filtered summary data frame.
data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "B", "D")) x <- summarise_sequence_subsequences(extract_sequence_subsequences(data)) filter_sequence_subsequences(x, min_sequences = 2L)data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "B", "D")) x <- summarise_sequence_subsequences(extract_sequence_subsequences(data)) filter_sequence_subsequences(x, min_sequences = 2L)
Fits a categorical HMM whose initial-state and transition probabilities may depend on explicitly declared numeric covariates. Multinomial-logit coefficients are estimated inside the EM algorithm with a small ridge penalty. Emission probabilities remain time-homogeneous.
fit_covariate_sequence_hmm( data, n_states, initial_covariate_cols = NULL, transition_covariate_cols = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", symbol_levels = NULL, state_names = NULL, emission_probs = NULL, max_iter = 100L, inner_maxit = 100L, tolerance = 1e-06, pseudocount = 1e-06, ridge = 1e-06, seed = 1L, keep_posteriors = FALSE )fit_covariate_sequence_hmm( data, n_states, initial_covariate_cols = NULL, transition_covariate_cols = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", symbol_levels = NULL, state_names = NULL, emission_probs = NULL, max_iter = 100L, inner_maxit = 100L, tolerance = 1e-06, pseudocount = 1e-06, ridge = 1e-06, seed = 1L, keep_posteriors = FALSE )
data |
Long-format sequence data. |
n_states |
Number of latent states. |
initial_covariate_cols |
Numeric sequence-constant covariates for initial-state probabilities. |
transition_covariate_cols |
Numeric row-level covariates for transition probabilities. |
sequence_id_col, order_col, state_col
|
Core sequence columns. |
symbol_levels |
Optional observed-symbol order. |
state_names |
Optional latent-state names. |
emission_probs |
Optional starting emission matrix. |
max_iter |
Maximum EM iterations. |
inner_maxit |
Maximum BFGS iterations in each multinomial M-step. |
tolerance |
Relative log-likelihood tolerance. |
pseudocount |
Emission smoothing count. |
ridge |
Non-negative coefficient penalty. |
seed |
Reproducibility seed. |
keep_posteriors |
Retain final posteriors. |
An object of class gp3_covariate_sequence_hmm.
sequences <- data.frame( sequence_id = rep(paste0("s", 1:8), each = 5L), sequence_order = rep(1:5, times = 8L), state = rep(c("A", "B", "C", "B", "A"), times = 8L), condition = rep(rep(c(0, 1), each = 4L), each = 5L), time_scaled = rep(seq(-1, 1, length.out = 5L), times = 8L) ) fit_covariate_sequence_hmm( sequences, 2L, initial_covariate_cols = "condition", transition_covariate_cols = c("condition", "time_scaled"), max_iter = 3L, inner_maxit = 10L, seed = 1L )sequences <- data.frame( sequence_id = rep(paste0("s", 1:8), each = 5L), sequence_order = rep(1:5, times = 8L), state = rep(c("A", "B", "C", "B", "A"), times = 8L), condition = rep(rep(c(0, 1), each = 4L), each = 5L), time_scaled = rep(seq(-1, 1, length.out = 5L), times = 8L) ) fit_covariate_sequence_hmm( sequences, 2L, initial_covariate_cols = "condition", transition_covariate_cols = c("condition", "time_scaled"), max_iter = 3L, inner_maxit = 10L, seed = 1L )
Fit a higher-order transition model
fit_higher_order_transition_model( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", order = 2L, smoothing = 0.5, backoff = TRUE, context_separator = " > " )fit_higher_order_transition_model( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", order = 2L, smoothing = 0.5, backoff = TRUE, context_separator = " > " )
data |
Long-format sequence data. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
order |
Context order. |
smoothing |
Additive smoothing over observed next states. |
backoff |
Retain lower-order context tables for prediction backoff. |
context_separator |
Context separator. |
An object of class gp3_higher_order_transition_model.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) fit_higher_order_transition_model(sequences, order = 2L)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) fit_higher_order_transition_model(sequences, order = 2L)
Fits a finite-state, time-homogeneous HMM to two or more categorical channels under conditional independence of channels given the latent state. Latent states are statistical model states only.
fit_multichannel_sequence_hmm( data, n_states, channel_cols, sequence_id_col = "sequence_id", order_col = "sequence_order", symbol_levels = NULL, state_names = NULL, initial_probs = NULL, transition_probs = NULL, emission_probs = NULL, max_iter = 200L, tolerance = 1e-06, pseudocount = 1e-06, seed = 1L, keep_posteriors = FALSE )fit_multichannel_sequence_hmm( data, n_states, channel_cols, sequence_id_col = "sequence_id", order_col = "sequence_order", symbol_levels = NULL, state_names = NULL, initial_probs = NULL, transition_probs = NULL, emission_probs = NULL, max_iter = 200L, tolerance = 1e-06, pseudocount = 1e-06, seed = 1L, keep_posteriors = FALSE )
data |
Long-format multichannel sequence data. |
n_states |
Number of latent states. |
channel_cols |
Names of categorical observation channels. |
sequence_id_col, order_col
|
Core sequence columns. |
symbol_levels |
Optional named list of symbol orders by channel. |
state_names |
Optional latent-state names. |
initial_probs, transition_probs, emission_probs
|
Optional starting values. |
max_iter |
Maximum EM iterations. |
tolerance |
Relative log-likelihood tolerance. |
pseudocount |
Non-negative smoothing count. |
seed |
Reproducibility seed. |
keep_posteriors |
Retain final forward-backward results. |
An object of class gp3_multichannel_sequence_hmm.
multichannel <- data.frame( sequence_id = rep(paste0("s", 1:4), each = 5L), sequence_order = rep(1:5, times = 4L), action = c("A", "B", "C", "C", "D", "A", "B", "B", "C", "D", "D", "C", "B", "A", "A", "D", "C", "C", "B", "A"), context = rep(c("x", "x", "y", "y", "z"), times = 4L) ) fit_multichannel_sequence_hmm(multichannel, 2L, channel_cols = c("action", "context"), max_iter = 5L, seed = 1L)multichannel <- data.frame( sequence_id = rep(paste0("s", 1:4), each = 5L), sequence_order = rep(1:5, times = 4L), action = c("A", "B", "C", "C", "D", "A", "B", "B", "C", "D", "D", "C", "B", "A", "A", "D", "C", "C", "B", "A"), context = rep(c("x", "x", "y", "y", "z"), times = 4L) ) fit_multichannel_sequence_hmm(multichannel, 2L, channel_cols = c("action", "context"), max_iter = 5L, seed = 1L)
Fits a finite-state, time-homogeneous categorical HMM by Baum-Welch EM. Latent states are statistical model states only; they are not psychological, diagnostic, or causal constructs.
fit_sequence_hmm( data, n_states, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", symbol_levels = NULL, state_names = NULL, initial_probs = NULL, transition_probs = NULL, emission_probs = NULL, max_iter = 200L, tolerance = 1e-06, pseudocount = 1e-06, seed = 1L, keep_posteriors = FALSE )fit_sequence_hmm( data, n_states, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", symbol_levels = NULL, state_names = NULL, initial_probs = NULL, transition_probs = NULL, emission_probs = NULL, max_iter = 200L, tolerance = 1e-06, pseudocount = 1e-06, seed = 1L, keep_posteriors = FALSE )
data |
Long-format sequence data. |
n_states |
Number of latent states. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
symbol_levels |
Optional observed-symbol ordering. |
state_names |
Optional latent-state names. |
initial_probs, transition_probs, emission_probs
|
Optional starting values. |
max_iter |
Maximum EM iterations. |
tolerance |
Relative log-likelihood tolerance. |
pseudocount |
Non-negative smoothing count. |
seed |
Reproducibility seed. |
keep_posteriors |
Retain final forward-backward results. |
An object of class gp3_sequence_hmm containing fitted parameters,
log likelihood, convergence diagnostics, symbol coding, and optional
posteriors.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) fit_sequence_hmm(sequences, n_states = 2L, max_iter = 5L, seed = 1L)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) fit_sequence_hmm(sequences, n_states = 2L, max_iter = 5L, seed = 1L)
Fits sequence-level mixture components, each containing a categorical HMM. Component membership is a statistical clustering device and should not be interpreted as a substantive latent type without external validation.
fit_sequence_hmm_mixture( data, n_components, n_states, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", symbol_levels = NULL, max_iter = 200L, inner_initial_iter = 20L, tolerance = 1e-06, pseudocount = 1e-06, seed = 1L )fit_sequence_hmm_mixture( data, n_components, n_states, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", symbol_levels = NULL, max_iter = 200L, inner_initial_iter = 20L, tolerance = 1e-06, pseudocount = 1e-06, seed = 1L )
data |
Long-format sequence data. |
n_components |
Number of mixture components. |
n_states |
Number of hidden states per component; scalar or vector. |
sequence_id_col, order_col, state_col
|
Sequence columns. |
symbol_levels |
Optional symbol ordering. |
max_iter |
Maximum mixture-EM iterations. |
inner_initial_iter |
Initial single-HMM iterations per component. |
tolerance |
Relative log-likelihood tolerance. |
pseudocount |
Smoothing count. |
seed |
Reproducibility seed. |
An object of class gp3_sequence_hmm_mixture.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) fit_sequence_hmm_mixture(sequences, n_components = 2L, n_states = 2L, max_iter = 5L, inner_initial_iter = 2L, seed = 1L)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) fit_sequence_hmm_mixture(sequences, n_components = 2L, n_states = 2L, max_iter = 5L, inner_initial_iter = 2L, seed = 1L)
Fits a binary generalized additive model for either occupancy of a target
state or occurrence of a target transition over aligned sequence time. Group
specific smooths model time-varying condition differences, and a participant
random-effect smooth accounts for repeated observations. The optional mgcv
package is required.
fit_time_varying_sequence_model( data, group_col, participant_id_col, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", time_col = NULL, outcome = c("state", "transition"), target_state = NULL, from_state = NULL, to_state = NULL, k = 5L, method = "REML", include_random_effect = TRUE )fit_time_varying_sequence_model( data, group_col, participant_id_col, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", time_col = NULL, outcome = c("state", "transition"), target_state = NULL, from_state = NULL, to_state = NULL, k = 5L, method = "REML", include_random_effect = TRUE )
data |
Long-format sequence data or a prepared result. |
group_col |
Group or condition column. |
participant_id_col |
Participant or repeated-unit column. |
sequence_id_col, order_col, state_col
|
Core sequence columns. |
time_col |
Optional numeric time column; defaults to |
outcome |
|
target_state |
Target state for occupancy models. |
from_state, to_state
|
Target transition for transition models. |
k |
Basis dimension for time smooths. |
method |
Smoothing-parameter estimation method passed to
|
include_random_effect |
Include a participant random-effect smooth. |
An object of class gp3_sequence_time_model.
set.seed(2026) n_participants <- 24L n_positions <- 12L participant_index <- rep( seq_len(n_participants), each = n_positions ) sequence_order <- rep( seq_len(n_positions), times = n_participants ) group_by_participant <- rep( c("control", "treatment"), each = n_participants %/% 2L ) group <- rep( group_by_participant, each = n_positions ) probability <- stats::plogis( -0.8 + 0.08 * sequence_order + 0.4 * (group == "treatment") ) observed_b <- stats::rbinom( length(probability), size = 1L, prob = probability ) sequences <- data.frame( participant_id = paste0("p", participant_index), sequence_id = paste0("s", participant_index), sequence_order = sequence_order, state = ifelse(observed_b == 1L, "B", "A"), group = group ) if (requireNamespace("mgcv", quietly = TRUE)) { fit <- fit_time_varying_sequence_model( sequences, group_col = "group", participant_id_col = "participant_id", target_state = "B", k = 4L ) }set.seed(2026) n_participants <- 24L n_positions <- 12L participant_index <- rep( seq_len(n_participants), each = n_positions ) sequence_order <- rep( seq_len(n_positions), times = n_participants ) group_by_participant <- rep( c("control", "treatment"), each = n_participants %/% 2L ) group <- rep( group_by_participant, each = n_positions ) probability <- stats::plogis( -0.8 + 0.08 * sequence_order + 0.4 * (group == "treatment") ) observed_b <- stats::rbinom( length(probability), size = 1L, prob = probability ) sequences <- data.frame( participant_id = paste0("p", participant_index), sequence_id = paste0("s", participant_index), sequence_order = sequence_order, state = ifelse(observed_b == 1L, "B", "A"), group = group ) if (requireNamespace("mgcv", quietly = TRUE)) { fit <- fit_time_varying_sequence_model( sequences, group_col = "group", participant_id_col = "participant_id", target_state = "B", k = 4L ) }
Format consensus sequences as paths
format_consensus_sequence( consensus, separator = " -> ", include_order = FALSE, include_agreement = FALSE, digits = 3L )format_consensus_sequence( consensus, separator = " -> ", include_order = FALSE, include_agreement = FALSE, digits = 3L )
consensus |
A consensus result. |
separator |
State separator. |
include_order |
Include position labels. |
include_agreement |
Include rounded agreement values. |
digits |
Number of decimal places for agreement values. |
One row per consensus group with a formatted path and position count.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) consensus <- create_consensus_sequence(sequences) format_consensus_sequence(consensus)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) consensus <- create_consensus_sequence(sequences) format_consensus_sequence(consensus)
Produces a deterministic report-ready table from positional motif summaries.
format_sequence_motif_positions( x, digits = 3L, position_units = c("proportion", "percent"), include_rank = TRUE )format_sequence_motif_positions( x, digits = 3L, position_units = c("proportion", "percent"), include_rank = TRUE )
x |
An object returned by |
digits |
Whole number from 0 to 15 controlling numeric rounding. |
position_units |
Display units for relative positions: |
include_rank |
Logical value indicating whether to include an
earlier-to-later rank based on mean position. Ranking is performed within
each supplied |
Formatting copies and transforms the summary table only. The input object is not modified. Rows are ordered deterministically by grouping values, mean and median position, occurrence and sequence counts, motif length, and motif key. Equal mean positions receive the same minimum rank.
A named list containing the formatted table, validation metadata,
and formatting settings.
sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "C", "A", "B", "C", "B") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) positions <- summarise_sequence_motif_positions( extracted, position = "centre", scale = "relative" ) formatted <- format_sequence_motif_positions( positions, position_units = "percent", digits = 1 ) formatted$tablesequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "C", "A", "B", "C", "B") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) positions <- summarise_sequence_motif_positions( extracted, position = "centre", scale = "relative" ) formatted <- format_sequence_motif_positions( positions, position_units = "percent", digits = 1 ) formatted$table
Produces a stable, report-ready table from motif extraction, summary, or filtered-motif output.
format_sequence_motifs( x, digits = 3L, prevalence = c("proportion", "percent"), include_rank = TRUE, rank_by = c("sequence_prevalence", "n_occurrences", "n_sequences"), ties = c("min", "first"), include_ids = TRUE )format_sequence_motifs( x, digits = 3L, prevalence = c("proportion", "percent"), include_rank = TRUE, rank_by = c("sequence_prevalence", "n_occurrences", "n_sequences"), ties = c("min", "first"), include_ids = TRUE )
x |
A motif extraction, motif summary, or filtered-motif object. |
digits |
Whole number from 0 to 15 controlling numeric rounding. |
prevalence |
Character value specifying whether prevalence and
occurrence share are shown as |
include_rank |
Logical value indicating whether to add a rank column. |
rank_by |
Metric used to order and rank motifs: one of
|
ties |
Character value specifying |
include_ids |
Logical value indicating whether |
Formatting changes display precision and units only. It does not change the underlying motif counts or introduce substantive interpretation.
A named list containing table, validation metadata, and formatting
settings. The table contains only structural motif measurements.
sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "A", "A", "B", "A", "C") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) formatted <- format_sequence_motifs( extracted, prevalence = "percent", digits = 1 ) formatted$tablesequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "A", "A", "B", "A", "C") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) formatted <- format_sequence_motifs( extracted, prevalence = "percent", digits = 1 ) formatted$table
Creates a compact one-row-per-sequence representation of ordered state paths.
format_sequence_paths( data, sequence_id_col, order_col, state_col, metadata_cols = NULL, expected_states = NULL, separator = " > ", collapse_repeats = FALSE )format_sequence_paths( data, sequence_id_col, order_col, state_col, metadata_cols = NULL, expected_states = NULL, separator = " > ", collapse_repeats = FALSE )
data |
A data frame containing ordered state observations. |
sequence_id_col |
Name of the sequence identifier column. |
order_col |
Name of the numeric sequence-order column. |
state_col |
Name of the categorical state column. |
metadata_cols |
Optional character vector naming columns that should remain constant within each sequence. |
expected_states |
Optional vector of known or permitted state values. |
separator |
Character value inserted between adjacent state labels. |
collapse_repeats |
Logical value indicating whether consecutive repeated states should be collapsed for path display. |
Repeat collapsing affects only the formatted representation. It does not
modify the supplied data or alter non-consecutive repeated states.
Input rows are ordered deterministically for formatting, but review-level
diagnostics such as unordered_rows remain reflected in the returned
status and audit.
A named list containing:
paths: one row per sequence with observation counts, formatted-state
counts, unique-state counts, start and end states, and the path string;
audit, status, and mapping from input validation;
settings: the path separator and repeat-collapsing choice.
sequences <- data.frame( id = c("s1", "s1", "s1", "s2", "s2"), position = c(1, 2, 3, 1, 2), state = c("A", "B", "C", "A", "C") ) paths <- format_sequence_paths( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) paths$pathssequences <- data.frame( id = c("s1", "s1", "s1", "s2", "s2"), position = c(1, 2, 3, 1, 2), state = c("A", "B", "C", "A", "C") ) paths <- format_sequence_paths( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) paths$paths
Plot a consensus sequence
plot_consensus_sequence( consensus, type = c("agreement", "states"), group = NULL, main = NULL, xlab = "Sequence position", ylab = NULL, ... )plot_consensus_sequence( consensus, type = c("agreement", "states"), group = NULL, main = NULL, xlab = "Sequence position", ylab = NULL, ... )
consensus |
A consensus result. |
type |
|
group |
Optional group value, encoded group key, or named list of values when grouped consensus was created. It is required when more than one consensus group is present. |
main, xlab, ylab
|
Plot labels. |
... |
Additional arguments passed to base graphics. |
The plotted data, invisibly.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) consensus <- create_consensus_sequence(sequences) plot_consensus_sequence(consensus)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) consensus <- create_consensus_sequence(sequences) plot_consensus_sequence(consensus)
Plot multichannel HMM emission profiles
plot_multichannel_sequence_hmm(model, channel = model$channel_names[1L], ...)plot_multichannel_sequence_hmm(model, channel = model$channel_names[1L], ...)
model |
A fitted multichannel HMM. |
channel |
Channel name to plot. |
... |
Additional arguments passed to |
The selected emission matrix, invisibly.
# See `fit_multichannel_sequence_hmm()`.# See `fit_multichannel_sequence_hmm()`.
Plot sequence-cluster silhouette values
plot_sequence_cluster_silhouette(clustering, distance = NULL, ...)plot_sequence_cluster_silhouette(clustering, distance = NULL, ...)
clustering |
A clustering result or named assignment vector. |
distance |
Optional distance when |
... |
Additional arguments passed to |
The ordered per-sequence silhouette table, invisibly.
data <- data.frame(sequence_id = rep(paste0("s", 1:4), each = 4L), sequence_order = rep(1:4, 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A")) distance <- compute_sequence_distance(data) plot_sequence_cluster_silhouette(cluster_sequences(distance, 2L))data <- data.frame(sequence_id = rep(paste0("s", 1:4), each = 4L), sequence_order = rep(1:4, 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A")) distance <- compute_sequence_distance(data) plot_sequence_cluster_silhouette(cluster_sequences(distance, 2L))
Plot a sequence-distance heatmap
plot_sequence_distance_heatmap( distance, order_by = NULL, palette = "Viridis", show_labels = TRUE, ... )plot_sequence_distance_heatmap( distance, order_by = NULL, palette = "Viridis", show_labels = TRUE, ... )
distance |
A sequence distance object or square matrix. |
order_by |
Optional clustering or named assignment vector used to order the matrix. |
palette |
Base HCL palette. |
show_labels |
Draw sequence identifiers. |
... |
Additional arguments passed to |
The ordered distance matrix, invisibly.
data <- data.frame(sequence_id = rep(paste0("s", 1:4), each = 4L), sequence_order = rep(1:4, 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A")) plot_sequence_distance_heatmap(compute_sequence_distance(data))data <- data.frame(sequence_id = rep(paste0("s", 1:4), each = 4L), sequence_order = rep(1:4, 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A")) plot_sequence_distance_heatmap(compute_sequence_distance(data))
Plot position-wise state entropy
plot_sequence_entropy( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", base = 2, normalise = TRUE, ... )plot_sequence_entropy( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", base = 2, normalise = TRUE, ... )
data |
Long-format sequence data or a prepared result. |
sequence_id_col, order_col, state_col
|
Core sequence columns. |
base |
Logarithm base. |
normalise |
Divide entropy by the maximum entropy for the observed alphabet. |
... |
Additional arguments passed to |
A data frame of position-wise entropy values, invisibly.
data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "C", "D")) plot_sequence_entropy(data)data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "C", "D")) plot_sequence_entropy(data)
Plot a descriptive sequence-group comparison
plot_sequence_group_comparison( comparison, component = c("state", "transition", "length"), measure = NULL, top_n = 12L, main = NULL, xlab = NULL, ylab = NULL, ... )plot_sequence_group_comparison( comparison, component = c("state", "transition", "length"), measure = NULL, top_n = 12L, main = NULL, xlab = NULL, ylab = NULL, ... )
comparison |
A result from |
component |
|
measure |
Measure to display. Defaults depend on |
top_n |
Maximum states or transitions to display. |
main, xlab, ylab
|
Plot labels. |
... |
Additional base-graphics arguments. |
The plotted summary data, invisibly.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) comparison <- compare_sequence_groups(sequences, group_col = "group") plot_sequence_group_comparison(comparison, component = "state")sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) comparison <- compare_sequence_groups(sequences, group_col = "group") plot_sequence_group_comparison(comparison, component = "state")
Plot sequence group inference
plot_sequence_group_inference( inference, type = c("permutation", "group_means"), ... )plot_sequence_group_inference( inference, type = c("permutation", "group_means"), ... )
inference |
A sequence group-inference object. |
type |
|
... |
Additional base-graphics arguments. |
The inference object, invisibly.
# See `test_sequence_group_difference()`.# See `test_sequence_group_difference()`.
Plot a sequence index heatmap
plot_sequence_index( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", sort_by = c("input", "length", "path"), state_levels = NULL, palette = "Dark 3", show_sequence_labels = TRUE, ... )plot_sequence_index( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", sort_by = c("input", "length", "path"), state_levels = NULL, palette = "Dark 3", show_sequence_labels = TRUE, ... )
data |
Long-format sequence data or a prepared result. |
sequence_id_col, order_col, state_col
|
Core sequence columns. |
sort_by |
|
state_levels |
Optional state ordering. |
palette |
Base HCL palette. |
show_sequence_labels |
Draw sequence identifiers. |
... |
Additional arguments passed to |
The plotted state-code matrix, invisibly.
data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "C", "D")) plot_sequence_index(data)data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "C", "D")) plot_sequence_index(data)
Shows occurrence locations for selected contiguous motifs.
plot_sequence_motif_positions( x, motifs = NULL, position = c("start", "centre", "end"), scale = c("absolute", "relative"), top_n = 10L, display = c("strip", "distribution") )plot_sequence_motif_positions( x, motifs = NULL, position = c("start", "centre", "end"), scale = c("absolute", "relative"), top_n = 10L, display = c("strip", "distribution") )
x |
An object returned by |
motifs |
Optional character vector of motif identifiers, motif keys, or displayed motif labels. When omitted, motifs are selected deterministically by occurrence count, sequence count, motif length, and motif key. |
position |
Occurrence position represented by motif |
scale |
Position scale: one-based |
top_n |
Positive whole number giving the number of motifs selected when
|
display |
Base-R display type: occurrence |
The strip display uses deterministic vertical stacking rather than random jitter. The distribution display uses one horizontal boxplot per motif. Empty inputs produce an informative blank plot and return an empty table. The function provides structural location summaries only.
Invisibly returns the exact occurrence-level data used by the plot, including deterministic motif ranks and plotting coordinates where relevant.
sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "C", "A", "B", "C", "B") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) plot_sequence_motif_positions( extracted, position = "centre", scale = "relative", top_n = 5, display = "strip" )sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "C", "A", "B", "C", "B") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) plot_sequence_motif_positions( extracted, position = "centre", scale = "relative", top_n = 5, display = "strip" )
Draws a dependency-free base-R bar chart of structural motif measurements.
plot_sequence_motifs( x, metric = c("sequence_prevalence", "n_occurrences", "n_sequences", "occurrence_share"), top_n = 20L, motif_lengths = NULL, ties = c("include", "first"), horizontal = TRUE )plot_sequence_motifs( x, metric = c("sequence_prevalence", "n_occurrences", "n_sequences", "occurrence_share"), top_n = 20L, motif_lengths = NULL, ties = c("include", "first"), horizontal = TRUE )
x |
A motif extraction, summary, or filtered-motif object. |
metric |
Structural metric to plot: |
top_n |
Positive whole number giving the requested number of motifs. |
motif_lengths |
Optional vector of positive whole-number motif lengths to retain. |
ties |
Top- |
horizontal |
Logical value indicating whether bars should be horizontal. |
The plot is descriptive. It does not perform inferential testing or assign substantive meaning to motif frequency or prevalence. Empty inputs produce an informative blank plot and return an empty table.
Invisibly returns the exact motif table used for plotting, including
plot_rank, plot_value, plot_label, and bar_midpoint.
sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "C", "A", "B", "C", "B") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) plot_sequence_motifs( extracted, metric = "sequence_prevalence", top_n = 10 )sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "C", "A", "B", "C", "B") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) plot_sequence_motifs( extracted, metric = "sequence_prevalence", top_n = 10 )
Plot longitudinal sequence changes
plot_sequence_panel_changes( changes, metric = c("distance", "length_change", "transition_change"), type = c("individual", "summary"), ... )plot_sequence_panel_changes( changes, metric = c("distance", "length_change", "transition_change"), type = c("individual", "summary"), ... )
changes |
A result from |
metric |
One of |
type |
Plot individual panel trajectories or occasion-transition means. |
... |
Additional graphical arguments passed to base plotting functions. |
The plotted data, invisibly.
panel_data <- data.frame( participant_id = rep(c("p1", "p2"), each = 6L), occasion = rep(rep(c(1, 2), each = 3L), times = 2L), sequence_id = rep(c("a", "b", "c", "d"), each = 3L), sequence_order = rep(1:3, times = 4L), state = c("A", "B", "C", "A", "C", "C", "C", "B", "A", "C", "B", "B") ) changes <- compare_sequence_panel_changes( prepare_sequence_panel(panel_data, "participant_id", "occasion") ) plot_sequence_panel_changes(changes)panel_data <- data.frame( participant_id = rep(c("p1", "p2"), each = 6L), occasion = rep(rep(c(1, 2), each = 3L), times = 2L), sequence_id = rep(c("a", "b", "c", "d"), each = 3L), sequence_order = rep(1:3, times = 4L), state = c("A", "B", "C", "A", "C", "C", "C", "B", "A", "C", "B", "B") ) changes <- compare_sequence_panel_changes( prepare_sequence_panel(panel_data, "participant_id", "occasion") ) plot_sequence_panel_changes(changes)
Plot state distributions over aligned positions
plot_sequence_state_distribution( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", proportion = TRUE, state_levels = NULL, palette = "Dark 3", ... )plot_sequence_state_distribution( data, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", proportion = TRUE, state_levels = NULL, palette = "Dark 3", ... )
data |
Long-format sequence data or a prepared result. |
sequence_id_col, order_col, state_col
|
Core sequence columns. |
proportion |
Plot position-wise proportions rather than counts. |
state_levels |
Optional state ordering. |
palette |
Base HCL palette. |
... |
Additional arguments passed to |
A position-by-state matrix, invisibly.
data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "C", "D")) plot_sequence_state_distribution(data)data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "C", "D")) plot_sequence_state_distribution(data)
Plot non-contiguous subsequence summaries
plot_sequence_subsequences( x, metric = "sequence_prevalence", top_n = 10L, decreasing = TRUE, ... )plot_sequence_subsequences( x, metric = "sequence_prevalence", top_n = 10L, decreasing = TRUE, ... )
x |
A summary or group-comparison table. |
metric |
Numeric column to plot. |
top_n |
Maximum number of subsequences. |
decreasing |
Sort metric in decreasing order. |
... |
Additional arguments passed to |
The plotted rows, invisibly.
data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "B", "D")) summary <- summarise_sequence_subsequences(extract_sequence_subsequences(data)) plot_sequence_subsequences(summary, top_n = 5L)data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "B", "D")) summary <- summarise_sequence_subsequences(extract_sequence_subsequences(data)) plot_sequence_subsequences(summary, top_n = 5L)
Plot predicted time-varying sequence probabilities
plot_time_varying_sequence_model( model, time = NULL, level = 0.95, show_interval = TRUE, ... )plot_time_varying_sequence_model( model, time = NULL, level = 0.95, show_interval = TRUE, ... )
model |
A fitted time-varying sequence model. |
time |
Optional prediction grid. |
level |
Pointwise confidence level. |
show_interval |
Draw pointwise confidence ribbons. |
... |
Additional arguments passed to |
Prediction data, invisibly.
# See `fit_time_varying_sequence_model()`.# See `fit_time_varying_sequence_model()`.
Plot a first-order transition network
plot_transition_network( network, weight_col = "weight", minimum_weight = 0, vertex_cex = 1, edge_scale = 5, ... )plot_transition_network( network, weight_col = "weight", minimum_weight = 0, vertex_cex = 1, edge_scale = 5, ... )
network |
A first-order network from |
weight_col |
Edge-weight column. |
minimum_weight |
Minimum plotted edge weight. |
vertex_cex |
Vertex label size. |
edge_scale |
Edge-width multiplier. |
... |
Additional arguments passed to |
The plotted network rows, invisibly.
data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "C", "D")) plot_transition_network(create_transition_network(data, normalise = "from"))data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "C", "D")) plot_transition_network(create_transition_network(data, normalise = "from"))
Predict covariate-dependent transition probabilities
predict_covariate_transition_probabilities(model, newdata)predict_covariate_transition_probabilities(model, newdata)
model |
A fitted covariate HMM. |
newdata |
Data frame containing transition covariates. |
A long data frame with one row per input row, origin state, and destination state.
# See `fit_covariate_sequence_hmm()`.# See `fit_covariate_sequence_hmm()`.
Predict the next state from a transition model
predict_next_state(model, history, top_n = NULL)predict_next_state(model, history, top_n = NULL)
model |
A higher-order transition model. |
history |
Character vector of observed recent states. |
top_n |
Optional number of states to retain. Returned probabilities remain on the full-model scale and are not renormalised after truncation. |
A probability table ordered from highest to lowest probability, with the context order actually used.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) model <- fit_higher_order_transition_model(sequences, order = 2L) predict_next_state(model, c("A", "B"))sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) model <- fit_higher_order_transition_model(sequences, order = 2L) predict_next_state(model, c("A", "B"))
Predict a time-varying sequence model
predict_time_varying_sequence_model( model, time = NULL, groups = NULL, level = 0.95 )predict_time_varying_sequence_model( model, time = NULL, groups = NULL, level = 0.95 )
model |
A fitted time-varying sequence model. |
time |
Optional numeric prediction grid. |
groups |
Optional subset of fitted groups. |
level |
Confidence level for pointwise intervals. |
A data frame with fitted probabilities and pointwise intervals.
# See `fit_time_varying_sequence_model()`.# See `fit_time_varying_sequence_model()`.
This optional compatibility helper recognises ordinary data frames or lists containing a data-frame component and maps common sequence-column names to the neutral gp3sequences contract. It does not require a gp3tools class.
prepare_gp3tools_sequences( data, sequence_id_col = NULL, order_col = NULL, state_col = NULL, duration_col = NULL, metadata_cols = NULL, ... )prepare_gp3tools_sequences( data, sequence_id_col = NULL, order_col = NULL, state_col = NULL, duration_col = NULL, metadata_cols = NULL, ... )
data |
A data frame or list with a data-frame |
sequence_id_col, order_col, state_col
|
Optional explicit mappings. |
duration_col |
Optional duration mapping. |
metadata_cols |
Optional constant-within-sequence metadata columns. |
... |
Additional arguments passed to |
A standard gp3sequences preparation result.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) names(sequences)[names(sequences) == "sequence_order"] <- "position" names(sequences)[names(sequences) == "state"] <- "aoi_label" prepare_gp3tools_sequences(sequences)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) names(sequences)[names(sequences) == "sequence_order"] <- "position" names(sequences)[names(sequences) == "state"] <- "aoi_label" prepare_gp3tools_sequences(sequences)
Applies explicit preprocessing policies and returns a deterministic, canonical long-format representation.
prepare_sequence_data( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL, missing_state_policy = c("error", "drop"), duplicate_position_policy = c("error", "first", "last"), repeated_state_policy = c("preserve", "collapse"), zero_duration_policy = c("preserve", "drop", "error"), unknown_state_policy = c("preserve", "drop", "error"), unused_state_levels = c("preserve", "drop") )prepare_sequence_data( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL, missing_state_policy = c("error", "drop"), duplicate_position_policy = c("error", "first", "last"), repeated_state_policy = c("preserve", "collapse"), zero_duration_policy = c("preserve", "drop", "error"), unknown_state_policy = c("preserve", "drop", "error"), unused_state_levels = c("preserve", "drop") )
data |
A data frame containing ordered state observations. |
sequence_id_col |
Name of the sequence identifier column. |
order_col |
Name of the numeric sequence-order column. |
state_col |
Name of the categorical state column. |
duration_col |
Optional name of a numeric duration column. |
metadata_cols |
Optional character vector naming columns that should remain constant within each sequence. |
expected_states |
Optional vector of known or permitted state values. |
missing_state_policy |
Policy for missing states: |
duplicate_position_policy |
Policy for duplicated sequence positions:
|
repeated_state_policy |
Policy for consecutive repeated states:
|
zero_duration_policy |
Policy for zero durations: |
unknown_state_policy |
Policy for states absent from
|
unused_state_levels |
Policy for unused factor levels: |
Rows are sorted deterministically by sequence identifier, sequence order, and original row number. When consecutive repeats are collapsed, the first row supplies non-duration values and available durations are summed.
Unresolved errors produce status = "fail" and data = NULL; diagnostics
and decision records remain available.
A named list containing:
data: canonical prepared data, or NULL when unresolved errors remain;
audit: input- and output-stage diagnostics;
decisions: a machine-readable preprocessing decision log;
mapping: source-to-contract column mappings;
status: pass, review, or fail;
row counts and final state levels.
The canonical columns are sequence_id, sequence_order, state,
original_row, and optional duration. Unmapped columns are preserved.
sequences <- data.frame( id = c("s2", "s1", "s1", "s2"), position = c(2, 2, 1, 1), state = c("product", "search", "home", "home") ) prepared <- prepare_sequence_data( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) prepared$status prepared$datasequences <- data.frame( id = c("s2", "s1", "s1", "s2"), position = c(2, 2, 1, 1), state = c("product", "search", "home", "home") ) prepared <- prepare_sequence_data( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) prepared$status prepared$data
Validates a collection of sequences observed repeatedly for the same panel units and creates an auditable panel index. Each sequence must map to exactly one panel unit and one occasion.
prepare_sequence_panel( data, panel_id_col, occasion_col, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", metadata_cols = NULL, require_unique_occasions = TRUE )prepare_sequence_panel( data, panel_id_col, occasion_col, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", metadata_cols = NULL, require_unique_occasions = TRUE )
data |
Long-format sequence data or a prepared gp3sequences result. |
panel_id_col |
Column identifying the repeatedly observed unit. |
occasion_col |
Column identifying the wave, visit, or occasion. |
sequence_id_col, order_col, state_col
|
Core sequence columns. |
metadata_cols |
Optional columns that must remain constant within each sequence. |
require_unique_occasions |
Require at most one sequence per panel unit and occasion. |
An object of class gp3_sequence_panel containing canonical data,
a panel index, state levels, and mapping information.
panel <- data.frame( participant_id = rep(c("p1", "p2"), each = 8L), occasion = rep(rep(c(1, 2), each = 4L), times = 2L), sequence_id = rep(c("p1_w1", "p1_w2", "p2_w1", "p2_w2"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "C", "C", "D", "D", "C", "B", "A", "D", "B", "B", "A") ) prepare_sequence_panel(panel, "participant_id", "occasion")panel <- data.frame( participant_id = rep(c("p1", "p2"), each = 8L), occasion = rep(rep(c(1, 2), each = 4L), times = 2L), sequence_id = rep(c("p1_w1", "p1_w2", "p2_w1", "p2_w2"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "C", "C", "D", "D", "C", "B", "A", "D", "B", "B", "A") ) prepare_sequence_panel(panel, "participant_id", "occasion")
Returns a deterministic, machine-readable inventory of native analytical capabilities, optional adapters, reference implementations, and specialist handoffs relevant to the current gp3sequences development series.
sequence_capabilities(include_optional = TRUE, check_versions = TRUE)sequence_capabilities(include_optional = TRUE, check_versions = TRUE)
include_optional |
Logical; include optional/reference capabilities. |
check_versions |
Logical; report installed optional-package versions. |
A data frame with capability family, capability, implementation role, backend, availability, and version information. The function never installs, attaches, or loads optional packages.
capabilities <- sequence_capabilities() capabilities[c("family", "capability", "role", "available")]capabilities <- sequence_capabilities() capabilities[c("family", "capability", "role", "available")]
Summarise consensus agreement
summarise_consensus_agreement( consensus, by = c("overall", "group", "position"), threshold = 0.5 )summarise_consensus_agreement( consensus, by = c("overall", "group", "position"), threshold = 0.5 )
consensus |
A result from |
by |
Summary level: |
threshold |
Agreement threshold used to count low-agreement positions. |
A data frame with position counts, mean, median, minimum and maximum agreement, weighted agreement, tie counts, and low-agreement counts.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) consensus <- create_consensus_sequence(sequences) summarise_consensus_agreement(consensus)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) consensus <- create_consensus_sequence(sequences) summarise_consensus_agreement(consensus)
Summarise a covariate-dependent HMM
summarise_covariate_sequence_hmm(model)summarise_covariate_sequence_hmm(model)
model |
A fitted covariate HMM. |
A list of fit, coefficient, and emission summaries.
# See `fit_covariate_sequence_hmm()`.# See `fit_covariate_sequence_hmm()`.
Summarise a multichannel sequence HMM
summarise_multichannel_sequence_hmm(model)summarise_multichannel_sequence_hmm(model)
model |
A fitted multichannel HMM. |
A list of convergence, fit, transition, initial, and channel-specific emission summaries.
# See `fit_multichannel_sequence_hmm()`.# See `fit_multichannel_sequence_hmm()`.
Summarise sequence-cluster stability
summarise_sequence_cluster_stability(bootstrap, threshold = 0.8)summarise_sequence_cluster_stability(bootstrap, threshold = 0.8)
bootstrap |
A result from |
threshold |
Pairwise stability threshold. |
Overall, cluster-level, and low-stability pair summaries.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) boot <- bootstrap_sequence_clusters(distance, k = 2L, n_boot = 5L) summarise_sequence_cluster_stability(boot)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) boot <- bootstrap_sequence_clusters(distance, k = 2L, n_boot = 5L) summarise_sequence_cluster_stability(boot)
Summarise a sequence-distance object
summarise_sequence_distance(distance)summarise_sequence_distance(distance)
distance |
A distance object returned by |
A list containing an overall summary and per-sequence mean, median, minimum, and maximum distances.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) summarise_sequence_distance(distance)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) summarise_sequence_distance(distance)
Summarise sequence group inference
summarise_sequence_group_inference(inference)summarise_sequence_group_inference(inference)
inference |
A sequence group-inference object. |
A list containing the estimate, optional interval, design, and interpretation statement.
# See `test_sequence_group_difference()`.# See `test_sequence_group_difference()`.
Summarise a fitted sequence HMM
summarise_sequence_hmm(model)summarise_sequence_hmm(model)
model |
A single or mixture HMM. |
A list of convergence, fit, initial, transition, emission, and mixture summaries.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) model <- fit_sequence_hmm(sequences, 2L, max_iter = 5L) summarise_sequence_hmm(model)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) model <- fit_sequence_hmm(sequences, 2L, max_iter = 5L) summarise_sequence_hmm(model)
Summarises where contiguous motif occurrences appear within sequences.
summarise_sequence_motif_positions( x, position = c("start", "centre", "end"), scale = c("absolute", "relative"), by = NULL )summarise_sequence_motif_positions( x, position = c("start", "centre", "end"), scale = c("absolute", "relative"), by = NULL )
x |
An object returned by |
position |
Position represented by each occurrence: motif |
scale |
Position scale: one-based |
by |
Optional character vector naming preserved metadata columns used
to produce separate summaries, such as |
Absolute positions use the one-based state index within each validated
sequence. Relative positions are calculated as
(absolute_position - 1) / (n_states - 1) and are constrained to the
interval from 0 to 1. A sequence containing one state is assigned relative
position 0.
Grouping is descriptive. The function does not test differences or attach behavioural, psychological, cognitive, or causal interpretations to motif location.
A named list containing:
summary: one row per motif and optional metadata group;
occurrences: occurrence-level absolute, relative, and selected-scale
positions;
sequences, validation metadata, and extraction settings;
settings: the resolved position basis, scale, and grouping columns.
The summary reports motif identifiers and labels, motif length, occurrence and sequence counts, and minimum, maximum, mean, and median positions.
sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "C", "A", "B", "C", "B"), group = c(rep("g1", 5L), rep("g2", 4L)) ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state", metadata_cols = "group", min_length = 2, max_length = 3 ) positions <- summarise_sequence_motif_positions( extracted, position = "centre", scale = "relative", by = "group" ) positions$summarysequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "C", "A", "B", "C", "B"), group = c(rep("g1", 5L), rep("g2", 4L)) ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state", metadata_cols = "group", min_length = 2, max_length = 3 ) positions <- summarise_sequence_motif_positions( extracted, position = "centre", scale = "relative", by = "group" ) positions$summary
Aggregates extracted contiguous motif occurrences by sequence and overall.
summarise_sequence_motifs(x)summarise_sequence_motifs(x)
x |
An object returned by |
Sequence prevalence uses every validated sequence in the extraction object as its denominator, including sequences too short to contain a requested motif. Results are sorted deterministically by sequence prevalence, occurrence count, motif length, and motif key.
The function reports structural recurrence only. It does not perform significance testing or infer psychological, cognitive, emotional, or diagnostic attributes.
A named list containing:
by_sequence: occurrence counts for each sequence-motif pair;
overall: total occurrence counts, sequence counts, sequence prevalence,
occurrence share, and mean occurrence rates;
sequences and state_dictionary from extraction;
audit, status, mapping, and extraction settings;
scalar counts for sequences, occurrences, and distinct motifs.
sequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "A", "A", "B", "A", "C") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state", min_length = 2, max_length = 3 ) summaries <- summarise_sequence_motifs(extracted) summaries$by_sequence summaries$overallsequences <- data.frame( id = c(rep("s1", 5L), rep("s2", 4L)), position = c(1:5, 1:4), state = c("A", "B", "A", "B", "A", "A", "B", "A", "C") ) extracted <- extract_sequence_ngrams( sequences, sequence_id_col = "id", order_col = "position", state_col = "state", min_length = 2, max_length = 3 ) summaries <- summarise_sequence_motifs(extracted) summaries$by_sequence summaries$overall
Summarise a sequence panel
summarise_sequence_panel(panel)summarise_sequence_panel(panel)
panel |
A result from |
A list with occasion-level sequence summaries and state prevalence.
panel_data <- data.frame( participant_id = rep(c("p1", "p2"), each = 6L), occasion = rep(rep(c(1, 2), each = 3L), times = 2L), sequence_id = rep(c("a", "b", "c", "d"), each = 3L), sequence_order = rep(1:3, times = 4L), state = c("A", "B", "C", "A", "C", "C", "C", "B", "A", "C", "B", "B") ) summarise_sequence_panel(prepare_sequence_panel(panel_data, "participant_id", "occasion"))panel_data <- data.frame( participant_id = rep(c("p1", "p2"), each = 6L), occasion = rep(rep(c(1, 2), each = 3L), times = 2L), sequence_id = rep(c("a", "b", "c", "d"), each = 3L), sequence_order = rep(1:3, times = 4L), state = c("A", "B", "C", "A", "C", "C", "C", "B", "A", "C", "B", "B") ) summarise_sequence_panel(prepare_sequence_panel(panel_data, "participant_id", "occasion"))
Produces per-sequence and overall state-frequency summaries from validated ordered sequence data.
summarise_sequence_states( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL )summarise_sequence_states( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL )
data |
A data frame containing ordered state observations. |
sequence_id_col |
Name of the sequence identifier column. |
order_col |
Name of the numeric sequence-order column. |
state_col |
Name of the categorical state column. |
duration_col |
Optional name of a numeric duration column. |
metadata_cols |
Optional character vector naming columns that should remain constant within each sequence. |
expected_states |
Optional vector of known or permitted state values. |
Observation proportions use state rows as the denominator. Sequence
proportions report the proportion of sequences in which each state occurs.
Missing durations are excluded from duration calculations; an all-missing
duration group returns NA.
A named list containing:
by_sequence: state counts and proportions within each sequence;
overall: state counts and proportions across all sequences;
audit, status, and mapping from input validation.
When duration_col is supplied, both tables also include duration sums,
duration proportions, and mean durations.
sequences <- data.frame( id = c("s1", "s1", "s1", "s2", "s2"), position = c(1, 2, 3, 1, 2), state = c("A", "B", "A", "B", "C") ) summaries <- summarise_sequence_states( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) summaries$by_sequence summaries$overallsequences <- data.frame( id = c("s1", "s1", "s1", "s2", "s2"), position = c(1, 2, 3, 1, 2), state = c("A", "B", "A", "B", "C") ) summaries <- summarise_sequence_states( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) summaries$by_sequence summaries$overall
Summarise non-contiguous subsequences
summarise_sequence_subsequences(occurrences)summarise_sequence_subsequences(occurrences)
occurrences |
A result from |
A data frame with occurrence counts, sequence prevalence, and span diagnostics.
data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "B", "D")) summarise_sequence_subsequences(extract_sequence_subsequences(data))data <- data.frame(sequence_id = rep(c("a", "b"), each = 4L), sequence_order = rep(1:4, 2L), state = c("A", "B", "C", "D", "A", "C", "B", "D")) summarise_sequence_subsequences(extract_sequence_subsequences(data))
Counts transitions between adjacent ordered states for each sequence and across the complete data set.
summarise_sequence_transitions( data, sequence_id_col, order_col, state_col, metadata_cols = NULL, expected_states = NULL, include_self = TRUE )summarise_sequence_transitions( data, sequence_id_col, order_col, state_col, metadata_cols = NULL, expected_states = NULL, include_self = TRUE )
data |
A data frame containing ordered state observations. |
sequence_id_col |
Name of the sequence identifier column. |
order_col |
Name of the numeric sequence-order column. |
state_col |
Name of the categorical state column. |
metadata_cols |
Optional character vector naming columns that should remain constant within each sequence. |
expected_states |
Optional vector of known or permitted state values. |
include_self |
Logical value indicating whether transitions from a state to the same state should be included. |
A transition is defined only between adjacent rows after deterministic ordering by sequence identifier, sequence order, and original row. Sequences with one state contribute no transitions.
A named list containing:
by_sequence: transition counts, proportions within each sequence, and
conditional proportions within each origin state;
overall: transition counts, sequence coverage, global proportions, and
conditional origin-state proportions;
audit, status, mapping, and the resolved include_self setting.
sequences <- data.frame( id = c("s1", "s1", "s1", "s2", "s2"), position = c(1, 2, 3, 1, 2), state = c("A", "B", "C", "A", "C") ) transitions <- summarise_sequence_transitions( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) transitions$by_sequence transitions$overallsequences <- data.frame( id = c("s1", "s1", "s1", "s2", "s2"), position = c(1, 2, 3, 1, 2), state = c("A", "B", "C", "A", "C") ) transitions <- summarise_sequence_transitions( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) transitions$by_sequence transitions$overall
Summarise a time-varying sequence model
summarise_time_varying_sequence_model(model)summarise_time_varying_sequence_model(model)
model |
A fitted time-varying sequence model. |
A list containing model metadata, parametric terms, smooth terms, deviance explained, and convergence information.
# See `fit_time_varying_sequence_model()`.# See `fit_time_varying_sequence_model()`.
Summarise transition-network centrality
summarise_transition_centrality( network, directed = TRUE, pagerank_damping = 0.85, pagerank_tolerance = 1e-10, pagerank_max_iter = 1000L )summarise_transition_centrality( network, directed = TRUE, pagerank_damping = 0.85, pagerank_tolerance = 1e-10, pagerank_max_iter = 1000L )
network |
A first-order transition network. |
directed |
Treat the network as directed. |
pagerank_damping |
Damping factor for PageRank. |
pagerank_tolerance |
Convergence tolerance. |
pagerank_max_iter |
Maximum PageRank iterations. |
A data frame containing degree, strength, weighted closeness, unweighted betweenness, and PageRank centrality.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) network <- create_transition_network(sequences) summarise_transition_centrality(network)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) network <- create_transition_network(sequences) summarise_transition_centrality(network)
Aggregates sequence metrics to the declared independent unit and performs a permutation or randomization test. For observational data the p-value tests exchangeability-based association only; it is not a causal estimate.
test_sequence_group_difference( data, design, metric = c("sequence_length", "transition_count", "state_prevalence", "subsequence_presence"), target_state = NULL, target_subsequence = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", separator = " > ", n_permutations = 999L, alternative = c("two.sided", "greater", "less"), seed = 1L )test_sequence_group_difference( data, design, metric = c("sequence_length", "transition_count", "state_prevalence", "subsequence_presence"), target_state = NULL, target_subsequence = NULL, sequence_id_col = "sequence_id", order_col = "sequence_order", state_col = "state", separator = " > ", n_permutations = 999L, alternative = c("two.sided", "greater", "less"), seed = 1L )
data |
Long-format sequence data. |
design |
A comparison design. |
metric |
|
target_state |
Required for state prevalence. |
target_subsequence |
Required for subsequence presence, expressed using
|
sequence_id_col, order_col, state_col
|
Core sequence columns. |
separator |
Subsequence label separator. |
n_permutations |
Number of permutations. |
alternative |
Alternative hypothesis. |
seed |
Reproducibility seed. |
An object of class gp3_sequence_group_inference.
data <- data.frame( participant_id = rep(paste0("p", 1:8), each = 4L), sequence_id = rep(paste0("s", 1:8), each = 4L), sequence_order = rep(1:4, times = 8L), state = c(rep(c("A", "B", "C", "D"), 4L), rep(c("A", "A", "C", "D"), 4L)), group = rep(rep(c("control", "treatment"), each = 4L), each = 4L) ) design <- declare_sequence_comparison_design("group", "participant_id", design = "randomized") test_sequence_group_difference(data, design, metric = "state_prevalence", target_state = "A", n_permutations = 99L)data <- data.frame( participant_id = rep(paste0("p", 1:8), each = 4L), sequence_id = rep(paste0("s", 1:8), each = 4L), sequence_order = rep(1:4, times = 8L), state = c(rep(c("A", "B", "C", "D"), 4L), rep(c("A", "A", "C", "D"), 4L)), group = rep(rep(c("control", "treatment"), each = 4L), each = 4L) ) design <- declare_sequence_comparison_design("group", "participant_id", design = "randomized") test_sequence_group_difference(data, design, metric = "state_prevalence", target_state = "A", n_permutations = 99L)
Validate sequence clusters descriptively
validate_sequence_clusters(clustering, distance = NULL)validate_sequence_clusters(clustering, distance = NULL)
clustering |
A result from |
distance |
Optional distance object when |
A list containing overall validation metrics, cluster sizes, and per-sequence silhouette values.
sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) fit <- cluster_sequences(distance, k = 2L) validate_sequence_clusters(fit)sequences <- data.frame( sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L), sequence_order = rep(1:4, times = 4L), state = c("A", "B", "C", "D", "A", "B", "C", "C", "D", "C", "B", "A", "D", "C", "A", "A"), group = rep(c("g1", "g2"), each = 8L), stringsAsFactors = FALSE ) distance <- compute_sequence_distance(sequences) fit <- cluster_sequences(distance, k = 2L) validate_sequence_clusters(fit)
Produces a compact validation result based on
audit_sequence_data() without modifying the input.
validate_sequence_data( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL )validate_sequence_data( data, sequence_id_col, order_col, state_col, duration_col = NULL, metadata_cols = NULL, expected_states = NULL )
data |
A data frame containing ordered state observations. |
sequence_id_col |
Name of the sequence identifier column. |
order_col |
Name of the numeric sequence-order column. |
state_col |
Name of the categorical state column. |
duration_col |
Optional name of a numeric duration column. |
metadata_cols |
Optional character vector naming columns that should remain constant within each sequence. |
expected_states |
Optional vector of known or permitted state values. |
A named list containing valid, status, issue counts, the
complete audit table, the column mapping, row and sequence counts,
and observed state_levels. A result is valid when no error-severity
issue is present. Review-severity issues do not automatically invalidate
the input.
sequences <- data.frame( id = rep(c("s1", "s2"), each = 2L), position = rep(1:2, times = 2L), state = c("home", "search", "home", "product") ) validation <- validate_sequence_data( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) validation$status validation$validsequences <- data.frame( id = rep(c("s1", "s2"), each = 2L), position = rep(1:2, times = 2L), state = c("home", "search", "home", "product") ) validation <- validate_sequence_data( sequences, sequence_id_col = "id", order_col = "position", state_col = "state" ) validation$status validation$valid