Skip to content

Commit

Permalink
add mean structure option fixing mean intercepts to zero (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliuspfadt authored Nov 5, 2023
1 parent 9a4d9b8 commit 6797d29
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
8 changes: 4 additions & 4 deletions R/sem.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ checkLavaanModel <- function(model, availableVars) {
"dependentCorrelation", "threshold", "scalingParameter", "efaConstrained", "standardizedVariable", "naAction", "estimator", "test",
"errorCalculationMethod", "informationMatrix", "emulation", "group", "equalLoading", "equalIntercept",
"equalResidual", "equalResidualCovariance", "equalMean", "equalThreshold", "equalRegression",
"equalVariance", "equalLatentCovariance", "dataType", "sampleSize", "freeParameters"))
"equalVariance", "equalLatentCovariance", "dataType", "sampleSize", "freeParameters", "manifestMeanFixedToZero"))
jaspResults[["modelContainer"]] <- modelContainer
}

Expand Down Expand Up @@ -331,15 +331,15 @@ checkLavaanModel <- function(model, availableVars) {

lavopts[["mimic"]] <- options[["emulation"]]


# model features
lavopts[["meanstructure"]] <- options[["meanStructure"]]
lavopts[["int.ov.free"]] <- !options[["manifestInterceptFixedToZero"]]
lavopts[["int.lv.free"]] <- !options[["latentInterceptFixedToZero"]]
lavopts[["fixed.x"]] <- options[["exogenousCovariateFixed"]]
lavopts[["orthogonal"]] <- options[["orthogonal"]]
lavopts[["std.lv"]] <- options[["factorScaling"]] == "factorVariance"
lavopts[["effect.coding"]] <- options[["factorScaling"]] == "effectCoding"
lavopts[["effect.coding"]] <- ifelse(options[["factorScaling"]] == "effectCoding", TRUE,
ifelse(options[["manifestMeanFixedToZero"]], "intercepts", FALSE))
lavopts[["auto.fix.first"]] <- options[["factorScaling"]] == "factorLoading"
lavopts[["auto.fix.single"]] <- options[["residualSingleIndicatorOmitted"]]
lavopts[["auto.var"]] <- options[["residualVariance"]]
Expand Down Expand Up @@ -751,7 +751,7 @@ checkLavaanModel <- function(model, availableVars) {

# Means
if (options[["meanStructure"]]) {
mutab <- createJaspTable(title = gettext("Means"))
mutab <- createJaspTable(title = gettext("Intercepts"))

if (options[["group"]] != "")
mutab$addColumnInfo(name = "group", title = gettext("Group"), type = "string", combine = TRUE)
Expand Down
3 changes: 2 additions & 1 deletion inst/qml/SEM.qml
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ Form
]
}
CheckBox { name: "meanStructure"; label: qsTr("Include mean structure") }
CheckBox { name: "manifestInterceptFixedToZero"; label: qsTr("Fix manifest intercepts to zero") }
CheckBox { name: "latentInterceptFixedToZero"; label: qsTr("Fix latent intercepts to zero"); checked: true }
CheckBox { name: "manifestInterceptFixedToZero"; label: qsTr("Fix manifest intercepts to zero") }
CheckBox { name: "manifestMeanFixedToZero"; label: qsTr("Fix mean of manifest intercepts to zero")}
CheckBox { name: "orthogonal"; label: qsTr("Assume factors uncorrelated") }
}

Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-sem.R
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,44 @@ test_that("Variance-covariance input works", {
"", "", 1095.63355300897, 1109.53848169019, 0, 0, "Model2", 75,
1, "", 0, 0))
})


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
))
})

0 comments on commit 6797d29

Please sign in to comment.