pkgdown/extra.css

Skip to contents

[Stable]

Takes a tbl_now or a data.frame and adds the temporal effects t_effect as columns

Usage

add_temporal_effects(x, t_effects = NULL, overwrite = FALSE, ...)

# S3 method for class 'data.frame'
add_temporal_effects(
  x,
  t_effects = NULL,
  overwrite = FALSE,
  ...,
  date_col = NULL,
  numeric_col = NULL,
  name_prefix = paste0(".", date_col),
  weekend_days = c("Sat", "Sun")
)

# S3 method for class 'tbl_now'
add_temporal_effects(
  x,
  t_effects = NULL,
  overwrite = FALSE,
  ...,
  date_type = "event_date",
  weekend_days = c("Sat", "Sun")
)

Arguments

x

A tbl_now object or a data.frame.

t_effects

A temporal_effects() object codifying the temporal effects to be used.

overwrite

If TRUE ignores that the columns already exist and overwrites them. If FALSE it throws an errors if the columns it is creating already exist (default).

...

Additional arguments (unused)

date_col

The column which contains the <Date> values from which effects will be calculated. This applies to all temporal_effects except for seasonal.

numeric_col

The column which contains the values from which the seasonal effects will be calculated. This applies only to seasonal effects. For date-related effects (such as month or day of the week) use date_col.

name_prefix

What preffix to add to the column names

weekend_days

A character or numeric vector defining weekend days.

  • Numeric: must be integers in 1-7 corresponding to lubridate::wday() when week_start = 1.

  • Character: any of c("Mon","Tuesday","wed",...) case-insensitive. Defaults to Saturday and Sunday (weekend_days = c("Sat", "Sun")).

date_type

Either event_date (default) or report_date to add temporal effects to those columns.

Value

A tbl_now or data.frame containing all of the effects as new columns.

Examples

data(denguedat)

# Get disease
disease_data <- tbl_now(denguedat,
  event_date = "onset_week",
  report_date = "report_week",
  strata = "gender"
)
#>  Identified data as <linelist-data> where each observation is a test.

# Add an effect for epidemiological week
disease_data <- disease_data |>
  add_temporal_effects(t_effects = temporal_effects(week_of_year = TRUE))

# Use the compute to calculate them
disease_data |> compute_temporal_effects()
#> # A tibble:  52,987 × 7
#> # Data type: "linelist"
#> # Frequency: Event: `weeks` | Report: `weeks`
#>    onset_week   report_week   gender   .event_num .report_num .delay
#>    <date>       <date>        <chr>         <dbl>       <dbl>  <dbl>
#>    [event_date] [report_date] [strata]      [...]       [...]  [...]
#>  1 1990-01-01   1990-01-01    Male              0           0      0
#>  2 1990-01-01   1990-01-01    Female            0           0      0
#>  3 1990-01-01   1990-01-01    Female            0           0      0
#>  4 1990-01-01   1990-01-08    Female            0           1      1
#>  5 1990-01-01   1990-01-08    Male              0           1      1
#>  6 1990-01-01   1990-01-15    Female            0           2      2
#>  7 1990-01-01   1990-01-15    Female            0           2      2
#>  8 1990-01-01   1990-01-15    Female            0           2      2
#>  9 1990-01-01   1990-01-22    Female            0           3      3
#> 10 1990-01-01   1990-01-08    Female            0           1      1
#> # ────────────────────────────────────────────────────────────────────────────────
#> # Now: 2010-12-20 | Event date: "onset_week" | Report date: "report_week"
#> # Strata: "gender"
#> # T. effects: [event_date] week_of_year
#> # T. effect cols: ".event_week_of_year"
#> # ────────────────────────────────────────────────────────────────────────────────
#> # ℹ 52,977 more rows
#> # ℹ 1 more variable: .event_week_of_year <int>

# Use replace to change them
disease_data |>
  replace_temporal_effects(t_effects = temporal_effects(seasons = 52))
#> # A tibble:  52,987 × 6
#> # Data type: "linelist"
#> # Frequency: Event: `weeks` | Report: `weeks`
#>    onset_week   report_week   gender   .event_num .report_num .delay
#>    <date>       <date>        <chr>         <dbl>       <dbl>  <dbl>
#>    [event_date] [report_date] [strata]      [...]       [...]  [...]
#>  1 1990-01-01   1990-01-01    Male              0           0      0
#>  2 1990-01-01   1990-01-01    Female            0           0      0
#>  3 1990-01-01   1990-01-01    Female            0           0      0
#>  4 1990-01-01   1990-01-08    Female            0           1      1
#>  5 1990-01-01   1990-01-08    Male              0           1      1
#>  6 1990-01-01   1990-01-15    Female            0           2      2
#>  7 1990-01-01   1990-01-15    Female            0           2      2
#>  8 1990-01-01   1990-01-15    Female            0           2      2
#>  9 1990-01-01   1990-01-22    Female            0           3      3
#> 10 1990-01-01   1990-01-08    Female            0           1      1
#> # ────────────────────────────────────────────────────────────────────────────────
#> # Now: 2010-12-20 | Event date: "onset_week" | Report date: "report_week"
#> # Strata: "gender"
#> # T. effects (lazy): [event_date] season(52)
#> # ────────────────────────────────────────────────────────────────────────────────
#> # ℹ 52,977 more rows

# Use remove to delete them
disease_data |> remove_temporal_effects()
#> # A tibble:  52,987 × 6
#> # Data type: "linelist"
#> # Frequency: Event: `weeks` | Report: `weeks`
#>    onset_week   report_week   gender   .event_num .report_num .delay
#>    <date>       <date>        <chr>         <dbl>       <dbl>  <dbl>
#>    [event_date] [report_date] [strata]      [...]       [...]  [...]
#>  1 1990-01-01   1990-01-01    Male              0           0      0
#>  2 1990-01-01   1990-01-01    Female            0           0      0
#>  3 1990-01-01   1990-01-01    Female            0           0      0
#>  4 1990-01-01   1990-01-08    Female            0           1      1
#>  5 1990-01-01   1990-01-08    Male              0           1      1
#>  6 1990-01-01   1990-01-15    Female            0           2      2
#>  7 1990-01-01   1990-01-15    Female            0           2      2
#>  8 1990-01-01   1990-01-15    Female            0           2      2
#>  9 1990-01-01   1990-01-22    Female            0           3      3
#> 10 1990-01-01   1990-01-08    Female            0           1      1
#> # ────────────────────────────────────────────────────────────────────────────────
#> # Now: 2010-12-20 | Event date: "onset_week" | Report date: "report_week"
#> # Strata: "gender"
#> # ────────────────────────────────────────────────────────────────────────────────
#> # ℹ 52,977 more rows