https://github.com/cran/BDgraph
Tip revision: 3a6c731b58d1fb11c921ac7e82d90325385a4d78 authored by Reza Mohammadi on 17 April 2018, 13:25:21 UTC
version 2.46
version 2.46
Tip revision: 3a6c731
rgwish.R
## ----------------------------------------------------------------------------|
# Sampling from G-Wishart AND Wishart distribution
## ----------------------------------------------------------------------------|
# sampling from G-Wishart distribution
rgwish = function( n = 1, adj.g = NULL, b = 3, D = NULL )
{
if( b <= 2 ) stop( "In G-Wishart distribution parameter 'b' has to be more than 2" )
if( is.null( adj.g ) ) stop( "Adjacency matrix should be determined" )
G <- as.matrix( adj.g )
if( sum( ( G == 1 ) * ( G == 0 ) ) != 0 ) stop( "Elements of matrix G should be zero or one" )
if( !isSymmetric( G ) )
{
G[ lower.tri( G, diag( TRUE ) ) ] <- 0
G = G + t( G )
}
p <- nrow( G )
if( is.null(D) ) D <- diag(p)
if( dim( D )[1] != p ) stop( "Dimension of matrix G and D must to be the same." )
Ti = chol( solve( D ) )
K = matrix( 0, p, p )
if( n > 1 )
{
samples = array( 0, c( p, p, n ) )
for( i in 1 : n )
{
result = .C( "rgwish_c", as.integer(G), as.double(Ti), K = as.double(K), as.integer(b), as.integer(p), PACKAGE = "BDgraph" )
samples[ , , i] = matrix( result $ K, p, p )
}
}else{
result = .C( "rgwish_c", as.integer(G), as.double(Ti), K = as.double(K), as.integer(b), as.integer(p), PACKAGE = "BDgraph" )
samples = matrix( result $ K, p, p )
}
return( samples )
}