Learn R Programming

psychmeta (version 2.7.0)

reshape_vec2mat: Assemble a variance-covariance matrix

Description

The reshape_vec2mat function facilitates the creation of square correlation/covariance matrices from scalars or vectors of variances/covariances. It allows the user to supply a vector of covariances that make up the lower triangle of a matrix, determines the order of the matrix necessary to hold those covariances, and constructs a matrix accordingly.

Usage

reshape_vec2mat(
  cov = NULL,
  var = NULL,
  order = NULL,
  var_names = NULL,
  by_row = FALSE,
  diag = FALSE
)

Value

A variance-covariance matrix

Arguments

cov

Scalar or vector of covariance information to include the lower-triangle positions of the matrix (default value is zero). If a vector, the elements must be provided in the order associated with concatenated column (by_row = FALSE; default) or row (by_row = TRUE) vectors of the lower triangle of the desired matrix. If variances are included in these values, set the diag argument to TRUE.

var

Scalar or vector of variance information to include the diagonal positions of the matrix (default value is 1).

order

If cov and var are scalars, this argument determines the number of variables to create in the output matrix.

var_names

Optional vector of variable names.

by_row

Logical scalar indicating whether cov values should fill the lower triangle by row (TRUE) or by column (FALSE; default).

diag

Logical scalar indicating whether cov values include variances (FALSE by default; if TRUE, the variance values supplied with the cov argument will supersede the var argument).

Examples

Run this code
## Specify the lower triangle covariances
## Can provide names for the variables
reshape_vec2mat(cov = c(.3, .2, .4), var_names = c("x", "y", "z"))

## Specify scalar values to repeat for the covariances and variances
reshape_vec2mat(cov = .3, var = 2, order = 3)

## Give a vector of variances to create a diagonal matrix
reshape_vec2mat(var = 1:5)

## Specify order only to create identity matrix
reshape_vec2mat(order = 3)

## Specify order and scalar variance to create a scalar matrix
reshape_vec2mat(var = 2, order = 3)

## A quick way to make a 2x2 matrix for bivariate correlations
reshape_vec2mat(cov = .2)

Run the code above in your browser using DataLab