---
title: "Model Comparison with bgmCompare"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Model Comparison with bgmCompare}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4
)
```

# Introduction

The function `bgmCompare()` extends `bgm()` to independent-sample
designs. It estimates whether edge weights and category thresholds
differ across groups in an ordinal Markov random field (MRF).  

Posterior inclusion probabilities indicate how plausible it is that a
group difference exists in a given parameter. These can be converted to
Bayes factors for hypothesis testing.

# ADHD dataset

We illustrate with a subset from the `ADHD` dataset included in **bgms**.

```{r}
library(bgms)

?ADHD
data_adhd = ADHD[ADHD$group == 1, -1]
data_adhd = data_adhd[, 1:5]
data_no_adhd = ADHD[ADHD$group == 0, -1]
data_no_adhd = data_no_adhd[, 1:5]
```

# Fitting a model

```{r, eval = FALSE}
fit = bgmCompare(x = data_adhd, y = data_no_adhd, seed = 1234)
```

```{r, include=FALSE}
fit = bgmCompare(x = data_adhd, y = data_no_adhd, seed = 1234, chains = 2,
                 display_progress = "none", verbose = FALSE)
```

# Posterior summaries

The summary shows both baseline effects and group differences:

```{r}
summary(fit)
```

You can extract posterior means and inclusion probabilities:

```{r}
coef(fit)
```

# Visualizing group networks

We can use the output to plot the network for the ADHD group:

```{r, fig.width= 7, fig.height= 7}
library(qgraph)

adhd_network = matrix(0, 5, 5)
adhd_network[lower.tri(adhd_network)] = coef(fit)$pairwise_effects_groups[, 1]
adhd_network = adhd_network + t(adhd_network)
colnames(adhd_network) = colnames(data_adhd)
rownames(adhd_network) = colnames(data_adhd)

qgraph(adhd_network,
  theme = "TeamFortress",
  maximum = 1,
  fade = FALSE,
  color = c("#f0ae0e"), vsize = 10, repulsion = .9,
  label.cex = 1, label.scale = "FALSE",
  labels = colnames(data_adhd)
)
```

# Next steps

- For a one-sample analysis, see the *Getting Started* vignette.
- For diagnostics and convergence checks, see the *Diagnostics* vignette.
- For additional analysis tools and more advanced plotting options,
  consider using the **easybgm** package, which integrates smoothly with
  **bgms** objects.
