---
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.

# Boredom dataset

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

```{r}
library(bgms)

?Boredom
data_french = Boredom[Boredom$language == "fr", -1]
data_french = data_french[, 1:5]
data_english = Boredom[Boredom$language != "fr", -1]
data_english = data_english[, 1:5]
```

# Fitting a model
```{r, include=FALSE}
  fit <- readRDS(system.file("extdata", "fit_boredom.rds", package = "bgms"))
```
```{r, eval = FALSE}
fit = bgmCompare(x = data_french, y = data_english, seed = 1234)
```
Note: During fitting, progress bars are shown in interactive sessions.
In this vignette, they are suppressed for clarity. Sampling can take a while; 
the progress bars usually help track progress.

# 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 French sample:

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

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

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

# 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.