File: rmvnorm.R

package info (click to toggle)
r-cran-bdgraph 2.73%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,344 kB
  • sloc: cpp: 8,160; ansic: 157; makefile: 5
file content (39 lines) | stat: -rwxr-xr-x 1,945 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
#     Copyright (C) 2012 - 2021  Reza Mohammadi                                |
#                                                                              |
#     This file is part of BDgraph package.                                    |
#                                                                              |
#     BDgraph is free software: you can redistribute it and/or modify it under |
#     the terms of the GNU General Public License as published by the Free     |
#     Software Foundation; see <https://cran.r-project.org/web/licenses/GPL-3>.|
#                                                                              |
#     Maintainer: Reza Mohammadi <a.mohammadi@uva.nl>                          |
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
#     Data generator from multivarate normal distribution                      |
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |

rmvnorm = function( n = 10, mean = rep( 0, length = ncol( sigma ) ), 
                    sigma = diag( length( mean ) ) )
{
    if( !isSymmetric( sigma, tol = sqrt( .Machine $ double.eps ), check.attributes = FALSE ) ) 
        stop( "'sigma' must be a symmetric matrix" )
    
    sigma = as.matrix( sigma )
    p     = nrow( sigma )
    
    if( length( mean ) == 1 ) mean = rep( mean, p )
    if( length( mean ) != nrow( sigma ) ) stop( "'mean' and 'sigma' have non-conforming size" )
    
    # - - generate multivariate normal data - - - - - - - - - - - - - - - - - -|
    
    chol_sig = chol( sigma )
    z        = matrix( stats::rnorm( p * n ), p, n )
    data     = t( chol_sig ) %*% z + mean
    data     = t( data )
    
    if( n == 1 ) data = as.vector( data )
        
    return( data )
}
   
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |