gg_hist_qq_boot_and_gg_scatter_boot
Source:vignettes/gg_hist_qq_boot_and_gg_scatter_boot.Rmd
gg_hist_qq_boot_and_gg_scatter_boot.Rmd
library(semboottools)
library(lavaan)
#> This is lavaan 0.6-19
#> lavaan is FREE software! Please report any bugs.
Overview
This vignette demonstrates two ggplot2-based utilities for visualizing bootstrap estimates stored/returned by semboottools:
-
gg_hist_qq_boot()
: modular histogram/density with optional CI, 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.
Dependencies: Requires ggplot2
(and
GGally
for the scatterplot matrix).
Optional: patchwork
to arrange histogram and QQ plots
side-by-side.
Example
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
fit2 <- store_boot(
fit0,
R = 5000,
iseed = 2345)
Basic usage: default settings
gg_hist_qq_boot(fit2, param = "ab", standardized = FALSE)
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_bar()`).
gg_scatter_boot(fit2, c("ab", "a", "b"), standardized = FALSE)
#> Warning in ggplot2::geom_histogram(ggplot2::aes(y =
#> ggplot2::after_stat(density)), : Ignoring unknown parameters: `size`
#> Warning in ggplot2::geom_histogram(ggplot2::aes(y = ggplot2::after_stat(density)), : Ignoring unknown parameters: `size`
#> Ignoring unknown parameters: `size`
gg_hist_qq_boot(fit2, param = "ab", standardized = TRUE)
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_bar()`).
gg_scatter_boot(fit2, c("ab", "a", "b"), standardized = TRUE)
#> Warning in ggplot2::geom_histogram(ggplot2::aes(y =
#> ggplot2::after_stat(density)), : Ignoring unknown parameters: `size`
#> Warning in ggplot2::geom_histogram(ggplot2::aes(y = ggplot2::after_stat(density)), : Ignoring unknown parameters: `size`
#> Ignoring unknown parameters: `size`