ssgraph with simple example

The R package ssgraph is designed for Bayesian structure learning in graphical models using spike-and-slab priors. To speed up the computations, the computationally intensive tasks of the package are implemented in C++ in parallel using OpenMP.

Install ssgraph using

install.packages( "ssgraph" )

First, we install ssgraph

library( ssgraph )

Example

This is a simple example to see the performance of the package for the Gaussian graphical models. First, by using the function bdgraph.sim(), we simulate 100 observations (n = 100) from a multivariate Gaussian distribution with 8 variables (p = 8) and “scale-free” graph structure, as follows:

set.seed( 10 )

data.sim <- bdgraph.sim( n = 100, p = 8, graph = "scale-free", vis = TRUE )


round( head( data.sim $ data, 4 ), 2 )
>       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7] [,8]
> [1,] -0.83  0.72 -0.26 -0.29  0.46  0.08 -0.85 0.35
> [2,] -0.40 -0.38  0.78  1.90  0.35  0.51 -0.97 0.38
> [3,] -0.65  0.21 -0.80 -0.64 -0.72  0.40  0.89 0.94
> [4,]  0.74 -0.38  0.18 -0.95  0.23 -1.44 -0.85 0.03

Since the generated data are Gaussian, we run ssgraph function by choosing method = "ggm", as follows:

ssgraph.obj <- ssgraph( data = data.sim, method = "ggm", iter = 5000, save = TRUE )
>   This OS does not support multi-threading for the BDgraph package  
> 5000 MCMC sampling ... in progress: 
> 10%->20%->30%->40%->50%->60%->70%->80%->90%-> done

summary( ssgraph.obj )

> $selected_g
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]    0    0    0    1    0    0    1    1
> [2,]    0    0    1    0    1    1    0    0
> [3,]    0    0    0    1    0    0    0    0
> [4,]    0    0    0    0    0    0    0    0
> [5,]    0    0    0    0    0    0    1    0
> [6,]    0    0    0    0    0    0    0    0
> [7,]    0    0    0    0    0    0    0    0
> [8,]    0    0    0    0    0    0    0    0
> 
> $p_links
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]    0 0.31 0.14 0.99 0.14 0.19 0.89 0.99
> [2,]    0 0.00 1.00 0.31 1.00 1.00 0.21 0.30
> [3,]    0 0.00 0.00 0.74 0.15 0.14 0.13 0.17
> [4,]    0 0.00 0.00 0.00 0.07 0.24 0.31 0.11
> [5,]    0 0.00 0.00 0.00 0.00 0.23 0.53 0.10
> [6,]    0 0.00 0.00 0.00 0.00 0.00 0.40 0.11
> [7,]    0 0.00 0.00 0.00 0.00 0.00 0.00 0.20
> [8,]    0 0.00 0.00 0.00 0.00 0.00 0.00 0.00
> 
> $K_hat
>       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]
> [1,]  1.26 -0.06  0.01 -0.39 -0.02 -0.03 -0.28 -0.43
> [2,] -0.06  2.19 -1.05  0.08 -0.49 -0.59 -0.03 -0.06
> [3,]  0.01 -1.05  1.92 -0.26  0.01 -0.02 -0.01 -0.02
> [4,] -0.39  0.08 -0.26  1.30  0.00 -0.04  0.07  0.00
> [5,] -0.02 -0.49  0.01  0.00  1.18  0.04  0.13  0.00
> [6,] -0.03 -0.59 -0.02 -0.04  0.04  1.08 -0.08 -0.01
> [7,] -0.28 -0.03 -0.01  0.07  0.13 -0.08  1.20  0.03
> [8,] -0.43 -0.06 -0.02  0.00  0.00 -0.01  0.03  1.24

To compare the result with true graph

compare( data.sim, ssgraph.obj, main = c( "Target", "ssgraph" ), vis = TRUE )

>                Target ssgraph
> true positive       7   6.000
> true negative      21  19.000
> false positive      0   2.000
> false negative      0   1.000
> F1-score            1   0.800
> specificity         1   0.905
> sensitivity         1   0.857
> MCC                 1   0.730