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.1550966  0.01793093  0.2588510  2.75541758
#> [2,]  0.3314665  0.49230253  0.3914245 -1.91172049
#> [3,]  0.6199512  0.15379277 -0.2737470  0.01917759
#> [4,]  0.2308265 -0.72680109 -1.6965668  2.68255718
#> [5,] -0.1745365  0.50651305  1.0265451 -0.66508825

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