This function extracts from a context tree a description of all of its contexts.
Arguments
- ct
a context tree.
- sequence
if
TRUE
the function returns its results as adata.frame
, ifFALSE
(default) as a list ofctx_node
objects. (see details)- reverse
logical (defaults to
FALSE
). See details.- ...
additional arguments for the contexts function.
Value
A list of class contexts
containing the contexts represented in
this tree (as ctx_node
) or a data.frame.
Details
The default behaviour consists in returning a list of all the contexts
contained in the tree using ctx_node
objects (as returned by e.g.
find_sequence()
) (with type="list"
). The properties of the contexts can
then be explored using adapted functions such as counts()
and
positions()
. The result list is of class contexts
. When sequence=TRUE
,
the method returns a data.frame whose first column, named context
, contains
the contexts as vectors (i.e. the value returned by as_sequence()
applied
to a ctx_node
object). Other columns contain context specific values which
depend on the actual class of the tree and on additional parameters. In all
implementations of contexts()
, setting the additional parameters to any no
default value leads to a data.frame
result.
State order in a context
Notice that contexts are given by default
in the temporal order and not in the "reverse" order used by many VLMC
research papers: older values are on the left. For instance, the context
c(1, 0)
is reported if the sequence 0, then 1 appeared in the time series
used to build the context tree. Set reverse to TRUE
for the reverse
convention which is somewhat easier to relate to the way the context trees
are represented by draw()
(i.e. recent values at the top the tree).
See also
find_sequence()
and find_sequence.covlmc()
for direct access to
a specific context, and contexts.ctx_tree()
, contexts.vlmc()
and
contexts.covlmc()
for concrete implementations of contexts()
.
Examples
dts <- sample(as.factor(c("A", "B", "C")), 100, replace = TRUE)
dts_tree <- ctx_tree(dts, max_depth = 3, min_size = 5)
contexts(dts_tree)
#> Contexts:
#> B, A, A
#> C, A, A
#> A, A
#> A, B, A
#> B, A
#> A, C, A
#> C, A
#> A, A, B
#> A, B
#> C, B
#> B
#> A, A, C
#> C, A, C
#> A, C
#> A, B, C
#> B, C
#> A, C, C
#> C, C, C
#> C, C
contexts(dts_tree, TRUE, TRUE)
#> context
#> 1 A, A, B
#> 2 A, A, C
#> 3 A, A
#> 4 A, B, A
#> 5 A, B
#> 6 A, C, A
#> 7 A, C
#> 8 B, A, A
#> 9 B, A
#> 10 B, C
#> 11 B
#> 12 C, A, A
#> 13 C, A, C
#> 14 C, A
#> 15 C, B, A
#> 16 C, B
#> 17 C, C, A
#> 18 C, C, C
#> 19 C, C