Fits a lag sequential analysis (LSA) on categorical event sequence data using a registered engine. Returns a tidy S3 object with named slots for observed/expected/probability/residual matrices and a long-format edge table suitable for transition-network visualization.
Usage
lsa_classical(data, ...)
lsa_two_cell(data, ...)
lsa_bidirectional(data, ...)
lsa_parallel_dominance(data, ...)
lsa_nonparallel_dominance(data, ...)
lsa(
data,
lag = 1,
engine = "classical",
alternative = c("two.sided", "greater", "less"),
alpha = 0.05,
loops = TRUE,
structural_zeros = NULL,
labels = NULL,
group = NULL,
actor = NULL,
action = NULL,
time = NULL,
order = NULL,
session = NULL,
time_threshold = 900,
custom_format = NULL,
is_unix_time = FALSE,
unix_time_unit = "seconds",
params = list(),
...
)Arguments
- data
Sequence input (any form accepted by
lsa_data()), or a raw long-format event-logdata.framewhen theactor/actionarguments are supplied (see below). Accepted already- sequenced forms include vectors, lists of sequences, wide matrices/data.frames, transition-count matrices, and sequence- bearing objects (tna,group_tna,nestimate_data,stslist).NAand empty-string cells are treated as missingness, not as a state: they are dropped wherever they occur and no transition is counted into or out of them. To model missingness as its own state, recode it (e.g.NA -> "missing") before callinglsa().- ...
Additional engine-specific parameters (merged into
params).- lag
Integer. The transition lag. Default
1. Positive lags count successors (state att -> t + lag); negative lags count predecessors (what occurred|lag|steps before);0pairs each event with itself (degenerate for single-stream event data – genuine co-occurrence needs concurrent codes, not yet supported). Pre-computed transition-matrix input supportslag = 1only. To analyse several lags at once, seelsa_lags().- engine
Character scalar. The engine name, registered via
register_lsa_engine(). Built-in engines:"classical","two_cell","bidirectional","parallel_dominance","nonparallel_dominance". Default"classical".- alternative
Character scalar. The alternative hypothesis for adjusted-residual and kappa p-values: one of
"two.sided"(default),"greater", or"less".- alpha
Numeric. Significance threshold used to mark edges as significant in
fit$edges$significant. Default0.05.- loops
Logical. Keep self-transitions (the diagonal)? Default
TRUE. Setloops = FALSEto forbid every self-transition – the common reason to exclude cells – without building a matrix by hand.- structural_zeros
Optional
K x K0/1 matrix for an arbitrary forbidden-cell pattern, where0marks a forbidden (structural-zero) cell and1an estimable one. DefaultNULL: every cell is part of the model. Combines withloops:loops = FALSEalso zeros the diagonal of a supplied matrix. When any cell is forbidden the engine switches to iterative proportional fitting and Christensen's design-matrix residuals (seeinst/REFERENCES.md§2.2, §4.2).- labels
Optional character vector of state labels.
- group
Optional grouping for a multi-group fit. Either a vector with one entry per input sequence (length
n_sequences), or — for long-format input (seeactor/action) — the name of a grouping column in the log, which must be constant within each actor/session so each recovered sequence maps to one group. Sequences are partitioned by group and a separatelsafit is built for each. All group fits share one global label set (derived from the full data) so theirK x Kmatrices are directly comparable, even when a group never visits some state. Returns anlsa_groupobject (a named list oflsafits). Requires event-level input; a pre-computed transition matrix cannot be split by group. DefaultNULL(single-group fit).- actor, action, time, order, session
Column names (each a single string) for long-format event-log input. Supplying
actionswitcheslsa()into long-format mode: the raw log indatais sequenced by grouping rows peractor, persession, peractor x session, or as one global sequence when neither boundary is supplied. Events are ordered within each sequence byorderif given else bytime, and – whentimeis given and nosessioncolumn is – a new sequence starts whenever the gap between consecutive events exceedstime_thresholdseconds. AllNULLby default exceptactionin long-format mode (input is otherwise taken as already-sequenced).- time_threshold
Numeric. Maximum gap in seconds between consecutive events before a new session is started in long-format mode. Default
900(15 minutes). Ignored unlesstimeis given andsessionis not.- custom_format
Optional
strptimeformat string for parsing thetimecolumn (e.g."%Y-%m-%d %H:%M:%S"). DefaultNULL(native date/time classes and ISO strings are parsed directly).- is_unix_time
Logical. Treat the
timecolumn as a Unix epoch. DefaultFALSE.- unix_time_unit
Character. Unit of the Unix epoch when
is_unix_time = TRUE:"seconds"(default),"milliseconds", or"microseconds".- params
Optional named list of engine-specific parameters forwarded to the engine function.
Value
An object of class c("lsa", "cograph_network"). Read it with
the verbs rather than by reaching into slots: transitions() for the
tidy edge table, nodes(), tests(), initial(), and summary()
for the other results, and plot()/plot_transitions() to draw it.
Every number a verb returns is backed by these slots:
- edges
The tidy one-row-per-transition frame that backs
transitions()(with extracograph_networkprotocol columns).- nodes
Data frame backing
nodes():id, label, name, outgoing, incoming.- obs, exp, prob, prob_col, adj_res, p, yules_q, kappa, kappa_z, kappa_p
The same per-cell quantities as
edges, inK x Kmatrix form (probis row-conditional P(to | from),prob_colcolumn-conditional P(from | to)). Convenient for matrix algebra; not the primary interface.- lrx2, x2
Lists
(statistic, df, p)backingtests(): the tablewise likelihood-ratio (G^2) and Pearson chi-square tests of independence;NULLfor engines without an expected table.- inits
Named numeric vector backing
initial()(proportion of sequences starting in each state, sums to 1);NULLfor transition-matrix input.- weights
K x Kmatrix used as the default edge weight for plotting. Equal toobs(counts) by default.- directed
Logical scalar;
TRUEfor directed engines,FALSEforbidirectional.- method
Engine name (the slot the
cograph_networkprotocol reads). Also recorded inparams$engine.- data
The canonical
lsa_dataobject (events + seq_id).- params
Immutable snapshot of all parameters used (recipe), including
params$engine.- meta
List with source, IPF info, version, and call.
When group is supplied, returns an object of class
c("lsa_group", "list"): a named list of lsa fits (one per group
level) carrying levels, group_sizes, labels, and engine
attributes. Downstream verbs (transitions(),
transition_probabilities(), reliability_lsa(), etc.) dispatch on
it and return grouped results.
Examples
seq <- c("Question", "Explain", "Agree",
"Question", "Explain", "Elaborate",
"Agree", "Question", "Explain")
fit <- lsa(seq, engine = "classical")
fit
#> Lag Sequential Analysis - classical (lag 1, directed)
#> 4 states | 8 transitions | 9 events | 1 sequences
#> states: Agree, Elaborate, Explain, Question
#> independence: G² = 18.4, df = 9, p 0.0312
#>
#> Significant transitions (p < 0.05): 2 of 16
#> strongest over-represented (of 2):
#> Agree -> Question z = +2.8 **
#> Question -> Explain z = +2.8 **
#>
#> Initial states:
#> Question 1.000 ████████████████████████
#> Agree 0.000
#> Elaborate 0.000
#> Explain 0.000
head(fit$edges)
#> from to from_label to_label lag count expected prob prob_col adj_res
#> 1 1 1 Agree Agree 1 0 0.500 0.0 0.0 -0.9428090
#> 2 2 1 Elaborate Agree 1 1 0.250 1.0 0.5 1.8516402
#> 3 3 1 Explain Agree 1 1 0.500 0.5 0.5 0.9428090
#> 4 4 1 Question Agree 1 0 0.750 0.0 0.0 -1.2649111
#> 5 1 2 Agree Elaborate 1 0 0.250 0.0 0.0 -0.6172134
#> 6 2 2 Elaborate Elaborate 1 0 0.125 0.0 0.0 -0.4040610
#> p yules_q kappa kappa_z kappa_p lift sign significant
#> 1 0.34577859 -1.0000000 -1.00 -0.8081220 0.41902033 0 under FALSE
#> 2 0.06407751 1.0000000 1.00 1.8708287 0.06136883 4 over FALSE
#> 3 0.34577859 0.6666667 0.25 0.5345225 0.59298010 2 over FALSE
#> 4 0.20590321 -1.0000000 -1.00 -1.0690450 0.28504941 0 under FALSE
#> 5 0.53709398 -1.0000000 -1.00 -0.5345225 0.59298010 0 under FALSE
#> 6 0.68616785 -1.0000000 -1.00 -0.3535534 0.72367361 0 under FALSE
#> edge weight
#> 1 Agree -> Agree 0
#> 2 Elaborate -> Agree 1
#> 3 Explain -> Agree 1
#> 4 Question -> Agree 0
#> 5 Agree -> Elaborate 0
#> 6 Elaborate -> Elaborate 0