# 3. Manipulating multiple signals

Various analyses involve working with multiple signals at once. The covidcast package provides some helper functions for fetching multiple signals from the API, and aggregating them into one data frame for various downstream uses.

## Fetching multiple signals

To load confirmed cases and deaths at the state level, in a single function call, we can use `covidcast_signals()` (note the plural form of “signals”):

    library(covidcast)

    start_day <- "2020-06-01"
    end_day <- "2020-10-01"

    signals <- covidcast_signals(data_source = "usa-facts",
                                 signal = c("confirmed_incidence_num",
                                            "deaths_incidence_num"),
                                 start_day = start_day, end_day = end_day,
                                 geo_type = "state")

    summary(signals[[1]])

    ## A `covidcast_signal` dataframe with 6273 rows and 15 columns.
    ##
    ## data_source : usa-facts
    ## signal      : confirmed_incidence_num
    ## geo_type    : state
    ##
    ## first date                          : 2020-06-01
    ## last date                           : 2020-10-01
    ## median number of geo_values per day : 51

    summary(signals[[2]])

    ## A `covidcast_signal` dataframe with 6273 rows and 15 columns.
    ##
    ## data_source : usa-facts
    ## signal      : deaths_incidence_num
    ## geo_type    : state
    ##
    ## first date                          : 2020-06-01
    ## last date                           : 2020-10-01
    ## median number of geo_values per day : 51

This returns a list of `covidcast_signal` objects. The argument structure for `covidcast_signals()` matches that of `covidcast_signal()`, except the first four arguments (`data_source`, `signal`, `start_day`, `end_day`) are allowed to be vectors. See the `covidcast_signals()` documentation for details.

## Aggregating signals, wide format

To aggregate multiple signals together, we can use the `aggregate_signals()` function, which accepts a list of `covidcast_signal` objects, as returned by `covidcast_signals()`. With all arguments set to their default values, `aggregate_signals()` returns a data frame in “wide” format:

    library(dplyr)

    aggregate_signals(signals) %>% head()

    ##   geo_value time_value value+0:usa-facts_confirmed_incidence_num
    ## 1        ne 2020-06-01                                       258
    ## 2        ne 2020-06-02                                       274
    ## 3        ne 2020-06-03                                       265
    ## 4        ne 2020-06-04                                       253
    ## 5        ne 2020-06-05                                       261
    ## 6        ne 2020-06-06                                       166
    ##   value+0:usa-facts_deaths_incidence_num
    ## 1                                      8
    ## 2                                      3
    ## 3                                      5
    ## 4                                      1
    ## 5                                      1
    ## 6                                      1

In “wide” format, only the latest issue of data is retained, and the columns `data_source`, `signal`, `issue`, `lag`, `stderr`, `sample_size` are all dropped from the returned data frame. Each unique signal—defined by a combination of data source name, signal name, and time-shift—is given its own column, whose name indicates its defining quantities.

As hinted above, `aggregate_signals()` can also apply time-shifts to the given signals, through the optional `dt` argument. This can be either be a single vector of shifts or a list of vectors of shifts, this list having the same length as the list of `covidcast_signal` objects (to apply, respectively, the same shifts or a different set of shifts to each `covidcast_signal` object). Negative shifts translate into in a lag value and positive shifts into a lead value; for example, if `dt = -1`, then the value on June 2 that gets reported is the original value on June 1; if `dt = 0`, then the values are left as is.

    aggregate_signals(signals, dt = c(-1, 0)) %>%
      filter(geo_value == "tx") %>% head()

    ##   geo_value time_value value-1:usa-facts_confirmed_incidence_num
    ## 1        tx 2020-06-01                                        NA
    ## 2        tx 2020-06-02                                       593
    ## 3        tx 2020-06-03                                      1688
    ## 4        tx 2020-06-04                                      1703
    ## 5        tx 2020-06-05                                      1649
    ## 6        tx 2020-06-06                                      1693
    ##   value+0:usa-facts_confirmed_incidence_num
    ## 1                                       593
    ## 2                                      1688
    ## 3                                      1703
    ## 4                                      1649
    ## 5                                      1693
    ## 6                                      1940
    ##   value-1:usa-facts_deaths_incidence_num value+0:usa-facts_deaths_incidence_num
    ## 1                                     NA                                     20
    ## 2                                     20                                     28
    ## 3                                     28                                     31
    ## 4                                     31                                     21
    ## 5                                     21                                     25
    ## 6                                     25                                     34

    aggregate_signals(signals, dt = list(0, c(-1, 0, 1))) %>%
      filter(geo_value == "tx") %>% head()

    ##   geo_value time_value value+0:usa-facts_confirmed_incidence_num
    ## 1        tx 2020-06-01                                       593
    ## 2        tx 2020-06-02                                      1688
    ## 3        tx 2020-06-03                                      1703
    ## 4        tx 2020-06-04                                      1649
    ## 5        tx 2020-06-05                                      1693
    ## 6        tx 2020-06-06                                      1940
    ##   value-1:usa-facts_deaths_incidence_num value+0:usa-facts_deaths_incidence_num
    ## 1                                     NA                                     20
    ## 2                                     20                                     28
    ## 3                                     28                                     31
    ## 4                                     31                                     21
    ## 5                                     21                                     25
    ## 6                                     25                                     34
    ##   value+1:usa-facts_deaths_incidence_num
    ## 1                                     28
    ## 2                                     31
    ## 3                                     21
    ## 4                                     25
    ## 5                                     34
    ## 6                                     18

Finally, `aggregate_signals()` also accepts a single data frame (instead of a list of data frames), intended to be convenient when applying shifts to a single `covidcast_signal` object:

    aggregate_signals(signals[[1]], dt = c(-1, 0, 1)) %>%
      filter(geo_value == "tx") %>% head()

    ##   geo_value time_value value-1:usa-facts_confirmed_incidence_num
    ## 1        tx 2020-06-01                                        NA
    ## 2        tx 2020-06-02                                       593
    ## 3        tx 2020-06-03                                      1688
    ## 4        tx 2020-06-04                                      1703
    ## 5        tx 2020-06-05                                      1649
    ## 6        tx 2020-06-06                                      1693
    ##   value+0:usa-facts_confirmed_incidence_num
    ## 1                                       593
    ## 2                                      1688
    ## 3                                      1703
    ## 4                                      1649
    ## 5                                      1693
    ## 6                                      1940
    ##   value+1:usa-facts_confirmed_incidence_num
    ## 1                                      1688
    ## 2                                      1703
    ## 3                                      1649
    ## 4                                      1693
    ## 5                                      1940
    ## 6                                      1425

## Aggregating signals, long format

We can also use `aggregate_signals()` in “long” format, with one observation per row:

    aggregate_signals(signals, format = "long") %>%
      filter(geo_value == "tx") %>% head()

    ##   data_source                  signal geo_value time_value    source geo_type
    ## 1   usa-facts confirmed_incidence_num        tx 2020-06-01 usa-facts    state
    ## 2   usa-facts confirmed_incidence_num        tx 2020-06-02 usa-facts    state
    ## 3   usa-facts confirmed_incidence_num        tx 2020-06-03 usa-facts    state
    ## 4   usa-facts confirmed_incidence_num        tx 2020-06-04 usa-facts    state
    ## 5   usa-facts confirmed_incidence_num        tx 2020-06-05 usa-facts    state
    ## 6   usa-facts confirmed_incidence_num        tx 2020-06-06 usa-facts    state
    ##   time_type      issue lag missing_value missing_stderr missing_sample_size
    ## 1       day 2021-10-20 506             0              5                   5
    ## 2       day 2021-10-20 505             0              5                   5
    ## 3       day 2021-10-20 504             0              5                   5
    ## 4       day 2021-10-20 503             0              5                   5
    ## 5       day 2021-10-20 502             0              5                   5
    ## 6       day 2021-10-20 501             0              5                   5
    ##   stderr sample_size dt value
    ## 1     NA          NA  0   593
    ## 2     NA          NA  0  1688
    ## 3     NA          NA  0  1703
    ## 4     NA          NA  0  1649
    ## 5     NA          NA  0  1693
    ## 6     NA          NA  0  1940

    aggregate_signals(signals, dt = c(-1, 0), format = "long") %>%
      filter(geo_value == "tx") %>% head()

    ##   data_source                  signal geo_value time_value    source geo_type
    ## 1   usa-facts confirmed_incidence_num        tx 2020-06-01 usa-facts    state
    ## 2   usa-facts confirmed_incidence_num        tx 2020-06-01 usa-facts    state
    ## 3   usa-facts confirmed_incidence_num        tx 2020-06-02 usa-facts    state
    ## 4   usa-facts confirmed_incidence_num        tx 2020-06-02 usa-facts    state
    ## 5   usa-facts confirmed_incidence_num        tx 2020-06-03 usa-facts    state
    ## 6   usa-facts confirmed_incidence_num        tx 2020-06-03 usa-facts    state
    ##   time_type      issue lag missing_value missing_stderr missing_sample_size
    ## 1       day 2021-10-20 506             0              5                   5
    ## 2       day 2021-10-20 506             0              5                   5
    ## 3       day 2021-10-20 505             0              5                   5
    ## 4       day 2021-10-20 505             0              5                   5
    ## 5       day 2021-10-20 504             0              5                   5
    ## 6       day 2021-10-20 504             0              5                   5
    ##   stderr sample_size dt value
    ## 1     NA          NA -1    NA
    ## 2     NA          NA  0   593
    ## 3     NA          NA -1   593
    ## 4     NA          NA  0  1688
    ## 5     NA          NA -1  1688
    ## 6     NA          NA  0  1703

    aggregate_signals(signals, dt = list(-1, 0), format = "long") %>%
      filter(geo_value == "tx") %>% head()

    ##   data_source                  signal geo_value time_value    source geo_type
    ## 1   usa-facts confirmed_incidence_num        tx 2020-06-01 usa-facts    state
    ## 2   usa-facts confirmed_incidence_num        tx 2020-06-02 usa-facts    state
    ## 3   usa-facts confirmed_incidence_num        tx 2020-06-03 usa-facts    state
    ## 4   usa-facts confirmed_incidence_num        tx 2020-06-04 usa-facts    state
    ## 5   usa-facts confirmed_incidence_num        tx 2020-06-05 usa-facts    state
    ## 6   usa-facts confirmed_incidence_num        tx 2020-06-06 usa-facts    state
    ##   time_type      issue lag missing_value missing_stderr missing_sample_size
    ## 1       day 2021-10-20 506             0              5                   5
    ## 2       day 2021-10-20 505             0              5                   5
    ## 3       day 2021-10-20 504             0              5                   5
    ## 4       day 2021-10-20 503             0              5                   5
    ## 5       day 2021-10-20 502             0              5                   5
    ## 6       day 2021-10-20 501             0              5                   5
    ##   stderr sample_size dt value
    ## 1     NA          NA -1    NA
    ## 2     NA          NA -1   593
    ## 3     NA          NA -1  1688
    ## 4     NA          NA -1  1703
    ## 5     NA          NA -1  1649
    ## 6     NA          NA -1  1693

As we can see, time-shifts work just as before, in “wide” format. However, in “long” format, all columns are retained, and an additional `dt` column is added to record the time-shift being used.

Just as before, `covidcast_signals()` can also operate on a single data frame, to conveniently apply shifts, in “long” format:

    aggregate_signals(signals[[1]], dt = c(-1, 0), format = "long") %>%
      filter(geo_value == "tx") %>% head()

    ##   data_source                  signal geo_value time_value    source geo_type
    ## 1   usa-facts confirmed_incidence_num        tx 2020-06-01 usa-facts    state
    ## 2   usa-facts confirmed_incidence_num        tx 2020-06-01 usa-facts    state
    ## 3   usa-facts confirmed_incidence_num        tx 2020-06-02 usa-facts    state
    ## 4   usa-facts confirmed_incidence_num        tx 2020-06-02 usa-facts    state
    ## 5   usa-facts confirmed_incidence_num        tx 2020-06-03 usa-facts    state
    ## 6   usa-facts confirmed_incidence_num        tx 2020-06-03 usa-facts    state
    ##   time_type      issue lag missing_value missing_stderr missing_sample_size
    ## 1       day 2021-10-20 506             0              5                   5
    ## 2       day 2021-10-20 506             0              5                   5
    ## 3       day 2021-10-20 505             0              5                   5
    ## 4       day 2021-10-20 505             0              5                   5
    ## 5       day 2021-10-20 504             0              5                   5
    ## 6       day 2021-10-20 504             0              5                   5
    ##   stderr sample_size dt value
    ## 1     NA          NA -1    NA
    ## 2     NA          NA  0   593
    ## 3     NA          NA -1   593
    ## 4     NA          NA  0  1688
    ## 5     NA          NA -1  1688
    ## 6     NA          NA  0  1703

## Pivoting longer or wider

The package also provides functions for pivoting an aggregated signal data frame longer or wider. These are essentially wrappers around `pivot_longer()` and `pivot_wider()` from the `tidyr` package, that set the column structure and column names appropriately. For example, to pivot longer:

    aggregate_signals(signals, dt = list(-1, 0)) %>%
      covidcast_longer() %>%
      filter(geo_value == "tx") %>% head()

    ##   data_source                  signal geo_value time_value dt value
    ## 1   usa-facts confirmed_incidence_num        tx 2020-06-01 -1    NA
    ## 2   usa-facts    deaths_incidence_num        tx 2020-06-01  0    20
    ## 3   usa-facts confirmed_incidence_num        tx 2020-06-02 -1   593
    ## 4   usa-facts    deaths_incidence_num        tx 2020-06-02  0    28
    ## 5   usa-facts confirmed_incidence_num        tx 2020-06-03 -1  1688
    ## 6   usa-facts    deaths_incidence_num        tx 2020-06-03  0    31

And to pivot wider:

    aggregate_signals(signals, dt = list(-1, 0), format = "long") %>%
      covidcast_wider() %>%
      filter(geo_value == "tx") %>% head()

    ##   geo_value time_value value-1:usa-facts_confirmed_incidence_num
    ## 1        tx 2020-06-01                                        NA
    ## 2        tx 2020-06-02                                       593
    ## 3        tx 2020-06-03                                      1688
    ## 4        tx 2020-06-04                                      1703
    ## 5        tx 2020-06-05                                      1649
    ## 6        tx 2020-06-06                                      1693
    ##   value+0:usa-facts_deaths_incidence_num
    ## 1                                     20
    ## 2                                     28
    ## 3                                     31
    ## 4                                     21
    ## 5                                     25
    ## 6                                     34

## A sanity check

Lastly, here’s a small sanity check, that lagging cases by 7 days using `aggregate_signals()` and correlating this with deaths using `covidcast_cor()` yields the same result as telling `covidcast_cor()` to do the time-shifting itself:

    df_cor1 <- covidcast_cor(x = aggregate_signals(signals[[1]], dt = -7,
                                                  format = "long"),
                            y = signals[[2]])

    df_cor2 <- covidcast_cor(x = signals[[1]], y = signals[[2]], dt_x = -7)
    identical(df_cor1, df_cor2)

    ## [1] TRUE