Skip to contents

Convert Data Between Formats

The package provides helper functions to convert data between hub format and various forecasting package formats:

Hub ↔︎ Fable/tsibble

Convert hub data to tsibble for fable modeling

## Loading required package: jsonlite
## Loading required package: remotes
## Loading required package: scales
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
##  dplyr     1.1.4      readr     2.1.5
##  forcats   1.0.1      stringr   1.5.2
##  ggplot2   4.0.0      tibble    3.3.0
##  lubridate 1.9.4      tidyr     1.3.1
##  purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
##  readr::col_factor() masks scales::col_factor()
##  purrr::discard()    masks scales::discard()
##  dplyr::filter()     masks stats::filter()
##  purrr::flatten()    masks jsonlite::flatten()
##  dplyr::lag()        masks stats::lag()
##  Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Registered S3 method overwritten by 'tsibble':
##   method               from 
##   as_tibble.grouped_df dplyr
## 
## Registered S3 method overwritten by 'epipredict':
##   method            from   
##   print.step_naomit recipes
hub_data <- data.frame(
  date = seq.Date(as.Date("2023-01-01"), by = "week", length.out = 20),
  location = "US",
  value = rnorm(20, 100, 10)
)

# Convert to tsibble format
ts_data <- convert_hub_to_tsibble(hub_data)

# Use with fable
library(fable)
## Loading required package: fabletools
## 
## Attaching package: 'tsibble'
## 
## The following object is masked from 'package:lubridate':
## 
##     interval
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, union
model <- ts_data %>% 
  model(arima = ARIMA(value))
## Warning: 1 error encountered for arima
## [1] The `feasts` package must be installed to use this functionality. It can be installed with install.packages("feasts")
# Convert fable forecasts back to hub format
forecasts <- model %>% forecast(h = 4)
hub_forecasts <- convert_fable_to_hub(forecasts, location = "US")

Hub ↔︎ EpiEstim

# Prepare data for EpiEstim
epiestim_data <- convert_hub_to_epiestim(
  hub_data, 
  location_filter = "US"
)

# Run EpiEstim (example)
library(EpiEstim)
res <- estimate_R(epiestim_data, method = "parametric_si",
                  config = make_config(list(mean_si = 7, std_si = 4.5)))

# Convert results back to hub format
hub_r_estimates <- convert_epiestim_to_hub(res, location = "US")

Hub ↔︎ EpiNow2

# Prepare data for EpiNow2
epinow2_data <- convert_hub_to_epinow2(
  hub_data,
  location_filter = "US"
)

# Run EpiNow2 (example)
library(EpiNow2)
res <- epinow(
  reported_cases = epinow2_data,
  generation_time = generation_time_opts(Generation_Time_Opts(...)),
  delays = delay_opts(Delay_Opts(...))
)

# Convert results back to hub format
hub_forecasts <- convert_epinow2_to_hub(res, location = "US")

Working with Hub Data

Converting to Fable Format

The package makes it easy to convert hub-formatted data to work with the fable forecasting package:

library(AMPHForecastSuite)

# Create sample hub data
hub_data <- data.frame(
  date = seq.Date(as.Date("2023-01-01"), by = "week", length.out = 20),
  location = "US",
  value = rnorm(20, 100, 10)
)

# Convert to tsibble format for fable
ts_data <- convert_hub_to_tsibble(hub_data)
print(ts_data)

Using Fable for Forecasting

Once your data is in tsibble format, you can use fable for forecasting:

library(fable)
library(tsibble)

# Fit a model
model <- ts_data %>% 
  model(
    arima = ARIMA(value),
    ets = ETS(value)
  )

# Generate forecasts
forecasts <- model %>% forecast(h = 4)

# Convert back to hub format
hub_forecasts <- convert_fable_to_hub(forecasts, location = "US", target = "inc")

Working with EpiEstim

EpiEstim is used to estimate time-varying reproduction numbers:

library(EpiEstim)

# Prepare daily case data
hub_data_daily <- data.frame(
  date = seq.Date(as.Date("2023-01-01"), by = "day", length.out = 50),
  location = "US",
  value = rpois(50, 100)
)

# Convert to EpiEstim format
epiestim_data <- convert_hub_to_epiestim(hub_data_daily, location_filter = "US")

# Estimate R
res <- estimate_R(
  epiestim_data, 
  method = "parametric_si",
  config = make_config(list(mean_si = 7, std_si = 4.5))
)

# Convert results back to hub format
hub_r_estimates <- convert_epiestim_to_hub(res, location = "US")
print(hub_r_estimates)

Working with EpiNow2

EpiNow2 provides more sophisticated estimation and forecasting with delay distributions:

library(EpiNow2)

# Prepare data for EpiNow2
epinow2_data <- convert_hub_to_epinow2(hub_data_daily, location_filter = "US")

# Run EpiNow2 (simplified example)
# Note: You would need to specify generation time and delay distributions
res <- epinow(
  reported_cases = epinow2_data,
  generation_time = generation_time_opts(
    Generation_Time_Opts(mean = 5, sd = 2)
  ),
  delays = delay_opts(
    Delay_Opts(mean = 3, sd = 1)
  )
)

# Convert results back to hub format
hub_forecasts <- convert_epinow2_to_hub(res, location = "US")
print(hub_forecasts)