epi_cor: Compute correlations between variables in an 'epi_df' object

View source: R/correlation.R

epi_corR Documentation

Compute correlations between variables in an epi_df object

Description

Computes correlations between variables in an epi_df object, allowing for grouping by geo value, time value, or any other variables. See the correlation vignette for examples.

Usage

epi_cor(
  x,
  var1,
  var2,
  dt1 = 0,
  dt2 = 0,
  shift_by = geo_value,
  cor_by = geo_value,
  use = "na.or.complete",
  method = c("pearson", "kendall", "spearman")
)

Arguments

x

The epi_df object under consideration.

var1, var2

The variables in x to correlate.

dt1, dt2

Time shifts to consider for the two variables, respectively, before computing correlations. Negative shifts translate into in a lag value and positive shifts into a lead value; for example, if dt = -1, then the new value on June 2 is the original value on June 1; if dt = 1, then the new value on June 2 is the original value on June 3; if dt = 0, then the values are left as is. Default is 0 for both dt1 and dt2.

shift_by

The variables(s) to group by, for the time shifts. The default is geo_value. However, we could also use, for example, shift_by = c(geo_value, age_group), assuming x has a column age_group, to perform time shifts per geo value and age group. To omit a grouping entirely, use cor_by = NULL. Note that the grouping here is always undone before the correlation computations.

cor_by

The variable(s) to group by, for the correlation computations. If geo_value, the default, then correlations are computed for each geo value, over all time; if time_value, then correlations are computed for each time, over all geo values. A grouping can also be any specified using number of columns of x; for example, we can use cor_by = c(geo_value, age_group), assuming x has a column age_group, in order to compute correlations for each pair of geo value and age group. To omit a grouping entirely, use cor_by = NULL. Note that the grouping here is always done after the time shifts.

use, method

Arguments to pass to cor(), with "na.or.complete" the default for use (different than cor()) and "pearson" the default for method (same as cor()).

Value

An tibble with the grouping columns first (geo_value, time_value, or possibly others), and then a column cor, which gives the correlation.

Examples


# linear association of case and death rates on any given day
epi_cor(
  x = cases_deaths_subset,
  var1 = case_rate_7d_av,
  var2 = death_rate_7d_av,
  cor_by = "time_value"
)

# correlation of death rates and lagged case rates
epi_cor(
  x = cases_deaths_subset,
  var1 = case_rate_7d_av,
  var2 = death_rate_7d_av,
  cor_by = time_value,
  dt1 = -2
)

# correlation grouped by location
epi_cor(
  x = cases_deaths_subset,
  var1 = case_rate_7d_av,
  var2 = death_rate_7d_av,
  cor_by = geo_value
)

# correlation grouped by location and incorporates lagged cases rates
epi_cor(
  x = cases_deaths_subset,
  var1 = case_rate_7d_av,
  var2 = death_rate_7d_av,
  cor_by = geo_value,
  dt1 = -2
)

cmu-delphi/epiprocess documentation built on Oct. 29, 2024, 5:37 p.m.