Skip to content

Commit 9c020be

Browse files
committed
add ~~ factor name check
1 parent c74fa07 commit 9c020be

File tree

2 files changed

+128
-59
lines changed

2 files changed

+128
-59
lines changed

R/plssem.R

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
2222

2323
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/.")
2424

25-
saveRDS(options, "~/Downloads/options.rds")
25+
2626
options <- .plsSemPrepOpts(options)
27+
saveRDS(options, "~/Downloads/options.rds")
2728

28-
# Read data, check if ready
29-
saveRDS(dataset, "~/Downloads/dataset.rds")
29+
# Handle data, check if ready
30+
# dataset <- .plsSemHandleData(dataset, options)
3031
dataset <- .plsSemReadData(dataset, options)
3132
ready <- .plsSemIsReady(dataset, options)
3233

33-
print(str(dataset))
34-
34+
saveRDS(dataset, "~/Downloads/dataset.rds")
3535

3636
# Store in container
3737
modelContainer <- .plsSemModelContainer(jaspResults)
@@ -68,10 +68,18 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
6868
return(options)
6969
}
7070

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

7481
variablesToRead <- if (options[["group"]] == "") character() else options[["group"]]
82+
7583
for (model in options[["models"]])
7684
variablesToRead <- unique(c(variablesToRead, model[["columns"]]))
7785

@@ -153,9 +161,35 @@ checkCSemModel <- function(model, availableVars) {
153161
}
154162
}
155163

156-
# check for '~~'
157-
if (grepl("~~", vmodel)) {
158-
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+
}
159193
}
160194

161195
# if checks pass, return empty string
@@ -565,7 +599,7 @@ checkCSemModel <- function(model, availableVars) {
565599
pe[["Total_effect"]] <- list()
566600
pe[["Total_effect"]][["mean"]] <- summ$Effect_estimates$Total_effect$Estimate
567601
names(pe[["Total_effect"]][["mean"]]) <- summ$Effect_estimates$Total_effect$Name
568-
} else{
602+
} else {
569603
IdxViFB <- 0
570604
IdxViF <- 0
571605
for (i in names(summ)) {
@@ -1743,14 +1777,14 @@ checkCSemModel <- function(model, availableVars) {
17431777
jaspResults[["modelContainer"]]$getError() ||
17441778
!options[["addConstructScores"]]) {
17451779

1746-
cat("===== DID NOT ENTER =====\n")
1780+
# cat("===== DID NOT ENTER =====\n")
17471781

17481782
return()
17491783
}
17501784

1751-
cat("===== ENTER: .plsAddConstructScores() =====\n")
1752-
cat("container scores: ")
1753-
print(is.null(jaspResults[["addedScoresContainer"]]))
1785+
# cat("===== ENTER: .plsAddConstructScores() =====\n")
1786+
# cat("container scores: ")
1787+
# print(is.null(jaspResults[["addedScoresContainer"]]))
17541788

17551789
container <- createJaspContainer()
17561790
container$dependOn(options = "addConstructScores")

0 commit comments

Comments
 (0)