-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for observed variables in qml/lms, and throw an error
- Loading branch information
Showing
4 changed files
with
142 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
checkModel <- function(model, covModel = NULL, method = "lms") { | ||
checkCovModelVariables(covModel = covModel, modelXis = model$info$xis) | ||
|
||
checkNodesLms(parTableMain = model$parTable, | ||
parTableCov = covModel$parTable, | ||
nodes = model$quad$m, method = method) | ||
|
||
checkOVsInStructuralModel(parTableMain = model$parTable, | ||
parTableCov = covModel$parTable) | ||
NULL | ||
} | ||
|
||
|
||
checkCovModelVariables <- function(covModel, modelXis) { | ||
if (is.null(covModel$info)) return(NULL) # nothing to check | ||
covModelEtas <- covModel$info$etas | ||
covModelXis <- covModel$info$xis | ||
|
||
stopif(!all(c(covModelXis, covModelEtas) %in% modelXis), | ||
"All latent variables in the cov-model must be an ", | ||
"exogenous variable in the main model") | ||
stopif(!all(modelXis %in% c(covModelXis, covModelEtas)), | ||
"All exogenous variables in main model must be ", | ||
"part of the cov-model") | ||
} | ||
|
||
checkNodesLms <- function(parTableMain, | ||
parTableCov, | ||
nodes, | ||
method = "lms", | ||
minNodesXiXi = 16, | ||
minNodesXiEta = 32, | ||
minNodesEtaEta = 48) { | ||
if (method != "lms") return(NULL) | ||
|
||
parTable <- rbind(parTableMain, parTableCov) | ||
|
||
etas <- getEtas(parTable, isLV = TRUE) | ||
xis <- getXis(parTable, etas = etas, isLV = TRUE) | ||
varsInts <- getVarsInts(getIntTermRows(parTable)) | ||
|
||
nodesXiXi_ok <- TRUE | ||
nodesXiEta_ok <- TRUE | ||
nodesEtaEta_ok <- TRUE | ||
|
||
lapply(varsInts, FUN = function(x) { | ||
if (all(x %in% xis)) nodesXiXi_ok <<- nodes >= minNodesXiXi | ||
else if (all(x %in% etas)) nodesEtaEta_ok <<- nodes >= minNodesEtaEta | ||
else if (any(x %in% etas)) nodesXiEta_ok <<- nodes >= minNodesXiEta | ||
else warning2("Unable to classify latent variables in interaction terms") | ||
}) | ||
|
||
warnif(!nodesXiXi_ok, "It is recommended that you have at least ", | ||
minNodesXiXi, " nodes for interaction effects between ", | ||
"exogenous variables in the lms approach 'nodes = ", nodes, "'") | ||
warnif(!nodesXiEta_ok, "It is recommended that you have at least ", | ||
minNodesXiEta, " nodes for interaction effects between exogenous ", | ||
"and endogenous variables in the lms approach 'nodes = ", nodes, "'") | ||
warnif(!nodesEtaEta_ok, "It is recommended that you have at least ", | ||
minNodesEtaEta, " nodes for interaction effects between endogenous ", | ||
"variables in the lms approach 'nodes = ", nodes, "'") | ||
} | ||
|
||
|
||
checkOVsInStructuralModel <- function(parTableMain, parTableCov) { | ||
parTable <- rbind(parTableMain, parTableCov) | ||
xisLVs <- getXis(parTable, isLV = TRUE) | ||
xisAll <- getXis(parTable, isLV = FALSE) | ||
|
||
stopif(length(xisAll) != length(xisLVs) || !all(xisLVs %in% xisAll), | ||
"Observed variables are not allowed in the structural model in LMS/QML directly. ", | ||
"Please redefine them as latent.\nSee:\n", | ||
" vignette(\"observed_lms_qml\", \"modsem\")") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
devtools::load_all() | ||
|
||
|
||
m1 <- ' | ||
# Outer Model | ||
X =~ x1 + x2 +x3 | ||
Y =~ y1 + y2 + y3 | ||
Z =~ z1 + z2 + z3 | ||
# Inner model | ||
Y ~ X + Z + X:Z + x1 | ||
' | ||
|
||
testthat::expect_error(modsem(m1, oneInt, method = "lms"), | ||
regexp = "Observed variables are not allowed in .*") | ||
testthat::expect_error(modsem(m1, oneInt, method = "qml"), | ||
regexp = "Observed variables are not allowed in .*") | ||
|
||
|
||
m2 <- ' | ||
# Outer Model | ||
X =~ x1 + x2 +x3 | ||
Y =~ y1 + y2 + y3 | ||
Z =~ z1 + z2 + z3 | ||
# Inner model | ||
Y ~ X + Z + X:Z + jk | ||
' | ||
|
||
testthat::expect_error(modsem(m2, oneInt, method = "lms"), | ||
regexp = "Observed variables are not allowed in .*") | ||
testthat::expect_error(modsem(m2, oneInt, method = "qml"), | ||
regexp = "Observed variables are not allowed in .*") | ||
|
||
|
||
m3 <- ' | ||
# Outer Model | ||
X =~ x1 + x2 +x3 | ||
Y =~ y1 + y2 + y3 | ||
Z =~ z1 + z2 + z3 + jk | ||
# Inner model | ||
Y ~ X + Z + X:Z | ||
' | ||
|
||
testthat::expect_error(modsem(m3, oneInt, method = "lms"), | ||
regexp = "Missing observed variables in data:.*jk.*") | ||
testthat::expect_error(modsem(m3, oneInt, method = "qml"), | ||
regexp = "Missing observed variables in data:.*jk.*") |