estimate_Rt: Estimate R(t) from cumulative case time series

View source: R/estimate_Rt.R

estimate_RtR Documentation

Estimate R(t) from cumulative case time series

Description

Given a case-tracking dataset, determine the basic reproduction index over time.

Usage

estimate_Rt(
  df,
  filter_expression,
  cases_column = "count",
  date_column = "date",
  method,
  config,
  cumulative = TRUE,
  estimation_family = "epiestim",
  invert = FALSE,
  quiet = TRUE,
  ...
)

Arguments

df

a data.frame containing at least a date column and a cases column, describing the cumulative cases at each date. Note that dates MUST NOT REPEAT and the function will check that this is the case. Use the filter_expression parameter to limit the data.frame to achieve non-duplicated dates from the typical case-tracking datasets in sars2pack.

filter_expression

a dplyr::filter expression, applied directly to the data.frame prior to calculating Rt. This is a useful way to write a one-liner from any of the case-tracking datasets.

cases_column

character(1) name of (cumulative) cases column in the input data.frame

date_column

character(1) name of date column in the input data.frame

method

character(1) passed to EpiEstim::estimate_R()

config

list() passed to EpiEstim::make_config(). Typically used to set up the serial interval distribution.

cumulative

logical(1) whether the case counts are cumulative (TRUE) or are daily incidence (FALSE)

estimation_family

One of epiestim

invert

Unused default FALSE, but if TRUE, returns 1/R(t) or related estimate, useful for plotting, since we are often most interested in looking at R(t) near or below 1.

quiet

logical(1) whether or not to provide messages, etc.

...

passed to the estimation method

Value

For the epiestim method, returns a data.frame with columns: - Mean(R) - Std(R) - Quantile.0.025(R) - Quantile.0.05(R) - Quantile.0.25(R) - Median(R) - Quantile.0.75(R) - Quantile.0.95(R) - Quantile.0.975(R)" - date_start - date_end

See Also

Other analysis: bulk_estimate_Rt()

Other case-tracking: align_to_baseline(), beoutbreakprepared_data(), bulk_estimate_Rt(), combined_us_cases_data(), coronadatascraper_data(), covidtracker_data(), ecdc_data(), jhu_data(), nytimes_county_data(), owid_data(), plot_epicurve(), test_and_trace_data(), usa_facts_data(), who_cases()

Examples

nyt = nytimes_state_data()
head(nyt)

nystate_Rt = estimate_Rt(
    nyt,
    filter_expression = state=='New York' & subset=='confirmed',
    estimation_family='epiestim',
    cumulative=TRUE,
    method = 'parametric_si',
    config = list(mean_si=3.96, std_si=4.75))
head(nystate_Rt)

library(ggplot2)
p = ggplot(nystate_Rt, aes(x=date_start,y=`Mean(R)`)) + geom_line()
p
p + geom_ribbon(aes(ymin=`Quantile.0.05(R)`, ymax=`Quantile.0.95(R)`), alpha=0.5)

# plot 1/Rt to expand region around 1 since that is typically what
# is most interesting with respect to controls

p = ggplot(nystate_Rt, aes(x=date_start,y=1/`Mean(R)`)) + geom_line()
p
p + geom_ribbon(aes(ymax=1/`Quantile.0.05(R)`, ymin=1/`Quantile.0.95(R)`), alpha=0.5)

# and simple loess smoothing
p + geom_smooth()


# super-cool use of tidyr, purrr, and dplyr to perform 
# calculations over all states:
## Not run: 
library(dplyr)
library(tidyr)
est_by = function(df) {
        estimate_Rt(
        df,
        estimation_family='epiestim',
        cumulative=TRUE,
        method = 'parametric_si',
        config = list(mean_si=3.96, std_si=4.75))
    }
z = nyt %>% dplyr::filter(subset=='confirmed') %>% tidyr::nest(-state) %>%
    dplyr::mutate(rt_df = purrr::map(data, est_by)) %>% tidyr::unnest(cols=rt_df)
p = ggplot(z,aes(x=date_start,y=1/`Mean(R)`, color=state)) + 
     ylim(c(0.5,1.25)) + 
     geom_smooth(se = FALSE)
p
library(plotly)
ggplotly(p)

## End(Not run)


seandavi/sars2pack documentation built on May 13, 2022, 3:41 p.m.