Skip to content

Commit fd600d1

Browse files
committed
move towards preloadData
1 parent 75c1036 commit fd600d1

File tree

5 files changed

+4904
-1057
lines changed

5 files changed

+4904
-1057
lines changed

DESCRIPTION

+1-1
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

+68-26
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@
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")
27+
saveRDS(dataset, "~/Downloads/dataset.rds")
2328

24-
# Read data, check if ready
25-
dataset <- .plsSemReadData(dataset, options)
29+
30+
# Handle data, check if ready
31+
dataset <- .plsSemHandleData(dataset, options)
32+
# dataset <- .plsSemReadData(dataset, options)
2633
ready <- .plsSemIsReady(dataset, options)
2734

28-
saveRDS(dataset, "~/Downloads/dataset.rds")
29-
saveRDS(options, "~/Downloads/options.rds")
3035

3136
# Store in container
3237
modelContainer <- .plsSemModelContainer(jaspResults)
@@ -45,7 +50,7 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
4550
.plsSemReliabilities(modelContainer, dataset, options, ready)
4651
.plsSemCor(modelContainer, options, ready)
4752

48-
.plsAddConstructScores(jaspResults, modelContainer, options, ready)
53+
.plsAddConstructScores(jaspResults, options, ready)
4954
}
5055

5156
.plsSemPrepOpts <- function(options) {
@@ -54,7 +59,7 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
5459
newModel <- c(model[1], model[[2]])
5560
names(newModel)[names(newModel) == "model"] <- "syntax"
5661
return(newModel)
57-
}
62+
}
5863

5964
options[["models"]] <- lapply(options[["models"]], fixModel)
6065

@@ -63,10 +68,18 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
6368
return(options)
6469
}
6570

71+
.plsSemHandleData <- function(dataset, options) {
72+
73+
# listwise deletion
74+
dataset <- dataset[complete.cases(dataset), ]
75+
return(dataset)
76+
}
77+
6678
.plsSemReadData <- function(dataset, options) {
6779
if (!is.null(dataset)) return(dataset)
6880

6981
variablesToRead <- if (options[["group"]] == "") character() else options[["group"]]
82+
7083
for (model in options[["models"]])
7184
variablesToRead <- unique(c(variablesToRead, model[["columns"]]))
7285

@@ -148,9 +161,35 @@ checkCSemModel <- function(model, availableVars) {
148161
}
149162
}
150163

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

156195
# if checks pass, return empty string
@@ -169,7 +208,8 @@ checkCSemModel <- function(model, availableVars) {
169208
"structuralModelIgnored", "innerWeightingScheme", "errorCalculationMethod",
170209
"bootstrapSamples", "ciLevel",
171210
"setSeed", "seed", "handlingOfInadmissibles", "endogenousIndicatorPrediction",
172-
"kFolds", "repetitions", "benchmark", "models"))
211+
"kFolds", "repetitions", "benchmark", "models"
212+
))
173213
jaspResults[["modelContainer"]] <- modelContainer
174214
}
175215

@@ -559,7 +599,7 @@ checkCSemModel <- function(model, availableVars) {
559599
pe[["Total_effect"]] <- list()
560600
pe[["Total_effect"]][["mean"]] <- summ$Effect_estimates$Total_effect$Estimate
561601
names(pe[["Total_effect"]][["mean"]]) <- summ$Effect_estimates$Total_effect$Name
562-
} else{
602+
} else {
563603
IdxViFB <- 0
564604
IdxViF <- 0
565605
for (i in names(summ)) {
@@ -1730,29 +1770,29 @@ checkCSemModel <- function(model, availableVars) {
17301770
}
17311771

17321772

1733-
.plsAddConstructScores <- function(jaspResults, modelContainer, options, ready) {
1773+
.plsAddConstructScores <- function(jaspResults, options, ready) {
17341774

17351775
if (!ready ||
17361776
!is.null(jaspResults[["addedScoresContainer"]]) ||
1737-
modelContainer$getError() ||
1738-
!options[["addConstructScores"]])
1739-
{
1777+
jaspResults[["modelContainer"]]$getError() ||
1778+
!options[["addConstructScores"]]) {
1779+
1780+
# cat("===== DID NOT ENTER =====\n")
1781+
17401782
return()
17411783
}
17421784

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

1747-
models <- modelContainer[["models"]][["object"]]
1748-
results <- modelContainer[["results"]][["object"]]
1789+
container <- createJaspContainer()
1790+
container$dependOn(options = "addConstructScores")
17491791

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

17541794
# loop over the models
1755-
for (i in seq_len(length(results))) {
1795+
for (i in seq_along(results)) {
17561796

17571797
if (options$group != "") {
17581798
scoresList <- cSEM::getConstructScores(results[[i]])
@@ -1768,7 +1808,7 @@ checkCSemModel <- function(model, availableVars) {
17681808
}
17691809

17701810
z <- 1
1771-
for (ll in seq_len(length(scores))) {
1811+
for (ll in seq_along(scores)) {
17721812
for (ii in seq_len(ncol(scores[[ll]]))) {
17731813

17741814
colNameR <- colNamesR[z]
@@ -1787,10 +1827,11 @@ checkCSemModel <- function(model, availableVars) {
17871827
}
17881828

17891829
jaspResults[["addedScoresContainer"]] <- container
1830+
# print(str(jaspResults[["addedScoresContainer"]]))
17901831

17911832
# check if there are previous colNames that are not needed anymore and delete the cols
17921833
oldNames <- jaspResults[["createdColumnNames"]][["object"]]
1793-
newNames <- colNamesR[1:z]
1834+
newNames <- colNamesR
17941835
if (!is.null(oldNames)) {
17951836
noMatch <- which(!(oldNames %in% newNames))
17961837
if (length(noMatch) > 0) {
@@ -1803,6 +1844,7 @@ checkCSemModel <- function(model, availableVars) {
18031844
# save the created col names
18041845
jaspResults[["createdColumnNames"]] <- createJaspState(newNames)
18051846

1847+
cat("===== EXIT: .plsAddConstructScores() =====\n\n")
18061848

18071849
return()
18081850

inst/Description.qml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Description
2727
title: qsTr("Partial Least Squares SEM")
2828
qml: "PLSSEM.qml"
2929
func: "PLSSEM"
30+
preloadData: true
3031
}
3132

3233
Analysis

inst/qml/PLSSEM.qml

+1
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)