Bootstrap a modsem Model
bootstrap_modsem.RdA 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, ...)
# 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.8437269 0.8307269 0.8281022 1.0376443 1.239684 0.9692303 1.158909
#> [2,] 0.8092461 0.7850037 0.7977322 1.0105589 1.191039 1.0249024 1.227655
#> [3,] 0.7863686 0.8047917 0.8143782 0.9997175 1.207396 0.9873804 1.185932
#> [4,] 0.8280870 0.8185702 0.7969667 1.0144560 1.213569 1.0379122 1.203201
#> [5,] 0.7962489 0.8421955 0.8151035 1.0141417 1.185240 1.0182663 1.210591
#> [6,] 0.8276120 0.8134586 0.8035415 1.0304363 1.227630 1.0112397 1.209996
#> [7,] 0.7992115 0.8283630 0.8080566 1.0261032 1.186344 0.9694229 1.182991
#> [8,] 0.8430520 0.8011515 0.8030153 1.0615445 1.256642 1.0567498 1.240979
#> [9,] 0.8229801 0.7824344 0.8121265 1.0413766 1.223534 1.0410223 1.216023
#> [10,] 0.8247024 0.7967354 0.8061774 1.0129461 1.202239 0.9995481 1.203658
#> y1~1 y2~1 x1~~x1 x2~~x2 z1~~z1 z2~~z2 y1~~y1
#> [1,] 1.0737298 1.245064 0.2256917 0.1272870 0.1686491 0.1626732 0.2039789
#> [2,] 0.9776911 1.167156 0.1792006 0.1483568 0.1645192 0.1590036 0.1454201
#> [3,] 1.0134770 1.224280 0.1471497 0.1834376 0.1632104 0.1591790 0.1761449
#> [4,] 1.0069714 1.195304 0.1672354 0.1518147 0.1786971 0.1588920 0.1738811
#> [5,] 1.0250271 1.206731 0.1642201 0.1504121 0.1909535 0.1493238 0.1863755
#> [6,] 0.9896400 1.199485 0.1642867 0.1649139 0.1761926 0.1466266 0.1897818
#> [7,] 1.0318394 1.199603 0.1632716 0.1548219 0.1958846 0.1376460 0.1616034
#> [8,] 1.0613314 1.243645 0.1938119 0.1425991 0.1625572 0.1693882 0.1439872
#> [9,] 1.0385475 1.224171 0.1703982 0.1627438 0.1314194 0.1726512 0.1624785
#> [10,] 1.0697933 1.248079 0.1921548 0.1440672 0.1554198 0.1652573 0.1952393
#> y2~~y2 X~~X X~~Z Z~~Z Y~~Y Y~X Y~Z
#> [1,] 0.1315218 0.9026554 0.2113799 1.0025561 0.9154441 0.6603187 0.5405408
#> [2,] 0.1546290 0.9695671 0.2202224 1.0408157 0.9750196 0.7533053 0.5723498
#> [3,] 0.1395586 1.0129084 0.1634353 1.0320925 0.9709077 0.6200670 0.5584930
#> [4,] 0.1370424 0.9006536 0.2179743 1.0649683 0.9706859 0.7820276 0.5864314
#> [5,] 0.1354589 0.9631058 0.2115380 0.9947168 0.9901809 0.6803392 0.5865532
#> [6,] 0.1225861 0.9459654 0.2195528 1.0465243 0.9382915 0.6838813 0.5632198
#> [7,] 0.1549709 1.0118320 0.1865688 0.9817782 0.9572839 0.6689487 0.5643617
#> [8,] 0.1579117 0.9671704 0.2025762 1.0512785 1.0091321 0.7199200 0.6311793
#> [9,] 0.1439877 0.9721045 0.2251818 1.0906231 0.9318039 0.7112641 0.5368693
#> [10,] 0.1284379 0.9682524 0.1885586 1.0618492 0.8822520 0.7462400 0.5379499
#> Y~X:Z
#> [1,] 0.7324089
#> [2,] 0.7181520
#> [3,] 0.6866947
#> [4,] 0.6798579
#> [5,] 0.7591866
#> [6,] 0.6991846
#> [7,] 0.7460267
#> [8,] 0.7162097
#> [9,] 0.7065186
#> [10,] 0.7386004
# }
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.21613756 0.17447574 0.21754632 0.19157083 0.22797399
#> BEH~INTPBC ATT~~ATT SN~~SN PBC~~PBC INT~~INT
#> 0.20376362 1.00193405 0.98106985 0.96499948 0.48918955
#> BEH~~BEH INTPBC~~INTPBC ATT~~SN ATT~~PBC ATT~~INTPBC
#> 0.44943800 1.01771341 0.62704719 0.67983189 0.08404097
#> SN~~PBC SN~~INTPBC PBC~~INTPBC
#> 0.67814613 0.05777815 0.08944000
cat("Standard Errors: \n")
#> Standard Errors:
print(se)
#> INT~ATT INT~SN INT~PBC BEH~INT BEH~PBC
#> 0.02471326 0.02505800 0.02761024 0.02340060 0.02034168
#> BEH~INTPBC ATT~~ATT SN~~SN PBC~~PBC INT~~INT
#> 0.01842627 0.03621253 0.03741315 0.03489927 0.02291011
#> BEH~~BEH INTPBC~~INTPBC ATT~~SN ATT~~PBC ATT~~INTPBC
#> 0.02483194 0.08937553 0.02929924 0.02795997 0.03848342
#> SN~~PBC SN~~INTPBC PBC~~INTPBC
#> 0.03046179 0.03568838 0.04466642
# }