Learn R Programming

modsem (version 1.0.11)

bootstrap_modsem: Bootstrap a modsem Model

Description

A generic interface for parametric and non-parametric bootstrap procedures for structural equation models estimated with the modsem ecosystem. The function dispatches on the class of model; currently dedicated methods exist for modsem_pi (product‑indicator approach) and modsem_da (distributional‑analytic approach).

Usage

bootstrap_modsem(model = modsem, FUN, ...)

# S3 method for modsem_pi bootstrap_modsem(model, FUN, ...)

# S3 method for modsem_da bootstrap_modsem( model, FUN = "coef", R = 1000L, P.max = 1e+05, type = c("nonparametric", "parameteric"), verbose = interactive(), calc.se = FALSE, optimize = FALSE, ... )

# S3 method for `function` bootstrap_modsem( model = modsem, FUN = "coef", data, R = 1000L, verbose = interactive(), FUN.args = list(), ... )

Value

Depending on the return type of FUN either

numeric

A matrix with R rows (bootstrap replicates) and as many columns as length(FUN(model)).

other

A list of length R; each element is the raw output of FUN. NOTE: Only applies for modsem_da objects

Arguments

model

A fitted modsem object, or a function to be bootstrapped (e.g., modsem, modsem_da and modsem_pi)

FUN

A function that returns the statistic of interest when applied to a fitted model. The function must accept a single argument, the model object, and should ideally return a numeric vector; see Value.

...

Additional arguments forwarded to lavaan::bootstrapLavaan for modsem_pi objects, or modsem_da for modsem_da objects.

R

number of bootstrap replicates.

P.max

ceiling for the simulated population size.

type

Bootstrap flavour, see Details.

verbose

Should progress information be printed to the console?

calc.se

Should standard errors for each replicate. Defaults to FALSE.

optimize

Should starting values be re-optimized for each replicate. Defaults to FALSE.

data

Dataset to be resampled.

FUN.args

Arguments passed to FUN

Methods (by class)

  • bootstrap_modsem(modsem_pi): Bootstrap a modsem_pi model by delegating to bootstrapLavaan.

  • bootstrap_modsem(modsem_da): Parametric or non-parametric bootstrap for modsem_da models.

  • bootstrap_modsem(`function`): Non-parametric bootstrap of modsem functions

Details

A thin wrapper around lavaan::bootstrapLavaan() that performs the necessary book‑keeping so that FUN receives a fully‑featured modsem_pi object—rather than a bare lavaan fit—at every iteration.

The function internally resamples the observed data (non-parametric case) or simulates from the estimated parameter table (parametric case), feeds the sample to modsem_da, evaluates FUN on the refitted object and finally collates the results.

This is a more general version of boostrap_modsem for bootstrapping modsem functions, not modsem objects. model is now a function to be boostrapped, and ... are now passed to the function (model), not FUN. To pass arguments to FUN use FUN.args.

See Also

bootstrapLavaan, modsem_pi, modsem_da

Examples

Run this code

m1 <- '
  X =~ x1 + x2
  Z =~ z1 + z2
  Y =~ y1 + y2

  Y ~ X + Z + X:Z
'

fit_pi <- modsem(m1, oneInt)
bootstrap_modsem(fit_pi, FUN = coef, R = 10L)


m1 <- '
  X =~ x1 + x2
  Z =~ z1 + z2
  Y =~ y1 + y2

  Y ~ X + Z + X:Z
'

if (FALSE) {
fit_lms <- modsem(m1, oneInt, method = "lms")
bootstrap_modsem(fit_lms, FUN = coef, R = 10L)
}

tpb <- "
# Outer Model (Based on Hagger et al., 2007)
  ATT =~ att1 + att2 + att3 + att4 + att5
  SN =~ sn1 + sn2
  PBC =~ pbc1 + pbc2 + pbc3
  INT =~ int1 + int2 + int3
  BEH =~ b1 + b2

# Inner Model (Based on Steinmetz et al., 2011)
  INT ~ ATT + SN + PBC
  BEH ~ INT + PBC + INT:PBC
"

if (FALSE) {
boot <- bootstrap_modsem(model = modsem, 
                         model.syntax = tpb, data = TPB,
                         method = "dblcent", rcs = TRUE, 
                         rcs.scale.corrected = TRUE,
                         FUN = "coef")
coef <- apply(boot, MARGIN = 2, FUN = mean, na.rm = TRUE)
se   <- apply(boot, MARGIN = 2, FUN = sd, na.rm = TRUE)

cat("Parameter Estimates:\n")
print(coef)

cat("Standard Errors: \n")
print(se)

}

Run the code above in your browser using DataLab