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 class 'modsem_pi'
bootstrap_modsem(model, FUN = "coef", ...)
# S3 method for class 'modsem_da'
bootstrap_modsem(
model,
FUN = "coef",
R = 1000L,
P.max = 1e+05,
type = c("nonparametric", "parametric"),
verbose = interactive(),
calc.se = FALSE,
optimize = FALSE,
convergence.abs = 10 * model$args$convergence.abs,
convergence.rel = 10 * model$args$convergence.rel,
algorithm = "EM",
...
)
# S3 method for class '`function`'
bootstrap_modsem(
model = modsem,
FUN = "coef",
data,
R = 1000L,
verbose = interactive(),
FUN.args = list(),
cluster.boot = NULL,
...
)Arguments
- model
A fitted
modsemobject, or a function to be bootstrapped (e.g.,modsem,modsem_daandmodsem_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::bootstrapLavaanformodsem_piobjects, ormodsem_daformodsem_daobjects.- 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.- convergence.abs
Absolute convergence criteria. Defaults to 10 times the original criteria.
- convergence.rel
Relative convergence criteria. Defaults to 10 times the original criteria.
- algorithm
Which algorithm should be used for the LMS approach? Defaults to
"EM", as opposed to"EMA".- data
Dataset to be resampled.
- FUN.args
Arguments passed to
FUN- cluster.boot
Variable to cluster bootstrapping by
Value
Depending on the return type of FUN either
- numeric
A matrix with
Rrows (bootstrap replicates) and as many columns aslength(FUN(model)).- other
A list of length
R; each element is the raw output ofFUN. NOTE: Only applies formodsem_daobjects
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.
Methods (by class)
bootstrap_modsem(modsem_pi): Bootstrap amodsem_pimodel by delegating tobootstrapLavaan.bootstrap_modsem(modsem_da): Parametric or non-parametric bootstrap formodsem_damodels.bootstrap_modsem(`function`): Non-parametric bootstrap ofmodsemfunctions
Examples
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)
#> X=~x2 Z=~z2 Y=~y2 Y~X Y~Z Y~XZ XZ=~x2z1
#> [1,] 0.8136484 0.8500430 0.8110152 0.6358254 0.5905760 0.7472894 0.8215820
#> [2,] 0.8369350 0.8154262 0.8004077 0.6899507 0.5333423 0.7045921 0.8009471
#> [3,] 0.8193689 0.8243714 0.8258594 0.6800059 0.6003536 0.7129113 0.7903911
#> [4,] 0.8297118 0.8362229 0.8166558 0.6677046 0.5576306 0.6903168 0.8284198
#> [5,] 0.8307095 0.8887698 0.7976096 0.7219239 0.5996120 0.7278261 0.8264926
#> [6,] 0.8121286 0.8724687 0.7876008 0.7034304 0.6188457 0.7483131 0.8518372
#> [7,] 0.7788113 0.8702692 0.8246190 0.6400031 0.5836320 0.6607929 0.7787292
#> [8,] 0.8032927 0.7888484 0.8038280 0.6765742 0.5504360 0.6863738 0.8032864
#> [9,] 0.8208329 0.8641453 0.7939066 0.6686026 0.6048411 0.7246705 0.8285727
#> [10,] 0.8296936 0.8573718 0.8125555 0.7296370 0.6051996 0.7124973 0.7832825
#> XZ=~x1z2 XZ=~x2z2 x1z1~~x1z2 x1z1~~x2z1 x1z2~~x2z2 x2z1~~x2z2 x1~~x1
#> [1,] 0.7918972 0.6596057 0.12370297 0.13889128 0.1172727 0.1342505 0.1665013
#> [2,] 0.7576199 0.6101557 0.10466718 0.11801127 0.1130634 0.1349378 0.1877195
#> [3,] 0.8214692 0.6505752 0.12175153 0.13952473 0.1119078 0.1348786 0.1836008
#> [4,] 0.8083159 0.6773176 0.08699855 0.12647627 0.1171038 0.1441987 0.1789220
#> [5,] 0.7866702 0.6561173 0.12263182 0.12617532 0.1437002 0.1337387 0.1704570
#> [6,] 0.7968685 0.6790424 0.13455646 0.12643290 0.1441988 0.1141236 0.1658553
#> [7,] 0.7633713 0.6018760 0.10623231 0.07292419 0.1505601 0.1193927 0.1536954
#> [8,] 0.7721933 0.6235792 0.12880970 0.14632912 0.1489538 0.1264121 0.1521982
#> [9,] 0.7856241 0.6609857 0.13753382 0.09753028 0.1272841 0.1354970 0.1798341
#> [10,] 0.8020772 0.6331495 0.12284117 0.12556676 0.1250333 0.1222860 0.1874125
#> x2~~x2 z1~~z1 z2~~z2 y1~~y1 y2~~y2 x1z1~~x1z1 x2z1~~x2z1
#> [1,] 0.1582221 0.2018391 0.1242125 0.1646320 0.1461259 0.3447423 0.2920502
#> [2,] 0.1383405 0.2099865 0.1357289 0.1738040 0.1454113 0.2833608 0.2954195
#> [3,] 0.1580124 0.1796666 0.1605001 0.2074109 0.1183935 0.3327544 0.3049153
#> [4,] 0.1436572 0.2235916 0.1185838 0.1790459 0.1383758 0.2849227 0.2992222
#> [5,] 0.1567684 0.2606311 0.1025253 0.1785238 0.1423744 0.3074855 0.2918664
#> [6,] 0.1494631 0.2317927 0.1245398 0.1308891 0.1803976 0.3542515 0.2592201
#> [7,] 0.1573897 0.2151401 0.1212760 0.2221052 0.1084483 0.2394239 0.2269702
#> [8,] 0.1701046 0.1849171 0.1635234 0.1865713 0.1311614 0.3750065 0.3016734
#> [9,] 0.1543777 0.2472318 0.1129482 0.1486773 0.1473279 0.2904846 0.2826039
#> [10,] 0.1378897 0.1965681 0.1346538 0.2137346 0.1177881 0.3068628 0.2726606
#> x1z2~~x1z2 x2z2~~x2z2 X~~X Z~~Z Y~~Y XZ~~XZ X~~Z
#> [1,] 0.2701594 0.2413436 0.9612714 0.9566120 0.8673828 0.9617711 0.1989054
#> [2,] 0.2611397 0.2263317 0.8979856 1.0565069 0.9394979 1.0639000 0.1617591
#> [3,] 0.2762069 0.2342220 0.9136864 0.9982688 0.8968371 1.0105718 0.1532475
#> [4,] 0.2409287 0.2413748 0.9392982 0.9768119 0.9282766 0.9512990 0.1957230
#> [5,] 0.3207610 0.2517865 0.9311308 0.9511813 0.9446710 0.9580283 0.1829304
#> [6,] 0.3166977 0.2369791 0.9692179 0.9057911 1.0169956 0.9009328 0.2067670
#> [7,] 0.3090138 0.2443715 0.9741209 0.9223777 1.0219705 1.0842307 0.1717285
#> [8,] 0.3177139 0.2466006 0.9950408 1.0815355 0.9854947 1.1713601 0.1924369
#> [9,] 0.3103478 0.2361521 0.8838294 0.9775601 0.9525338 0.9327275 0.1762067
#> [10,] 0.3023479 0.2211091 0.9456390 0.9598558 0.9827999 1.1117709 0.2242517
#> X~~XZ Z~~XZ
#> [1,] 0.044921413 0.056898032
#> [2,] -0.006111644 -0.031508318
#> [3,] -0.028332321 0.053120218
#> [4,] 0.034576119 0.062798313
#> [5,] -0.010095331 0.033752225
#> [6,] -0.012587866 0.017041183
#> [7,] -0.060305244 -0.020297514
#> [8,] -0.016367164 0.020789443
#> [9,] 0.016583979 0.004200049
#> [10,] -0.065478686 -0.004399217
#> attr(,"error.idx")
#> integer(0)
#> attr(,"nonadmissible")
#> integer(0)
#> attr(,"seed")
#> [1] 874600660
m1 <- '
X =~ x1 + x2
Z =~ z1 + z2
Y =~ y1 + y2
Y ~ X + Z + X:Z
'
# \dontrun{
fit_lms <- modsem(m1, oneInt, method = "lms")
bootstrap_modsem(fit_lms, FUN = coef, R = 10L)
#> X=~x2 Z=~z2 Y=~y2 x1~1 x2~1 z1~1 z2~1
#> [1,] 0.8431234 0.8303118 0.8279736 1.037046 1.239184 0.9689804 1.158705
#> [2,] 0.8089250 0.7851873 0.7981283 1.013098 1.193063 1.0260361 1.228556
#> [3,] 0.7872039 0.8052319 0.8145761 1.001397 1.208730 0.9895594 1.187686
#> [4,] 0.8271239 0.8189191 0.7967612 1.014717 1.213782 1.0361022 1.201727
#> [5,] 0.7956916 0.8413756 0.8158422 1.015170 1.186053 1.0192242 1.211390
#> [6,] 0.8273984 0.8137009 0.8034805 1.032836 1.229612 1.0132434 1.211618
#> [7,] 0.7992006 0.8280334 0.8082939 1.027944 1.187820 0.9718437 1.184966
#> [8,] 0.8427601 0.8010622 0.8035766 1.058910 1.254435 1.0539612 1.238749
#> [9,] 0.8226039 0.7830645 0.8123081 1.040810 1.223067 1.0386706 1.214157
#> [10,] 0.8241939 0.7965335 0.8062671 1.012118 1.201561 0.9976598 1.202145
#> y1~1 y2~1 x1~~x1 x2~~x2 z1~~z1 z2~~z2 y1~~y1
#> [1,] 1.0730986 1.244664 0.2251917 0.1275528 0.1682194 0.1628933 0.2033712
#> [2,] 0.9817662 1.170456 0.1789364 0.1484934 0.1647108 0.1588929 0.1457016
#> [3,] 1.0168482 1.227041 0.1480661 0.1829213 0.1636832 0.1589015 0.1761928
#> [4,] 1.0059568 1.194429 0.1664488 0.1522980 0.1790630 0.1586754 0.1740342
#> [5,] 1.0278430 1.208977 0.1637895 0.1507999 0.1902729 0.1497780 0.1873547
#> [6,] 0.9940733 1.203094 0.1640371 0.1650337 0.1763969 0.1464609 0.1893085
#> [7,] 1.0361635 1.203168 0.1632601 0.1548066 0.1955837 0.1377763 0.1614688
#> [8,] 1.0557217 1.239182 0.1935819 0.1427642 0.1625071 0.1694391 0.1444182
#> [9,] 1.0359097 1.222102 0.1700418 0.1629463 0.1321327 0.1722522 0.1624817
#> [10,] 1.0676156 1.246419 0.1917087 0.1442873 0.1551571 0.1653622 0.1949863
#> y2~~y2 X~~X X~~Z Z~~Z Y~~Y Y~X Y~Z
#> [1,] 0.1322953 0.9036818 0.2118500 1.0032742 0.9199633 0.6598647 0.5395163
#> [2,] 0.1548382 0.9701640 0.2200201 1.0404255 0.9743780 0.7531775 0.5753324
#> [3,] 0.1400337 1.0114544 0.1629161 1.0312830 0.9695011 0.6230350 0.5601309
#> [4,] 0.1366129 0.9022520 0.2182965 1.0643356 0.9706750 0.7779324 0.5880104
#> [5,] 0.1352747 0.9641794 0.2117099 0.9962407 0.9875750 0.6803898 0.5866660
#> [6,] 0.1232549 0.9462522 0.2191770 1.0460216 0.9414511 0.6851658 0.5657857
#> [7,] 0.1554673 1.0118589 0.1863376 0.9823447 0.9577996 0.6715681 0.5654687
#> [8,] 0.1579576 0.9675951 0.2029958 1.0514357 1.0076820 0.7171406 0.6288822
#> [9,] 0.1444150 0.9726905 0.2251029 1.0892490 0.9333552 0.7077886 0.5378074
#> [10,] 0.1289724 0.9692018 0.1888480 1.0622157 0.8858716 0.7435039 0.5373279
#> Y~X:Z
#> [1,] 0.7309517
#> [2,] 0.7175128
#> [3,] 0.6879411
#> [4,] 0.6791506
#> [5,] 0.7568514
#> [6,] 0.6986647
#> [7,] 0.7450641
#> [8,] 0.7165689
#> [9,] 0.7071547
#> [10,] 0.7375959
# }
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
"
# \dontrun{
boot <- bootstrap_modsem(model = modsem,
model.syntax = tpb, data = TPB,
method = "dblcent", rcs = TRUE,
rcs.scale.corrected = TRUE,
FUN = "coef", R = 50L)
coef <- apply(boot, MARGIN = 2, FUN = mean, na.rm = TRUE)
se <- apply(boot, MARGIN = 2, FUN = sd, na.rm = TRUE)
cat("Parameter Estimates:\n")
#> Parameter Estimates:
print(coef)
#> INT~ATT INT~SN INT~PBC BEH~INT BEH~PBC
#> 0.21612326 0.17445969 0.21757670 0.19161372 0.22770807
#> BEH~INTPBC ATT~~ATT SN~~SN PBC~~PBC INT~~INT
#> 0.20618658 1.00193407 0.98106989 0.96499984 0.48918824
#> BEH~~BEH INTPBC~~INTPBC ATT~~SN ATT~~PBC ATT~~INTPBC
#> 0.44894202 1.00576629 0.62704722 0.67982854 0.08403181
#> SN~~PBC SN~~INTPBC PBC~~INTPBC
#> 0.67814243 0.05772328 0.08952534
cat("Standard Errors: \n")
#> Standard Errors:
print(se)
#> INT~ATT INT~SN INT~PBC BEH~INT BEH~PBC
#> 0.02472549 0.02507163 0.02763722 0.02339406 0.02031414
#> BEH~INTPBC ATT~~ATT SN~~SN PBC~~PBC INT~~INT
#> 0.01850903 0.03621257 0.03741312 0.03489979 0.02291498
#> BEH~~BEH INTPBC~~INTPBC ATT~~SN ATT~~PBC ATT~~INTPBC
#> 0.02476584 0.08868858 0.02929927 0.02795720 0.03848745
#> SN~~PBC SN~~INTPBC PBC~~INTPBC
#> 0.03045774 0.03569416 0.04491760
# }