Skip to content

Commit de08b18

Browse files
committed
move towards preloadData
1 parent 41e3fbe commit de08b18

File tree

6 files changed

+4985
-1151
lines changed

6 files changed

+4985
-1151
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: jaspSem
22
Type: Package
33
Title: SEM Module for JASP
44
Version: 0.20.0
5-
Date: 2023-03-27
5+
Date: 2025-05-08
66
Author: JASP Team
77
Website: https://github.com/jasp-stats/jaspSem/
88
Maintainer: JASP Team <[email protected]>

R/plssem.R

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@
1717

1818
PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
1919

20+
sink(file = "~/Downloads/log.txt", append = TRUE)
21+
on.exit(sink(NULL), add = TRUE)
22+
2023
jaspResults$addCitation("Rademaker ME, Schuberth F (2020). cSEM: Composite-Based Structural Equation Modeling. Package version: 0.4.0, https://m-e-rademaker.github.io/cSEM/.")
2124

2225
options <- .plsSemPrepOpts(options)
26+
saveRDS(options, "~/Downloads/options.rds")
2327

24-
# Read data, check if ready
25-
dataset <- .plsSemReadData(dataset, options)
26-
ready <- .plsSemIsReady(dataset, options)
28+
29+
# Handle data, check if ready
30+
# dataset <- .plsSemReadData(dataset, options)
2731

2832
saveRDS(dataset, "~/Downloads/dataset.rds")
29-
saveRDS(options, "~/Downloads/options.rds")
33+
34+
# dataset <- .plsSemHandleData(dataset, options)
35+
36+
ready <- .plsSemIsReady(dataset, options)
37+
3038

3139
# Store in container
3240
modelContainer <- .plsSemModelContainer(jaspResults)
@@ -45,7 +53,7 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
4553
.plsSemReliabilities(modelContainer, dataset, options, ready)
4654
.plsSemCor(modelContainer, options, ready)
4755

48-
.plsAddConstructScores(jaspResults, modelContainer, options, ready)
56+
.plsAddConstructScores(jaspResults, options, ready)
4957
}
5058

5159
.plsSemPrepOpts <- function(options) {
@@ -54,7 +62,7 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
5462
newModel <- c(model[1], model[[2]])
5563
names(newModel)[names(newModel) == "model"] <- "syntax"
5664
return(newModel)
57-
}
65+
}
5866

5967
options[["models"]] <- lapply(options[["models"]], fixModel)
6068

@@ -63,10 +71,18 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
6371
return(options)
6472
}
6573

74+
.plsSemHandleData <- function(dataset, options) {
75+
76+
# listwise deletion
77+
dataset <- dataset[complete.cases(dataset), ]
78+
return(dataset)
79+
}
80+
6681
.plsSemReadData <- function(dataset, options) {
6782
if (!is.null(dataset)) return(dataset)
6883

6984
variablesToRead <- if (options[["group"]] == "") character() else options[["group"]]
85+
7086
for (model in options[["models"]])
7187
variablesToRead <- unique(c(variablesToRead, model[["columns"]]))
7288

@@ -148,9 +164,35 @@ checkCSemModel <- function(model, availableVars) {
148164
}
149165
}
150166

151-
# check for '~~'
152-
if (grepl("~~", vmodel)) {
153-
return(gettext("Using '~~' is not yet supported. Try '~' instead"))
167+
checkTildeTilde <- function(vmodel) {
168+
# Extract all lines with "~~"
169+
lines <- unlist(strsplit(vmodel, "\n"))
170+
tildeLines <- grep("~~", lines, value = TRUE)
171+
172+
# Extract variable pairs using a regex
173+
variablePairs <- lapply(tildeLines, function(line) {
174+
match <- regexec("\\s*(\\w+)\\s*~~\\s*(\\w+)", line)
175+
subMatch <- regmatches(line, match)[[1]]
176+
if (length(subMatch) == 3) {
177+
return(list(subMatch[2], subMatch[3]))
178+
} else {
179+
return(NULL)
180+
}
181+
})
182+
183+
# Clean up the result (remove NULLs)
184+
variablePairs <- Filter(Negate(is.null), variablePairs)
185+
return(variablePairs)
186+
}
187+
188+
checkTildeTilde(vmodel)
189+
tildeResult <- checkTildeTilde(vmodel)
190+
if (!is.null(tildeResult)) {
191+
latents <- unique(rownames(parsed$measurement))
192+
for (i in seq_along(tildeResult)) {
193+
if (all(unlist(tildeResult[[i]]) %in% latents))
194+
return(gettext("Using '~~' is not supported for composite covariances. Try '~' instead"))
195+
}
154196
}
155197

156198
# if checks pass, return empty string
@@ -169,7 +211,8 @@ checkCSemModel <- function(model, availableVars) {
169211
"structuralModelIgnored", "innerWeightingScheme", "errorCalculationMethod",
170212
"bootstrapSamples", "ciLevel",
171213
"setSeed", "seed", "handlingOfInadmissibles", "endogenousIndicatorPrediction",
172-
"kFolds", "repetitions", "benchmark", "models"))
214+
"kFolds", "repetitions", "benchmark", "models"
215+
))
173216
jaspResults[["modelContainer"]] <- modelContainer
174217
}
175218

@@ -559,7 +602,7 @@ checkCSemModel <- function(model, availableVars) {
559602
pe[["Total_effect"]] <- list()
560603
pe[["Total_effect"]][["mean"]] <- summ$Effect_estimates$Total_effect$Estimate
561604
names(pe[["Total_effect"]][["mean"]]) <- summ$Effect_estimates$Total_effect$Name
562-
} else{
605+
} else {
563606
IdxViFB <- 0
564607
IdxViF <- 0
565608
for (i in names(summ)) {
@@ -1730,29 +1773,29 @@ checkCSemModel <- function(model, availableVars) {
17301773
}
17311774

17321775

1733-
.plsAddConstructScores <- function(jaspResults, modelContainer, options, ready) {
1776+
.plsAddConstructScores <- function(jaspResults, options, ready) {
17341777

17351778
if (!ready ||
17361779
!is.null(jaspResults[["addedScoresContainer"]]) ||
1737-
modelContainer$getError() ||
1738-
!options[["addConstructScores"]])
1739-
{
1780+
jaspResults[["modelContainer"]]$getError() ||
1781+
!options[["addConstructScores"]]) {
1782+
1783+
# cat("===== DID NOT ENTER =====\n")
1784+
17401785
return()
17411786
}
17421787

1743-
container <- createJaspContainer()
1744-
container$dependOn(optionsFromObject = modelContainer, options = "addConstructScores")
1745-
jaspResults[["addedScoresContainer"]] <- container
1788+
# cat("===== ENTER: .plsAddConstructScores() =====\n")
1789+
# cat("container scores: ")
1790+
# print(is.null(jaspResults[["addedScoresContainer"]]))
17461791

1747-
models <- modelContainer[["models"]][["object"]]
1748-
results <- modelContainer[["results"]][["object"]]
1792+
container <- createJaspContainer()
1793+
container$dependOn(options = "addConstructScores")
17491794

1750-
modelNames <- sapply(models, function(x) x[["name"]])
1751-
modelNames <- gsub(" ", "_", modelNames)
1752-
colNamesR <- c()
1795+
results <- jaspResults[["modelContainer"]][["results"]][["object"]]
17531796

17541797
# loop over the models
1755-
for (i in seq_len(length(results))) {
1798+
for (i in seq_along(results)) {
17561799

17571800
if (options$group != "") {
17581801
scoresList <- cSEM::getConstructScores(results[[i]])
@@ -1768,7 +1811,7 @@ checkCSemModel <- function(model, availableVars) {
17681811
}
17691812

17701813
z <- 1
1771-
for (ll in seq_len(length(scores))) {
1814+
for (ll in seq_along(scores)) {
17721815
for (ii in seq_len(ncol(scores[[ll]]))) {
17731816

17741817
colNameR <- colNamesR[z]
@@ -1787,10 +1830,11 @@ checkCSemModel <- function(model, availableVars) {
17871830
}
17881831

17891832
jaspResults[["addedScoresContainer"]] <- container
1833+
# print(str(jaspResults[["addedScoresContainer"]]))
17901834

17911835
# check if there are previous colNames that are not needed anymore and delete the cols
17921836
oldNames <- jaspResults[["createdColumnNames"]][["object"]]
1793-
newNames <- colNamesR[1:z]
1837+
newNames <- colNamesR
17941838
if (!is.null(oldNames)) {
17951839
noMatch <- which(!(oldNames %in% newNames))
17961840
if (length(noMatch) > 0) {
@@ -1803,6 +1847,7 @@ checkCSemModel <- function(model, availableVars) {
18031847
# save the created col names
18041848
jaspResults[["createdColumnNames"]] <- createJaspState(newNames)
18051849

1850+
cat("===== EXIT: .plsAddConstructScores() =====\n\n")
18061851

18071852
return()
18081853

R/sem.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
SEMInternal <- function(jaspResults, dataset, options, ...) {
2121
jaspResults$addCitation("Rosseel, Y. (2012). lavaan: An R Package for Structural Equation Modeling. Journal of Statistical Software, 48(2), 1-36. URL http://www.jstatsoft.org/v48/i02/")
2222

23+
saveRDS(options, "~/Downloads/options.rds")
24+
25+
saveRDS(dataset, "~/Downloads/dataset.rds")
26+
2327
# Read dataset
2428
options <- .semPrepOpts(options)
2529

inst/Description.qml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ Description
2020
title: qsTr("Structural Equation Modeling")
2121
qml: "SEM.qml"
2222
func: "SEM"
23+
preloadData: true
2324
}
2425

2526
Analysis
2627
{
2728
title: qsTr("Partial Least Squares SEM")
2829
qml: "PLSSEM.qml"
2930
func: "PLSSEM"
31+
preloadData: true
3032
}
3133

3234
Analysis

inst/qml/PLSSEM.qml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Form
5151
label: qsTr("Grouping Variable")
5252
showVariableTypeIcon: true
5353
addEmptyValue: true
54+
allowedColumns: ["nominal"]
5455
}
5556
}
5657
}

0 commit comments

Comments
 (0)