Introduction

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = "center",
  fig.width = 7,
  fig.height = 5
)

trending aims to provides a coherent interface to several modelling tools. Whilst it is useful in an interactive context, it's main focus is to provide an intuitive interface on which other packages can be developed (e.g. trendbreaker).

Main features

*   Requires brms

Example usage

Setup

library(outbreaks)  # for data
library(trending)   # for trend fitting
library(dplyr)      # for data manipulation

# load data
data(covid19_england_nhscalls_2020)

# select 6 weeks of data (from a period when the prevalence was decreasing)
last_date <- as.Date("2020-05-28")
first_date <- last_date - 8*7
pathways_recent <-
  covid19_england_nhscalls_2020 %>%
  filter(date >= first_date, date <= last_date) %>%
  group_by(date, day, weekday) %>%
  summarise(count = sum(count), .groups = "drop")

# split data for fitting and prediction
dat <-
  pathways_recent %>%
  group_by(date <= first_date + 6*7) %>%
  group_split()

fitting_data <- dat[[2]]
pred_data <- select(dat[[1]], date, day, weekday)

A succesful model fit

(model  <- glm_nb_model(count ~ day + weekday))
(fitted_model <- fit(model, fitting_data))
fitted_model %>% get_result()

# default
fitted_model %>% 
  predict(pred_data) %>%
  get_result()

# without uncertainty
fitted_model %>% 
  predict(pred_data, uncertain = FALSE) %>% 
  get_result()

# without prediction intervals
fitted_model %>% 
  predict(pred_data, add_pi = FALSE) %>% 
  get_result()

# bootstraped prediction intervals
fitted_model %>% 
  predict(pred_data, simulate_pi = TRUE) %>% 
  get_result()

Example of failed model fitting

(model2  <- glm_nb_model(count ~ day + nonexistent))
(fitted_model2 <- fit(model2, fitting_data))
get_result(fitted_model2)
get_errors(fitted_model2)

Multiple models

The fit function also works with a list input of multiple models.

models  <- list(
  simple = lm_model(count ~ day),
  glm_poisson = glm_model(count ~ day, family = "poisson"),
  glm_negbin = glm_nb_model(count ~ day + weekday),
  will_error = glm_nb_model(count ~ day + nonexistant)
)
(fitted_tbl <- fit(models, fitting_data))
get_result(fitted_tbl)

This can also then be used with predict()

(pred <- predict(fitted_tbl, pred_data))
get_result(pred)


Try the trending package in your browser

Any scripts or data that you put into this service are public.

trending documentation built on April 4, 2023, 1:07 a.m.