<center> BDgraph with Simple Examples <center>

knitr::opts_chunk $ set( collapse = TRUE, comment = " ", fig.width = 7, fig.height = 7, fig.align = "center" )

The R package BDgraph provides statistical tools for Bayesian structure learning for undirected graphical models with continuous, count, binary, and mixed data. The package is implemented the recent improvements in the Bayesian graphical models' literature, including Mohammadi and Wit (2015), Mohammadi et al. (2021), Mohammadi et al. (2017), and Dobra and Mohammadi (2018). Besides, the package contains several functions for simulation and visualization, as well as several multivariate datasets taken from the literature.

Install BDgraph using

install.packages( "BDgraph" )

First, we load BDgraph package

library( BDgraph )

Here are two simple examples to show how to use the functionality of the package.

Example 1: Gaussian Graphical Models

Here 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 200 observations (n = 200) from a multivariate Gaussian distribution with 15 variables (p = 15) and "scale-free" graph structure, as follows

set.seed( 20 )

data.sim = bdgraph.sim( n = 200, p = 15, graph = "scale-free", vis = TRUE )

Since the generated data are Gaussian, we run the bdgraph() function by choosing method = "ggm", as follows

bdgraph.obj = bdgraph( data = data.sim, method = "ggm", iter = 5000, verbose = FALSE )

To report confusion matrix with cutoff point 0.5:

conf.mat( actual = data.sim, pred = bdgraph.obj, cutoff = 0.5 )

conf.mat.plot( actual = data.sim, pred = bdgraph.obj, cutoff = 0.5 )

To compare the result with the true graph

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

Now, as an alternative, we run the bdgraph.mpl() function which is based on the GGMs and marginal pseudo-likelihood, as follows

bdgraph.mpl.obj = bdgraph.mpl( data = data.sim, method = "ggm", iter = 5000, verbose = FALSE )

conf.mat( actual = data.sim, pred = bdgraph.mpl.obj )
conf.mat.plot( actual = data.sim, pred = bdgraph.mpl.obj )

We could compare the results of both algorithms with the true graph as follows

compare( list( bdgraph.obj, bdgraph.mpl.obj ), data.sim, 
         main = c( "Target", "BDgraph", "BDgraph.mpl" ), vis = TRUE )

To see the performance of the BDMCMC algorithm we could plot the ROC curve as follows

plotroc( list( bdgraph.obj, bdgraph.mpl.obj ), data.sim, cut = 200,
         labels = c( "BDgraph", "BDgraph.mpl" ), color = c( "blue", "red" ) )

Example 2: Gaussian Copula Graphical Models

Here is a simple example to see the performance of the package for the mixed data using Gaussian copula graphical models. First, by using the function bdgraph.sim(), we simulate 300 observations (n = 300) from mixed data (type = "mixed") with 10 variables (p = 10) and "random" graph structure, as follows

set.seed( 2 )

data.sim = bdgraph.sim( n = 300, p = 10, type = "mixed", graph = "random", vis = TRUE )

Since the generated data are mixed data, we are using run the bdgraph() function by choosing method = "gcgm", as follows:

bdgraph.obj = bdgraph( data = data.sim, method = "gcgm", iter = 5000, verbose = FALSE )

To compare the result with the true graph, we could run

compare( bdgraph.obj, data.sim, main = c( "Target", "BDgraph" ), vis = TRUE )
plotroc( bdgraph.obj, data.sim, labels = "BDgraph", color = "blue" )

For more examples see Mohammadi and Wit (2019).



Try the BDgraph package in your browser

Any scripts or data that you put into this service are public.

BDgraph documentation built on Dec. 28, 2022, 1:54 a.m.