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