Skip to content

Commit

Permalink
Add auto-generated integration tests for confounder and direct path m…
Browse files Browse the repository at this point in the history
…odels
  • Loading branch information
maltelueken committed Sep 27, 2023
1 parent 9c536be commit 1090e6e
Show file tree
Hide file tree
Showing 22 changed files with 2,726 additions and 0 deletions.
160 changes: 160 additions & 0 deletions tests/generate-tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,163 @@ for (k in jaspProcess:::.procHardCodedModelNumbers()) {
writeLines(paste(header, testCode, sep = "\n\n"), fileContext)
close(fileContext)


customModels <- list(
"one_confounder" = list(
list(
processDependent = "Y",
processIndependent = "X",
processType = "confounders",
processVariable = "W"
)
),
"one_direct" = list( # Not working yet
list(
processDependent = "Y",
processIndependent = "X",
processType = "directs",
processVariable = ""
)
),
"two_confounder" = list(
list(
processDependent = "Y",
processIndependent = "X",
processType = "confounders",
processVariable = "W"
),
list(
processDependent = "Y",
processIndependent = "X",
processType = "confounders",
processVariable = "Z"
)
),
"confounder_X_Y" = list(
list(
processDependent = "Y",
processIndependent = "X",
processType = "mediators",
processVariable = "M"
),
list(
processDependent = "Y",
processIndependent = "X",
processType = "confounders",
processVariable = "Z"
)
),
"confounder_X_M" = list(
list(
processDependent = "Y",
processIndependent = "X",
processType = "mediators",
processVariable = "M"
),
list(
processDependent = "M",
processIndependent = "X",
processType = "confounders",
processVariable = "Z"
)
),
"confounder_M_Y" = list( # Not working yet
list(
processDependent = "Y",
processIndependent = "",
processType = "mediators",
processVariable = "M"
),
list(
processDependent = "Y",
processIndependent = "M",
processType = "confounders",
processVariable = "Z"
)
),
"confounder_moderator" = list(
list(
processDependent = "Y",
processIndependent = "X",
processType = "moderators",
processVariable = "W"
),
list(
processDependent = "Y",
processIndependent = "X",
processType = "confounders",
processVariable = "Z"
)
)
)

addProcessRelationshipsFromCustomModel <- function(k, options, replaceFun) {
processRelationships <- customModels[[as.character(k)]]
processRelationships <- lapply(processRelationships, function(row) {
row <- lapply(row, replaceFun)
return(row)
})

options$processModels[[1]][["processRelationships"]] <- processRelationships

return(options)
}

replaceVariablesContinuousCustom <- function(v) {
return(switch(v,
"Y" = "contNormal",
"X" = "contGamma",
"M" = "debCollin1",
"W" = "contcor1",
"Z" = "contcor2",
v
))
}

replaceVariablesFactorsCustom <- function(v) {
return(switch(v,
"Y" = "contNormal",
"X" = "contGamma",
"M" = "debCollin1",
"W" = "facExperim",
"Z" = "facGender",
v
))
}

errorContinuousCustom <- c()
errorFactorCustom <- c()

fileContext <- file("tests/testthat/test-classic-process-integration.R")
header <- "# This code is automatically generated by 'generate-tests.R'"
testCode <- "context('Integration tests for variable input for models with confounder and direct paths')"
for (k in names(customModels)) {
context <- paste0("\n\ncontext('Model number ", k, " - continuous')")
opts <- getOptionsOneModel()
opts <- addProcessRelationshipsFromCustomModel(k, opts, replaceVariablesContinuousCustom)
out <- try(paste(capture.output(jaspTools::runAnalysis("ClassicProcess", "debug", opts, makeTests = TRUE)), collapse = "\n"))
if (!inherits(out, "try-error")) {
out <- polishCapturedCode(out)
out <- gsub("conceptual-path-plot", paste0("conceptual-path-plot-continuous-", k), out)
out <- gsub("statistical-path-plot", paste0("statistical-path-plot-continuous-", k), out)
testCode <- paste(testCode, context, out, sep = "\n")
} else {
cat("Continuous", k)
errorContinuousCustom <- append(errorContinuousCustom, k)
}
context <- paste0("\n\ncontext('Model number ", k, " - factors')")
opts <- getOptionsOneModel()
opts <- addProcessRelationshipsFromCustomModel(k, opts, replaceVariablesFactorsCustom)
out <- try(paste(capture.output(jaspTools::runAnalysis("ClassicProcess", "debug", opts, makeTests = TRUE)), collapse = "\n"))
if (!inherits(out, "try-error")) {
out <- polishCapturedCode(out)
out <- gsub("conceptual-path-plot", paste0("conceptual-path-plot-factor-", k), out)
out <- gsub("statistical-path-plot", paste0("statistical-path-plot-factor-", k), out)
testCode <- paste(testCode, context, out, sep = "\n")
} else {
cat("Factor", k)
errorFactorCustom <- append(errorFactorCustom, k)
}
}
writeLines(paste(header, testCode, sep = "\n\n"), fileContext)
close(fileContext)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1090e6e

Please sign in to comment.