Skip to contents

This is a flexible function that allows lazy R programmers to create covariance matrix. The user may be lazy because the correlation and standard deviation infomation may be supplied in a variety of formats.

Usage

lazyCov(Rho, Sd, d)

Arguments

Rho

Required. May be a single value (correlation common among all variables), a vector of the lower triangular values (vech) of a correlation matrix, or a symmetric matrix of correlation coefficients.

Sd

Required. May be a single value (standard deviation common among all variables) or a vector of standard deviations, one for each variable.

d

Optional. Number of rows or columns. lazyCov may be able to deduce the required dimension of the final matrix from the input. However, when the user supplies only a single value for both Rho and Sd, d is necessary.

Value

covariance matrix.

Examples

##correlation 0.8 for all pairs, standard deviation 1.0 of each
lazyCov(Rho = 0.8, Sd = 1.0, d = 3)
#>      [,1] [,2] [,3]
#> [1,]  1.0  0.8  0.8
#> [2,]  0.8  1.0  0.8
#> [3,]  0.8  0.8  1.0
## supply a vech (lower triangular values in a column)
lazyCov(Rho = c(0.1, 0.2, 0.3), Sd = 1.0)
#>      [,1] [,2] [,3]
#> [1,]  1.0  0.1  0.2
#> [2,]  0.1  1.0  0.3
#> [3,]  0.2  0.3  1.0
## supply vech with different standard deviations
lazyCov(Rho = c(0.1, 0.2, 0.3), Sd = c(1.0, 2.2, 3.3))
#>      [,1]  [,2]   [,3]
#> [1,] 1.00 0.220  0.660
#> [2,] 0.22 4.840  2.178
#> [3,] 0.66 2.178 10.890
newRho <- lazyCor(c(0.5, 0.6, 0.7, -0.1, 0.1, 0.2))
lazyCov(Rho = newRho, Sd = 1.0)
#>      [,1] [,2] [,3] [,4]
#> [1,]  1.0  0.5  0.6  0.7
#> [2,]  0.5  1.0 -0.1  0.1
#> [3,]  0.6 -0.1  1.0  0.2
#> [4,]  0.7  0.1  0.2  1.0
lazyCov(Rho = newRho, Sd = c(3, 4, 5, 6))
#>      [,1] [,2] [,3] [,4]
#> [1,]  9.0  6.0    9 12.6
#> [2,]  6.0 16.0   -2  2.4
#> [3,]  9.0 -2.0   25  6.0
#> [4,] 12.6  2.4    6 36.0