Skip to contents

Create Covariance Matrix

Usage

covariance_matrix(diag, upper_tri)

Arguments

diag

Diagonal entries of the covariance matrix

upper_tri

Upper triangle entries of the matrix, specified column wise.

Value

A symmetric matrix with diag values on the main diagonal and upper_tri values in the lower and upper triangles.

Examples

m1 <- covariance_matrix(c(1, 1, 1, 1), c(.8, .3, .8, 0, 0, 0))
m1
#>      [,1] [,2] [,3] [,4]
#> [1,]  1.0  0.8  0.3    0
#> [2,]  0.8  1.0  0.8    0
#> [3,]  0.3  0.8  1.0    0
#> [4,]  0.0  0.0  0.0    1
mvtnorm::rmvnorm(5, mean = c(0, 0, 0, 0), sigma = m1)
#>            [,1]       [,2]        [,3]       [,4]
#> [1,] -0.2205092  0.6996618  1.40289207  1.9236931
#> [2,]  1.1888846  0.9338955 -0.11177033 -0.5986102
#> [3,] -0.1897136 -0.2969340  0.08322573 -0.2858696
#> [4,]  0.3280343 -1.2154025 -1.96403396  0.5905301
#> [5,]  1.1333199  0.2251879 -0.48732664  1.1038123

# No correlation
covariance_matrix(c(1, 2, 3))
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    0    2    0
#> [3,]    0    0    3