Skip to content

Commit

Permalink
Add a new counting mode for contexts.*
Browse files Browse the repository at this point in the history
In (co)vlmc models, a content may be valid and still have "descendants" in the context tree. The default reporting mode in contexts.* for those contexts is to count all occurences in the time series, even if some of them are matched by longer contexts. With counts="local", this default behaviour is replaced yb one in which only proper matches are attached to a given context.
  • Loading branch information
fabrice-rossi committed Aug 30, 2022
1 parent c60a441 commit 6f3c319
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
13 changes: 10 additions & 3 deletions R/covlmc_contexts.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ covlmc_context_extractor <- function(path, ct, vals, control, is_leaf, p_summary
#' @param hsize if TRUE, adds a `hsize` column to the result data frame that
#' gives for each context the size of the history of covariates used by the model.
#' @param ct a fitted covlmc model.
#' @param counts specifies how the counts reported by `frequency` are computed.
#' The default value `"desc"` includes both counts that are specific to the
#' context (if any) and counts from the descendants of the context in the
#' tree. When `counts = "local"` the counts include only the number of times
#' the context appears without being the last part of a longer context.
#' @details The default result for `type="auto"` (or `type="list"`),
#' `frequency=NULL` and `model=NULL` is the list of all contexts.
#'
Expand All @@ -112,9 +117,11 @@ covlmc_context_extractor <- function(path, ct, vals, control, is_leaf, p_summary
#' contexts(m_cov, model = "coef")
#' contexts(m_cov, model = "full")
#' @export
contexts.covlmc <- function(ct, type = c("auto", "list", "data.frame"), reverse = TRUE, frequency = NULL, model = NULL, hsize = FALSE, ...) {
contexts.covlmc <- function(ct, type = c("auto", "list", "data.frame"), reverse = TRUE, frequency = NULL,
counts = c("desc", "local"), model = NULL, hsize = FALSE, ...) {
type <- match.arg(type)
if (is.null(model) && !hsize) {
counts <- match.arg(counts)
if (is.null(model) && !hsize && counts == "desc") {
NextMethod()
} else {
assertthat::assert_that(type %in% c("auto", "data.frame"))
Expand All @@ -124,7 +131,7 @@ contexts.covlmc <- function(ct, type = c("auto", "list", "data.frame"), reverse
if (!is.null(model)) {
assertthat::assert_that(model %in% c("coef", "full"))
}
control <- list(frequency = frequency, model = model, hsize = hsize)
control <- list(frequency = frequency, counts = counts, model = model, hsize = hsize)
preres <- contexts_extractor(ct, reverse, covlmc_context_extractor, control, no_summary)
rownames(preres) <- 1:nrow(preres)
preres
Expand Down
11 changes: 11 additions & 0 deletions R/ctx_tree_contexts.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ frequency_context_extractor <-
res <-
cbind(res, data.frame(freq_by_val, check.names = FALSE))
}
if (!is_leaf && isTRUE(control$counts == "local")) {
## remove sub counts
for (child in ct$children) {
if (!is.null(child[["f_by"]])) {
res$freq <- res$freq - sum(child[["f_by"]])
if (control$frequency == "detailed") {
res[, -(1:2)] <- res[, -(1:2)] - child[["f_by"]]
}
}
}
}
res
}
} else {
Expand Down
13 changes: 10 additions & 3 deletions R/vlmc_contexts.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ vlmc_context_extractor <-
#' `cutoff=NULL` does not include those values. Setting `cutoff` to `quantile`
#' adds the cut off values in quantile scale, while `cutoff="native"` adds
#' them in the native scale.
#' @param counts specifies how the counts reported by `frequency` are computed.
#' The default value `"desc"` includes both counts that are specific to the
#' context (if any) and counts from the descendants of the context in the
#' tree. When `counts = "local"` the counts include only the number of times
#' the context appears without being the last part of a longer context.
#' @details The default result for `type="auto"` (or `type="list"`),
#' `frequency=NULL` and `cutoff=NULL` is the list of all contexts.
#'
Expand Down Expand Up @@ -56,9 +61,11 @@ vlmc_context_extractor <-
#' contexts(model, frequency = "total")
#' contexts(model, cutoff = "quantile")
#' @export
contexts.vlmc <- function(ct, type = c("auto", "list", "data.frame"), reverse = TRUE, frequency = NULL, cutoff = NULL, ...) {
contexts.vlmc <- function(ct, type = c("auto", "list", "data.frame"), reverse = TRUE, frequency = NULL,
counts = c("desc", "local"), cutoff = NULL, ...) {
type <- match.arg(type)
if (is.null(cutoff)) {
counts <- match.arg(counts)
if (is.null(cutoff) && counts == "desc") {
NextMethod()
} else {
assertthat::assert_that(type %in% c("auto", "data.frame"))
Expand All @@ -68,7 +75,7 @@ contexts.vlmc <- function(ct, type = c("auto", "list", "data.frame"), reverse =
if (!is.null(cutoff)) {
assertthat::assert_that(cutoff %in% c("quantile", "native"))
}
control <- list(frequency = frequency, p_value = !is.null(cutoff))
control <- list(frequency = frequency, counts = counts, p_value = !is.null(cutoff))
preres <- contexts_extractor(ct, reverse, vlmc_context_extractor, control, vlmc_parent_summary)
if (!is.null(cutoff)) {
if ((cutoff == "quantile")) {
Expand Down
7 changes: 7 additions & 0 deletions man/contexts.covlmc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions man/contexts.vlmc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f3c319

Please sign in to comment.