17
17
18
18
PLSSEMInternal <- function (jaspResults , dataset , options , ... ) {
19
19
20
+ sink(file = " ~/Downloads/log.txt" , append = TRUE )
21
+ on.exit(sink(NULL ), add = TRUE )
22
+
20
23
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/." )
21
24
22
25
options <- .plsSemPrepOpts(options )
26
+ saveRDS(options , " ~/Downloads/options.rds" )
23
27
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)
27
31
28
32
saveRDS(dataset , " ~/Downloads/dataset.rds" )
29
- saveRDS(options , " ~/Downloads/options.rds" )
33
+
34
+ # dataset <- .plsSemHandleData(dataset, options)
35
+
36
+ ready <- .plsSemIsReady(dataset , options )
37
+
30
38
31
39
# Store in container
32
40
modelContainer <- .plsSemModelContainer(jaspResults )
@@ -45,7 +53,7 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
45
53
.plsSemReliabilities(modelContainer , dataset , options , ready )
46
54
.plsSemCor(modelContainer , options , ready )
47
55
48
- .plsAddConstructScores(jaspResults , modelContainer , options , ready )
56
+ .plsAddConstructScores(jaspResults , options , ready )
49
57
}
50
58
51
59
.plsSemPrepOpts <- function (options ) {
@@ -54,7 +62,7 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
54
62
newModel <- c(model [1 ], model [[2 ]])
55
63
names(newModel )[names(newModel ) == " model" ] <- " syntax"
56
64
return (newModel )
57
- }
65
+ }
58
66
59
67
options [[" models" ]] <- lapply(options [[" models" ]], fixModel )
60
68
@@ -63,10 +71,18 @@ PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
63
71
return (options )
64
72
}
65
73
74
+ .plsSemHandleData <- function (dataset , options ) {
75
+
76
+ # listwise deletion
77
+ dataset <- dataset [complete.cases(dataset ), ]
78
+ return (dataset )
79
+ }
80
+
66
81
.plsSemReadData <- function (dataset , options ) {
67
82
if (! is.null(dataset )) return (dataset )
68
83
69
84
variablesToRead <- if (options [[" group" ]] == " " ) character () else options [[" group" ]]
85
+
70
86
for (model in options [[" models" ]])
71
87
variablesToRead <- unique(c(variablesToRead , model [[" columns" ]]))
72
88
@@ -148,9 +164,35 @@ checkCSemModel <- function(model, availableVars) {
148
164
}
149
165
}
150
166
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
+ }
154
196
}
155
197
156
198
# if checks pass, return empty string
@@ -169,7 +211,8 @@ checkCSemModel <- function(model, availableVars) {
169
211
" structuralModelIgnored" , " innerWeightingScheme" , " errorCalculationMethod" ,
170
212
" bootstrapSamples" , " ciLevel" ,
171
213
" setSeed" , " seed" , " handlingOfInadmissibles" , " endogenousIndicatorPrediction" ,
172
- " kFolds" , " repetitions" , " benchmark" , " models" ))
214
+ " kFolds" , " repetitions" , " benchmark" , " models"
215
+ ))
173
216
jaspResults [[" modelContainer" ]] <- modelContainer
174
217
}
175
218
@@ -559,7 +602,7 @@ checkCSemModel <- function(model, availableVars) {
559
602
pe [[" Total_effect" ]] <- list ()
560
603
pe [[" Total_effect" ]][[" mean" ]] <- summ $ Effect_estimates $ Total_effect $ Estimate
561
604
names(pe [[" Total_effect" ]][[" mean" ]]) <- summ $ Effect_estimates $ Total_effect $ Name
562
- } else {
605
+ } else {
563
606
IdxViFB <- 0
564
607
IdxViF <- 0
565
608
for (i in names(summ )) {
@@ -1730,29 +1773,29 @@ checkCSemModel <- function(model, availableVars) {
1730
1773
}
1731
1774
1732
1775
1733
- .plsAddConstructScores <- function (jaspResults , modelContainer , options , ready ) {
1776
+ .plsAddConstructScores <- function (jaspResults , options , ready ) {
1734
1777
1735
1778
if (! ready ||
1736
1779
! 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
+
1740
1785
return ()
1741
1786
}
1742
1787
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"]]))
1746
1791
1747
- models <- modelContainer [[ " models " ]][[ " object " ]]
1748
- results <- modelContainer [[ " results " ]][[ " object " ]]
1792
+ container <- createJaspContainer()
1793
+ container $ dependOn( options = " addConstructScores " )
1749
1794
1750
- modelNames <- sapply(models , function (x ) x [[" name" ]])
1751
- modelNames <- gsub(" " , " _" , modelNames )
1752
- colNamesR <- c()
1795
+ results <- jaspResults [[" modelContainer" ]][[" results" ]][[" object" ]]
1753
1796
1754
1797
# loop over the models
1755
- for (i in seq_len(length( results ) )) {
1798
+ for (i in seq_along( results )) {
1756
1799
1757
1800
if (options $ group != " " ) {
1758
1801
scoresList <- cSEM :: getConstructScores(results [[i ]])
@@ -1768,7 +1811,7 @@ checkCSemModel <- function(model, availableVars) {
1768
1811
}
1769
1812
1770
1813
z <- 1
1771
- for (ll in seq_len(length( scores ) )) {
1814
+ for (ll in seq_along( scores )) {
1772
1815
for (ii in seq_len(ncol(scores [[ll ]]))) {
1773
1816
1774
1817
colNameR <- colNamesR [z ]
@@ -1787,10 +1830,11 @@ checkCSemModel <- function(model, availableVars) {
1787
1830
}
1788
1831
1789
1832
jaspResults [[" addedScoresContainer" ]] <- container
1833
+ # print(str(jaspResults[["addedScoresContainer"]]))
1790
1834
1791
1835
# check if there are previous colNames that are not needed anymore and delete the cols
1792
1836
oldNames <- jaspResults [[" createdColumnNames" ]][[" object" ]]
1793
- newNames <- colNamesR [ 1 : z ]
1837
+ newNames <- colNamesR
1794
1838
if (! is.null(oldNames )) {
1795
1839
noMatch <- which(! (oldNames %in% newNames ))
1796
1840
if (length(noMatch ) > 0 ) {
@@ -1803,6 +1847,7 @@ checkCSemModel <- function(model, availableVars) {
1803
1847
# save the created col names
1804
1848
jaspResults [[" createdColumnNames" ]] <- createJaspState(newNames )
1805
1849
1850
+ cat(" ===== EXIT: .plsAddConstructScores() =====\n\n " )
1806
1851
1807
1852
return ()
1808
1853
0 commit comments