This vignette covers two complementary aspects of
mixvlmc: the representation of the discrete time series
used to estimate models, and the detailed exploration of the contexts of
fitted models. It assumes some familiarity with the notions introduced
in vignette("context-trees") (contexts, context trees,
contexts() with frequency, and the basic node
functions find_sequence(), is_context(),
children(), counts() and
positions()), in
vignette("variable-length-markov-chains") and in
vignette("covlmc").
Since version 0.3.0, mixvlmc represents discrete time
series by objects of class dts, created by the
dts() function. A dts stores the time series
together with its state space, i.e. the set of values that the
series can take. A dts can be built from a numeric,
character, factor or logical vector. When the state space is not
specified, it is computed from the vector in a way that depends on its
type. For numeric and character vectors, the state space consists of the
unique values of the series, in increasing order:
x_num <- dts(c(3, 1, 2, 2, 1, 3))
x_num
#> Discrete time series of length 6 with state space 1, 2, 3
#> 3 1 2 2 1 ...
states(x_num)
#> [1] 1 2 3
x_chr <- dts(c("b", "a", "c", "a", "b"))
states(x_chr)
#> [1] "a" "b" "c"For a factor, the state space is given by the levels of the factor, including levels that do not appear in the series, and in the order of the levels:
x_fct <- dts(factor(c("low", "high", "low"),
levels = c("low", "medium", "high")
))
x_fct
#> Discrete time series of length 3 with state space low, medium, high
#> low high lowFor a logical vector, the state space is always
c(FALSE, TRUE):
By default, only the first 5 values of the series are printed, but
this can be changed with the n parameter of
print():
set.seed(0)
x_long <- dts(sample(c("A", "B", "C"), 50, replace = TRUE))
x_long
#> Discrete time series of length 50 with state space A, B, C
#> B A C A B ...
print(x_long, n = 15)
#> Discrete time series of length 50 with state space A, B, C
#> B A C A B A C C B B C C A A A ...is_dts() tests whether an object is a discrete time
series, states() returns its state space and
dts_data() recovers the original series as a vector (of the
same type as the state space):
is_dts(x_chr)
#> [1] TRUE
is_dts(c("b", "a", "c"))
#> [1] FALSE
dts_data(x_chr)
#> [1] "b" "a" "c" "a" "b"
dts_data(x_fct)
#> [1] low high low
#> Levels: low medium highThe length of a dts is given by length()
and a dts can be subsetted with [. The result
is a dts with the same state space, even if some states do
not appear in the subset:
length(x_long)
#> [1] 50
x_long[1:2]
#> Discrete time series of length 2 with state space A, B, C
#> B ADiscrete time series cannot contain missing values:
The state space can be given explicitly with the vals
parameter of dts(). This has two uses. First, it sets the
order of the states, which is the order used by all the functions of the
package (for instance to order the columns of the results of
contexts() or predict(), or the probabilities
displayed by draw()). Second, the state space can include
states that do not appear in the series:
x_abc <- dts(c("b", "a", "b"), vals = c("c", "b", "a"))
x_abc
#> Discrete time series of length 3 with state space c, b, a
#> b a bWhen vals is given, dts() checks that the
series only contains values of the state space:
All the estimation functions of the package (ctx_tree(),
vlmc(), covlmc(), tune_vlmc() and
tune_covlmc()) accept either a plain vector or a
dts. In the former case, the vector is converted with
dts() using the default state space. Using a
dts directly is useful mainly when the state space of the
model cannot be deduced reliably from the series used to estimate it, a
common situation when several series are analysed with the same state
space.
Let us consider the household electricity consumption data set
powerconsumption. As in vignette("covlmc"),
the active power is discretised into three levels. The following
function extracts the discretised series of some weeks of the data set
(as a character vector):
power_levels <- c("low", "typical", "high")
power_weeks <- function(weeks) {
pc <- powerconsumption[powerconsumption$week %in% weeks, ]
as.character(cut(pc$active_power,
breaks = c(0, 0.4, 2, 8),
labels = power_levels
))
}
week_33 <- power_weeks(33)
week_35 <- power_weeks(35)
table(week_33)
#> week_33
#> low typical
#> 925 83
table(week_35)
#> week_35
#> high low typical
#> 28 891 89The consumption is very low during week 33, in August (probably
because of holidays), and the high level is never observed.
A model estimated on this week from the character vector has therefore
only two states and it cannot be used on week 35:
model_33_raw <- vlmc(week_33)
states(model_33_raw)
#> [1] "low" "typical"
try(loglikelihood(model_33_raw, newdata = week_35))
#> Error : x contains unknown statesIf we specify the state space with a dts, the model
knows that the high level exists:
week_33_dts <- dts(week_33, vals = power_levels)
model_33 <- vlmc(week_33_dts)
model_33
#> VLMC context tree on low, typical, high [C++]
#> cutoff: 2.996 (quantile: 0.05)
#> Number of contexts: 42
#> Maximum context length: 24
loglikelihood(model_33, newdata = week_35)
#> 'log Lik.' -Inf (df= 84, nb obs.= 984, initial="truncated")The log likelihood is now computed, and its value, \(-\infty\), tells us that week 35 is
impossible according to the model of week 33, as the high
level was never observed during that week. The predicted probabilities
include all the states of the state space, in the specified order (the
last row is the prediction of the state that follows the series, see
predict.vlmc()):
predict(model_33, newdata = week_35[1:5], type = "probs")
#> low typical high
#> [1,] 0.9176587 0.08234127 0
#> [2,] 0.9155844 0.08441558 0
#> [3,] 0.9088757 0.09112426 0
#> [4,] 0.9009126 0.09908735 0
#> [5,] 0.8942029 0.10579710 0
#> [6,] 0.8847403 0.11525974 0Notice that the new data (newdata in
loglikelihood() and predict(),
init in simulate() or the sequence given to
find_sequence()) can be given as a plain vector or as a
dts. In both cases, it is interpreted in the state space of
the model: a dts with a different state space is accepted
provided its values belong to the state space of the model. For
instance, the following call computes the log likelihood of week 34, in
which the high level is also absent, with the model of week
33:
week_34_dts <- dts(power_weeks(34))
states(week_34_dts)
#> [1] "low" "typical"
loglikelihood(model_33, newdata = week_34_dts)
#> 'log Lik.' -201.5814 (df= 84, nb obs.= 984, initial="truncated")When the series is a factor, its levels define the state space, so
discretising directly with cut() (without the conversion to
character used above) also gives a model with three states:
states(vlmc(factor(week_33, levels = power_levels)))
#> [1] low typical high
#> Levels: low typical highFinally, simulate() does not return a dts
but a vector of the same type as the state space of the model (here a
character vector), whose values are taken from this state space. It can
be converted into a dts with the same state space as
follows:
sim_33 <- simulate(model_33, 20, seed = 0)
sim_33
#> [1] "low" "low" "low" "low" "typical" "low" "low"
#> [8] "low" "low" "low" "low" "low" "low" "low"
#> [15] "low" "low" "low" "low" "typical" "low"
dts(sim_33, vals = states(model_33))
#> Discrete time series of length 20 with state space low, typical, high
#> low low low low typical ...In the rest of this vignette, we use a VLMC estimated on week 5 of
the electricity consumption data set. We keep the positions of the
contexts in the time series with keep_match = TRUE (they
are not kept by default). The model is estimated with the default C++
backend (see vlmc()), but all the functions described below
work in the same way with the R backend
(backend = "R").
week_5 <- dts(power_weeks(5), vals = power_levels)
model_5 <- vlmc(week_5, keep_match = TRUE)
model_5
#> VLMC context tree on low, typical, high [C++]
#> cutoff: 2.996 (quantile: 0.05)
#> Number of contexts: 26
#> Maximum context length: 9
draw(model_5)
#> ▪ (0.1667, 0.5496, 0.2837)
#> ├─ low (0.7665, 0.2335, 0)
#> │ └─ low (0.7638, 0.2362, 0)
#> │ └─ low (0.7917, 0.2083, 0)
#> │ └─ low (0.8133, 0.1867, 0)
#> │ ├─ low (0.9167, 0.08333, 0)
#> │ │ └─ low (0.9091, 0.09091, 0)
#> │ │ └─ low (0.9, 0.1, 0)
#> │ │ └─ low (0.8889, 0.1111, 0)
#> │ │ └─ typical (0.4, 0.6, 0)
#> │ └─ typical (0.4, 0.6, 0)
#> ├─ typical (0.0704, 0.8466, 0.08303)
#> │ ├─ low (0.3846, 0.5385, 0.07692)
#> │ ├─ typical (0.04691, 0.8849, 0.06823)
#> │ │ └─ typical (0.04337, 0.8892, 0.06747)
#> │ │ └─ low (0.2941, 0.6471, 0.05882)
#> │ └─ high (0.04444, 0.7111, 0.2444)
#> └─ high (0.003497, 0.1573, 0.8392)
#> ├─ typical (0.02174, 0.2826, 0.6957)
#> │ └─ typical (0.03125, 0.3125, 0.6562)
#> │ └─ high (0.3333, 0.6667, 0)
#> └─ high (0, 0.1333, 0.8667)
#> └─ high (0, 0.1106, 0.8894)
#> └─ high (0, 0.0973, 0.9027)
#> └─ high (0, 0.1018, 0.8982)
#> └─ high (0, 0.1, 0.9)
#> └─ high (0, 0.1037, 0.8963)
#> └─ typical (0, 0.3571, 0.6429)contexts() gives two representations of the contexts of
a model. By default, it returns a list of ctx_node objects,
one per context, which can be explored with the node functions described
in the next section:
ctxs_5 <- contexts(model_5)
ctxs_5
#> Contexts:
#> typical, high, high, high, high, high, high, high
#> high, high, high, high, high, high, high
#> high, high, high, high, high, high
#> high, high, high, high, high
#> high, high, high, high
#> high, high, high
#> high, high
#> high, typical, typical, high
#> typical, typical, high
#> typical, high
#> high
#> low, typical
#> low, typical, typical, typical
#> typical, typical, typical
#> typical, typical
#> high, typical
#> typical, low, low, low, low
#> typical, low, low, low, low, low, low, low, low
#> low, low, low, low, low, low, low, low
#> low, low, low, low, low, low, low
#> low, low, low, low, low, low
#> low, low, low, low, low
#> low, low, low, low
#> low, low, low
#> low, low
#> lowWith sequence = TRUE, or as soon as additional
information is requested (see below), the function returns a data frame
with one row per context. Its first column, context, is a
list: each element is a vector of states (of the same type as
states(model_5)) that represents a context. As the default
printing of such a column is truncated, we define a small helper
function that replaces each context by a character string, only for
display purposes:
flat_contexts <- function(ctx_df) {
ctx_df$context <- vapply(ctx_df$context, paste, character(1), collapse = ", ")
ctx_df
}
ctx_df_5 <- contexts(model_5, sequence = TRUE)
ctx_df_5$context[[1]]
#> [1] "typical" "high" "high" "high" "high" "high" "high"
#> [8] "high"Contexts are written by default in temporal order (the most recent
state is on the right), while reverse = TRUE uses the
reverse order of Bühlmann and Wyner (the most recent state is on the
left), which corresponds to the paths from the root in the
representation of draw():
| context |
|---|
| high, high, high, high, high, high, high, typical |
| high, high, high, high, high, high, high |
| high, high, high, high, high, high |
| high, high, high, high, high |
| high, high, high, high |
| high, high, high |
The parameter reverse has the same effect on the list
representation and can also be given to the print()
function of a list of contexts
(print(ctxs_5, reverse = TRUE)).
The additional columns depend on the parameters of
contexts() and on the class of the model, as summarised
below (contexts() accepts the same parameters for both
backends of ctx_tree() and vlmc(), but the
order of the contexts may differ from one backend to the other).
| parameter | ctx_tree |
vlmc |
covlmc |
columns |
|---|---|---|---|---|
frequency |
yes | yes | yes | freq, one per state if "detailed" |
positions |
yes | yes | yes | positions |
local |
no | yes | yes | (modifies the counts) |
cutoff |
no | yes | no | cutoff |
metrics |
no | yes | yes | accuracy, auc |
model |
no | no | yes | coef or model |
hsize |
no | no | yes | hsize |
merging |
no | no | yes | merged |
As for context trees, frequency = "total" adds a
freq column with the number of occurrences of each context
in the time series (not including a possible final occurrence not
followed by any state), while frequency = "detailed" adds
in addition one column per state with the number of times the context is
followed by this state:
| context | freq | low | typical | high |
|---|---|---|---|---|
| typical, high, high, high, high, high, high, high | 14 | 0 | 5 | 9 |
| high, high, high, high, high, high, high | 135 | 0 | 14 | 121 |
| high, high, high, high, high, high | 150 | 0 | 15 | 135 |
| high, high, high, high, high | 167 | 0 | 17 | 150 |
| high, high, high, high | 185 | 0 | 18 | 167 |
| high, high, high | 208 | 0 | 23 | 185 |
| high, high | 240 | 0 | 32 | 208 |
| high, typical, typical, high | 3 | 1 | 2 | 0 |
| typical, typical, high | 32 | 1 | 10 | 21 |
| typical, high | 46 | 1 | 13 | 32 |
| high | 286 | 1 | 45 | 240 |
| low, typical | 39 | 15 | 21 | 3 |
| low, typical, typical, typical | 17 | 5 | 11 | 1 |
| typical, typical, typical | 415 | 18 | 369 | 28 |
| typical, typical | 469 | 22 | 415 | 32 |
| high, typical | 45 | 2 | 32 | 11 |
| typical, low, low, low, low | 15 | 6 | 9 | 0 |
| typical, low, low, low, low, low, low, low, low | 5 | 2 | 3 | 0 |
| low, low, low, low, low, low, low, low | 45 | 40 | 5 | 0 |
| low, low, low, low, low, low, low | 50 | 45 | 5 | 0 |
| low, low, low, low, low, low | 55 | 50 | 5 | 0 |
| low, low, low, low, low | 60 | 55 | 5 | 0 |
| low, low, low, low | 75 | 61 | 14 | 0 |
| low, low, low | 96 | 76 | 20 | 0 |
| low, low | 127 | 97 | 30 | 0 |
| low | 167 | 128 | 39 | 0 |
These counts are the ones used to estimate the transition
probabilities of the model shown by draw(): for instance,
the probabilities associated with the context low are
obtained by dividing the counts of the low row by its
freq value.
In a VLMC, a context can be the suffix of longer contexts. For
instance, high, high is the last part of
high, high, high. The counts above include all the
occurrences of high, high, including the ones that are the
last part of an occurrence of high, high, high. With
local = TRUE, the counts include only the occurrences of
the context that are not the last part of a longer context, that is the
time steps at which the context is the one actually used by the model to
predict the next state:
| context | freq | low | typical | high |
|---|---|---|---|---|
| typical, high, high, high, high, high, high, high | 14 | 0 | 5 | 9 |
| high, high, high, high, high, high, high | 121 | 0 | 9 | 112 |
| high, high, high, high, high, high | 15 | 0 | 1 | 14 |
| high, high, high, high, high | 17 | 0 | 2 | 15 |
| high, high, high, high | 18 | 0 | 1 | 17 |
| high, high, high | 23 | 0 | 5 | 18 |
| high, high | 32 | 0 | 9 | 23 |
| high, typical, typical, high | 3 | 1 | 2 | 0 |
| typical, typical, high | 29 | 0 | 8 | 21 |
| typical, high | 14 | 0 | 3 | 11 |
| high | 0 | 0 | 0 | 0 |
| low, typical | 39 | 15 | 21 | 3 |
| low, typical, typical, typical | 17 | 5 | 11 | 1 |
| typical, typical, typical | 398 | 13 | 358 | 27 |
| typical, typical | 54 | 4 | 46 | 4 |
| high, typical | 45 | 2 | 32 | 11 |
| typical, low, low, low, low | 15 | 6 | 9 | 0 |
| typical, low, low, low, low, low, low, low, low | 5 | 2 | 3 | 0 |
| low, low, low, low, low, low, low, low | 40 | 38 | 2 | 0 |
| low, low, low, low, low, low, low | 5 | 5 | 0 | 0 |
| low, low, low, low, low, low | 5 | 5 | 0 | 0 |
| low, low, low, low, low | 5 | 5 | 0 | 0 |
| low, low, low, low | 0 | 0 | 0 | 0 |
| low, low, low | 21 | 15 | 6 | 0 |
| low, low | 31 | 21 | 10 | 0 |
| low | 40 | 31 | 9 | 0 |
Each time step is associated with at most one context, so that the
sum of the local counts, 1006, is smaller than the length of the time
series, 1008 (the first time steps do not have a long enough past and
the last state is not followed by any state). Some contexts have zero
local counts. For instance, the context high exists only
because the model must define a next state distribution after the
sequence low, high, which never appears in the time series:
the context high is used only for this unobserved sequence,
while all the actual occurrences of high are covered by
longer contexts.
For a VLMC, cutoff adds a cutoff column
that summarises, for each context, the likelihood ratio test that
compares the next state distribution of the context with the one of its
parent, the context obtained by removing its oldest state. With
cutoff = "native" the value is the log likelihood ratio,
while cutoff = "quantile" gives it in the quantile scale
(the p-value of the test). See cutoff(),
prune(),
vignette("variable-length-markov-chains") and
vignette("model-selection") for details on these two
scales.
| context | cutoff |
|---|---|
| typical, high, high, high, high, high, high, high | 0.0410956 |
| high, high, high, high, high, high, high | 0.9898744 |
| high, high, high, high, high, high | 0.9973425 |
| high, high, high, high, high | 0.9811938 |
| high, high, high, high | 0.8419314 |
| high, high, high | 0.6121980 |
| high, high | 0.2441270 |
| high, typical, typical, high | 0.0205994 |
| typical, typical, high | 0.8660173 |
| typical, high | 0.0320983 |
| high | 0.0000000 |
| low, typical | 0.0000001 |
| low, typical, typical, typical | 0.0026391 |
| typical, typical, typical | 0.9391320 |
| typical, typical | 0.0430851 |
| high, typical | 0.0046179 |
| typical, low, low, low, low | 0.0019297 |
| typical, low, low, low, low, low, low, low, low | 0.0313613 |
| low, low, low, low, low, low, low, low | 0.9705409 |
| low, low, low, low, low, low, low | 0.9760067 |
| low, low, low, low, low, low | 0.9800842 |
| low, low, low, low, low | 0.0784120 |
| low, low, low, low | 0.8961669 |
| low, low, low | 0.8082117 |
| low, low | 0.9974477 |
| low | 0.0000000 |
The model was estimated with the default alpha = 0.05
and a context with a quantile value lower than alpha is
significantly different from its parent. Some contexts have larger
values: they are kept in the model because one of their descendants in
the context tree is significant (for instance a context
low, low, low, low, low, low, low, low is kept because its
extension typical, low, low, low, low, low, low, low, low
is significant). Notice that those values are not those reported by
cutoff(model_5) (see the dedicated section in the
documentation of contexts.vlmc()).
With metrics = TRUE, contexts() adds
predictive quality metrics for each context, computed on the time steps
at which the context is used by the model (i.e. the ones counted by
local = TRUE):
| context | freq | accuracy | auc |
|---|---|---|---|
| typical, high, high, high, high, high, high, high | 14 | 0.6428571 | NA |
| high, high, high, high, high, high, high | 121 | 0.9256198 | NA |
| high, high, high, high, high, high | 15 | 0.9333333 | NA |
| high, high, high, high, high | 17 | 0.8823529 | NA |
| high, high, high, high | 18 | 0.9444444 | NA |
| high, high, high | 23 | 0.7826087 | NA |
| high, high | 32 | 0.7187500 | NA |
| high, typical, typical, high | 3 | 0.6666667 | NA |
| typical, typical, high | 29 | 0.7241379 | NA |
| typical, high | 14 | 0.7857143 | NA |
| high | 0 | NaN | NA |
| low, typical | 39 | 0.5384615 | NA |
| low, typical, typical, typical | 17 | 0.6470588 | NA |
| typical, typical, typical | 398 | 0.8994975 | NA |
| typical, typical | 54 | 0.8518519 | NA |
| high, typical | 45 | 0.7111111 | NA |
| typical, low, low, low, low | 15 | 0.6000000 | NA |
| typical, low, low, low, low, low, low, low, low | 5 | 0.6000000 | NA |
| low, low, low, low, low, low, low, low | 40 | 0.9500000 | NA |
| low, low, low, low, low, low, low | 5 | 1.0000000 | NA |
| low, low, low, low, low, low | 5 | 1.0000000 | NA |
| low, low, low, low, low | 5 | 1.0000000 | NA |
| low, low, low, low | 0 | NaN | NA |
| low, low, low | 21 | 0.7142857 | NA |
| low, low | 31 | 0.6774194 | NA |
| low | 40 | 0.7750000 | NA |
The accuracy column gives the proportion of those time
steps at which the most probable state according to the context is the
observed next state (it is not defined when the local count is zero).
For a VLMC, the predicted probabilities are identical for all the time
steps of a context, so that they cannot separate the next states: the
auc column is therefore always missing. It is informative
for a VLMC with covariates, see below. The metrics of the whole model
are given by metrics(model_5) (see
vignette("prediction")).
With positions = TRUE, a positions column
gives for each context the list of its positions in the time series. A
position of a context is the index \(t\) of its last state, so that \(x_{t+1}\) is the state that follows the
context. Positions are available only if they have been kept during the
estimation (keep_match = TRUE for vlmc(),
keep_position = TRUE, the default, for
ctx_tree(), always for covlmc()). As the
positions are numerous, the column is better explored
programmatically:
ctx_pos_5 <- contexts(model_5, frequency = "total", positions = TRUE)
head(ctx_pos_5$positions[[1]])
#> [1] 51 140 222 274 308 335Positions cover all the occurrences of a context, not only the local
ones. Unlike the counts, they include a possible final occurrence of the
context, at the end of the time series, which is not followed by any
state. Here the series ends with a sequence of low states,
so the last time step, 1008, is a position of several contexts, which
have therefore one more position than their count:
at_end <- sapply(ctx_pos_5$positions, function(pos) length(week_5) %in% pos)
ctx_pos_end <- flat_contexts(ctx_pos_5[at_end, c("context", "freq")])
ctx_pos_end$nb_positions <- lengths(ctx_pos_5$positions[at_end])
ctx_pos_end| context | freq | nb_positions | |
|---|---|---|---|
| 22 | low, low, low, low, low | 60 | 61 |
| 23 | low, low, low, low | 75 | 76 |
| 24 | low, low, low | 96 | 97 |
| 25 | low, low | 127 | 128 |
| 26 | low | 167 | 168 |
We use now the data of vignette("covlmc"): weeks 15 and
16 of the electricity consumption data set, with a covariate that
indicates whether the observation is made during the day or during the
night.
elec_dts <- dts(power_weeks(c(15, 16)), vals = power_levels)
pc_15_16 <- powerconsumption[powerconsumption$week %in% c(15, 16), ]
elec_cov <- data.frame(day = (pc_15_16$hour >= 7 & pc_15_16$hour <= 18))
elec_model <- covlmc(elec_dts, elec_cov)
elec_model
#> VLMC with covariates context tree on low, typical, high
#> cutoff in quantile scale: 0.05
#> Number of contexts: 7
#> Maximum context length: 3
draw(elec_model)
#> ▪
#> ├─ low ([ -1.681 • 0.4984
#> │ -4.277 • -0.8766 ])
#> ├─ typical
#> │ ├─ low (0.3667, 0.5833, 0.05)
#> │ ├─ typical
#> │ │ ├─ low (0.4058, 0.5507, 0.04348)
#> │ │ ├─ typical ([ 1.792 • -14.02 ⁞ -3.253 ⁞ 18.34
#> │ │ │ -0.2877 • 16.01 ⁞ -36.08 ⁞ 20.01 ])
#> │ │ └─ high (0.05172, 0.8276, 0.1207)
#> │ └─ high (0.02817, 0.8169, 0.1549)
#> └─ high (0, 0.1864, 0.8136)contexts() supports for COVLMC the
frequency, local, positions and
metrics parameters with the same meaning as for a VLMC. As
the transition probabilities of a COVLMC depend on the covariates, the
auc column is now informative: for a context whose model
uses the covariates, it measures how well the probabilities computed by
its model separate the possible next states. For a context whose model
does not use the covariates, the predictions are constant and the AUC is
0.5 (or missing when some states never follow the context). The
parameter hsize adds a column that gives the number of past
values of the covariates used by the model of each context (0 for a
context whose model does not use the covariates).
| context | freq | low | typical | high | hsize | accuracy | auc |
|---|---|---|---|---|---|---|---|
| low | 659 | 533 | 120 | 6 | 1 | 0.8088012 | 0.5653481 |
| low, typical | 120 | 44 | 70 | 6 | 0 | 0.5833333 | 0.5000000 |
| low, typical, typical | 69 | 28 | 38 | 3 | 0 | 0.5507246 | 0.5000000 |
| typical, typical, typical | 655 | 49 | 568 | 38 | 3 | 0.8702290 | 0.6152189 |
| high, typical, typical | 58 | 3 | 48 | 7 | 0 | 0.8275862 | 0.5000000 |
| high, typical | 71 | 2 | 58 | 11 | 0 | 0.8169014 | 0.5000000 |
| high | 381 | 0 | 71 | 310 | 0 | 0.8136483 | NA |
The models themselves can be included in the data frame, either as
their coefficients (model = "coef") or as the R objects
produced by the estimation engine (model = "full", see
covlmc() and the article Computing
engines and control of VLMC with covariates for details about the
engines). As for contexts, the corresponding column is a list:
ctx_coef <- contexts(elec_model, model = "coef")
ctx_coef$coef[[1]]
#> [,1] [,2]
#> [1,] -1.681411 0.4984117
#> [2,] -4.276666 -0.8766255Finally, when the state space has at least three states, some contexts of a COVLMC can be merged: they share a single model. This happens for instance with a stricter cut off:
elec_model_merged <- covlmc(elec_dts, elec_cov, alpha = 1e-7)
draw(elec_model_merged)
#> ▪
#> ├─ low (0.8088, 0.1821, 0.009105)
#> ├─ typical
#> │ ├─ typical
#> │ │ ├─ low (0.4058, 0.5507, 0.04348)
#> │ │ ├─ typical (0.07481, 0.8672, 0.05802)
#> │ │ └─ high (0.05172, 0.8276, 0.1207)
#> │ └─ low, high (0.2408, 0.6702, 0.08901)
#> └─ high (0, 0.1864, 0.8136)In this representation (see the article Drawing
context trees and models), the leaf low, high denotes
the two merged contexts low, typical and
high, typical. contexts() reports merged
contexts individually; merging = TRUE adds a
merged column that identifies them:
| context | freq | accuracy | auc | merged |
|---|---|---|---|---|
| low | 659 | 0.8088012 | 0.5 | FALSE |
| low, typical, typical | 69 | 0.5507246 | 0.5 | FALSE |
| typical, typical, typical | 655 | 0.8671756 | 0.5 | FALSE |
| high, typical, typical | 58 | 0.8275862 | 0.5 | FALSE |
| low, typical | 120 | 0.6701571 | 0.5 | TRUE |
| high, typical | 71 | 0.6701571 | 0.5 | TRUE |
| high | 381 | 0.8136483 | NA | FALSE |
As merged contexts share their model, they share also their metrics, computed on all the time steps of the merged contexts.
Using the node functions, we can easily compute summary information about the contexts of a VLMC. The following function gives, for a context, its length, the most probable next state with its probability and the number of time steps at which the context is used by the model:
context_summary <- function(node) {
freqs <- counts(node)
probs <- unlist(freqs[, -1]) / freqs$total
data.frame(
context = paste(as_sequence(node), collapse = ", "),
length = length(as_sequence(node)),
next_state = names(probs)[which.max(probs)],
probability = max(probs),
usage = counts(node, frequency = "total", local = TRUE)
)
}
summary_5 <- do.call(rbind, lapply(contexts(model_5), context_summary))We look for the contexts whose next state is almost deterministic (with a probability of at least 0.9) and that are actually used, ordered by decreasing length:
predictable <- summary_5[summary_5$probability >= 0.9 & summary_5$usage > 0, ]
predictable[order(predictable$length, decreasing = TRUE), ]| context | length | next_state | probability | usage | |
|---|---|---|---|---|---|
| 20 | low, low, low, low, low, low, low | 7 | low | 0.9000000 | 5 |
| 3 | high, high, high, high, high, high | 6 | high | 0.9000000 | 15 |
| 21 | low, low, low, low, low, low | 6 | low | 0.9090909 | 5 |
| 22 | low, low, low, low, low | 5 | low | 0.9166667 | 5 |
| 5 | high, high, high, high | 4 | high | 0.9027027 | 18 |
Long sequences of the low level are likely to be
followed by another low observation. The positions of the
corresponding contexts can be related to other information available in
the data set, for instance the hour of the day. Let us consider the
longest context made only of low states:
all_low <- function(node) all(as_sequence(node) == "low")
low_ctxs <- Filter(all_low, contexts(model_5))
low_lengths <- sapply(low_ctxs, function(node) length(as_sequence(node)))
longest_low <- low_ctxs[[which.max(low_lengths)]]
longest_low
#> Context [T]: low, low, low, low, low, low, low, low
#> followed by low (40), typical (5), high (0)
pc_5 <- powerconsumption[powerconsumption$week == 5, ]
table(hour = pc_5$hour[positions(longest_low)])
#> hour
#> 2 3 4 5 6 7 8 17 21
#> 2 6 6 11 12 5 1 1 1As expected, those long periods of low consumption happen mostly during the night and the early morning.
In a COVLMC, the dependency of the transition probabilities on the covariates is context specific. The contexts that actually use the covariates are obtained as follows:
uses_covariate <- function(node) covariate_memory(node) > 0
uses_cov <- Filter(uses_covariate, contexts(elec_model))
uses_cov
#> [[1]]
#> Context [T]: low
#> followed by low (533), typical (120), high (6)
#>
#> [[2]]
#> Context [T]: typical, typical, typical
#> followed by low (49), typical (568), high (38)To understand the effect of the covariate, we can for instance compare the empirical distribution of the next state during the day and during the night, using the positions of each context (the value of the covariate at the position of a context is the most recent one available to its model):
next_state_by_day <- function(node) {
pos <- positions(node)
## remove a possible final occurrence
pos <- pos[pos < length(elec_dts)]
prop.table(table(
day = elec_cov$day[pos],
next_state = factor(dts_data(elec_dts)[pos + 1], levels = power_levels)
), margin = 1)
}
next_state_by_day(uses_cov[[1]])
#> next_state
#> day low typical high
#> FALSE 0.833333333 0.155092593 0.011574074
#> TRUE 0.762114537 0.233480176 0.004405286After a low observation, the next observation is more
likely to stay low at night than during the day. The
comparison with a context whose model does not use the covariate shows
smaller differences:
next_state_by_day(find_sequence(elec_model, "high"))
#> next_state
#> day low typical high
#> FALSE 0.0000000 0.1737089 0.8262911
#> TRUE 0.0000000 0.2023810 0.7976190Finally, the predictive metrics of the contexts that use the
covariates show that their AUC is larger than 0.5, the value obtained by
a context whose model does not use the covariates (see the
auc column of contexts() for
elec_model above):