tests/testthat/test-confirmatoryfactoranalysis.R

context("Confirmatory Factor Analysis")

# 3-factor run
options <- jaspTools::analysisOptions("confirmatoryFactorAnalysis")
options$group <- ""
options$invarianceTesting <- "configural"
options$packageMimiced <- "lavaan"
options$seType <- "standard"
options$estimator <- "default"
options$standardized <- "none"
options$factors <- list(
  list(indicators = list("x1", "x2", "x3"), name = "Factor1", title = "Factor 1"),
  list(indicators = list("x4", "x5", "x6"), name = "Factor2", title = "Factor 2"),
  list(indicators = list("x7", "x8", "x9"), name = "Factor3", title = "Factor 3")
)
options$modelIdentification <- "factorVariance"
options$naAction <- "listwise"
options$kaiserMeyerOlkinTest <- TRUE
options$bartlettTest <- TRUE

set.seed(1)

results <- jaspTools::runAnalysis("confirmatoryFactorAnalysis", "holzingerswineford.csv", options)


test_that("[CFA 3-Factor] Factor Covariances table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fc"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.333504675491998, 0.583514923484581, 0.45850979948829, "Factor 1",
                                      "<unicode>", 6.52589093874667e-13, "Factor 2", 0.063779296447443,
                                      7.18900685689003, 0.327797106505315, 0.613272259288624, 0.47053468289697,
                                      "Factor 1", "<unicode>", 1.03996145028873e-10, "Factor 3", 0.0728266322838328,
                                      6.46102487704112, 0.148278758433621, 0.417692356258714, 0.282985557346168,
                                      "Factor 2", "<unicode>", 3.83174073319559e-05, "Factor 3", 0.0687292215444246,
                                      4.11739797115633))
})

test_that("[CFA 3-Factor] Factor loadings table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fl1"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.741164020131046, 1.05807660760393, 0.899620313867488,
                                      "Factor 1", 0, "x1", 0.0808465333987386, 11.1275063512065, 0.346131928251774,
                                      0.649749093967046, 0.49794051110941,  "Factor 1",
                                      1.28623778294923e-10, "x2", 0.0774547818506273, 6.42878979466621,
                                      0.510293170576109, 0.802019014680793, 0.656156092628451,
                                      "Factor 1", 0, "x3", 0.0744212256974568, 8.81678696472846, 0.878687521771487,
                                      1.1006993764173, 0.989693449094392, "Factor 2",
                                      0, "x4", 0.0566367179185465, 17.4744138690689, 0.978762425338572,
                                      1.22444687472433, 1.10160465003145, "Factor 2",
                                      0, "x5", 0.0626757561168699, 17.5762482701815, 0.811432261987188,
                                      1.02176969353156, 0.916600977759373, "Factor 2",
                                      0, "x6", 0.0536584940344529, 17.0821226769958, 0.483096088879333,
                                      0.75585477823652, 0.619475433557926, "Factor 3",
                                      0, "x7", 0.0695825769015842, 8.90273774186456, 0.601768916407813,
                                      0.860128689422337, 0.730948802915075, "Factor 3",
                                      0, "x8", 0.0659093164600047, 11.0902197469857, 0.54254918241612,
                                      0.797411035146398, 0.669980108781259, "Factor 3",
                                      0, "x9", 0.0650169734598685, 10.3046954222623))
})

test_that("[CFA 3-Factor] Factor variances table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fv"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(1, 1, 1, "Factor 1", "", 0, "", 1, 1, 1, "Factor 2", "", 0, "",
                                      1, 1, 1, "Factor 3", "", 0, ""))
})

test_that("[CFA 3-Factor] Residual variances table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_rv"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.326399894319287, 0.771706936936134, 0.549053415627711, "x1",
                                      1.34368055770828e-06, 0.113600822803218, 4.83318168019605, 0.934462587447057,
                                      1.33321007058646, 1.13383632901676, "x2", 0, 0.10172316590628,
                                      11.1462941495686, 0.666706476085054, 1.02194282083783, 0.844324648461441,
                                      "x3", 0, 0.0906231817407956, 9.31687270566615, 0.277647747273574,
                                      0.464697982595281, 0.371172864934427, "x4", 7.32747196252603e-15,
                                      0.047717773591029, 7.77850341710428, 0.331807290171594, 0.560702710555418,
                                      0.446255000363506, "x5", 2.1316282072803e-14, 0.0583927618541264,
                                      7.64229993913142, 0.271855645519897, 0.44054959934542, 0.356202622432658,
                                      "x6", 2.22044604925031e-16, 0.0430349626718039, 8.27705196700539,
                                      0.639885213665674, 0.958894859526553, 0.799390036596114, "x7",
                                      0, 0.0813815071034943, 9.82274800563124, 0.342279857209668,
                                      0.63311496350216, 0.487697410355914, "x8", 4.92208496183366e-11,
                                      0.0741939924882706, 6.57327357646934, 0.427489578257306, 0.704773115754749,
                                      0.566131347006028, "x9", 1.11022302462516e-15, 0.0707368961074339,
                                      8.00333882541577))
})

test_that("[CFA 3-Factor] Chi-square test table results match", {
  table <- results[["results"]][["maincontainer"]][["collection"]][["maincontainer_cfatab"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(918.851589292384, 36, "Baseline model", "", 85.305521772505, 24,
                                      "Factor model", 8.50255310602677e-09))
})

test_that("Kaiser-Meyer-Olkin (KMO) test table results match", {
  table <- results[["results"]][["maincontainer"]][["collection"]][["maincontainer_kmo"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list("x1", 0.805021226632053, "x2", 0.777938285398466, "x3", 0.734302877282004,
                                      "x4", 0.763262045280562, "x5", 0.73872385651986, "x6", 0.807559054803425,
                                      "x7", 0.593046810632567, "x8", 0.682937939231282, "x9", 0.787864851859471,
                                      "Overall", 0.752244592603047))
})

test_that("Bartlett's test of sphericity table results match", {
  table <- results[["results"]][["maincontainer"]][["collection"]][["maincontainer_bartlett"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(904.097051036859, 36, 1.9120788740615e-166))
})


# Second-order factor
options <- jaspTools::analysisOptions("confirmatoryFactorAnalysis")
options$secondOrder <- list("Factor 1", "Factor 2", "Factor 3")
options$group <- ""
options$invarianceTesting <- "configural"
options$packageMimiced <- "lavaan"
options$seType <- "standard"
options$estimator <- "default"
options$standardized <- "none"
options$factors <- list(
  list(indicators = list("x1", "x2", "x3"), name = "Factor1", title = "Factor 1"),
  list(indicators = list("x4", "x5", "x6"), name = "Factor2", title = "Factor 2"),
  list(indicators = list("x7", "x8", "x9"), name = "Factor3", title = "Factor 3")
)
options$modelIdentification <- "factorVariance"
options$naAction <- "listwise"
set.seed(1)
results <- jaspTools::runAnalysis("confirmatoryFactorAnalysis", "holzingerswineford.csv", options)


test_that("[CFA Second order] Factor loadings table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fl1"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.0577039904414908, 0.819337251278242, 0.438520620859867,
                                      "Factor 1", 0.0240111123688183, "x1", 0.194297769460158, 2.25695139001474,
                                      0.0315641080012427, 0.453878987089273, 0.242721547545258,
                                      "Factor 1", 0.0242627980684818, "x2", 0.107735367185111, 2.25294212928438,
                                      0.0503061064249717, 0.589382650102084, 0.319844378263528,
                                      "Factor 1", 0.0200309356495567, "x3", 0.137522053448247, 2.32576790590095,
                                      0.71768085442071, 0.966830831063184, 0.842255842741947,
                                      "Factor 2", 0, "x4", 0.0635598354377267, 13.2513848870354, 0.799268757944165,
                                      1.07572151104007, 0.937495134492118, "Factor 2",
                                      0, "x5", 0.0705249574167, 13.2930974910468, 0.663204523777401,
                                      0.896899759208491, 0.780052141492946, "Factor 2",
                                      0, "x6", 0.0596172269680586, 13.0843412410121, 0.392491072303089,
                                      0.651164920657023, 0.521827996480056, "Factor 3",
                                      2.66453525910038e-15, "x7", 0.0659894391923322, 7.90775013194371,
                                      0.483529756039699, 0.747933394180798, 0.615731575110249,
                                      "Factor 3", 0, "x8", 0.0674511471197128, 9.12855602021777, 0.438789772679437,
                                      0.689958088778821, 0.564373930729129, "Factor 3",
                                      0, "x9", 0.0640747274135055, 8.80805823935775))
})

test_that("[CFA Second order] Second-order factor loadings table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fl2"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(-0.14910405407519, 3.73161619230762, 1.79125606911621,
                                      "SecondOrder", 0.0703961021446211, "Factor 1", 0.989997846132234,
                                      1.80935350123676, 0.364985992598308, 0.869100374746346, 0.617043183672327,
                                      "SecondOrder", 1.6021965671964e-06, "Factor 2",
                                      0.128602970800593, 4.79804766430388, 0.360410276598517, 0.919053360468363,
                                      0.63973181853344, "SecondOrder", 7.15860527811252e-06,
                                      "Factor 3", 0.142513609504142, 4.48891737960541))
})

test_that("[CFA Second order] Factor variances table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fv"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(1, 1, 1, "Factor 1", "", 0, "", 1, 1, 1, "Factor 2", "", 0, "",
                                      1, 1, 1, "Factor 3", "", 0, "", 1, 1, 1, "Second-Order", "",
                                      0, ""))
})

test_that("[CFA Second order] Residual variances table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_rv"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.326401450955574, 0.771708269797423, 0.549054860376498, "x1",
                                      1.34357828596165e-06, 0.11360076571671, 4.83319682673349, 0.934464040890642,
                                      1.33321206655867, 1.13383805372466, "x2", 0, 0.101723304308982,
                                      11.1462959390372, 0.666705522978852, 1.02194196604298, 0.844323744510915,
                                      "x3", 0, 0.0906232068206831, 9.3168601524065, 0.2776477911548,
                                      0.464698082724097, 0.371172936939448, "x4", 7.32747196252603e-15,
                                      0.0477177879401676, 7.77850258701964, 0.331807404885689, 0.560702857278213,
                                      0.446255131081951, "x5", 2.1316282072803e-14, 0.0583927700197611,
                                      7.64230110904024, 0.271855604188449, 0.44054957527582, 0.356202589732134,
                                      "x6", 2.22044604925031e-16, 0.0430349670754176, 8.27705036018498,
                                      0.63988809304374, 0.958898264282657, 0.799393178663198, "x7",
                                      0, 0.0813816411309667, 9.82277043758238, 0.342280111000572,
                                      0.633115637435664, 0.487697874218118, "x8", 4.92219598413612e-11,
                                      0.0741940996694749, 6.57327033269154, 0.427488355941731, 0.704772286642682,
                                      0.566130321292206, "x9", 1.11022302462516e-15, 0.0707369964162943,
                                      8.00331297586447))
})

test_that("[CFA Second order] Chi-square test table results match", {
  table <- results[["results"]][["maincontainer"]][["collection"]][["maincontainer_cfatab"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(918.851589292384, 36, "Baseline model", "", 85.3055217707089,
                                      24, "Factor model", 8.50255321704907e-09))
})


test_that("Bootstrapping works", {
  options <- jaspTools::analysisOptions("confirmatoryFactorAnalysis")
  options$group <- ""
  options$invarianceTesting <- "configural"
  options$packageMimiced <- "lavaan"
  options$seType <- "bootstrap"
  options$bootstrapSamples <- 100
  options$estimator <- "default"
  options$standardized <- "none"
  options$factors <- list(
    list(indicators = list("x1", "x2", "x3"), name = "Factor1", title = "Factor 1"),
    list(indicators = list("x4", "x5", "x6"), name = "Factor2", title = "Factor 2"),
    list(indicators = list("x7", "x8", "x9"), name = "Factor3", title = "Factor 3")
  )
  options$modelIdentification <- "factorVariance"
  options$naAction <- "listwise"
  set.seed(1)
  results <- jaspTools::runAnalysis("confirmatoryFactorAnalysis", "holzingerswineford.csv", options)

  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fl1"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.712022271311838, 1.11279383173753, 0.899620313867488,
                                      "Factor 1", 0, "x1", 0.0808465333987386, 11.1275063512065, 0.339182433474584,
                                      0.693156723678194, 0.49794051110941, "Factor 1",
                                      1.28623778294923e-10, "x2", 0.0774547818506273, 6.42878979466621,
                                      0.539990088382033, 0.805660643384033, 0.656156092628451,
                                      "Factor 1", 0, "x3", 0.0744212256974568, 8.81678696472846, 0.856032790026098,
                                      1.08543684864592, 0.989693449094392, "Factor 2",
                                      0, "x4", 0.0566367179185465, 17.4744138690689, 0.954112872546464,
                                      1.24054019999221, 1.10160465003145, "Factor 2",
                                      0, "x5", 0.0626757561168699, 17.5762482701815, 0.803928010900409,
                                      1.03630968014001, 0.916600977759373, "Factor 2",
                                      0, "x6", 0.0536584940344529, 17.0821226769958, 0.375767927218761,
                                      0.741040160352204, 0.619475433557926, "Factor 3",
                                      0, "x7", 0.0695825769015842, 8.90273774186456, 0.506218568773711,
                                      0.900974372672674, 0.730948802915075, "Factor 3",
                                      0, "x8", 0.0659093164600047, 11.0902197469857, 0.498364732719689,
                                      0.860170344673322, 0.669980108781259, "Factor 3",
                                      0, "x9", 0.0650169734598685, 10.3046954222623))



})



# # validate the following tests with:
# library(lavaan)
#
# HS.model <- '
# visual  =~ x1 + x2 + x3
# textual =~ x4 + x5 + x6
# speed   =~ x7 + x8 + x9
# x7 ~~ x8
# g =~ visual + textual + speed
# '
# # Configural model
# fit <- cfa(HS.model, data = HolzingerSwineford1939, effect.coding = TRUE, <- = "school")
# summary(fit)

options <- jaspTools::analysisOptions("confirmatoryFactorAnalysis")
options$group <- "school"
options$invarianceTesting <- "configural"
options$packageMimiced <- "lavaan"
options$seType <- "standard"
options$estimator <- "default"
options$standardized <- "none"
options$factors <- list(
  list(indicators = list("x1", "x2", "x3"), name = "Factor1", title = "visual"),
  list(indicators = list("x4", "x5", "x6"), name = "Factor2", title = "textual"),
  list(indicators = list("x7", "x8", "x9"), name = "Factor3", title = "speed")
)
options$modelIdentification <- "effectsCoding"
options$residualsCovarying <-  list(c("x7", "x8"))
options$naAction <- "listwise"
options$secondOrder <- list("visual", "textual", "speed")
set.seed(1)

results <- jaspTools::runAnalysis("confirmatoryFactorAnalysis", "holzingerswineford.csv", options)


test_that("Factor loadings table results match for multiple groups and effects coding", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fl1"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(1.10105262470584, 1.66239528529133, 1.38172395499858, "Pasteur",
                                      "visual", 0, "x1", 0.143202289688303, 9.64875602203059, 0.421600080181834,
                                      0.92883305854499, 0.675216569363412, "Pasteur", "visual", 1.80752648182292e-07,
                                      "x2", 0.129398545678427, 5.21811559645668, 0.700542606262643,
                                      1.18557634501337, 0.943059475638005, "Pasteur", "visual", 2.50910403565285e-14,
                                      "x3", 0.123735370286549, 7.62158365432657, 0.879064138342627,
                                      1.07622533217526, 0.977644735258943, "Pasteur", "textual", 0,
                                      "x4", 0.0502971471383697, 19.437379471432, 1.0651533306052,
                                      1.2703138855335, 1.16773360806935, "Pasteur", "textual", 0,
                                      "x5", 0.0523378379772753, 22.3114605646563, 0.764826837719821,
                                      0.944416475623597, 0.854621656671709, "Pasteur", "textual",
                                      0, "x6", 0.0458145249913662, 18.6539455954801, 0.195451723833617,
                                      0.94752882557255, 0.571490274703083, "Pasteur", "speed", 0.00289488302453056,
                                      "x7", 0.191859928976048, 2.97868490702105, 0.309395340929874,
                                      1.11953466025498, 0.714465000592425, "Pasteur", "speed", 0.00054622557003281,
                                      "x8", 0.206671991351723, 3.45699964431328, 1.02162210406388,
                                      2.40646734534511, 1.71404472470449, "Pasteur", "speed", 1.22372831157236e-06,
                                      "x9", 0.353283338929875, 4.85175646804199, 0.97824694709509,
                                      1.41103286870157, 1.19463990789833, "Grant-White", "visual",
                                      0, "x1", 0.110406600585583, 10.8203667313558, 0.576588928452012,
                                      1.01820949425935, 0.797399211355682, "Grant-White", "visual",
                                      1.46349599106088e-12, "x2", 0.112660377764792, 7.07790287212119,
                                      0.803379282477899, 1.21254247901407, 1.00796088074599, "Grant-White",
                                      "visual", 0, "x3", 0.104380284475531, 9.65662132279655, 0.918525808353015,
                                      1.11584455993924, 1.01718518414613, "Grant-White", "textual",
                                      0, "x4", 0.0503373411814332, 20.2073681341222, 0.904533581821138,
                                      1.10788011779749, 1.00620684980931, "Grant-White", "textual",
                                      0, "x5", 0.0518750695370728, 19.3967325497312, 0.875800310154247,
                                      1.07741562193487, 0.97660796604456, "Grant-White", "textual",
                                      0, "x6", 0.0514334225962675, 18.9878082528273, 0.448709591199949,
                                      0.861628177339534, 0.655168884269742, "Grant-White", "speed",
                                      4.98220797950921e-10, "x7", 0.105338309631359, 6.21966392438387,
                                      0.632652017321973, 1.05862185080329, 0.845636934062633, "Grant-White",
                                      "speed", 7.105427357601e-15, "x8", 0.108667770643062, 7.7818559178901,
                                      1.15381004087615, 1.8445783224591, 1.49919418166763, "Grant-White",
                                      "speed", 0, "x9", 0.176219636440169, 8.50753191842297))
})

test_that("Second-order factor loadings table results match for multiple groups and effects coding", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fl2"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.895669276497663, 2.01755133623086, 1.45661030636426, "Pasteur",
                                      "SecondOrder", 3.59027865659556e-07, "visual", 0.286199661979113,
                                      5.08948996058061, 0.592187450357759, 1.46800533965735, 1.03009639500755,
                                      "Pasteur", "SecondOrder", 4.01822369711091e-06, "textual", 0.223427036467998,
                                      4.61043753384383, 0.216566327907608, 0.810020269348763, 0.513293298628185,
                                      "Pasteur", "SecondOrder", 0.000697793364336396, "speed", 0.151394093494127,
                                      3.39044467839889, 0.894743205210185, 1.46886037814872, 1.18180179167945,
                                      "Grant-White", "SecondOrder", 6.66133814775094e-16, "visual",
                                      0.146461153742391, 8.06904603358591, 0.749885842194451, 1.26363322117426,
                                      1.00675953168436, "Grant-White", "SecondOrder", 1.57651669496772e-14,
                                      "textual", 0.131060413107635, 7.6816447301867, 0.56185444727119,
                                      1.06102290600119, 0.811438676636188, "Grant-White", "SecondOrder",
                                      1.86384685463281e-10, "speed", 0.127341232458192, 6.37215975510994
                                 ))
})

test_that("Factor variances table results match for multiple groups and effects coding", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fv"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(-0.322593861704547, 0.374611235772924, 0.0260086870341885, "Pasteur",
                                      "visual", 0.88373996127601, 0.177861711484736, 0.146229825503622,
                                      0.453300232388463, 0.952377694237456, 0.702838963312959, "Pasteur",
                                      "textual", 3.38341130667885e-08, 0.127318018541578, 5.52034167169694,
                                      0.10185269536012, 0.363320003790462, 0.232586349575291, "Pasteur",
                                      "speed", 0.000488574525319363, 0.0667020696535148, 3.48694352039548,
                                      0.126201051830929, 0.302878789671743, 0.214539920751336, "Pasteur",
                                      "Second-Order", 1.93620086053059e-06, 0.0450716796926947, 4.75997172091434,
                                      -0.127514015381487, 0.232797490998318, 0.0526417378084156, "Grant-White",
                                      "visual", 0.566845167322731, 0.0919178896198851, 0.572703942900658,
                                      0.402546923419005, 0.818456895883089, 0.610501909651047, "Grant-White",
                                      "textual", 8.7184031105636e-09, 0.106101432410169, 5.75394597210484,
                                      0.112134953780972, 0.390602552366561, 0.251368753073767, "Grant-White",
                                      "speed", 0.000402463070696024, 0.0710389580579302, 3.53846340016394,
                                      0.188780508669139, 0.407359188716625, 0.298069848692882, "Grant-White",
                                      "Second-Order", 9.01677925657651e-08, 0.0557608919785278, 5.34550001114871
                                 ))
})

test_that("Residual covariances table results match for multiple groups and effects coding", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_rc"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.122168637770757, 0.493921275449714, 0.308044956610236, "Pasteur",
                                      "x7", "<unicode>", 0.00116151745589432, "x8", 0.0948365991955195,
                                      3.24816536256384, 0.208580408543772, 0.561085317759383, 0.384832863151578,
                                      "Grant-White", "x7", "<unicode>", 1.87379671678922e-05, "x8",
                                      0.0899263741569042, 4.27942154634322))
})


test_that("Residual variances table results match for multiple groups and effects coding", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_rv"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.145806292617985, 0.80727193782207, 0.476539115220027, "Pasteur",
                                      "x1", 0.00474239054532877, 0.168744336738236, 2.82403027225296,
                                      0.980200194655344, 1.58852574174061, 1.28436296819798, "Pasteur",
                                      "x2", 2.22044604925031e-16, 0.155187940156977, 8.27617769073297,
                                      0.661807192404595, 1.1738532769286, 0.917830234666598, "Pasteur",
                                      "x3", 2.11963779861435e-12, 0.130626401444863, 7.02637617292099,
                                      0.293428309541256, 0.567242028683073, 0.430335169112164, "Pasteur",
                                      "x4", 7.24262871898418e-10, 0.0698517220983715, 6.16069520098771,
                                      0.269842486146815, 0.608344243183404, 0.439093364665109, "Pasteur",
                                      "x5", 3.68010061757573e-07, 0.0863540758163536, 5.08480185230533,
                                      0.195590984648919, 0.394088799248797, 0.294839891948858, "Pasteur",
                                      "x6", 5.79779713127948e-09, 0.0506381280894964, 5.82248797640715,
                                      0.821118830524024, 1.32986765676015, 1.07549324364209, "Pasteur",
                                      "x7", 2.22044604925031e-16, 0.129785248670147, 8.28671405003417,
                                      0.583281164346786, 1.02554323626783, 0.80441220030731, "Pasteur",
                                      "x8", 1.00519592649562e-12, 0.112824030290749, 7.12979494026521,
                                      -0.618627025731548, 0.874943807048513, 0.128158390658482, "Pasteur",
                                      "x9", 0.736602350153651, 0.381019968877275, 0.336356099750146,
                                      0.414991134180042, 0.883789180220234, 0.649390157200138, "Grant-White",
                                      "x1", 5.63612934101343e-08, 0.119593535834845, 5.4299770691363,
                                      0.688644414789547, 1.1677638546569, 0.928204134723222, "Grant-White",
                                      "x2", 3.10862446895044e-14, 0.122226592847263, 7.5941259025614,
                                      0.40622745406912, 0.787625738823265, 0.596926596446192, "Grant-White",
                                      "x3", 8.51159587256234e-10, 0.097297268664773, 6.13508071334291,
                                      0.186519967210478, 0.439406093549079, 0.312963030379778, "Grant-White",
                                      "x4", 1.2273786600403e-06, 0.0645129523637512, 4.85116583434534,
                                      0.276773271697164, 0.558601647697828, 0.417687459697496, "Grant-White",
                                      "x5", 6.26294172079156e-09, 0.0718963149893798, 5.80958091884396,
                                      0.274236151148393, 0.545213766052058, 0.409724958600226, "Grant-White",
                                      "x6", 3.08463521392355e-09, 0.0691282128245982, 5.92702952757996,
                                      0.655173489603016, 1.08413872653021, 0.869656108066612, "Grant-White",
                                      "x7", 1.99840144432528e-15, 0.109431918216563, 7.94700597631476,
                                      0.556982773635263, 0.99157734143102, 0.774280057533141, "Grant-White",
                                      "x8", 2.87303514312498e-12, 0.110867998397875, 6.98380117547052,
                                      -0.344694371374334, 0.434630459583225, 0.0449680441044453, "Grant-White",
                                      "x9", 0.821057629495469, 0.198811008035039, 0.226184880550075
                                 ))
})

test_that("Chi-square test table results match for multiple groups and effects coding", {
  table <- results[["results"]][["maincontainer"]][["collection"]][["maincontainer_cfatab"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(957.769050808683, 72, "Baseline model", "", 84.655095268701, 46,
                                      "Factor model", 0.000448372893282811))
})



options <- jaspTools::analysisOptions("confirmatoryFactorAnalysis")
options$packageMimiced <- "lavaan"
options$seType <- "robust"
options$estimator <- "default"
options$factors <- list(
  list(indicators = list("V1", "V2", "V3", "V4"),
       name = "Factor1", title = "Factor 1"),
  list(indicators = list("V5", "V6", "V7", "V8"),
       name = "Factor2", title = "Factor 2")
)
options$modelIdentification <- "factorVariance"
options$naAction <- "pairwise"
options$thresholds <- TRUE
options$group <- "gender"
options$fitMeasures <- TRUE
dt <- read.csv("cavalini_group.csv")
dt[, c("V1", "V5", "V8")] <- lapply(dt[, c("V1", "V5", "V8")], ordered)

set.seed(1)
results <- jaspTools::runAnalysis("confirmatoryFactorAnalysis", dt, options)


test_that("Thresholds table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_Thresholds"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(-1.37345271503234, -1.05964335802485, -1.2165480365286, "f", "V1",
                                      0, "t1", 0.0800548784270476, -15.1964260071572, -0.358874219848855,
                                      -0.118911115849374, -0.238892667849114, "f", "V1", 9.52271353014122e-05,
                                      "t2", 0.0612162024129726, -3.90244181168758, 0.124863894782197,
                                      0.364954328270292, 0.244909111526244, "f", "V1", 6.3717733790325e-05,
                                      "t3", 0.0612486850222499, 3.99860195263418, 0.547166075781201,
                                      0.805483371744301, 0.676324723762751, "f", "V5", 0, "t1", 0.065898480278381,
                                      10.2631308173677, 1.32801878330997, 1.69596338637514, 1.51199108484256,
                                      "f", "V5", 0, "t2", 0.0938651439433244, 16.1081208777083, 1.58695081056115,
                                      2.03777591515951, 1.81236336286033, "f", "V5", 0, "t3", 0.115008517542776,
                                      15.7585142525312, -0.667907099127094, -0.417271559864424, -0.542589329495759,
                                      "f", "V8", 0, "t1", 0.0639388124576908, -8.48607142734779, 0.575854076158956,
                                      0.836151253707481, 0.706002664933219, "f", "V8", 0, "t2", 0.0664035613923819,
                                      10.6320000031537, 1.28086251788209, 1.63744971087504, 1.45915611437857,
                                      "f", "V8", 0, "t3", 0.090967792215996, 16.040359767267, -1.33296389680349,
                                      -1.01400542884757, -1.17348466282553, "m", "V1", 0, "t1", 0.0813684512755931,
                                      -14.4218630738216, -0.342662717731549, -0.0942651168100136,
                                      -0.218463917270781, "m", "V1", 0.000565698466995501, "t2", 0.0633678993289836,
                                      -3.44754867344733, 0.276480375367169, 0.53014885331, 0.403314614338585,
                                      "m", "V1", 4.59333238111981e-10, "t3", 0.0647125355220137, 6.23240321346065,
                                      0.501097035494698, 0.766182966064704, 0.633640000779701, "m",
                                      "V5", 0, "t1", 0.067625204509106, 9.36987925403433, 1.35781354298621,
                                      1.74921140203548, 1.55351247251084, "m", "V5", 0, "t2", 0.0998482273492178,
                                      15.5587386351633, 1.60472242820073, 2.08355527282974, 1.84413885051524,
                                      "m", "V5", 0, "t3", 0.122153480473616, 15.0968997638471, -0.629660973554337,
                                      -0.371855294149489, -0.500758133851913, "m", "V8", 2.66453525910038e-14,
                                      "t1", 0.0657679634519783, -7.61401307822997, 0.682580238630726,
                                      0.961593110504241, 0.822086674567484, "m", "V8", 0, "t2", 0.0711780609425307,
                                      11.549720007563, 1.45747588382256, 1.8793065040716, 1.66839119394708,
                                      "m", "V8", 0, "t3", 0.107611829496968, 15.5037898876544))
})

test_that("Fit indices table results match", {
  table <- results[["results"]][["maincontainer"]][["collection"]][["maincontainer_fits"]][["collection"]][["maincontainer_fits_indices"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list("Comparative Fit Index (CFI)", 0.837270996361255, "Tucker-Lewis Index (TLI)",
                                      0.760188836742902, "Bentler-Bonett Non-normed Fit Index (NNFI)",
                                      0.760188836742902, "Bentler-Bonett Normed Fit Index (NFI)",
                                      0.814729950768132, "Parsimony Normed Fit Index (PNFI)", 0.648989559539764,
                                      "Bollen's Relative Fit Index (RFI)", 0.726970453763563, "Bollen's Incremental Fit Index (IFI)",
                                      0.839626473453737, "Relative Noncentrality Index (RNI)", 0.837270996361255
                                 ))
})

test_that("Other fit measures table results match", {
  table <- results[["results"]][["maincontainer"]][["collection"]][["maincontainer_fits"]][["collection"]][["maincontainer_fits_others"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list("Root mean square error of approximation (RMSEA)", 0.112727222947185,
                                      "RMSEA 90% CI lower bound", 0.0992231977494561, "RMSEA 90% CI upper bound",
                                      0.12668058010314, "RMSEA p-value", 1.10023101740353e-13, "Standardized root mean square residual (SRMR)",
                                      0.0652605072256331, "Hoelter's critical N (<unicode> = .05)",
                                      415.284725169067, "Hoelter's critical N (<unicode> = .01)",
                                      475.650388677281, "Goodness of fit index (GFI)", 0.986569391374822,
                                      "McDonald fit index (MFI)", 0.959420192033006, "Expected cross validation index (ECVI)",
                                      ""))
})

test_that("Chi-square test table results match", {
  table <- results[["results"]][["maincontainer"]][["collection"]][["maincontainer_cfatab"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(1281.53663843542, 56, "Baseline model", "", 237.430356095373,
                                      38, "Factor model", 0))
})


# additional output test
options <- jaspTools::analysisOptions("confirmatoryFactorAnalysis")
options$group <- "sex"
options$invarianceTesting <- "metric"
options$packageMimiced <- "lavaan"
options$seType <- "standard"
options$estimator <- "default"
options$standardized <- "none"
options$factors <- list(
  list(indicators = list("x1", "x2", "x3"), name = "f1", title = "Factor 1"),
  list(indicators = list("x4", "x5", "x6"), name = "f2", title = "Factor 2"),
  list(indicators = list("x7", "x8", "x9"), name = "f3", title = "Factor 3")
)
options$secondOrder <- list("Factor 1", "Factor 2", "Factor 3")
options$modelIdentification <- "factorVariance"
options$naAction <- "listwise"
options$ave <- TRUE
options$htmt <- TRUE
options$reliability <- TRUE
set.seed(1)
results <- jaspTools::runAnalysis("confirmatoryFactorAnalysis", "holzingerswineford.csv", options)

test_that("Average variance extracted table results match", {
  table <- results[["results"]][["resAveTable"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.305051834487418, "Factor 1", 1, 0.700620392588814, "Factor 2", 1, 0.440212867852147,
                                      "Factor 3", 1, 0.429925397038108, "Factor 1", 2, 0.740548656102918, "Factor 2",
                                      2, 0.400676692760547, "Factor 3", 2))
})

test_that("Heterotrait-monotrait ratio table results match", {
  table <- results[["results"]][["resHtmtTable"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(1, "", "", "Factor 1", 1, 0.376412961722238, 1, "", "Factor 2", 1, 0.341052011589284,
                                      0.0950654664695311, 1, "Factor 3", 1, 1, "", "", "Factor 1", 2, 0.438572930736534,
                                      1, "", "Factor 2", 2, 0.446623050626322, 0.355871932355235, 1, "Factor 3",
                                      2))
})

test_that("Reliability table results match", {
  table <- results[["results"]][["resRelTable"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.56769636941697, "Factor 1", 1, 0.535028679477941, 0.872217272102272,
                                      "Factor 2", 1, 0.87499706003875, 0.69693988501424, "Factor 3",
                                      1, 0.702706608978988, 0.720218762931056, "total", 1, 0.843988152680847,
                                      "", "SecondOrder", 1, 0.51731822568291, 0.663613252882612, "Factor 1",
                                      2, 0.676466492554183, 0.892257640765487, "Factor 2", 2, 0.895339124317018,
                                      0.686170258023581, "Factor 3", 2, 0.648172924932421, 0.796625177163525,
                                      "total", 2, 0.832040767424991, "", "SecondOrder", 2, 0.568295638332184
                                 ))
})


# structural invariance test
options <- jaspTools::analysisOptions("confirmatoryFactorAnalysis")
options$group <- "sex"
options$invarianceTesting <- "structural"

options$factors <- list(
  list(indicators = list("x1", "x2", "x3"), name = "f1", title = "Factor 1"),
  list(indicators = list("x4", "x5", "x6"), name = "f2", title = "Factor 2"),
  list(indicators = list("x7", "x8", "x9"), name = "f3", title = "Factor 3")
)
options$modelIdentification <- "markerVariable"
set.seed(1)
results <- jaspTools::runAnalysis("confirmatoryFactorAnalysis", "holzingerswineford.csv", options)

test_that("Factor Covariances table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fc"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.252069643751425, 0.564393435959575, 0.4082315398555, 1, "Factor 1",
                                      "<unicode>", 2.99674828285745e-07, "Factor 2", 0.0796759008511687,
                                      5.12365138635909, 0.153674632748802, 0.370774902932515, 0.262224767840658,
                                      1, "Factor 1", "<unicode>", 2.1939154322137e-06, "Factor 3",
                                      0.0553837396748543, 4.73468872597123, 0.0768417287748731, 0.270147907288013,
                                      0.173494818031443, 1, "Factor 2", "<unicode>", 0.000434506858657757,
                                      "Factor 3", 0.0493137067920418, 3.51818651076219, 0.252069643751426,
                                      0.564393435959575, 0.4082315398555, 2, "Factor 1", "<unicode>",
                                      2.99674828285745e-07, "Factor 2", 0.0796759008511687, 5.1236513863591,
                                      0.153674632748802, 0.370774902932515, 0.262224767840658, 2,
                                      "Factor 1", "<unicode>", 2.1939154322137e-06, "Factor 3", 0.0553837396748543,
                                      4.73468872597123, 0.0768417287748731, 0.270147907288013, 0.173494818031443,
                                      2, "Factor 2", "<unicode>", 0.000434506858657757, "Factor 3",
                                      0.0493137067920418, 3.51818651076219))
})

test_that("Factor variances table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_fv"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0.51579922628888, 1.10282884918973, 0.809314037739304, 1, "Factor 1",
                                      6.50814258040811e-08, 0.149755206608709, 5.40424641030303, 0.759562812574203,
                                      1.19941902306092, 0.97949091781756, 1, "Factor 2", 0, 0.112210278851102,
                                      8.7290658917023, 0.203306550279352, 0.564190199988996, 0.383748375134174,
                                      1, "Factor 3", 3.06899737032573e-05, 0.0920638472329718, 4.16828523538758,
                                      0.51579922628888, 1.10282884918973, 0.809314037739304, 2, "Factor 1",
                                      6.50814258040811e-08, 0.149755206608709, 5.40424641030303, 0.759562812574203,
                                      1.19941902306092, 0.97949091781756, 2, "Factor 2", 0, 0.112210278851102,
                                      8.72906589170229, 0.203306550279352, 0.564190199988996, 0.383748375134174,
                                      2, "Factor 3", 3.06899737032573e-05, 0.0920638472329717, 4.16828523538759
                                 ))
})


# fix manifest intercepts tests
options <- jaspTools::analysisOptions("confirmatoryFactorAnalysis")
options$group <- "sex"
options$meanStructure <- TRUE
options$interceptsFixedToZero <- "manifest"

options$factors <- list(
  list(indicators = list("x1", "x2", "x3"), name = "f1", title = "Factor 1"),
  list(indicators = list("x4", "x5", "x6"), name = "f2", title = "Factor 2"),
  list(indicators = list("x7", "x8", "x9"), name = "f3", title = "Factor 3")
)
options$modelIdentification <- "markerVariable"
set.seed(1)
results <- jaspTools::runAnalysis("confirmatoryFactorAnalysis", "holzingerswineford.csv", options)

test_that("Intercepts table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_Intercepts"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(0, 0, 0, 1, "x1", "", 0, "", 0, 0, 0, 1, "x2", "", 0, "", 0, 0,
                                      0, 1, "x3", "", 0, "", 0, 0, 0, 1, "x4", "", 0, "", 0, 0, 0,
                                      1, "x5", "", 0, "", 0, 0, 0, 1, "x6", "", 0, "", 0, 0, 0, 1,
                                      "x7", "", 0, "", 0, 0, 0, 1, "x8", "", 0, "", 0, 0, 0, 1, "x9",
                                      "", 0, "", 0, 0, 0, 2, "x1", "", 0, "", 0, 0, 0, 2, "x2", "",
                                      0, "", 0, 0, 0, 2, "x3", "", 0, "", 0, 0, 0, 2, "x4", "", 0,
                                      "", 0, 0, 0, 2, "x5", "", 0, "", 0, 0, 0, 2, "x6", "", 0, "",
                                      0, 0, 0, 2, "x7", "", 0, "", 0, 0, 0, 2, "x8", "", 0, "", 0,
                                      0, 0, 2, "x9", "", 0, ""))
})

test_that("Factor Intercepts table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_Factor Intercepts"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(4.86032703729577, 5.21479167387077, 5.03755935558327, 1, "Factor 1",
                                      0, 0.090426313792235, 55.7089982364829, 2.75710153479434, 3.10340246097582,
                                      2.93025199788508, 1, "Factor 2", 0, 0.088343696341633, 33.1687728635842,
                                      3.90410200426111, 4.22060103316011, 4.06235151871061, 1, "Factor 3",
                                      0, 0.0807410318239286, 50.3133465964288, 4.68082259954853, 5.03876634316866,
                                      4.85979447135859, 2, "Factor 1", 0, 0.0913138574084872, 53.2207773199043,
                                      3.03093169693174, 3.3907128242691, 3.21082226060042, 2, "Factor 2",
                                      0, 0.0917825863575222, 34.9829132957014, 4.13415749314571, 4.48968795507957,
                                      4.31192272411264, 2, "Factor 3", 0, 0.0906982130126474, 47.5414297689786
                                 ))
})

options$interceptsFixedToZero <- "meanManifest"
set.seed(1)
results <- jaspTools::runAnalysis("confirmatoryFactorAnalysis", "holzingerswineford.csv", options)

test_that("Factor Intercepts table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_Factor Intercepts"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(3.65754400738779, 7.85746191244071, 5.75750295991425, 1, "Factor 1",
                                      7.71475485716877e-08, 1.0714273165684, 5.37367572291749, 2.67329743888011,
                                      3.38328888769281, 3.02829316328646, 1, "Factor 2", 0, 0.181123595742837,
                                      16.7194845644854, 3.71374413938686, 5.75309790047635, 4.7334210199316,
                                      1, "Factor 3", 0, 0.520252866168884, 9.09830839527763, 4.96696097397706,
                                      7.02507093096112, 5.99601595246909, 2, "Factor 1", 0, 0.525037697941945,
                                      11.4201627349282, 2.92991249413903, 3.60159845584644, 3.26575547499273,
                                      2, "Factor 2", 0, 0.171351608245249, 19.058796753857, 2.793007701454,
                                      5.28401148493782, 4.03850959319591, 2, "Factor 3", 2.08242978416706e-10,
                                      0.635471825791837, 6.35513555327757))
})

test_that("Intercepts table results match", {
  table <- results[["results"]][["estimates"]][["collection"]][["estimates_Intercepts"]][["data"]]
  jaspTools::expect_equal_tables(table,
                                 list(-2.82086049306897, 1.37206410197526, -0.724398195546852, 1, "x1",
                                      0.498257853951389, 1.06964327613096, -0.677233440074615, 1.6579921814542,
                                      4.92350462308137, 3.29074840226779, 1, "x2", 7.80789775793878e-05,
                                      0.833054195736533, 3.95022126904759, -3.9895831284748, -1.14311728496707,
                                      -2.56635020672093, 1, "x3", 0.000409050402817135, 0.726152589017015,
                                      -3.53417483534002, -0.437370677504958, 0.216400318827097, -0.110485179338931,
                                      1, "x4", 0.507679587603854, 0.166781380037826, -0.662455121272365,
                                      0.417590399459313, 1.09105191834536, 0.754321158902338, 1, "x5",
                                      1.13050308931939e-05, 0.17180456482829, 4.39057693057366, -0.968470973968885,
                                      -0.319200985157928, -0.643835979563407, 1, "x6", 0.000101440416994558,
                                      0.165633142734335, -3.88712046957944, -1.69451524371202, 0.339644579320786,
                                      -0.677435332195615, 1, "x7", 0.191739032345593, 0.518927857623404,
                                      -1.30545185085678, -2.84093538587357, -0.0293351814438569, -1.43513528365872,
                                      1, "x8", 0.0454071513513012, 0.717258129896075, -2.00086304196601,
                                      1.05054048576094, 3.17460074594772, 2.11257061585433, 1, "x9",
                                      9.67009333712188e-05, 0.541862064033088, 3.89872396700082, -2.16953548981766,
                                      -0.134323505968075, -1.15192949789287, 2, "x1", 0.0265086185992982,
                                      0.51919627092719, -2.21867829642869, 1.74677342548164, 3.48493867238887,
                                      2.61585604893525, 2, "x2", 3.65035446314721e-09, 0.443417649665415,
                                      5.89930520562065, -2.34974383123603, -0.578109270848743, -1.46392655104239,
                                      2, "x3", 0.00119911054203281, 0.451955896731194, -3.23909160524368,
                                      -0.36798865865047, 0.227876404599755, -0.0700561270253575, 2,
                                      "x4", 0.64489351397835, 0.152009186890762, -0.460867717657759,
                                      0.592141420248959, 1.21531497293793, 0.903728196593445, 2, "x5",
                                      1.31048960572144e-08, 0.158975766290729, 5.68469155821359, -1.12184965654026,
                                      -0.545494482595909, -0.833672069568086, 2, "x6", 1.4279661453287e-08,
                                      0.14703208285728, -5.67000108661529, -0.977643544907075, 1.51717429862637,
                                      0.269765376859647, 2, "x7", 0.671665767304788, 0.636444818173255,
                                      0.423862947983357, 0.174180036260091, 2.10442292951146, 1.13930148288577,
                                      2, "x8", 0.0206848446354611, 0.492417949635013, 2.31368796310175,
                                      -2.8898649408433, 0.071731221352461, -1.40906685974542, 2, "x9",
                                      0.0621783869832733, 0.755523107964345, -1.86502152600198))
})
jasp-stats/jaspFactor documentation built on April 20, 2024, 4:12 p.m.