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,
convergence.grad = Inf,
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.
- convergence.grad
Gradient convergence criterion (see
modsem_da). Defaults toInf(disabled).- 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.8431198 0.8303126 0.8279740 1.037075 1.239208 0.9689993 1.158721
#> [2,] 0.8089110 0.7851843 0.7981295 1.013130 1.193088 1.0260549 1.228571
#> [3,] 0.7872003 0.8052296 0.8145833 1.001381 1.208717 0.9895684 1.187693
#> [4,] 0.8271300 0.8189199 0.7967628 1.014780 1.213834 1.0361278 1.201748
#> [5,] 0.7956778 0.8414410 0.8151827 1.015184 1.186065 1.0192380 1.211405
#> [6,] 0.8273826 0.8136767 0.8035041 1.032645 1.229454 1.0131460 1.211539
#> [7,] 0.7991850 0.8280249 0.8082902 1.028160 1.187993 0.9718814 1.184997
#> [8,] 0.8427499 0.8010538 0.8035885 1.059113 1.254606 1.0540424 1.238814
#> [9,] 0.8225737 0.7830806 0.8123309 1.040954 1.223185 1.0387047 1.214184
#> [10,] 0.8241394 0.7965383 0.8062882 1.012165 1.201600 0.9976779 1.202159
#> y1~1 y2~1 x1~~x1 x2~~x2 z1~~z1 z2~~z2 y1~~y1
#> [1,] 1.0731425 1.244699 0.2251888 0.1275567 0.1682197 0.1628929 0.2033729
#> [2,] 0.9818129 1.170493 0.1789204 0.1485049 0.1647063 0.1588955 0.1457066
#> [3,] 1.0168584 1.227048 0.1480695 0.1829294 0.1636795 0.1589036 0.1762139
#> [4,] 1.0060335 1.194489 0.1664564 0.1522940 0.1790640 0.1586751 0.1740403
#> [5,] 1.0278660 1.208887 0.1636774 0.1506086 0.1903064 0.1497468 0.1871113
#> [6,] 0.9938058 1.202878 0.1640120 0.1650441 0.1763754 0.1464762 0.1893429
#> [7,] 1.0363484 1.203318 0.1632368 0.1548166 0.1955758 0.1377836 0.1614570
#> [8,] 1.0559591 1.239372 0.1935549 0.1427647 0.1624950 0.1694464 0.1444620
#> [9,] 1.0360313 1.222197 0.1699981 0.1629649 0.1321575 0.1722389 0.1625526
#> [10,] 1.0676728 1.246461 0.1916662 0.1443462 0.1551634 0.1653583 0.1950508
#> y2~~y2 X~~X X~~Z Z~~Z Y~~Y Y~X Y~Z
#> [1,] 0.1322943 0.9036824 0.2118458 1.0032724 0.9199627 0.6598776 0.5395438
#> [2,] 0.1548348 0.9702060 0.2200237 1.0404303 0.9743636 0.7531882 0.5753617
#> [3,] 0.1400194 1.0113626 0.1629060 1.0312828 0.9695177 0.6230287 0.5601115
#> [4,] 0.1366090 0.9023049 0.2182986 1.0643370 0.9706635 0.7779596 0.5880706
#> [5,] 0.1346588 0.9641880 0.2117051 0.9961465 0.9876349 0.6804005 0.5866890
#> [6,] 0.1232400 0.9462429 0.2192135 1.0460693 0.9412058 0.6850968 0.5655637
#> [7,] 0.1554750 1.0121260 0.1863428 0.9823625 0.9577886 0.6716005 0.5656676
#> [8,] 0.1579295 0.9678577 0.2030090 1.0514561 1.0076445 0.7172093 0.6290923
#> [9,] 0.1443679 0.9730057 0.2251284 1.0892410 0.9332202 0.7078141 0.5379843
#> [10,] 0.1289304 0.9693193 0.1888444 1.0622116 0.8858047 0.7435089 0.5374154
#> Y~X:Z
#> [1,] 0.7309430
#> [2,] 0.7175160
#> [3,] 0.6878897
#> [4,] 0.6791739
#> [5,] 0.7569267
#> [6,] 0.6986418
#> [7,] 0.7451094
#> [8,] 0.7166253
#> [9,] 0.7072594
#> [10,] 0.7376377
# }
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.00193408 0.98106990 0.96499983 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.02763723 0.02339406 0.02031414
#> BEH~INTPBC ATT~~ATT SN~~SN PBC~~PBC INT~~INT
#> 0.01850903 0.03621255 0.03741312 0.03489979 0.02291498
#> BEH~~BEH INTPBC~~INTPBC ATT~~SN ATT~~PBC ATT~~INTPBC
#> 0.02476585 0.08868858 0.02929926 0.02795720 0.03848745
#> SN~~PBC SN~~INTPBC PBC~~INTPBC
#> 0.03045774 0.03569416 0.04491760
# }