if (FALSE) {
# Load lavaan package
library(lavaan)
# Holzinger and Swineford data set
dat <- HolzingerSwineford1939
# Introduce missing data
dat$x5 <- ifelse(dat$x1 <= quantile(dat$x1, 0.3), NA, dat$x5)
dat$x9 <- ifelse(is.na(dat$x5), NA, dat$x9)
# Model specification
model <- 'visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9'
# Model estimation
fit <- sem(model, data = dat, meanstructure = TRUE, std.lv = TRUE,
missing = "fiml", group = "school")
#————————————————————————————————————————————————————————————————————————————
# Bollen-Stine Bootstrapping with Incomplete Data
# Example 1: Default setting, transformation method 2, 500 replicates
# Plot bootstrap sampling distribution of the test statistic
boot.bs(fit, seed = 42, plot = TRUE)
#————————————————————————————————————————————————————————————————————————————
# Transformed Data and Bootstrap Samples
# Example 2: Return transformed data only
transdat <- boot.bs(fit, return = "transdat")
# Example 3: Return bootstrap samples only
bootsamp <- boot.bs(fit, return = "bootsamp")
#————————————————————————————————————————————————————————————————————————————
# Plot Bootstrap Sampling Distribution of Chi-Square Test Statistic
# Bollen-Stine Bootstrapping
object <- boot.bs(fit, seed = 42)
# Load ggplot2 package
library(ggplot2)
# Plot data
plotdat <- data.frame(chisq = object$boot.chisq)
# Example 3: Plot bootstrap sampling distribution, create plot manually
ggplot(plotdat, aes(chisq)) +
geom_histogram(aes(y = after_stat(density)), color = "black", alpha = 0.4, fill = "gray85") +
geom_density(color = "#0072B2") +
geom_vline(aes(xintercept = object$result$chisq, color = "Observed Test Statistic")) +
scale_x_continuous(name = expression(paste(chi^2, " Test Statistic")),
limits = c(0, max(c(plotdat$chisq, object$result$chisq), na.rm = TRUE))) +
scale_y_continuous(name = "Probability Density, f(x)", expand = expansion(mult = c(0, 0.05))) +
scale_color_manual(values = c("Observed Test Statistic" = "#CC79A7")) +
theme_bw() +
theme(legend.position = "bottom", legend.box.margin = margin(-12, 0, 0, 0),
legend.title = element_blank())
}
Run the code above in your browser using DataLab