knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.align = "center", fig.path = "man/figures/README-", echo = TRUE, fig.width = 8, fig.height = 6 )
The trendbreaker package implements tools for detecting changes in temporal trends of a single response variable. It implements the Automatic Selection of Models and Outlier Detection for Epidemmics (ASMODEE), an algorithm originally designed for detecting changes in COVID-19 case incidence.
ASMODEE proceeds by:
The algorithm can be applied with fixed, user-specified value of k, so as to monitor potential changes in this recent time period. Alternatively, the optimal value of k can be determined automatically.
Disclaimer: this is work in progress. Please reach out to the authors before using this package. Also note this package may soon be renamed to avoid clashes with other projects and reflect a more general scope.
Once it is released on CRAN, you will be able to install the stable version of the package with:
install.packages("trendbreaker")
The development version can be installed from GitHub with:
if (!require(remotes)) { install.packages("remotes") } remotes::install_github("reconhub/trendbreaker")
The best place to start for using this package is to read the documentation of
the function asmodee
and run its example:
library(trendbreaker) ?asmodee example(asmodee)
We illustrate ASMODEE using publicly available NHS pathways data recording
self-reporting of potential COVID-19 cases in England
(see ?nhs_pathways_covid19
for more information).
library(trendbreaker) # for ASMODEE library(dplyr) # for data manipulation library(future) plan("multisession") # load data data(nhs_pathways_covid19) # select last 6 weeks of data first_date <- max(nhs_pathways_covid19$date, na.rm = TRUE) - 6*7 pathways_recent <- nhs_pathways_covid19 %>% filter(date >= first_date) # define candidate models models <- list( regression = lm_model(count ~ day), poisson_constant = glm_model(count ~ 1, family = "poisson"), negbin_time = glm_nb_model(count ~ day), negbin_time_weekday = glm_nb_model(count ~ day + weekday) ) # analyses on all data counts_overall <- pathways_recent %>% group_by(date, day, weekday) %>% summarise(count = sum(count)) # results with fixed 'k' = 7 res <- asmodee( counts_overall, models, k = 7, date_index = "date", method = evaluate_aic, simulate_pi = TRUE ) res plot(res, "date")
ASMODEE would typically be more useful to investigate shifts in temporal trends from a large number of time series (e.g. at a fine geographic scale). To make this sort of analysis easier trendbreaker also works with incidence2 objects. To illustrate this we can consider trends over NHS regions.
library(incidence2) # for `incidence()` objects # select last 6 weeks of data first_date <- max(nhs_pathways_covid19$date, na.rm = TRUE) - 6*7 pathways_recent <- filter(nhs_pathways_covid19, date >= first_date) # create incidence object with extra variables lookup <- select(pathways_recent, date, day, weekday) %>% distinct() dat <- pathways_recent %>% incidence(date_index = date, groups = nhs_region, count = count) %>% left_join(lookup, by = c("date_index" = "date")) # define candidate models models <- list( regression = lm_model(count ~ day), poisson_constant = glm_model(count ~ 1, family = "poisson"), negbin_time = glm_nb_model(count ~ day), negbin_time_weekday = glm_nb_model(count ~ day + weekday) ) # analyses on all data res <- asmodee(dat, models, method = evaluate_aic, k = 7) plot(res)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.