Skip to contents

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, 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", "parameteric"),
  verbose = interactive(),
  calc.se = FALSE,
  optimize = FALSE,
  ...
)

Arguments

model

A fitted modsem object.

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 for modsem_pi objects, or modsem_da for modsem_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.

Value

Depending on the return type of FUN either

numeric

A matrix with R rows (bootstrap replicates) and as many columns as length(FUN(model)).

other

A list of length R; each element is the raw output of FUN. NOTE: Only applies for modsem_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.

Methods (by class)

  • bootstrap_modsem(modsem_pi): Bootstrap a modsem_pi model by delegating to bootstrapLavaan.

  • bootstrap_modsem(modsem_da): Parametric or non‑parametric bootstrap for modsem_da models.

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.8884237 0.8262879 0.7797705 0.7407114 0.5918870 0.6707317 0.8246330
#>  [2,] 0.8347994 0.8233353 0.8025760 0.6824978 0.6016801 0.6865373 0.8116811
#>  [3,] 0.8206546 0.8518766 0.8007877 0.6971329 0.5362172 0.6820370 0.8238181
#>  [4,] 0.8189006 0.8113157 0.8325491 0.6869278 0.5782950 0.7256083 0.7789803
#>  [5,] 0.7947128 0.8558326 0.7924540 0.7333650 0.6106507 0.7282757 0.8221329
#>  [6,] 0.8254009 0.8749817 0.7929519 0.6579637 0.6131392 0.7361599 0.8396432
#>  [7,] 0.8332576 0.7692559 0.8131625 0.7067837 0.5708261 0.7091723 0.8355242
#>  [8,] 0.8619048 0.8171167 0.7882294 0.7320317 0.6299300 0.7734579 0.8497304
#>  [9,] 0.8272198 0.8581544 0.8065377 0.7420436 0.5475846 0.7715898 0.7902094
#> [10,] 0.8325728 0.8515355 0.8051629 0.6431409 0.6255022 0.7367125 0.8150739
#>        XZ=~x1z2  XZ=~x2z2 x1z1~~x1z2 x1z1~~x2z1 x1z2~~x2z2 x2z1~~x2z2    x1~~x1
#>  [1,] 0.7706057 0.6378158  0.1321030 0.12470048 0.13859185  0.1236854 0.2695500
#>  [2,] 0.7620211 0.6213905  0.1510716 0.08864615 0.15350198  0.1165963 0.2064331
#>  [3,] 0.8140617 0.6704431  0.1392288 0.12953526 0.12479679  0.1391153 0.2037414
#>  [4,] 0.7923717 0.6249382  0.1028956 0.14624920 0.13305408  0.1534371 0.2024947
#>  [5,] 0.8138680 0.6766186  0.1016855 0.17156133 0.13094839  0.1542808 0.1466010
#>  [6,] 0.7600386 0.6436328  0.1071415 0.07537641 0.17072580  0.1236519 0.1713316
#>  [7,] 0.7848949 0.6607577  0.1366991 0.11834861 0.13430508  0.1109281 0.2181188
#>  [8,] 0.8331755 0.7173464  0.1227065 0.15731268 0.08557116  0.1282096 0.2078369
#>  [9,] 0.8248556 0.6578495  0.1125763 0.16228584 0.10511858  0.1216584 0.2034808
#> [10,] 0.8145436 0.6751385  0.1228619 0.15560824 0.12230915  0.1129503 0.1708143
#>          x2~~x2    z1~~z1    z2~~z2    y1~~y1    y2~~y2 x1z1~~x1z1 x2z1~~x2z1
#>  [1,] 0.1068363 0.2120969 0.1364207 0.1522135 0.1715433  0.3467576  0.2808781
#>  [2,] 0.1326718 0.1849395 0.1475439 0.1745294 0.1382708  0.3035680  0.2380796
#>  [3,] 0.1548883 0.1971554 0.1424606 0.1466531 0.1517053  0.3601218  0.2922566
#>  [4,] 0.1458213 0.1939336 0.1492024 0.1856174 0.1279031  0.3284856  0.3442310
#>  [5,] 0.1768187 0.2159352 0.1325914 0.1723244 0.1427089  0.3648349  0.3440650
#>  [6,] 0.1504944 0.2341332 0.1190925 0.1613587 0.1484900  0.2417272  0.2421190
#>  [7,] 0.1401831 0.1358201 0.1775705 0.1744732 0.1359542  0.3335131  0.2663161
#>  [8,] 0.1369614 0.2094457 0.1333073 0.1566526 0.1512443  0.3620863  0.3218547
#>  [9,] 0.1455015 0.2406770 0.1118286 0.2188779 0.1184675  0.3601850  0.3186712
#> [10,] 0.1532201 0.2130209 0.1350956 0.1946312 0.1367605  0.3495799  0.2831961
#>       x1z2~~x1z2 x2z2~~x2z2      X~~X      Z~~Z      Y~~Y    XZ~~XZ      X~~Z
#>  [1,]  0.3058370  0.2433937 0.9250376 0.9509946 1.0404153 0.9839416 0.1510574
#>  [2,]  0.3521438  0.2414933 0.9325914 1.0209942 0.9759887 1.1139648 0.1684377
#>  [3,]  0.3048880  0.2515079 0.9511693 0.9631768 0.9344947 1.1739524 0.1752234
#>  [4,]  0.2790108  0.2581793 0.9726406 1.0246144 0.9579877 1.1140874 0.1988152
#>  [5,]  0.2749447  0.2623786 1.0739408 0.9043633 0.9671002 0.9805299 0.1828608
#>  [6,]  0.3181851  0.2738636 0.9577705 0.9431554 0.9825834 1.0354563 0.1617813
#>  [7,]  0.3089064  0.2212350 0.8914256 1.0704986 0.9203757 0.9886425 0.1810837
#>  [8,]  0.2373762  0.1982495 0.8862861 1.0080460 0.9482141 0.8543592 0.2248769
#>  [9,]  0.2559218  0.2049166 0.9444809 0.9177360 0.9206756 0.9224049 0.1949300
#> [10,]  0.2906092  0.2260151 0.9269864 0.9611296 0.9645808 0.8736839 0.1970902
#>               X~~XZ      Z~~XZ
#>  [1,]  0.0045452923 0.06431493
#>  [2,] -0.0094172460 0.08608002
#>  [3,]  0.0412521613 0.13538289
#>  [4,] -0.0004327796 0.06455298
#>  [5,] -0.0391503620 0.04776190
#>  [6,]  0.0258802984 0.03401701
#>  [7,]  0.0028456548 0.05461465
#>  [8,]  0.0450855234 0.02570986
#>  [9,] -0.0242799280 0.04269811
#> [10,]  0.0395353338 0.04479171
#> attr(,"error.idx")
#> integer(0)
#> attr(,"nonadmissible")
#> integer(0)
#> attr(,"seed")
#> [1] 80750137


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.8144003 0.7994969 0.8106792 1.005678 1.208908 0.9603175 1.168526
#>  [2,] 0.8305667 0.8108889 0.8212616 1.076854 1.277735 1.0245837 1.200803
#>  [3,] 0.8408636 0.7971195 0.8096376 1.013152 1.206650 1.0068273 1.196258
#>  [4,] 0.8191455 0.8223470 0.8043703 1.014141 1.216866 1.0273827 1.221252
#>  [5,] 0.8008017 0.8275087 0.8096398 1.025941 1.219654 0.9858933 1.175210
#>  [6,] 0.8363479 0.8233188 0.8009718 1.004424 1.206887 1.0223509 1.218075
#>  [7,] 0.8125059 0.8008573 0.8140952 1.036395 1.229375 1.0026206 1.198682
#>  [8,] 0.8045093 0.7978530 0.8107005 1.046770 1.210967 0.9939469 1.212620
#>  [9,] 0.8055632 0.8456173 0.8016989 1.028315 1.226056 1.0126419 1.214307
#> [10,] 0.8064619 0.7876747 0.8108583 1.023187 1.217798 1.0113295 1.216341
#>            y1~1     y2~1    x1~~x1    x2~~x2    z1~~z1    z2~~z2    y1~~y1
#>  [1,] 0.9665918 1.159976 0.1789661 0.1396912 0.1693366 0.1540438 0.1973418
#>  [2,] 1.0666389 1.261958 0.1727841 0.1387836 0.1808257 0.1426023 0.2013036
#>  [3,] 1.0488971 1.227408 0.1939891 0.1303049 0.1561264 0.1580959 0.1854701
#>  [4,] 1.0681430 1.247761 0.1668152 0.1415997 0.1663170 0.1502107 0.1882028
#>  [5,] 1.0197591 1.208849 0.1591775 0.1524078 0.1819070 0.1524821 0.1896973
#>  [6,] 0.9934481 1.188831 0.1975663 0.1472956 0.1708857 0.1433798 0.1893417
#>  [7,] 1.0246796 1.201612 0.1658557 0.1521500 0.1584666 0.1626799 0.1793366
#>  [8,] 1.0630799 1.237754 0.1937012 0.1342085 0.1665521 0.1675723 0.2023360
#>  [9,] 1.0513124 1.234489 0.1761450 0.1558055 0.1800036 0.1431390 0.1806828
#> [10,] 1.0198644 1.200668 0.1568596 0.1628377 0.1550352 0.1532384 0.2126190
#>          y2~~y2      X~~X      X~~Z      Z~~Z      Y~~Y       Y~X       Y~Z
#>  [1,] 0.1451493 0.9902767 0.2198218 1.0515644 0.9866315 0.6443917 0.5409160
#>  [2,] 0.1331143 0.9295143 0.1841595 1.0742640 0.9083721 0.7405474 0.5659758
#>  [3,] 0.1310227 0.9245535 0.1959096 1.0876359 0.9546722 0.6641445 0.5606902
#>  [4,] 0.1337160 0.9683446 0.1975083 0.9778979 0.9539470 0.6622323 0.6199707
#>  [5,] 0.1345668 0.9636634 0.2145205 1.0405604 0.9411491 0.6203583 0.5886062
#>  [6,] 0.1424908 0.9230809 0.1642395 0.9896369 0.9251874 0.6755627 0.5761540
#>  [7,] 0.1348998 0.9797343 0.2209607 1.0334759 0.9256103 0.6400104 0.5748232
#>  [8,] 0.1233925 0.9785712 0.2420869 1.0436009 0.9968020 0.6749390 0.5781515
#>  [9,] 0.1445184 0.9671654 0.2444591 1.0410265 0.9125756 0.6249548 0.6094542
#> [10,] 0.1175989 1.0169742 0.2238477 1.0504369 0.9804817 0.6439944 0.5900795
#>           Y~X:Z
#>  [1,] 0.7502220
#>  [2,] 0.7320932
#>  [3,] 0.7468363
#>  [4,] 0.7604845
#>  [5,] 0.7705250
#>  [6,] 0.7076948
#>  [7,] 0.7296513
#>  [8,] 0.7172962
#>  [9,] 0.7245370
#> [10,] 0.6588935
# }