Create a correlation matrix
genCorMat(nvars, cors = NULL, rho = NULL, corstr = "cs", nclusters = 1)
number of rows and columns (i.e. number of variables) for correlation matrix. It can be a scalar or vector (see details).
vector of correlations.
Correlation coefficient, -1 <= rho <= 1
. Use if corMatrix is not provided. It can
be a scalar or vector (see details).
Correlation structure. Options include "cs" for a compound symmetry structure, "ar1" for an autoregressive structure of order 1, "arx" for an autoregressive structure that has a general decay pattern, and "structured" that imposes a prescribed pattern between observation based on distance (see details).
An integer that indicates the number of matrices that will be generated.
A single correlation matrix of size nvars x nvars
, or a list of matrices of potentially
different sizes with length indicated by nclusters
.
This function can generate correlation matrices randomly or deterministically,
depending on the combination of arguments provided. A single matrix will be
generated when nclusters == 1
(the default), and a list of matrices of matrices will be generated when
nclusters > 1
.
If the vector `cors` is specified with length `nvars - 1` then `corstr` must be "structured". If
`cors` is specified with length `choose(nvars, 2)` then `corstr` should not be specified as
"structured". In this case the `cors` vector should be interpreted as the lower triangle of the correlation
matrix, and is specified by reading down the columns. For example, if CM is the correlation matrix and
nvars = 3
, then CM[2,1] = CM[1,2] = cors[1]
, CM[3,1] = CM[1,3] = cors[2]
,
and CM[3,2] = CM[2,3] = cors[3]
.
If the vector cors
and rho
are not specified, random correlation matrices are generated
based on the specified corstr
. If the structure is "arx", then a random vector of
length nvars - 1
is randomly generated and sorted in descending order; the correlation matrix
will be generated base on this set of structured correlations. If the structure is not specified
as "arx" then a random positive definite of dimensions nvars x nvars with no structural
assumptions is generated.
If cors
is not specified but rho
is specified, then a matrix with either a "cs" or "ar1"
structure is generated.
If nclusters > 1
, nvars
can be of length 1 or nclusters
. If it is of length 1,
each cluster will have correlation matrices with the same dimension. Likewise, if nclusters > 1
,
rho
can be of length 1 or nclusters
. If length of rho
is 1,
each cluster will have correlation matrices with the same autocorrelation.
genCorMat(nvars = 3, cors = c(.3, -.2, .1))
#> [,1] [,2] [,3]
#> [1,] 1.0 0.3 -0.2
#> [2,] 0.3 1.0 0.1
#> [3,] -0.2 0.1 1.0
genCorMat(nvars = 3)
#> [,1] [,2] [,3]
#> [1,] 1.00000000 0.34882294 -0.04048001
#> [2,] 0.34882294 1.00000000 -0.03448154
#> [3,] -0.04048001 -0.03448154 1.00000000
genCorMat(nvars = 4, c(.3, -.2, .1, .2, .5, .2))
#> [,1] [,2] [,3] [,4]
#> [1,] 1.0 0.3 -0.2 0.1
#> [2,] 0.3 1.0 0.2 0.5
#> [3,] -0.2 0.2 1.0 0.2
#> [4,] 0.1 0.5 0.2 1.0
genCorMat(4)
#> [,1] [,2] [,3] [,4]
#> [1,] 1.000000000 -0.03444972 -0.21452382 -0.005877583
#> [2,] -0.034449723 1.00000000 0.12050698 -0.144038321
#> [3,] -0.214523816 0.12050698 1.00000000 -0.043593674
#> [4,] -0.005877583 -0.14403832 -0.04359367 1.000000000
genCorMat(nvars = 4, cors = c(.3, .2, .1), corstr = "structured")
#> [,1] [,2] [,3] [,4]
#> [1,] 1.0 0.3 0.2 0.1
#> [2,] 0.3 1.0 0.3 0.2
#> [3,] 0.2 0.3 1.0 0.3
#> [4,] 0.1 0.2 0.3 1.0
genCorMat(nvars = 4, corstr = "arx")
#> [,1] [,2] [,3] [,4]
#> [1,] 1.0000000 0.4718393 0.2530470 0.1303249
#> [2,] 0.4718393 1.0000000 0.4718393 0.2530470
#> [3,] 0.2530470 0.4718393 1.0000000 0.4718393
#> [4,] 0.1303249 0.2530470 0.4718393 1.0000000
genCorMat(nvars = 4, rho = .4, corstr = "cs")
#> [,1] [,2] [,3] [,4]
#> [1,] 1.0 0.4 0.4 0.4
#> [2,] 0.4 1.0 0.4 0.4
#> [3,] 0.4 0.4 1.0 0.4
#> [4,] 0.4 0.4 0.4 1.0
genCorMat(nvars = 4, rho = .4, corstr = "ar1")
#> [,1] [,2] [,3] [,4]
#> [1,] 1.000 0.40 0.16 0.064
#> [2,] 0.400 1.00 0.40 0.160
#> [3,] 0.160 0.40 1.00 0.400
#> [4,] 0.064 0.16 0.40 1.000
genCorMat(nvars = c(3, 2, 5), rho = c(.4, .8, .7), corstr = "ar1", nclusters = 3)
#> $`1`
#> [,1] [,2] [,3]
#> [1,] 1.00 0.4 0.16
#> [2,] 0.40 1.0 0.40
#> [3,] 0.16 0.4 1.00
#>
#> $`2`
#> [,1] [,2]
#> [1,] 1.0 0.8
#> [2,] 0.8 1.0
#>
#> $`3`
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.0000 0.700 0.49 0.343 0.2401
#> [2,] 0.7000 1.000 0.70 0.490 0.3430
#> [3,] 0.4900 0.700 1.00 0.700 0.4900
#> [4,] 0.3430 0.490 0.70 1.000 0.7000
#> [5,] 0.2401 0.343 0.49 0.700 1.0000
#>