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,
...
)
# 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.- 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.8437237 0.8307279 0.8281025 1.0376528 1.239691 0.9692380 1.158915
#> [2,] 0.8092459 0.7850038 0.7977327 1.0105661 1.191046 1.0249077 1.227659
#> [3,] 0.7863667 0.8047921 0.8143894 0.9997527 1.207427 0.9874139 1.185961
#> [4,] 0.8280876 0.8185726 0.7969698 1.0144687 1.213580 1.0379204 1.203209
#> [5,] 0.7962514 0.8421839 0.8151124 1.0141178 1.185219 1.0182412 1.210570
#> [6,] 0.8276002 0.8134703 0.8035323 1.0304554 1.227636 1.0112513 1.210008
#> [7,] 0.7992174 0.8282352 0.8080703 1.0261167 1.186361 0.9694574 1.183019
#> [8,] 0.8430516 0.8011524 0.8030156 1.0615541 1.256650 1.0567573 1.240985
#> [9,] 0.8229809 0.7824355 0.8121230 1.0413852 1.223541 1.0410240 1.216026
#> [10,] 0.8247023 0.7967362 0.8061781 1.0129566 1.202248 0.9995556 1.203664
#> y1~1 y2~1 x1~~x1 x2~~x2 z1~~z1 z2~~z2 y1~~y1
#> [1,] 1.0737417 1.245074 0.2256889 0.1272898 0.1686499 0.1626735 0.2039789
#> [2,] 0.9777018 1.167164 0.1792007 0.1483599 0.1645197 0.1590036 0.1454203
#> [3,] 1.0135434 1.224328 0.1471692 0.1834372 0.1632139 0.1591691 0.1761582
#> [4,] 1.0069916 1.195321 0.1672365 0.1518139 0.1786979 0.1588893 0.1738829
#> [5,] 1.0249801 1.206694 0.1642156 0.1504197 0.1909431 0.1493302 0.1863805
#> [6,] 0.9896563 1.199500 0.1642975 0.1649038 0.1761803 0.1466219 0.1897728
#> [7,] 1.0319170 1.199673 0.1632830 0.1548120 0.1957585 0.1376908 0.1616026
#> [8,] 1.0613462 1.243657 0.1938121 0.1426006 0.1625580 0.1693880 0.1439879
#> [9,] 1.0385546 1.224176 0.1704001 0.1627422 0.1314217 0.1726541 0.1624790
#> [10,] 1.0698087 1.248091 0.1921558 0.1440692 0.1554207 0.1652574 0.1952392
#> y2~~y2 X~~X X~~Z Z~~Z Y~~Y Y~X Y~Z
#> [1,] 0.1315211 0.9026643 0.2113817 1.0025567 0.9154440 0.6603231 0.5405474
#> [2,] 0.1546277 0.9695679 0.2202235 1.0408136 0.9750182 0.7533080 0.5723560
#> [3,] 0.1395923 1.0129014 0.1634294 1.0320943 0.9709083 0.6201017 0.5585269
#> [4,] 0.1370439 0.9006582 0.2179743 1.0649657 0.9706819 0.7820318 0.5864445
#> [5,] 0.1354557 0.9631285 0.2115499 0.9947380 0.9901969 0.6803062 0.5865133
#> [6,] 0.1225632 0.9459621 0.2195594 1.0465334 0.9382860 0.6838925 0.5632305
#> [7,] 0.1550209 1.0118572 0.1865902 0.9819868 0.9572663 0.6690290 0.5642246
#> [8,] 0.1579113 0.9671767 0.2025774 1.0512785 1.0091314 0.7199252 0.6311865
#> [9,] 0.1439880 0.9721091 0.2251856 1.0906265 0.9318047 0.7112648 0.5368782
#> [10,] 0.1284372 0.9682583 0.1885595 1.0618492 0.8822521 0.7462453 0.5379584
#> Y~X:Z
#> [1,] 0.7324072
#> [2,] 0.7181520
#> [3,] 0.6867020
#> [4,] 0.6798566
#> [5,] 0.7591716
#> [6,] 0.6991829
#> [7,] 0.7459125
#> [8,] 0.7162094
#> [9,] 0.7065196
#> [10,] 0.7386003
# }
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
# }