Skip to contents

This function extracts from a context tree a description of all of its contexts.


contexts(ct, sequence = FALSE, reverse = FALSE, ...)



a context tree.


if TRUE the function returns its results as a data.frame, if FALSE (default) as a list of ctx_node objects. (see details)


logical (defaults to FALSE). See details.


additional arguments for the contexts function.


A list of class contexts containing the contexts represented in this tree (as ctx_node) or a data.frame.


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().


dts <- sample(as.factor(c("A", "B", "C")), 100, replace = TRUE)
dts_tree <- ctx_tree(dts, max_depth = 3, min_size = 5)
#> 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