---
title: "gg_hist_qq_boot_and_gg_scatter_boot"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{gg_hist_qq_boot_and_gg_scatter_boot}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
bibliography: references.bib
csl: apa.csl
link-citations: true
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```


## Overview

This vignette demonstrates two `ggplot2`-based utilities for visualizing bootstrap estimates stored/returned by [`semboottools`](https://yangzhen1999.github.io/semboottools/) [@yang_forming_2026]:

- `gg_hist_qq_boot()`: modular histogram/density with optional confidence intervals (CIs), mean/boot-mean lines, SD arrow, and a side-by-side QQ plot.
- `gg_scatter_boot()`: a scatterplot matrix (via **GGally**) for multiple parameters’ bootstrap draws.

Compared to base plots, these functions are **modern** (*ggplot2*) and **modular** (optional layers), and can **return the `ggplot` objects** for further customization.

The following packages will be used:

```{r setup}
library(semboottools)
library(lavaan)
```


## Example

```{r}
library(lavaan)

# Simulate data
set.seed(1234)
n <- 200
x <- runif(n) - 0.5
m <- 0.4 * x + rnorm(n)
y <- 0.3 * m + rnorm(n)
dat <- data.frame(x, m, y)

# Specify model
model <- '
  m ~ a * x
  y ~ b * m + cp * x
  ab := a * b
'

# Fit model
fit0 <- sem(model, data = dat, fixed.x = FALSE)

# Store bootstrap draws
# `R`, the number of bootstrap samples, should be ≥2000 in real studies.
# `parallel` should be used unless fitting the model is fast.
# Set `ncpus` to a larger value or omit it in real studies.
# `iseed` is set to make the results reproducible.

fit2 <- store_boot(
  fit0,
  R = 500,
  iseed = 2345)
```

### Basic Usage: Default Settings

Visualizing the bootstrap estimates
for the unstandardized solution:

```{r}
gg_hist_qq_boot(fit2,
                param = "ab",
                standardized = FALSE)
gg_scatter_boot(fit2,
                param = c("ab", "a", "b"),
                standardized = FALSE)
```

Visualizing the bootstrap estimates
for the standardized solution:

```{r}
gg_hist_qq_boot(fit2,
                param = "ab",
                standardized = TRUE)
gg_scatter_boot(fit2,
                param = c("ab", "a", "b"),
                standardized = TRUE)
```

## Reference(s)

