Create Symmetric Matrices, possibly covariance or correlation matrices, or check a matrix for symmetry and serviceability.
makeSymmetric.RdCheck X and do the right thing. If X is a matrix, check that it is a valid for the intended purpose (symmetric or correlation or covariance). If X a single value, use that to fill up a matrix. If it is a vector, try to use it as a vech to fill the lower triangle. If d is supplied as an integer, use that as desired size.
Arguments
- X
A single value, a vector (a vech), or a matrix
- d
Optional. An integer, the desired number of rows (or columns). Don't specify this argument if X is already a matrix. Only required if X is an integer and diag is not supplied. Otherwise, the function tries to deduce desired size of output from X (as a vech) and diag.
- diag
Values for the diagonal. This is important because it alters the way X is interpreted. If diag is not provided, then X is understood to include diagonal elements.
- corr
TRUE or FALSE: Should we construct a correlation matrix
- cov
TRUE or FALSE: Should this be a covariance matrix?
Author
Paul E. Johnson pauljohn@ku.edu
Examples
makeSymmetric(X = 3, d = 4)
#> [,1] [,2] [,3] [,4]
#> [1,] 3 3 3 3
#> [2,] 3 3 3 3
#> [3,] 3 3 3 3
#> [4,] 3 3 3 3
makeSymmetric(X = 3, d = 4, diag = c(99, 98, 97, 96))
#> [,1] [,2] [,3] [,4]
#> [1,] 99 3 3 3
#> [2,] 3 98 3 3
#> [3,] 3 3 97 3
#> [4,] 3 3 3 96
makeSymmetric(c(1,2,3))
#> [,1] [,2]
#> [1,] 1 2
#> [2,] 2 3
makeSymmetric(c(1,2,3), d = 5)
#> [,1] [,2]
#> [1,] 1 2
#> [2,] 2 3
makeSymmetric(c(0.8,0.4, 0.2), cov = TRUE)
#> [,1] [,2]
#> [1,] 0.8 0.4
#> [2,] 0.4 0.2
makeSymmetric(c(0.8,0.4, 0.2), cov = TRUE, diag = c(44, 55, 66))
#> [,1] [,2] [,3]
#> [1,] 44.0 0.8 0.4
#> [2,] 0.8 55.0 0.2
#> [3,] 0.4 0.2 66.0