Skip to contents

Turns a core table (the long id, item, resp layout from irw_fetch()) into an item-by-item matrix in the layout of the IRW covariance family: one row per cell of the lower triangle, diagonal included, with columns stat ("cor" or "cov"), var1, var2, value and n (the number of people who answered both items). irw_fetch() with source = "cov" calls this on a core table, and irw_cov2matrix() turns the result into a square matrix.

Usage

irw_cov_compute(
  df,
  method = c("pearson", "spearman", "kendall", "polychoric", "tetrachoric"),
  type = c("cor", "cov"),
  use = c("pairwise", "complete"),
  max_items = 500,
  table = NULL
)

Arguments

df

A core IRW table in long format, with columns id, item and a numeric resp.

method

The association measure: "pearson" (default), "spearman", "kendall", "polychoric" or "tetrachoric". The last two need the psych package and give correlations only; "tetrachoric" needs every item dichotomous. Or a function of your own: it receives the wide person-by-item matrix (one column per item, NA where an item was not answered, after use is applied) and must return a square symmetric matrix with one row and column per item, in the same order. Say what it returns with type; n is still the pairwise count.

type

"cor" (default) for a correlation matrix or "cov" for a covariance matrix. "cov" is available for "pearson", "spearman" and "kendall".

use

How missing responses are handled: "pairwise" (default) uses, for each pair of items, everyone who answered both, and records that count in n; "complete" first drops anyone with a missing response, so n is the same everywhere. For polychoric and tetrachoric correlations, "pairwise" is what psych does internally.

max_items

Refuse tables with more items than this (default 500). An item bank of 27,000 items has 380 million pairs, almost all answered by nobody in common.

table

Optional table name, recorded in the irw_table attribute.

Value

A tibble with columns stat, var1, var2, value and n. A pair answered by fewer than three people in common has value NA.

Details

The result is computed, not published: its attributes irw_computed, irw_method, irw_type, irw_use and irw_table say how it was made, and irw_cov2matrix() carries them forward. Cite the source table, not the matrix.

Tables with repeated measures (more than one response per id and item, for example across waves) are refused: filter to one occasion first, e.g. df[df$wave == 1, ]. Items with no variation have no correlation and are dropped with a warning.

Examples

df <- data.frame(id = rep(1:50, each = 3), item = rep(c("a", "b", "c"), 50),
                 resp = c(replicate(50, sample(1:5, 3, replace = TRUE))))
irw_cov_compute(df)
#> # A tibble: 6 × 5
#>   stat  var1  var2   value     n
#>   <chr> <chr> <chr>  <dbl> <int>
#> 1 cor   a     a     1         50
#> 2 cor   b     a     0.0629    50
#> 3 cor   b     b     1         50
#> 4 cor   c     a     0.261     50
#> 5 cor   c     b     0.0343    50
#> 6 cor   c     c     1         50
irw_cov_compute(df, method = "spearman", type = "cov")
#> # A tibble: 6 × 5
#>   stat  var1  var2  value     n
#>   <chr> <chr> <chr> <dbl> <int>
#> 1 cov   a     a     202.     50
#> 2 cov   b     a      12.8    50
#> 3 cov   b     b     200.     50
#> 4 cov   c     a      56.3    50
#> 5 cov   c     b      14.1    50
#> 6 cov   c     c     201.     50
# Your own measure: here, a correlation shrunk towards zero
shrunk <- function(x) { r <- stats::cor(x, use = "pairwise.complete.obs"); 0.9 * r + 0.1 * diag(ncol(x)) }
irw_cov_compute(df, method = shrunk)
#> # A tibble: 6 × 5
#>   stat  var1  var2   value     n
#>   <chr> <chr> <chr>  <dbl> <int>
#> 1 cor   a     a     1         50
#> 2 cor   b     a     0.0566    50
#> 3 cor   b     b     1         50
#> 4 cor   c     a     0.235     50
#> 5 cor   c     b     0.0309    50
#> 6 cor   c     c     1         50