Bootstrap a modsem Model
bootstrap_modsem.Rd
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, ...)
# 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
modsem
object, or a function to be bootstrapped (e.g.,modsem
,modsem_da
andmodsem_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
formodsem_pi
objects, ormodsem_da
formodsem_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
- cluster.boot
Variable to cluster bootstrapping by
Value
Depending on the return type of FUN
either
- numeric
A matrix with
R
rows (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_da
objects
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_pi
model by delegating tobootstrapLavaan
.bootstrap_modsem(modsem_da)
: Parametric or non-parametric bootstrap formodsem_da
models.bootstrap_modsem(`function`)
: Non-parametric bootstrap ofmodsem
functions
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.8439914 0.8306664 0.8280576 1.0368173 1.238975 0.9685517 1.158335
#> [2,] 0.8091064 0.7849298 0.7976872 1.0104667 1.190907 1.0246590 1.227494
#> [3,] 0.7862627 0.8048153 0.8142898 0.9993567 1.207108 0.9874045 1.185939
#> [4,] 0.8280318 0.8185326 0.7969995 1.0138076 1.212999 1.0373556 1.202727
#> [5,] 0.7960258 0.8422551 0.8151019 1.0134844 1.184783 1.0180378 1.210387
#> [6,] 0.8273825 0.8133551 0.8035218 1.0302584 1.227456 1.0112090 1.209964
#> [7,] 0.7991845 0.8285807 0.8080086 1.0255405 1.185888 0.9689374 1.182577
#> [8,] 0.8430450 0.8009657 0.8029533 1.0607866 1.255990 1.0560955 1.240445
#> [9,] 0.8229511 0.7825258 0.8120604 1.0407533 1.223069 1.0404157 1.215530
#> [10,] 0.8247294 0.7966398 0.8061111 1.0124835 1.201855 0.9991429 1.203303
#> y1~1 y2~1 x1~~x1 x2~~x2 z1~~z1 z2~~z2 y1~~y1
#> [1,] 1.0725567 1.244097 0.2259017 0.1269995 0.1685929 0.1626381 0.2039092
#> [2,] 0.9775742 1.167126 0.1790474 0.1484282 0.1644433 0.1589852 0.1453440
#> [3,] 1.0132930 1.224122 0.1468946 0.1834565 0.1632646 0.1591024 0.1760099
#> [4,] 1.0059908 1.194518 0.1671207 0.1518639 0.1786966 0.1589233 0.1738612
#> [5,] 1.0244786 1.206281 0.1638713 0.1505128 0.1910553 0.1491868 0.1864432
#> [6,] 0.9896160 1.199491 0.1640741 0.1649867 0.1760568 0.1466788 0.1897061
#> [7,] 1.0308388 1.198788 0.1630729 0.1547994 0.1960561 0.1374712 0.1616821
#> [8,] 1.0600403 1.242605 0.1937487 0.1424855 0.1623829 0.1694605 0.1439453
#> [9,] 1.0376854 1.223458 0.1702579 0.1627613 0.1315106 0.1726509 0.1624597
#> [10,] 1.0692477 1.247629 0.1921070 0.1439855 0.1553083 0.1652408 0.1952157
#> y2~~y2 X~~X X~~Z Z~~Z Y~~Y Y~X Y~Z
#> [1,] 0.1315184 0.9018577 0.2112101 1.0024653 0.9154232 0.6599517 0.5399378
#> [2,] 0.1546452 0.9690961 0.2201002 1.0406903 0.9750058 0.7528829 0.5725847
#> [3,] 0.1396114 1.0122897 0.1632057 1.0319942 0.9707447 0.6203496 0.5583714
#> [4,] 0.1370952 0.9001471 0.2178026 1.0648874 0.9706347 0.7816199 0.5860735
#> [5,] 0.1354281 0.9628799 0.2113690 0.9943785 0.9898472 0.6800490 0.5862176
#> [6,] 0.1225859 0.9456681 0.2194834 1.0466016 0.9386279 0.6839265 0.5631715
#> [7,] 0.1549185 1.0112645 0.1865090 0.9813222 0.9575413 0.6684284 0.5644568
#> [8,] 0.1579272 0.9666427 0.2025390 1.0514988 1.0089014 0.7194884 0.6306179
#> [9,] 0.1439958 0.9715419 0.2249601 1.0902798 0.9320522 0.7107070 0.5366545
#> [10,] 0.1284270 0.9676143 0.1883906 1.0618651 0.8823236 0.7460848 0.5375465
#> Y~X:Z
#> [1,] 0.7326105
#> [2,] 0.7180157
#> [3,] 0.6869211
#> [4,] 0.6798119
#> [5,] 0.7590759
#> [6,] 0.6988168
#> [7,] 0.7462527
#> [8,] 0.7160884
#> [9,] 0.7065497
#> [10,] 0.7385862
# }
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
# }