Skip to contents

Rebuilds the square matrix from a table in the IRW covariance layout (from irw_fetch(..., source = "cov") or irw_cov_compute()), with what an SEM fit needs alongside it.

Usage

irw_cov2matrix(df, group = NULL, complete = FALSE)

Arguments

df

A table with columns stat, var1, var2, value, n and, optionally, group.

group

For a table holding several samples (a group column), which one to return. Required when there is more than one.

complete

A published matrix can hold cells the source did not report: rows with an empty value, which become NA here, with a warning. Most fitting functions (lavaan among them) refuse a matrix with NA. complete = TRUE returns the complete part instead: the matrix after dropping the fewest variables that leaves no blank cell (ties keep the variables listed first), with the dropped variables in dropped. The IRW's own record (irw_metadata(source = "cov"): n_missing_cells, and is_pd judged on the complete part) uses the same rule.

Value

A list: matrix (square, with dimnames), type ("cor" or "cov"), n (the largest cell n, the matrix N when n is the same everywhere), n_pairwise (a matrix of cell n), sd, mean and rel (named vectors of standard deviations, means and reliabilities, empty when the table has none), dropped (the variables complete = TRUE left out; empty otherwise), and computed (TRUE when the matrix was computed from an IRW table rather than published, with method, use and table then set).

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))))
m <- irw_cov2matrix(irw_cov_compute(df))
m$matrix
#>             a           b          c
#> a  1.00000000 -0.06804574  0.2362722
#> b -0.06804574  1.00000000 -0.1669792
#> c  0.23627216 -0.16697921  1.0000000
# lavaan::cfa("f =~ a + b + c", sample.cov = m$matrix, sample.nobs = m$n)