---
title: "<center> BDgraph with Simple Examples <center>"
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{BDgraph with Simple Examples}
output:
  knitr:::html_vignette:
    number_sections: false
    fig_caption: true
    toc: false
    fig_width: 7
    fig_height: 5
    theme: cosmo
    highlight: tango
---

<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{BDgraph with Simple Examples}
-->

```{r opts, echo = FALSE, message = FALSE, warning = FALSE}
library( "knitr" )
knitr::opts_chunk $ set( echo = TRUE )
```

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)](https://projecteuclid.org/euclid.ba/1422468425), [Mohammadi et al. (2021)](https://doi.org/10.1080/01621459.2021.1996377), [Mohammadi et al. (2017)](https://rss.onlinelibrary.wiley.com/doi/full/10.1111/rssc.12171), and [Dobra and Mohammadi (2018)](https://projecteuclid.org/euclid.aoas/1532743478). Besides, the package contains several functions for simulation and visualization, as well as several multivariate datasets taken from the literature. 

Install caret using
```{r eval = FALSE}
install.packages( "BDgraph" )
```

First, we install **BDgraph** package as well as **pROC** and **ggplot2**
```{r loadpkg, message = FALSE, warning = FALSE}
library( BDgraph )

library( pROC )    
library( ggplot2 )  
```

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

```{r fig.align = 'center'}
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

```{r}
bdgraph.obj = bdgraph( data = data.sim, method = "ggm", iter = 5000 )
```

To report confusion matix with cutoff point 0.5:
```{r fig.align = 'center', fig.width = 3, fig.height = 3}
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

```{r fig.align = 'center'}
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

```{r fig.align = 'center', fig.width = 3, fig.height = 3}
bdgraph.mpl.obj = bdgraph.mpl( data = data.sim, method = "ggm", iter = 5000 )

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

```{r fig.align = 'center'}
compare( data.sim, bdgraph.obj, bdgraph.mpl.obj, 
         main = c( "Target", "BDgraph", "BDgraph.mpl" ), vis = TRUE )
```

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

```{r fig.align = 'center'}
roc.bdgraph     = BDgraph::roc( pred = bdgraph.obj,     actual = data.sim )
roc.bdgraph.mpl = BDgraph::roc( pred = bdgraph.mpl.obj, actual = data.sim )

pROC::ggroc( list( BDgraph = roc.bdgraph, BDgraph.mpl = roc.bdgraph.mpl ), size = 0.8 ) + 
    theme_minimal() + ggtitle( "ROC plots with AUC" ) +
  scale_color_manual( values = c( "red", "blue" ), 
    labels = c( paste( "AUC=", round( auc( roc.bdgraph ), 3 ), "; BDgraph; " ),
                paste( "AUC=", round( auc( roc.bdgraph.mpl ), 3 ), "; BDgraph.mpl" ) ) ) +
  theme( legend.title = element_blank() ) +
  theme( legend.position = c( .7, .3 ), text = element_text( size = 17 ) ) + 
    geom_segment( aes( x = 1, xend = 0, y = 0, yend = 1 ), color = "grey", linetype = "dashed" )
```

# 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` to simulate 300 observations (n = 300) from mixed data (`type = "mixed"`) with 10 variables (p = 10) and “random” graph structure, as follows

```{r fig.align = 'center'}
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:

```{r}
bdgraph.obj = bdgraph( data = data.sim, method = "gcgm", iter = 5000 )
```
To compare the result with the true graph, we could run

```{r fig.align = 'center'}
compare( data.sim, bdgraph.obj, main = c( "Target", "BDgraph" ), vis = TRUE )
```

For more examples see [Mohammadi and Wit (2019)](https://www.jstatsoft.org/article/view/v089i03).
