tests/testthat/test-sem.R

context("Structural Equation Modeling")

options <- jaspTools::analysisOptions("SEM")
options$models <- list(list(name = "Model1", syntax = list(model = "x1 ~ x2 + x3 + y1", columns = c("x1", "x2", "x3", "y1"))))
options$emulation         <- "lavaan"
options$estimator         <- "default"
options$group             <- ""
options$samplingWeights   <- ""
options$informationMatrix <- "expected"
options$naAction          <- "fiml"
options$modelTest         <- "standard"
results <- jaspTools::runAnalysis("SEM", testthat::test_path("poldem_grouped.csv"), options, makeTests = F)


test_that("Model fit table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_fittab"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(46.1563554263454, 55.4263078804906, 6.66133814775094e-14, 0, "Model1",
                                      75, "", 4, 4))
})

parcont <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]]
parcov  <- parcont[["modelContainer_params_cov"]][["data"]]
parreg  <- parcont[["modelContainer_params_reg"]][["data"]]
parvar  <- parcont[["modelContainer_params_var"]][["data"]]
partot  <- parcont[["modelContainer_params_toteff"]][["data"]]


test_that("Basic SEM covariance parameter table works", {
  expect_equal_tables(parcov, list(1.782009228184, 1.782009228184, 1.782009228184, "", "x2 - x3",
                                   "", 0, "", 1.2564136096, 1.2564136096, 1.2564136096, "", "x2 - y1",
                                   "", 0, "", 0.899301179999998, 0.899301179999998, 0.899301179999998,
                                   "", "x3 - y1", "", 0, ""),
                      "Covariance parameter table")
  expect_equal_tables(parreg, list(0.26348889641759, 0.446867412394827, 0.355178154406208, "x1",
                                   3.13082892944294e-14, "x2", 0.0467810932812294, 7.59234403247094,
                                   -0.018377879348242, 0.174214412813761, 0.0779182667327594, "x1",
                                   0.112759784761359, "x3", 0.0491315895805092, 1.58590974560428,
                                   0.00202079123204174, 0.0593563876703967, 0.0306885894512192,
                                   "x1", 0.0358943944804584, "y1", 0.014626696431825, 2.09812171834281),
                      "Regressions parameter table")
  expect_equal_tables(partot, list(0.26348889641759, 0.446867412394827, 0.355178154406208, "x2 <unicode> x1",
                                   3.13082892944294e-14, 0.0467810932812294, 7.59234403247094,
                                   -0.018377879348242, 0.174214412813761, 0.0779182667327594, "x3 <unicode> x1",
                                   0.112759784761359, 0.0491315895805092, 1.58590974560428, 0.00202079123204174,
                                   0.0593563876703967, 0.0306885894512192, "y1 <unicode> x1", 0.0358943944804584,
                                   0.014626696431825, 2.09812171834281),
                      "Total effects table")
  expect_equal_tables(parvar, list(0.0662130628477228, 0.128548647454031, "x1", 0.0973808551508767,
                                   "", "x1", 9.14129882900738e-10, 0.0159022270557018, 6.12372435695796,
                                   2.25167664969695, 2.25167664969695, "x2", 2.25167664969695,
                                   "", "x2", "", 0, "", 1.94967853807201, 1.94967853807201, "x3",
                                   1.94967853807201, "", "x3", "", 0, "", 6.78685155555555, 6.78685155555555,
                                   "y1", 6.78685155555555, "", "y1", "", 0, ""))
})


# Reliability, AVE, HTMT works
options <- jaspTools::analysisOptions("SEM")
options$models <- list(list(name = "Model1", syntax = list(model = "
# latent variable definitions
  ind60 =~ x1 + x2 + x3
  dem60 =~ y1 + y2 + y3 + y4
  dem65 =~ y5 + y6 + y7 + y8
# regressions
  dem60 ~ ind60
  dem65 ~ ind60 + dem60
# residual (co)variances
  y1 ~~ y5
  y2 ~~ y4 + y6
  y3 ~~ y7
  y4 ~~ y8
  y6 ~~ y8
", columns = c("x1", "x2", "x3", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8"))))
options$emulation         <- "lavaan"
options$estimator         <- "default"
options$group             <- ""
options$samplingWeights   <- ""
options$informationMatrix <- "expected"
options$naAction          <- "fiml"
options$modelTest         <- "standard"
options$reliability       <- TRUE
options$ave               <- TRUE
options$htmt              <- TRUE
results <- jaspTools::runAnalysis("SEM", "poldem_grouped.csv", options)


container   <- results[["results"]][["modelContainer"]][["collection"]]
ave         <- container[["modelContainer_AVE"]][["data"]]
htmt        <- container[["modelContainer_htmt"]][["collection"]][["modelContainer_htmt_htmttab"]][["data"]]
reliability <- container[["modelContainer_reliability"]][["data"]]

test_that("reliability/ AVE/ htmt works", {
  expect_equal_tables(ave, list(0.8588015398276, "ind60", 0.597128239158634, "dem60", 0.640021072526866,
                                "dem65"))
  expect_equal_tables(htmt, list("", "", 1, 1, "", 0.420934414880351, 0.980709420149052, 1, 0.549916280338394
  ))
  expect_equal_tables(reliability, list("ind60", 0.902334680203148, 0.943690008441057, "dem60", 0.858794528217608,
                                        0.841179471771507, "dem65", 0.882739385479519, 0.857553923970666,
                                        "total", 0.91494164193877, 0.919205517992938))
})


# Multigroup, multimodel SEM works
options <- jaspTools::analysisOptions("SEM")
options$emulation                   = "lavaan"
options$estimator                   = "default"
options$group                       = "group"
options$informationMatrix           = "expected"
options$meanStructure               = TRUE
options$latentInterceptFixedToZero  = TRUE
options$modificationIndexLowHidden  = TRUE
options$naAction                    = "listwise"
options$impliedCovariance           = TRUE
options$mardiasCoefficient          = TRUE
options$observedCovariance          = TRUE
options$pathPlot                    = TRUE
options$rSquared                    = TRUE
options$residualCovariance          = TRUE
options$standardizedResidual        = TRUE
options$pathPlotParameter           = TRUE
options$standardizedEstimate        = TRUE
options$modelTest                   = "satorraBentler"
options$samplingWeights             = ""

modelDefault <- list(model = "
# latent variable definitions
  ind60 =~ x1 + x2 + x3
  dem60 =~ y1 + y2 + y3 + y4
  dem65 =~ y5 + y6 + y7 + y8
# regressions
  dem60 ~ ind60
  dem65 ~ ind60 + dem60
# residual (co)variances
  y1 ~~ y5
  y2 ~~ y4 + y6
  y3 ~~ y7
  y4 ~~ y8
  y6 ~~ y8
", columns = c("x1", "x2", "x3", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8"))
modelConstrained <- list(model = "
# latent variable definitions
  ind60 =~ x1 + x2 + x3
  dem60 =~ c(a1,a2)*y1 + c(b1,b2)*y2 + c(c1,c2)*y3 + c(d1,d2)*y4
  dem65 =~ c(a1,a3)*y5 + c(b1,b3)*y6 + c(c1,c3)*y7 + c(d1,d3)*y8
# regressions
  dem60 ~ ind60
  dem65 ~ ind60 + dem60
# residual (co)variances
  y1 ~~ y5
  y2 ~~ y4 + y6
  y3 ~~ y7
  y4 ~~ y8
  y6 ~~ y8
", columns = c("x1", "x2", "x3", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8"))
modelMoreConstrained <- list(model = "
# latent variable definitions
  ind60 =~ x1 + x2 + x3
  dem60 =~ c(a1, a2)*y1 + c(b1, b2)*y2 + c(c1, c2)*y3 + c(d1, d2)*y4
  dem65 =~ c(a1, a2)*y5 + c(b1, b2)*y6 + c(c1, c2)*y7 + c(d1, d2)*y8
# regressions
  dem60 ~ ind60
  dem65 ~ ind60 + dem60
# residual (co)variances
  y1 ~~ y5
  y2 ~~ y4 + y6
  y3 ~~ y7
  y4 ~~ y8
  y6 ~~ y8
", columns = c("x1", "x2", "x3", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8"))

options$models = list(
  list(name = "default",          syntax = modelDefault),
  list(name = "constrained",      syntax = modelConstrained),
  list(name = "more constrained", syntax = modelMoreConstrained)
)

results <- jaspTools::runAnalysis("SEM", "poldem_grouped.csv", options)


test_that("Model fit table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_fittab"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(3189.26691715402, 3383.93591869107, 85.6796813843016, 70, "default",
                                      75, 0.0980338951401504, "", "", "", "all", 84, 84, 3184.34803034567,
                                      3372.06456754211, 87.9549367720077, 73, "constrained", 75, 0.111927575441422,
                                      0.647754490400879, 1.65156784896072, 3, "all", 81, 84, 3181.07183366569,
                                      3361.83590652152, 92.7433708195404, 76, "more constrained",
                                      75, 0.0929761753673403, 0.110596895610441, 6.02091896551089,
                                      3, "all", 78, 84, 1638.81154499845, 1774.12864966057, 51.603527732831,
                                      70, "default", 37, 0.951434618744164, "", "", "", 1, 84, 84,
                                      1718.45537215556, 1856.01260957258, 34.0761536514706, 70, "default",
                                      38, 0.999909631385078, "", "", "", 2, 81, 84, 1639.89265814398,
                                      1775.2097628061, 53.4279668719256, 73, "constrained", 37, 0.958714294372556,
                                      0.609631201049421, 1.82443913909461, 3, 1, 78, 84, 1718.45537220169,
                                      1856.0126096187, 34.5269699000821, 73, "constrained", 38, 0.999963257835091,
                                      0.929556093266985, 0.450816248611517, 3, 2, 84, 84, 1639.89265813238,
                                      1775.20976279449, 54.5525537466108, 76, "more constrained",
                                      37, 0.970044580307764, 0.771142273547436, 1.12458687468522,
                                      3, 1, 81, 84, 1721.17917553332, 1858.73641295033, 38.1908170729296,
                                      76, "more constrained", 38, 0.999911473827655, 0.300125123328536,
                                      3.66384717284748, 3, 2, 78, 84))
})

test_that("R-Squared table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_rsquared"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(1, "x1", 0.883076871616544, 0.88344099961725, 0.883440941017384,
                                      1, "x2", 0.993698159869737, 0.993307380054294, 0.993308312239004,
                                      1, "x3", 0.734550879193546, 0.734754000085834, 0.734752992615783,
                                      1, "y1", 0.76374604003533, 0.78382522538215, 0.783824495338279,
                                      1, "y2", 0.52698823283752, 0.46980581123522, 0.469797871844076,
                                      1, "y3", 0.540743668690899, 0.602813004892641, 0.602813008521032,
                                      1, "y4", 0.758371834315632, 0.715732725592278, 0.715727580943501,
                                      1, "y5", 0.748735676904241, 0.760689523184986, 0.760685788176715,
                                      1, "y6", 0.626089440009365, 0.636528021958327, 0.63653017192483,
                                      1, "y7", 0.729758226751309, 0.710375682244039, 0.710376016894816,
                                      1, "y8", 0.574575906571732, 0.588831799668115, 0.588835817501575,
                                      1, "dem60", 0.275927380324135, 0.258157185403395, 0.258145005894785,
                                      1, "dem65", 0.941713333219214, 0.938883931908498, 0.938882940520488,
                                      2, "x1", 0.787475869571126, 0.78747617521673, 0.788439850883418,
                                      2, "x2", 0.90066092940223, 0.900660946749863, 0.900202685338523,
                                      2, "x3", 0.749867733548725, 0.749868295135394, 0.749372699570641,
                                      2, "y1", 0.73192194477315, 0.731929437708713, 0.683849857838006,
                                      2, "y2", 0.550065444500199, 0.550067399151619, 0.519184271086936,
                                      2, "y3", 0.465824888188388, 0.465819890503162, 0.543696244446078,
                                      2, "y4", 0.651968126202521, 0.651959003998254, 0.694898810552798,
                                      2, "y5", 0.536001769615457, 0.535995620432757, 0.598318029638634,
                                      2, "y6", 0.625808175301569, 0.625823134164411, 0.607599332020874,
                                      2, "y7", 0.658946836103404, 0.658945492261667, 0.618117097165968,
                                      2, "y8", 0.842442637402788, 0.842449295416932, 0.821972120799249,
                                      2, "dem60", 0.075359995596017, 0.0753581296999277, 0.0905522598183016,
                                      2, "dem65", 0.956917027167473, 0.956910675588687, 0.977886505597789
                                 ))
})

test_that("Mardia's coefficients table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_mardiasTable"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(330.897809673901, 26.471824773912, 286, "Skewness", 0.0347860345067629,
                                      "", "", 134.567190822067, "", "Kurtosis", 0.0308358026617142,
                                      -2.15918518879413))
})

# use more constrained model (model 3)
test_that("Residual covariances table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_more constrained"]][["collection"]][["modelContainer_params_more constrained_cov"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(-0.358864175060675, 0.603322975607713, 0.122229400273519, 1, "",
                                      "y1 - y5", 0.618512432702144, 0.245460416175501, 0.497959720666841,
                                      0.298874371767042, 0.804248632606797, 0.551561502186919, 1,
                                      "", "y2 - y4", 1.88428791543238e-05, 0.128924374331896, 4.27817862250785,
                                      -0.0660654532796924, 0.453186076071358, 0.193560311395833, 1,
                                      "", "y2 - y6", 0.143954146938327, 0.132464558901806, 1.46122338684807,
                                      -0.0394544944434154, 0.65625806127924, 0.308401783417912, 1,
                                      "", "y3 - y7", 0.0822705135019619, 0.177480954040571, 1.73766128926383,
                                      -0.354330258818438, 0.243417993457062, -0.0554561326806879,
                                      1, "", "y4 - y8", 0.716103275347807, 0.152489601082077, -0.363671570304909,
                                      0.035096348829382, 0.655763540344476, 0.345429944586929, 1,
                                      "", "y6 - y8", 0.0291375249733508, 0.158336376691316, 2.18162087452816,
                                      0.0448493338644849, 0.71299582648642, 0.378922580175452, 2,
                                      "", "y1 - y5", 0.0262097889471165, 0.17044866586636, 2.22308915267513,
                                      -0.0302077568737734, 0.574856704162812, 0.272324473644519, 2,
                                      "", "y2 - y4", 0.0776878549933768, 0.154356015163865, 1.7642621400625,
                                      0.0810795951962878, 0.625128959164326, 0.353104277180307, 2,
                                      "", "y2 - y6", 0.0109543949492443, 0.138790653363896, 2.54415026244239,
                                      -0.269314188853154, 0.474306408672304, 0.102496109909575, 2,
                                      "", "y3 - y7", 0.588990938105511, 0.189702617851921, 0.540298869199486,
                                      -0.085586356856639, 0.665092688920064, 0.289753166031713, 2,
                                      "", "y4 - y8", 0.130268105961029, 0.191503275493316, 1.51304548334906,
                                      -0.0312697882233236, 0.654724627539894, 0.311727419658285, 2,
                                      "", "y6 - y8", 0.0748665063273699, 0.17500179114878, 1.78128130924824
                                 ))
})

test_that("Factor Loadings table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_more constrained"]][["collection"]][["modelContainer_params_more constrained_ind"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.789134643460113, 0.981542622829009, 0.88533863314456, 1, "a1",
                                      "dem60", 0, "y1", 0.049084570146846, 18.0370049181626, 0.527821385546085,
                                      0.843014668610759, 0.685418027078422, 1, "b1", "dem60", 0, "y2",
                                      0.0804079272759291, 8.5242593647058, 0.641994888847432, 0.910825781443935,
                                      0.776410335145684, 1, "c1", "dem60", 0, "y3", 0.068580569519901,
                                      11.3211415504559, 0.737693950515967, 0.954319738487169, 0.846006844501568,
                                      1, "d1", "dem60", 0, "y4", 0.055262696069907, 15.3088232146939,
                                      0.778492136747106, 0.965853916278744, 0.872173026512925, 1,
                                      "a1", "dem65", 0, "y5", 0.0477972506151957, 18.247347186025,
                                      0.670874445220357, 0.924782374932184, 0.79782841007627, 1, "b1",
                                      "dem65", 0, "y6", 0.0647736212794269, 12.3171808881044, 0.736192764203544,
                                      0.949483381317672, 0.842838072760608, 1, "c1", "dem65", 0, "y7",
                                      0.0544118715436962, 15.4899665982589, 0.638932516077672, 0.895780248581011,
                                      0.767356382329342, 1, "d1", "dem65", 0, "y8", 0.0655235847518938,
                                      11.7111477529038, 0.892603067660146, 0.987227713040483, 0.939915390350315,
                                      1, "", "ind60", 0, "x1", 0.0241393837148856, 38.9370085604428,
                                      0.966178591286554, 1.02711848866817, 0.99664853997736, 1, "",
                                      "ind60", 0, "x2", 0.0155461778538532, 64.1089114859403, 0.767581949486914,
                                      0.946772730987092, 0.857177340237003, 1, "", "ind60", 0, "x3",
                                      0.0457127740391182, 18.7513743861504, 0.706136919918976, 0.947767379413977,
                                      0.826952149666476, 2, "a2", "dem60", 0, "y1", 0.0616415560186184,
                                      13.4154976460474, 0.566525632002294, 0.874563221728711, 0.720544426865503,
                                      2, "b2", "dem60", 0, "y2", 0.0785824617585268, 9.16927786100209,
                                      0.590939430066724, 0.883775789148265, 0.737357609607495, 2,
                                      "c2", "dem60", 0, "y3", 0.0747045255401114, 9.87032049633435,
                                      0.71791749736262, 0.949294319477457, 0.833605908420039, 2, "d2",
                                      "dem60", 0, "y4", 0.0590257841317257, 14.1227417929715, 0.636987861102235,
                                      0.910032539071769, 0.773510200087002, 2, "a2", "dem65", 0, "y5",
                                      0.0696555345208574, 11.1047916781887, 0.639973401045241, 0.91899976359984,
                                      0.77948658232254, 2, "b2", "dem65", 0, "y6", 0.071181502506047,
                                      10.950690205736, 0.655212218760868, 0.917196248738552, 0.78620423374971,
                                      2, "c2", "dem65", 0, "y7", 0.0668338887969831, 11.7635566013211,
                                      0.821079140048613, 0.992174422803808, 0.90662678142621, 2, "d2",
                                      "dem65", 0, "y8", 0.0436475578390146, 20.7715351399527, 0.80150660939515,
                                      0.974376101690713, 0.887941355542931, 2, "", "ind60", 0, "x1",
                                      0.0441001706304644, 20.1346467110842, 0.883183837546062, 1.01439639563358,
                                      0.94879011658982, 2, "", "ind60", 0, "x2", 0.0334732064268791,
                                      28.3447634053945, 0.769812525814014, 0.961513786087519, 0.865663155950766,
                                      2, "", "ind60", 0, "x3", 0.0489042813504788, 17.7011732315803
                                 ))
})

test_that("Indirect effects table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_more constrained"]][["collection"]][["modelContainer_params_more constrained_indeff"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.181426726046339, 0.595283467192247, 0.388355096619293, 1, "ind60 <unicode> dem60 <unicode> dem65",
                                      0.000234716419733383, 0.105577639285813, 3.67838397643997, -0.0178805535995936,
                                      0.584218782435681, 0.283169114418044, 2, "ind60 <unicode> dem60 <unicode> dem65",
                                      0.0652481912180689, 0.153599591825298, 1.84355382102913))
})

test_that("Factor variances table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_more constrained"]][["collection"]][["modelContainer_params_more constrained_lvar"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(1, 1, "ind60", 1, 1, "", "ind60", "", 0, "", 0.476503615944186,
                                      1.00720637226624, "dem60", 0.741854994105215, 1, "", "dem60",
                                      4.2638236719128e-08, 0.13538584395126, 5.47956102686991, -0.0597329835617426,
                                      0.181967102520766, "dem65", 0.0611170594795115, 1, "", "dem65",
                                      0.321585212838986, 0.0616593182295715, 0.99120556688543, 1,
                                      1, "ind60", 1, 2, "", "ind60", "", 0, "", 0.713892727083267,
                                      1.10500275328013, "dem60", 0.909447740181698, 2, "", "dem60",
                                      0, 0.0997747992518967, 9.11500446005067, -0.102083243995661,
                                      0.146310232800084, "dem65", 0.0221134944022115, 2, "", "dem65",
                                      0.727107499018486, 0.0633668472367455, 0.348975771503876))
})

test_that("Intercepts table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_more constrained"]][["collection"]][["modelContainer_params_more constrained_mu"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(5.86051219064208, 9.39574920418507, 7.62813069741357, 1, "", "x1",
                                      0, 0.901862748863879, 8.45819467210848, 2.6290529874543, 4.34339813302047,
                                      3.48622556023739, 1, "", "x2", 1.55431223447522e-15, 0.437340981540657,
                                      7.97141294181069, 2.10017843849562, 3.53720634191898, 2.8186923902073,
                                      1, "", "x3", 1.48769885299771e-14, 0.366595487151409, 7.68883548488183,
                                      1.59714903783991, 2.75140024062117, 2.17427463923054, 1, "",
                                      "y1", 1.53654866608122e-13, 0.294457248165234, 7.38400787475419,
                                      0.702486381776094, 1.4805125947743, 1.0914994882752, 1, "",
                                      "y2", 3.81302425189745e-08, 0.198479721855907, 5.49929976759847,
                                      1.54022734175689, 2.62630651943813, 2.08326693059751, 1, "",
                                      "y3", 5.52891066263328e-14, 0.277066105869316, 7.51902483366236,
                                      1.30626921553763, 2.3132920801797, 1.80978064785866, 1, "",
                                      "y4", 1.85806925401266e-12, 0.256898308485599, 7.04473555519777,
                                      1.45013261172104, 2.5320981839712, 1.99111539784612, 1, "",
                                      "y5", 5.44231326671252e-13, 0.276016697445606, 7.2137498067069,
                                      0.47031947992967, 1.21287409008052, 0.841596785005096, 1, "",
                                      "y6", 8.88084004246892e-06, 0.189430677300202, 4.44276923357755,
                                      1.51678499770959, 2.64170208021454, 2.07924353896206, 1, "",
                                      "y7", 4.31210622764411e-13, 0.286973916709222, 7.24540948809948,
                                      0.896223710043323, 1.72190724881864, 1.30906547943098, 1, "",
                                      "y8", 5.13962650217081e-10, 0.210637426322169, 6.21478102105546,
                                      0, 0, 0, 1, "", "ind60", "", 0, "", 0, 0, 0, 1, "", "dem60",
                                      "", 0, "", 0, 0, 0, 1, "", "dem65", "", 0, "", 5.73878418637196,
                                      9.14485948124601, 7.44182183380898, 2, "", "x1", 0, 0.868912725371675,
                                      8.56452163320057, 2.34618162199462, 3.88457672245525, 3.11537917222494,
                                      2, "", "x2", 1.99840144432528e-15, 0.392454941160987, 7.93818307653054,
                                      1.83077322070298, 3.11088429435698, 2.47082875752998, 2, "",
                                      "x3", 3.84137166520304e-14, 0.326564948068267, 7.56611746651226,
                                      1.61330533065698, 2.76370727596503, 2.18850630331101, 2, "",
                                      "y1", 8.83737527601625e-14, 0.293475276684234, 7.45720841645459,
                                      0.751411267033191, 1.54504323868885, 1.14822725286102, 2, "",
                                      "y2", 1.41673031084366e-08, 0.202460855892181, 5.67135433563759,
                                      1.30682402464077, 2.26888082935336, 1.78785242699706, 2, "",
                                      "y3", 3.22408766351145e-13, 0.245427164045148, 7.28465585279784,
                                      0.643615688374986, 1.41364427783669, 1.02862998310584, 2, "",
                                      "y4", 1.63764532601363e-07, 0.196439474280036, 5.2363710851693,
                                      1.42407129347664, 2.45848855033384, 1.94127992190524, 2, "",
                                      "y5", 1.88737914186277e-13, 0.263886802261816, 7.35648734709815,
                                      0.540424665987707, 1.29070379657038, 0.915564231279042, 2, "",
                                      "y6", 1.72285330291011e-06, 0.191401254436504, 4.78348082918533,
                                      1.34769819223259, 2.35223475424012, 1.84996647323635, 2, "",
                                      "y7", 5.23803223018149e-13, 0.256264035954533, 7.21898594293807,
                                      0.77694170490636, 1.6047174690235, 1.19082958696493, 2, "",
                                      "y8", 1.70874236982144e-08, 0.211171167084328, 5.63916752181132,
                                      0, 0, 0, 2, "", "ind60", "", 0, "", 0, 0, 0, 2, "", "dem60",
                                      "", 0, "", 0, 0, 0, 2, "", "dem65", "", 0, ""))
})

test_that("Regression coefficients table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_more constrained"]][["collection"]][["modelContainer_params_more constrained_reg"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.246948088250118, 0.769211359661054, 0.508079723955586, 1, "dem60",
                                      0.000137028525597716, "ind60", 0.133232874565676, 3.8134711542618,
                                      0.125064277507403, 0.520137737372459, 0.322601007439931, 1,
                                      "dem65", 0.00137020643230534, "ind60", 0.100785897848467, 3.20085462675508,
                                      0.597192504934487, 0.931524656206316, 0.764358580570402, 1,
                                      "dem65", 0, "dem60", 0.0852903813307282, 8.96183800147954, -0.0240106154690183,
                                      0.62584866617137, 0.300919025351176, 2, "dem60", 0.0695035021561337,
                                      "ind60", 0.165783475300157, 1.81513280986751, -0.036532801570809,
                                      0.301009323420742, 0.132238260924966, 2, "dem65", 0.124611236346324,
                                      "ind60", 0.0861092672248164, 1.53570301068426, 0.840981893953649,
                                      1.04104676201174, 0.941014327982693, 2, "dem65", 0, "dem60",
                                      0.051037893970546, 18.4375618736493))
})

test_that("Total effects table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_more constrained"]][["collection"]][["modelContainer_params_more constrained_toteff"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.529395308701935, 0.892516899416513, 0.710956104059224, 1, " ind60 <unicode> dem65",
                                      1.66533453693773e-14, 0.0926347610412321, 7.67483065825338,
                                      0.246948088250118, 0.769211359661054, 0.508079723955586, 1,
                                      " ind60 <unicode> dem60", 0.000137028525597716, 0.133232874565676,
                                      3.8134711542618, 0.597192504934487, 0.931524656206316, 0.764358580570402,
                                      1, " dem60 <unicode> dem65", 0, 0.0852903813307282, 8.96183800147954,
                                      0.122249254267857, 0.708565496418163, 0.41540737534301, 2, " ind60 <unicode> dem65",
                                      0.0054815182186132, 0.14957321837929, 2.77728446204596, -0.0240106154690183,
                                      0.62584866617137, 0.300919025351176, 2, " ind60 <unicode> dem60",
                                      0.0695035021561337, 0.165783475300157, 1.81513280986751, 0.840981893953649,
                                      1.04104676201174, 0.941014327982693, 2, " dem60 <unicode> dem65",
                                      0, 0.051037893970546, 18.4375618736493))
})


test_that("Residual variances table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_more constrained"]][["collection"]][["modelContainer_params_more constrained_var"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.0276198983128451, 0.205498219652386, "x1", 0.116559058982616,
                                      1, "", "x1", 0.0102102198050174, 0.0453779566213009, 2.5686273173416,
                                      -0.0540439743092332, 0.067427349831225, "x2", 0.00669168776099586,
                                      1, "", "x2", 0.829031845764272, 0.0309881521034592, 0.215943426979916,
                                      0.111648727477944, 0.418845287290491, "x3", 0.265247007384217,
                                      1, "", "x3", 0.000712721709110964, 0.0783679093686603, 3.38463804280443,
                                      0.045829287543847, 0.386521721779596, "y1", 0.216175504661721,
                                      1, "", "y1", 0.0128729427940009, 0.0869129323097483, 2.48726511598176,
                                      0.314162967920203, 0.746241288391645, "y2", 0.530202128155924,
                                      1, "", "y2", 1.50829964407073e-06, 0.110226086774966, 4.81013291561702,
                                      0.188463907098477, 0.605910075859458, "y3", 0.397186991478968,
                                      1, "", "y3", 0.000191716232100569, 0.106493326421746, 3.72968903146086,
                                      0.101005522604982, 0.467539315508016, "y4", 0.284272419056499,
                                      1, "", "y4", 0.0023643958031232, 0.0935052367783812, 3.04017645268638,
                                      0.0759023054939618, 0.402726118152608, "y5", 0.239314211823285,
                                      1, "", "y5", 0.00410034577114349, 0.0833749536309316, 2.87033697053237,
                                      0.160894871830018, 0.566044784320323, "y6", 0.36346982807517,
                                      1, "", "y6", 0.000437008899217162, 0.1033564687122, 3.51666260084085,
                                      0.109854525953604, 0.469393440256764, "y7", 0.289623983105184,
                                      1, "", "y7", 0.00159035616645609, 0.0917207961827762, 3.1576697451256,
                                      0.214070428429448, 0.608257936567403, "y8", 0.411164182498425,
                                      1, "", "y8", 4.33704886162456e-05, 0.100559885601791, 4.08874950521129,
                                      0.0580621807719502, 0.365058117461213, "x1", 0.211560149116582,
                                      2, "", "x1", 0.00690606036137908, 0.0783167290600256, 2.70134046270539,
                                      -0.0246958612340235, 0.224290490556977, "x2", 0.0997973146614766,
                                      2, "", "x2", 0.116144659991988, 0.0635180936371722, 1.57116356847135,
                                      0.0846785876448888, 0.41657601321383, "x3", 0.250627300429359,
                                      2, "", "x3", 0.00307565081309691, 0.0846692664219612, 2.96007407434385,
                                      0.116333308361028, 0.515966975962959, "y1", 0.316150142161994,
                                      2, "", "y1", 0.00192832793198194, 0.10194923752533, 3.1010545035557,
                                      0.258860957379336, 0.702770500446792, "y2", 0.480815728913064,
                                      2, "", "y2", 2.17788845422451e-05, 0.113244311265145, 4.2458267752391,
                                      0.240378636764273, 0.672228874343571, "y3", 0.456303755553922,
                                      2, "", "y3", 3.44449726010776e-05, 0.110167901294533, 4.14189387464136,
                                      0.112224099609071, 0.497978279285333, "y4", 0.305101189447202,
                                      2, "", "y4", 0.00193289187825618, 0.0984084867678799, 3.10035444571825,
                                      0.190479118720523, 0.61288482200221, "y5", 0.401681970361366,
                                      2, "", "y5", 0.000193302989238919, 0.10775853704802, 3.72761157830463,
                                      0.174903358412964, 0.609897977545288, "y6", 0.392400667979126,
                                      2, "", "y6", 0.000406088435395802, 0.110970054185563, 3.53609512817716,
                                      0.17590994488275, 0.587855860785313, "y7", 0.381882902834032,
                                      2, "", "y7", 0.000279213231582753, 0.10509017490932, 3.63385923720796,
                                      0.0229083123686103, 0.333147446032891, "y8", 0.178027879200751,
                                      2, "", "y8", 0.0244861252084452, 0.0791440904300812, 2.24941468444858
                                 ))
})

# default model covariance tables check for group 1
test_that("1 table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_covars"]][["collection"]][["modelContainer_covars_default"]][["collection"]][["modelContainer_covars_default_implied"]][["collection"]][["modelContainer_covars_default_implied_1"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.490083005636341, "", "", "", "", "", "", "", "", "", "", 0.974061924820938,
                                      2.20622852804643, "", "", "", "", "", "", "", "", "", 0.788491080043325,
                                      1.77466015582184, 1.95570584174645, "", "", "", "", "", "",
                                      "", "", 0.835178540092756, 1.8797398164842, 1.52162613108251,
                                      7.64797977172546, "", "", "", "", "", "", "", 0.9556665272501,
                                      2.15092263069126, 1.74114526494314, 6.68378929397842, 14.5127221504755,
                                      "", "", "", "", "", "", 0.788253740427642, 1.77412597456094,
                                      1.43613303237535, 5.51292920802482, 6.30825824454684, 9.62227326013949,
                                      "", "", "", "", "", 0.904244744111687, 2.03518740934666, 1.64745903478501,
                                      6.32415300473141, 9.23882105681506, 5.96882824654786, 9.02873362112342,
                                      "", "", "", "", 1.14334478995369, 2.57333087773905, 2.08307951619226,
                                      5.82366205597871, 6.32427984118542, 5.21639829184989, 5.98398776520799,
                                      7.98119516009608, "", "", "", 1.18710886159188, 2.67183085593611,
                                      2.1628140302039, 5.73848644656919, 7.58931007411125, 5.41606756969488,
                                      6.21303824196685, 6.20454284533334, 10.2893215377554, "", "",
                                      1.25970543427748, 2.83522426425122, 2.29507897323412, 6.08941841405952,
                                      6.96791532513606, 6.91162808862571, 6.59299099686961, 6.58397607191047,
                                      6.83599243915777, 9.94033521063152, "", 1.15531617741013, 2.60027492931626,
                                      2.10489039267505, 5.58480055205704, 6.39049819021455, 5.27101657115424,
                                      5.85298682041024, 6.03837521104467, 7.68811256412373, 6.65291356937699,
                                      10.6193105678442))
})

test_that("1 table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_covars"]][["collection"]][["modelContainer_covars_default"]][["collection"]][["modelContainer_covars_default_observed"]][["collection"]][["modelContainer_covars_default_observed_1"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.490082575997078, "", "", "", "", "", "", "", "", "", "", 0.973932449184952,
                                      2.20622591129467, "", "", "", "", "", "", "", "", "", 0.78518275394412,
                                      1.77565205254938, 1.95570361423433, "", "", "", "", "", "",
                                      "", "", 0.842173686632578, 1.60646357669832, 1.14904644265887,
                                      7.62580350620892, "", "", "", "", "", "", "", 0.966640855417165,
                                      2.56661076528232, 2.30600417597728, 6.02486581738495, 14.676007521986,
                                      "", "", "", "", "", "", 0.569651258731629, 1.29778304025924,
                                      0.7519816050897, 6.44636651314828, 5.79397804058276, 9.73209047934931,
                                      "", "", "", "", "", 1.07850808055391, 2.50663181045844, 2.0195628491114,
                                      6.09461043973703, 9.30788611653126, 5.64475186161118, 9.01279131583053,
                                      "", "", "", "", 1.23810412812272, 2.61063748721578, 1.81722127066318,
                                      5.59878435062089, 6.6520705502466, 5.03859665459321, 6.05661431962235,
                                      7.93189524242966, "", "", "", 1.43792308816034, 2.91763544604397,
                                      2.84145873393433, 5.38621551022644, 8.42865402090701, 4.21966332811403,
                                      6.90922843998977, 6.08535469304967, 10.3746040258343, "", "",
                                      1.04665319855464, 2.45836864046896, 1.77472198140307, 6.2463340788897,
                                      7.67698969559927, 6.7341948942526, 7.01801387855084, 6.81611022694302,
                                      6.30443702471928, 9.78003437719123, "", 1.23565159843141, 2.65937421505933,
                                      1.72765599854139, 5.4174694403214, 7.39753077439182, 4.59181056574472,
                                      6.45111697395995, 5.63862367744565, 7.72100725388949, 6.57011319209561,
                                      10.545706988345))
})


test_that("1 table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_covars"]][["collection"]][["modelContainer_covars_default"]][["collection"]][["modelContainer_covars_default_residual"]][["collection"]][["modelContainer_covars_default_residual_1"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(-4.29639262722326e-07, "", "", "", "", "", "", "", "", "", "",
                                      -0.000129475635985754, -2.6167517583886e-06, "", "", "", "",
                                      "", "", "", "", "", -0.00330832609920539, 0.000991896727537744,
                                      -2.22751211897965e-06, "", "", "", "", "", "", "", "", 0.00699514653982192,
                                      -0.27327623978588, -0.372579688423637, -0.0221762655165412,
                                      "", "", "", "", "", "", "", 0.0109743281670655, 0.415688134591064,
                                      0.56485891103414, -0.658923476593472, 0.16328537151047, "",
                                      "", "", "", "", "", -0.218602481696013, -0.476342934301694,
                                      -0.684151427285654, 0.933437305123464, -0.514280203964078, 0.10981721920982,
                                      "", "", "", "", "", 0.17426333644222, 0.471444401111772, 0.37210381432639,
                                      -0.229542564994381, 0.0690650597162001, -0.324076384936683,
                                      -0.0159423052928904, "", "", "", "", 0.0947593381690282, 0.0373066094767256,
                                      -0.265858245529077, -0.224877705357818, 0.327790709061186, -0.177801637256686,
                                      0.0726265544143585, -0.0492999176664268, "", "", "", 0.250814226568451,
                                      0.245804590107868, 0.67864470373043, -0.352270936342745, 0.83934394679576,
                                      -1.19640424158085, 0.696190198022921, -0.119188152283667, 0.0852824880789473,
                                      "", "", -0.213052235722839, -0.376855623782266, -0.520356991831051,
                                      0.156915664830179, 0.709074370463213, -0.17743319437311, 0.425022881681229,
                                      0.232134155032551, -0.53155541443849, -0.16030083344029, "",
                                      0.0803354210212781, 0.0590992857430699, -0.37723439413366, -0.16733111173564,
                                      1.00703258417727, -0.67920600540952, 0.598130153549713, -0.399751533599017,
                                      0.0328946897657652, -0.0828003772813819, -0.073603579499224
                                 ))
})

test_that("1 table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_covars"]][["collection"]][["modelContainer_covars_default"]][["collection"]][["modelContainer_covars_default_stdres"]][["collection"]][["modelContainer_covars_default_stdres_1"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(-4.29639262722326e-07, "", "", "", "", "", "", "", "", "", "",
                                      -0.9060620509028, -2.6167517583886e-06, "", "", "", "", "",
                                      "", "", "", "", -0.386009809456628, 1.26765266846776, -2.22751211897965e-06,
                                      "", "", "", "", "", "", "", "", 0.0846242017037104, -1.78626926801871,
                                      -1.50899731338325, -0.979353662951696, "", "", "", "", "", "",
                                      "", 0.0462187188848004, 0.91510411685768, 1.26724625016894,
                                      -3.1061390877585, 1.19239959132506, "", "", "", "", "", "",
                                      -1.56053141398549, -1.9857167211822, -1.9527355831674, 3.4813985041659,
                                      -0.952297065273916, 1.24738002520413, "", "", "", "", "", 1.70227576368341,
                                      2.48446501830487, 1.72887724327009, -2.99331507437673, 0.771585871131433,
                                      -1.33482628389661, -0.761717885856068, "", "", "", "", 1.47327037945439,
                                      0.272709782087367, -1.29757118956185, -2.40643758667, 0.874896574845803,
                                      -0.503887231903609, 0.37603567535386, -2.13674122503159, "",
                                      "", "", 2.35212346611509, 1.25498978664378, 3.03360225283525,
                                      -1.19107613633595, 2.12597830042828, -2.99704518860994, 2.32847953539746,
                                      -0.590849981828658, 0.80444014644474, "", "", -2.69577565127534,
                                      -2.24649212532582, -1.71719477088293, 0.644131212413233, 1.49678459768839,
                                      -0.98716825331695, 1.96201526731965, 1.27341637632093, -2.11609347217155,
                                      -2.41207540474613, "", 0.623012707174326, 0.275105560993087,
                                      -1.61236921599312, -0.529375302615407, 1.4412554289476, -1.29066565900695,
                                      2.49936790364071, -2.14139792711848, 0.273074594418215, -0.249446746350653,
                                      -2.59387512655443))
})


# bootstrapping works
options <- jaspTools::analysisOptions("SEM")
options$models <- list(list(name = "Model1", syntax = list(model = "x1 ~ x2 + x3 + y1", columns = c("x1", "x2", "x3", "y1"))))
options$emulation         <- "lavaan"
options$estimator         <- "default"
options$group             <- ""
options$samplingWeights   <- ""
options$informationMatrix <- "expected"
options$naAction          <- "fiml"
options$modelTest         <- "standard"
options$errorCalculationMethod    <- "bootstrap"
options$bootstrapCiType   <- "percentile"
options$bootstrapSamples  <- 100

set.seed(1)
results <- jaspTools::runAnalysis("SEM", "poldem_grouped.csv", options)

# Model fit table results match
test_that("Bootstrapping model fit table works", {

  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_fittab"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(46.1563554263454, 55.4263078804906, 9.99200722162641e-14, "", "Model1",
                                      75, 0, 4, 4))
})

test_that("Residual covariances table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_cov"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(1.78200922818399, 1.78200922818399, 1.78200922818399, "", "x2 - x3",
                                      "", 0, "", 1.2564136096, 1.2564136096, 1.2564136096, "", "x2 - y1",
                                      "", 0, "", 0.899301179999995, 0.899301179999995, 0.899301179999995,
                                      "", "x3 - y1", "", 0, ""))
})

test_that("Regression coefficients table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_reg"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.24960931619795, 0.447867152439228, 0.355178159312481, "x1",
                                      4.22839541158737e-12, "x2", 0.051257217491476, 6.92932969628222,
                                      -0.0367153099339115, 0.199977964917624, 0.0779182588187343,
                                      "x1", 0.152733815036115, "x3", 0.0544905227320188, 1.42994148178632,
                                      0.00301905711375213, 0.0567699544441715, 0.0306885890333793,
                                      "x1", 0.0306723196916876, "y1", 0.0141991468224129, 2.16129809890678
                                 ))
})

test_that("Total effects table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_toteff"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.24960931619795, 0.447867152439228, 0.355178159312481, " x2 <unicode> x1",
                                      3.13082892944294e-14, 0.046781092952712, 7.59234419066496, -0.0367153099339115,
                                      0.199977964917624, 0.0779182588187343, " x3 <unicode> x1", 0.112759818779606,
                                      0.0491315892354857, 1.5859095956631, 0.00301905711375213, 0.0567699544441715,
                                      0.0306885890333793, " y1 <unicode> x1", 0.0358943957021149,
                                      0.0146266963291098, 2.09812170450981))
})

test_that("Residual variances table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_var"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.0579281075433116, 0.121472727075618, "x1", 0.0973808537831735,
                                      "", "x1", 4.33176605696417e-10, 0.0156019590066371, 6.24157862110443,
                                      2.25167664969695, 2.25167664969695, "x2", 2.25167664969695,
                                      "", "x2", "", 0, "", 1.949678538072, 1.949678538072, "x3", 1.949678538072,
                                      "", "x3", "", 0, "", 6.78685155555556, 6.78685155555556, "y1",
                                      6.78685155555556, "", "y1", "", 0, ""))
})


# t-size RMSEA and CFI match values of original article (Katerina M. Marcoulides & Ke-Hai Yuan (2017)),
# also checks variance-covariance matrix input

options <- jaspTools::analysisOptions("SEM")
options$sampleSize <- 600
options$samplingWeights <- ""
options$additionalFitMeasures <- TRUE
options$informationMatrix <- "expected"
options$estimator <- "default"
options$modelTest <- "standard"
options$naAction <- "fiml"
options$emulation <- "mplus"
options$group <- ""
options$dataType <- "varianceCovariance"
options$models <- list(list(name = "Model1", syntax = list(model = "factor1 =~ V1 + V2 + V3 + V4 + V5 + V6 + V7\n factor2 =~ V8 + V9 + V10 + V11 + V12",
                                                           columns = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12"))))
set.seed(1)
dataset <- structure(list(V1 = c(1.321, 0.443, 0.283, 0.379, 0.462, 0.316, 0.392, 0.404, 0.398, 0.313, 0.374, 0.381),
                          V2 = c(0.443, 1.41, 0.507, 0.526, 0.466, 0.392, 0.404, 0.342, 0.493, 0.423, 0.448, 0.486),
                          V3 = c(0.283, 0.507, 1.485, 0.542, 0.411, 0.37, 0.352, 0.389, 0.437, 0.372, 0.359, 0.387),
                          V4 = c(0.379, 0.526, 0.542, 1.547, 0.527, 0.418, 0.481, 0.449, 0.45, 0.379, 0.368, 0.359),
                          V5 = c(0.462, 0.466, 0.411, 0.527, 1.524, 0.496, 0.478, 0.426, 0.447, 0.398, 0.348, 0.37),
                          V6 = c(0.316, 0.392, 0.37, 0.418, 0.496, 1.441, 0.387, 0.391, 0.48, 0.3, 0.404, 0.438),
                          V7 = c(0.392, 0.404, 0.352, 0.481, 0.478, 0.387, 1.422, 0.405, 0.412, 0.351, 0.335, 0.371),
                          V8 = c(0.404, 0.342, 0.389, 0.449, 0.426, 0.391, 0.405, 1.566, 0.657, 0.538, 0.591, 0.556),
                          V9 = c(0.398,0.493, 0.437, 0.45, 0.447, 0.48, 0.412, 0.657, 1.646, 0.599, 0.608, 0.69),
                          V10 = c(0.313, 0.423, 0.372, 0.379, 0.398, 0.3, 0.351, 0.538, 0.599, 1.675, 0.659, 0.529),
                          V11 = c(0.374, 0.448, 0.359, 0.368, 0.348, 0.404, 0.335, 0.591, 0.608, 0.659, 1.63, 0.64),
                          V12 = c(0.381, 0.486, 0.387, 0.359, 0.37, 0.438, 0.371, 0.556, 0.69, 0.529, 0.64, 1.673)),
                     class = "data.frame", row.names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12"))
results <- jaspTools::runAnalysis("SEM", dataset, options)


test_that("Fit indices table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_addfit"]][["collection"]][["modelContainer_addfit_fitMeasures"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list("Comparative Fit Index (CFI)", 0.996320596799013, "Tucker-Lewis Index (TLI)",
                                      0.995418101674243, "Bentler-Bonett Non-normed Fit Index (NNFI)",
                                      0.995418101674243, "Bentler-Bonett Normed Fit Index (NFI)",
                                      0.961983689254564, "Parsimony Normed Fit Index (PNFI)", 0.772502053492302,
                                      "Bollen's Relative Fit Index (RFI)", 0.952658933788703, "Bollen's Incremental Fit Index (IFI)",
                                      0.99635284058013, "Relative Noncentrality Index (RNI)", 0.996320596799013,
                                      "Root mean square error of approximation (RMSEA)", 0.0130437119174326,
                                      "RMSEA 90% CI lower bound", 0, "RMSEA 90% CI upper bound",
                                      0.0297980428059356, "RMSEA p-value", 0.999994986450328, "Standardized root mean square residual (SRMR)",
                                      0.0253510370222869, "Hoelter's critical N (<unicode> = .05)",
                                      730.254899917344, "Hoelter's critical N (<unicode> = .01)",
                                      821.1621871771, "Goodness of fit index (GFI)", 0.984016681546699,
                                      "McDonald fit index (MFI)", 0.99550148064049, "Expected cross validation index (ECVI)",
                                      0.18068400295767, "Log-likelihood", -10988.8301074082, "Number of free parameters",
                                      25, "Akaike (AIC)", 22027.6602148165, "Bayesian (BIC)", 22137.5834561969,
                                      "Sample-size adjusted Bayesian (SSABIC)", 22058.2153051905
                                 ))
})

test_that("T-size fit indices table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_addfit"]][["collection"]][["modelContainer_addfit_fitTSize"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.974924004613771, "Estimate", 0.0298232086120238, 0.86665680157994,
                                      "Poor-fair limit", 0.0894318589913011, 0.927136160819729, "Fair-close limit",
                                      0.0604540069726537))
})



test_that("Variance-covariance input works", {
  options <- jaspTools::analysisOptions("SEM")
  options$dataType          <- "varianceCovariance"
  options$sampleSize        <- 75
  options$emulation         <- "lavaan"
  options$estimator         <- "default"
  options$group             <- ""
  options$samplingWeights   <- ""
  options$informationMatrix <- "expected"
  options$naAction          <- "fiml"
  options$modelTest         <- "standard"
  options$models <- list(
    list(name = "Model1", syntax = list(model = "F =~ x1 + x3 + y1", columns = c("x1", "x2", "x3"))),
    list(name = "Model2", syntax = list(model = "F =~ y1 + y2 + y3", columns = c("y1", "y2", "y3")))
  )

  data <- read.csv("poldem_grouped.csv")
  # data <- read.csv("tests/testthat/poldem_grouped.csv")
  data <- cov(data)
  data <- as.data.frame(data)

  set.seed(1)
  results <- jaspTools::runAnalysis("SEM", data, options)
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_fittab"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(707.570148255052, 721.47507693627, 6.66133814775094e-14, 0, "Model1",
                                      75, "", "", "", "", 6, 6, 1095.63355300897, 1109.53848169019,
                                      0, 0, "Model2", 75, "", "", -6.66133814775094e-14, 0, 6, 6
                                 ))
})


test_that("Fixing mean manifest intercepts works", {
  options <- jaspTools::analysisOptions("SEM")
  options$models <- list(list(name = "Model1", syntax = list(model = "factor =~ x2 + x3 + y1 + x1", columns = c("x1", "x2", "x3", "y1"))))
  options$emulation         <- "lavaan"
  options$estimator         <- "default"
  options$meanStructure     <- TRUE
  options$manifestMeanFixedToZero <- TRUE
  options$group             <- "group"
  options$samplingWeights   <- ""
  options$informationMatrix <- "expected"
  options$naAction          <- "fiml"
  options$modelTest         <- "standard"

  set.seed(1)

  results <- jaspTools::runAnalysis("SEM", "poldem_grouped.csv", options)
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_mu"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(-3.0031653252094, -0.238164753555845, -1.62066503938262, 1, "",
                                      "x2", 0.0215840525894235, 0.705370250031003, -2.29760900649183,
                                      -2.90748296601922, -0.257527727837225, -1.58250534692822, 1,
                                      "", "x3", 0.0192367987813871, 0.676021411384215, -2.34091009586205,
                                      -2.02601609127232, 3.81817181538369, 0.896077862055687, 1, "",
                                      "y1", 0.547816766514775, 1.49089165738611, 0.601034862336494,
                                      1.63165251250819, 2.98253253600213, 2.30709252425516, 1, "",
                                      "x1", 2.16224815829946e-11, 0.344618583338652, 6.6946259888371,
                                      5.33697746290641, 8.26080927533766, 6.79889336912203, 1, "",
                                      "factor", 0, 0.745889168243412, 9.11515230222956, -4.07239223241945,
                                      -0.696581025547053, -2.38448662898325, 2, "", "x2", 0.00562595928754916,
                                      0.861192152891677, -2.76882066444372, -3.8707672830057, -0.886230339808109,
                                      -2.37849881140691, 2, "", "x3", 0.00178440810521097, 0.761375455554092,
                                      -3.12394994356096, -0.553574385842091, 6.22823829704087, 2.83733195559939,
                                      2, "", "y1", 0.101006267898262, 1.73008604657459, 1.63999470501311,
                                      1.12212007800144, 2.7291868915801, 1.92565348479077, 2, "",
                                      "x1", 2.63986228365987e-06, 0.409973557232427, 4.69701874869713,
                                      5.05992393096499, 8.54168933431517, 6.80080663264008, 2, "",
                                      "factor", 1.90958360235527e-14, 0.88822178132198, 7.65665374983052
                                 ))
})


# multigroup sem with clickable equality constraints
options <- jaspTools::analysisOptions("SEM")
options$emulation                   = "lavaan"
options$estimator                   = "default"
options$group                       = "group"
options$informationMatrix           = "expected"
options$naAction                    = "listwise"
options$modelTest                   = "default"
options$equalLatentVariance <- TRUE
options$equalThreshold <- TRUE
options$equalResidualCovariance <- TRUE
options$samplingWeights             = ""

modelDefault <- list(model = "
  # latent variable definitions
    ind60 =~ x1 + x2 + x3
    dem60 =~ y1 + y2 + y3 + y4
    dem65 =~ y5 + y6 + y7 + y8
  # regressions
    dem60 ~ ind60
    dem65 ~ ind60 + dem60
  # residual (co)variances
    y1 ~~ y5
    y2 ~~ y4 + y6
    y3 ~~ y7
    y4 ~~ y8
    y6 ~~ y8
  ", columns = c("x1", "x2", "x3", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8"))

options$models = list(
  list(name = "default", syntax = modelDefault)
)

results <- jaspTools::runAnalysis("SEM", "poldem_grouped.csv", options)


test_that("Residual covariances table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_cov"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(-0.00951791257482959, 1.24816024605178, 0.619321166738473, 1,
                                      ".p15.", "y1 - y5", 0.0535698529369328, 0.320842160505757, 1.93029857971973,
                                      0.275192537634027, 2.86800908936388, 1.57160081349895, 1, ".p16.",
                                      "y2 - y4", 0.0175009166166575, 0.661444948014774, 2.37601151572156,
                                      0.268525824232089, 2.7211595743112, 1.49484269927165, 1, ".p17.",
                                      "y2 - y6", 0.0168880472594957, 0.625683372098972, 2.38913604856865,
                                      -0.133131830969623, 2.16082046556287, 1.01384431729662, 1, ".p18.",
                                      "y3 - y7", 0.0831904444650153, 0.585202665617047, 1.73246701846037,
                                      -0.317356322859665, 1.22820518019921, 0.455424428669772, 1,
                                      ".p19.", "y4 - y8", 0.248061966101383, 0.394283138682666, 1.15506950206236,
                                      -0.107502588115688, 1.92491220954654, 0.908704810715428, 1,
                                      ".p20.", "y6 - y8", 0.0796666998015847, 0.518482689910034, 1.75262323776538,
                                      -0.00951791257482848, 1.24816024605178, 0.619321166738474, 2,
                                      ".p15.", "y1 - y5", 0.0535698529369324, 0.320842160505757, 1.93029857971974,
                                      0.275192537634027, 2.86800908936388, 1.57160081349895, 2, ".p16.",
                                      "y2 - y4", 0.0175009166166575, 0.661444948014774, 2.37601151572156,
                                      0.268525824232089, 2.7211595743112, 1.49484269927165, 2, ".p17.",
                                      "y2 - y6", 0.0168880472594957, 0.625683372098972, 2.38913604856865,
                                      -0.133131830969623, 2.16082046556287, 1.01384431729662, 2, ".p18.",
                                      "y3 - y7", 0.0831904444650153, 0.585202665617047, 1.73246701846037,
                                      -0.317356322859664, 1.22820518019921, 0.455424428669772, 2,
                                      ".p19.", "y4 - y8", 0.248061966101383, 0.394283138682666, 1.15506950206236,
                                      -0.107502588115688, 1.92491220954654, 0.908704810715428, 2,
                                      ".p20.", "y6 - y8", 0.0796666998015847, 0.518482689910034, 1.75262323776538
                                 ))
})

test_that("Factor Loadings table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_ind"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(1, 1, 1, 1, "", "dem60", "", "y1", 0, "", 0.756011678010104, 1.67072819069569,
                                      1.2133699343529, 1, "", "dem60", 1.9952676000301e-07, "y2",
                                      0.233350337021689, 5.19977793835421, 0.589605318467516, 1.32872910566561,
                                      0.959167212066564, 1, "", "dem60", 3.63917969981031e-07, "y3",
                                      0.188555451280791, 5.08692379642846, 0.805825133517716, 1.44228139685688,
                                      1.1240532651873, 1, "", "dem60", 4.42068603945245e-12, "y4",
                                      0.162364275149812, 6.92303318664248, 1, 1, 1, 1, "", "dem65",
                                      "", "y5", 0, "", 0.700926359250009, 1.43830977067363, 1.06961806496182,
                                      1, "", "dem65", 1.29983670493772e-08, "y6", 0.18811146971067,
                                      5.68608637531234, 0.772761413508819, 1.46047960799357, 1.11662051075119,
                                      1, "", "dem65", 1.95760740950846e-10, "y7", 0.175441538699023,
                                      6.36463017271413, 0.624931701232413, 1.39554577224672, 1.01023873673957,
                                      1, "", "dem65", 2.76438375079735e-07, "y8", 0.196588834563496,
                                      5.13884086541687, 1, 1, 1, 1, "", "ind60", "", "x1", 0, "",
                                      1.96896816200596, 2.59152950311334, 2.28024883255965, 1, "",
                                      "ind60", 0, "x2", 0.158819586997022, 14.3574786691921, 1.41188756682593,
                                      2.28258726764383, 1.84723741723488, 1, "", "ind60", 0, "x3",
                                      0.222121352148783, 8.31634329327129, 1, 1, 1, 2, "", "dem60",
                                      "", "y1", 0, "", 0.946873365788966, 1.95400766095123, 1.4504405133701,
                                      2, "", "dem60", 1.64848641404092e-08, "y2", 0.256926735161057,
                                      5.64534676572633, 0.693457886245215, 1.60751683650406, 1.15048736137464,
                                      2, "", "dem60", 8.06253452667605e-07, "y3", 0.233182588422242,
                                      4.93384763055875, 0.985835136390041, 1.80735761387033, 1.39659637513018,
                                      2, "", "dem60", 2.6662450025583e-11, "y4", 0.209575911588261,
                                      6.66391649949722, 1, 1, 1, 2, "", "dem65", "", "y5", 0, "",
                                      0.990824643773827, 2.24686561848509, 1.61884513112946, 2, "",
                                      "dem65", 4.36775339229811e-07, "y6", 0.320424503873223, 5.05218892925231,
                                      1.02335110163161, 2.23722284550416, 1.63028697356788, 2, "",
                                      "dem65", 1.40458174646696e-07, "y7", 0.309666849352186, 5.26464804669404,
                                      1.1798456720653, 2.29947786139703, 1.73966176673116, 2, "",
                                      "dem65", 1.12414877406763e-09, "y8", 0.285625704901529, 6.09070450200173,
                                      1, 1, 1, 2, "", "ind60", "", "x1", 0, "", 1.84998352886442,
                                      2.73263225194019, 2.2913078904023, 2, "", "ind60", 0, "x2",
                                      0.225169628125308, 10.1759189704181, 1.46115134483908, 2.34512680229753,
                                      1.90313907356831, 2, "", "ind60", 0, "x3", 0.225508086993215,
                                      8.43933846871561))
})

test_that("Factor variances table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_lvar"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.236834368913409, 0.526943426776363, "ind60", 0.381888897844886,
                                      1, ".p32.", "ind60", 2.46885135668506e-07, 0.0740087726487062,
                                      5.16004906144815, 2.10074625444669, 5.55500000431209, "dem60",
                                      3.82787312937939, 1, ".p33.", "dem60", 1.39965271599429e-05,
                                      0.881203373406886, 4.34391565545212, -0.163743825408124, 0.471589188522818,
                                      "dem65", 0.153922681557347, 1, ".p34.", "dem65", 0.342272660831397,
                                      0.162077726668033, 0.949684356522418, 0.236834368913409, 0.526943426776364,
                                      "ind60", 0.381888897844886, 2, ".p32.", "ind60", 2.46885135668506e-07,
                                      0.0740087726487062, 5.16004906144815, 2.10074625444669, 5.55500000431209,
                                      "dem60", 3.82787312937939, 2, ".p33.", "dem60", 1.39965271599429e-05,
                                      0.881203373406886, 4.34391565545212, -0.163743825408123, 0.471589188522818,
                                      "dem65", 0.153922681557347, 2, ".p34.", "dem65", 0.342272660831395,
                                      0.162077726668033, 0.949684356522422))
})

test_that("Regression coefficients table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_reg"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.74768461194592, 3.13257692292812, 1.94013076743702, 1, "dem60",
                                      0.00142817392050287, "ind60", 0.608402075189629, 3.18889570985161,
                                      0.348414558253314, 1.77200632968998, 1.06021044397165, 1, "dem65",
                                      0.0035077327559101, "ind60", 0.363167839477097, 2.91934011970383,
                                      0.561449579948802, 1.07483914739603, 0.818144363672416, 1, "dem65",
                                      4.188163149621e-10, "dem60", 0.130969132978152, 6.24684874266441,
                                      -0.159200803124074, 2.11012259112266, 0.975460893999292, 2,
                                      "dem60", 0.0919948186806887, "ind60", 0.578919666929308, 1.68496762110935,
                                      -0.060427080504905, 0.893216563537904, 0.4163947415165, 2, "dem65",
                                      0.0869740956449763, "ind60", 0.243280910150653, 1.7115800054293,
                                      0.520132317569988, 1.00885279137076, 0.764492554470373, 2, "dem65",
                                      8.68684457699942e-10, "dem60", 0.124675881203873, 6.13183999253438
                                 ))
})

test_that("Residual variances table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_params"]][["collection"]][["modelContainer_params_var"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.0203903214908377, 0.0937153039795448, "x1", 0.0570528127351912,
                                      1, "", "x1", 0.00228823496004704, 0.0187056963972515, 3.05002345400914,
                                      -0.118209788938257, 0.149904178675381, "x2", 0.0158471948685619,
                                      1, "", "x2", 0.816777242474263, 0.0683976771329697, 0.231692003776004,
                                      0.265272329814418, 0.77118752500936, "x3", 0.518229927411889,
                                      1, "", "x3", 5.93589306818743e-05, 0.129062370325561, 4.0153448763156,
                                      0.955937394672428, 3.44425722642889, "y1", 2.20009731055066,
                                      1, "", "y1", 0.000528495119347161, 0.63478713164732, 3.46588202700525,
                                      3.66970107786074, 9.74166689190863, "y2", 6.70568398488469,
                                      1, "", "y2", 1.49759018457374e-05, 1.54899933415685, 4.3290425224971,
                                      2.27047423539234, 6.74954147085745, "y3", 4.51000785312489,
                                      1, "", "y3", 7.91345249973041e-05, 1.1426401890023, 3.94700614990867,
                                      0.808965794775632, 3.41798227151407, "y4", 2.11347403314485,
                                      1, "", "y4", 0.00149631062392186, 0.66557765788505, 3.17539810434842,
                                      1.11694253229257, 3.50064331730598, "y5", 2.30879292479928,
                                      1, "", "y5", 0.000146608976578477, 0.608098108897852, 3.79674412897592,
                                      2.01550902816011, 5.70617179577256, "y6", 3.86084041196633,
                                      1, "", "y6", 4.11943023612693e-05, 0.941512904503329, 4.1006771054328,
                                      1.24180041617095, 4.19509083800568, "y7", 2.71844562708831,
                                      1, "", "y7", 0.000308309240888649, 0.753404257713385, 3.60821643793057,
                                      2.48138223648679, 6.90158475152541, "y8", 4.6914834940061, 1,
                                      "", "y8", 3.17543815182564e-05, 1.12762340275245, 4.16050561078682,
                                      0.029510673036908, 0.141539110406694, "x1", 0.0855248917218009,
                                      2, "", "x1", 0.00276651561179819, 0.0285792081521528, 2.99255638107519,
                                      -0.0267791677359907, 0.438546862055045, "x2", 0.205883847159527,
                                      2, "", "x2", 0.0828515610609226, 0.118707801128354, 1.73437503856139,
                                      0.176021005738021, 0.655774374392718, "x3", 0.415897690065369,
                                      2, "", "x3", 0.000678354170778217, 0.12238831234628, 3.39818142837566,
                                      0.574750605514609, 2.32900512597363, "y1", 1.45187786574412,
                                      2, "", "y1", 0.00117756468638741, 0.44752213160455, 3.24425936330466,
                                      3.6870584232221, 9.87516092384412, "y2", 6.78110967353311, 2,
                                      "", "y2", 1.74240725272501e-05, 1.57862658432323, 4.29557549636745,
                                      3.2208970735798, 9.03569722755529, "y3", 6.12829715056754, 2,
                                      "", "y3", 3.60771566090268e-05, 1.48339464394292, 4.13126552370331,
                                      1.74649287998714, 5.53471771963364, "y4", 3.64060529981039,
                                      2, "", "y4", 0.000165104461237187, 0.966401645521942, 3.76717622189492,
                                      1.12693384686582, 3.23414942031535, "y5", 2.18054163359059,
                                      2, "", "y5", 4.98493706548864e-05, 0.537564871107577, 4.05633208341514,
                                      2.42234871563612, 6.77525180716898, "y6", 4.59880026140255,
                                      2, "", "y6", 3.45243234869397e-05, 1.1104548669945, 4.14136620775002,
                                      1.93723745179962, 6.03541205409186, "y7", 3.98632475294574,
                                      2, "", "y7", 0.000137321827685, 1.04547191545817, 3.81294293419518,
                                      0.313473463282918, 2.61122637106262, "y8", 1.46234991717277,
                                      2, "", "y8", 0.0126047867836248, 0.586172227118478, 2.494744461643
                                 ))
})


# Sensitivity analysis works
options <- analysisOptions("SEM")
options$samplingWeights <- ""
options$sensitivityAnalysis <- TRUE
options$sizeOfSolutionArchive <- 10
options$maxIterations <- 10
options$informationMatrix <- "expected"
options$estimator <- "default"
options$modelTest <- "standard"
options$emulation <- "lavaan"
options$group <- ""
options$setSeed <- TRUE
options$seed <- 1
model <- "
# latent variable definitions
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
# residual (co)variances
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
"
options$models <- list(list(name = "Model1",
                            syntax = list(model=model,
                                          columns = c("x1", "x2", "x3", "y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8"))))
set.seed(1)
results <- runAnalysis("SEM", "poldem_grouped.csv", options)

test_that("Sensitivity parameters that led to a change in significance table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_sensitivity"]][["collection"]][["modelContainer_sensitivity_senpar"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.163825629932947, -0.00736627172133829, 1.83420695785249, "dem60~ind60",
                                      0.136451103548822, -0.356128009622868, -0.859719801919833, "dem65~ind60"
                                 ))
})

test_that("Summary of sensitivity analysis table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_sensitivity"]][["collection"]][["modelContainer_sensitivity_sensum"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.446712905437343, 0.773905343716094, 0.379067079696325, -0.0960469939853047,
                                      "dem60~ind60", 1.54185576191068e-05, 0.243857952730406, 0.885228943292037,
                                      1.20492628703677, 0.894340089736308, 0.770806763710209, "dem65~dem60",
                                      0, "", 0.182259409976201, 0.380588637180451, 0.0646300844286729,
                                      -0.56059414912222, "dem65~ind60", 0.00967602063729744, 0.125746802403301
                                 ))
})

test_that("Summary of sensitivity parameters table results match", {
  table <- results[["results"]][["modelContainer"]][["collection"]][["modelContainer_sensitivity"]][["collection"]][["modelContainer_sensitivity_sensumpar"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.332587428764211, 0.0594414507904125, -0.268377141254013, "dem60~phantom",
                                      0.467843048994546, 0.0319319696951054, -0.462805738408711, "dem65~phantom",
                                      2.16041463170958, 0.508721525156672, -1.30877622339889, "ind60~phantom"
                                 ))
})
jasp-stats/jaspSem documentation built on Oct. 19, 2024, 2:22 a.m.