---
title: "semboottools"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{semboottools}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# Overview

This vignette demonstrates how to form bootstrapping confidence intervals and examining bootstrap estimates in SEM using semboottools.

```{r setup}
library(semboottools)
library(lavaan)
```

# Example: Simple Mediation Model

We use a simple mediation model with a large sample (N = 1000).

This model includes: A predictor x, A mediator m, An outcome y.

Indirect effect (ab) and total effect (total) defined.

```{r}
# Set seed for reproducibility
set.seed(1234)

# Generate data
n <- 1000
x <- runif(n) - 0.5
m <- 0.20 * x + rnorm(n)
y <- 0.17 * m + rnorm(n)
dat <- data.frame(x, y, m)

# Specify mediation model in lavaan syntax
mod <- '
  m ~ a * x
  y ~ b * m + cp * x
  ab := a * b
  total := a * b + cp
'
```
## Fit the Model with Bootstrapping

```{r}
fit <- sem(mod, data = dat, fixed.x = FALSE)
summary(fit, ci = TRUE)
# Ensure bootstrap estimates are stored
fit <- store_boot(fit) 
```

## Form Bootstrap CIs for Standardized Coefficients

```{r}
# Basic usage: default settings
# Compute standardized solution with percentile bootstrap CIs
std_boot <- standardizedSolution_boot(fit)
print(std_boot)
```

## Form Bootstrap CIs for for Unstandardized Cofficients

'parameterEstimates_boot()' computes bootstrap CIs, standard errors, and optional asymmetric p-values for unstandardized parameter estimates, including both free and user-defined parameters.

It requires bootstrap estimates stored via store_boot(), supports percentile and bias-corrected CIs, and outputs bootstrap SEs as the standard deviation of estimates.

```{r}
# Basic usage: default settings
# Compute unstandardized solution with percentile bootstrap CIs
est_boot <- parameterEstimates_boot(fit)

# Print results
print(est_boot)
```


## Visualize Bootstrap Estimates

To examine the distribution of bootstrap estimates, two functions are available:

-   `hist_qq_boot()`\
    For histogram + normal QQ-plot of **one parameter**.

-   `scatter_boot()`\
    For scatterplot matrix of **two or more parameters**.

### Histogram and QQ Plot: `hist_qq_boot()`

```{r, fig.width = 6, fig.height = 3, fig.align='center'}
# For estimates of user-defined parameters,
hist_qq_boot(fit, "ab", standardized = FALSE)
# For estimates in standardized solution,
hist_qq_boot(fit, "ab", standardized = TRUE)
```

### Scatterplot Matrix: `scatter_boot()`

```{r}
# standardized solution
scatter_boot(fit, c("a", "b", "ab"), standardized = TRUE)
# unstandardized solution
scatter_boot(fit, c("a", "b", "ab"), standardized = FALSE)
```
