18
18
PLSSEMInternal <- function (jaspResults , dataset , options , ... ) {
19
19
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/." )
20
20
21
- sink(file = " ~/Downloads/log.txt" )
22
- on.exit(sink(NULL ))
23
-
24
21
options <- .plsSemPrepOpts(options )
25
22
26
23
# Read data, check if ready
@@ -1046,8 +1043,8 @@ checkCSemModel <- function(model, availableVars) {
1046
1043
}
1047
1044
}
1048
1045
1049
- .plsSemPredictionTables <- function (fit , name , parent , modelContainer , options , ready ) {
1050
1046
1047
+ .plsSemPredictionTables <- function (fit , name , parent , modelContainer , options , ready ) {
1051
1048
1052
1049
if (is.null(name )) {
1053
1050
predictcont <- parent
@@ -1114,7 +1111,7 @@ checkCSemModel <- function(model, availableVars) {
1114
1111
if (" MAXVAR" %in% benchmarks )
1115
1112
metricstab $ addColumnInfo(name = " rmseMAXVAR" , title = gettext(" MAXVAR RMSE" ), type = " number" )
1116
1113
1117
- metricstab $ addColumnInfo(name = " q2" , title = gettext(" Target Q2 prediction " ), type = " number" )
1114
+ metricstab $ addColumnInfo(name = " q2" , title = gettext(" Target Q2 Prediction " ), type = " number" )
1118
1115
1119
1116
predictcont [[" metrics" ]] <- metricstab
1120
1117
@@ -1140,12 +1137,12 @@ checkCSemModel <- function(model, availableVars) {
1140
1137
progressbarTick()
1141
1138
prediction_list [i ] <- prediction
1142
1139
}
1143
- }
1144
- else if (options [[" benchmark" ]] == " none" ) {
1140
+ } else if (options [[" benchmark" ]] == " none" ) {
1145
1141
prediction <- try(cSEM :: predict(fit , .handle_inadmissibles = " ignore" , .cv_folds = options [[" kFolds" ]], .r = options [[" repetitions" ]]))
1146
1142
} else {
1147
1143
prediction <- try(cSEM :: predict(fit , .handle_inadmissibles = " ignore" , .benchmark = benchmarks , .cv_folds = options [[" kFolds" ]], .r = options [[" repetitions" ]]))
1148
1144
}
1145
+
1149
1146
if (isTryError(prediction )) {
1150
1147
err <- .extractErrorMessage(prediction )
1151
1148
if (grepl(" attempt to set 'colnames'" , err ))
@@ -1207,62 +1204,9 @@ checkCSemModel <- function(model, availableVars) {
1207
1204
}
1208
1205
}
1209
1206
1210
- # create scores table
1211
- if (options [[" predictedScore" ]]) {
1212
-
1213
- scorestab <- createJaspTable(gettext(" Indicator Scores" ))
1214
-
1215
- if (options [[" group" ]] != " " ) {
1216
- scorestab $ addColumnInfo(name = " group" , title = gettext(" Group" ), type = " string" , combine = TRUE )
1217
- group_names <- names(prediction )
1218
- indicator_names <- names(prediction [[group_names [1 ]]][[" Actual" ]])
1219
- } else {
1220
- indicator_names <- names(prediction [[" Actual" ]])
1221
- }
1222
- for (j in indicator_names ) {
1223
- scorestab $ addColumnInfo(name = paste0(" actual" , j ), title = gettext(" Actual scores" ), type = " number" , overtitle = gettext(j ))
1224
- scorestab $ addColumnInfo(name = paste0(" prediction" , j ), title = gettext(" Predicted scores" ), type = " number" , overtitle = gettext(j ))
1225
- scorestab $ addColumnInfo(name = paste0(" target_residuals" , j ), title = gettext(" Target residuals" ), type = " number" , overtitle = gettext(j ))
1226
- if (options [[" benchmark" ]] != " none" ) {
1227
- scorestab $ addColumnInfo(name = paste0(" benchmark_residuals" , j ), title = gettext(paste0(ifelse(options [[" benchmark" ]] == " lm" , " Linear model" , options [[" benchmark" ]]) , " residuals" )), type = " number" , overtitle = gettext(j ))
1228
- }
1229
- }
1230
-
1231
- predictcont [[" scores" ]] <- scorestab
1232
- }
1233
1207
1234
1208
if (! is.null(name )) parent [[name ]] <- predictcont
1235
1209
1236
- # Fill indicator scores table
1237
- if (options [[" predictedScore" ]]) {
1238
- if (options [[" group" ]] != " " ) {
1239
- group_list <- list ()
1240
- for (i in group_names ) {
1241
- group_i <- rep(i , length(prediction [[i ]][[" Actual" ]][[indicator_names [1 ]]]))
1242
- group_list <- c(group_list , group_i )
1243
- }
1244
- scorestab [[" group" ]] <- group_list
1245
- for (j in indicator_names ) {
1246
- scorestab [[paste0(" actual" ,j )]] <- unlist(lapply(prediction , function (x ) x [[" Actual" ]][[j ]]))
1247
- scorestab [[paste0(" prediction" ,j )]] <- unlist(lapply(prediction , function (x ) x [[" Predictions_target" ]][, j ]))
1248
- scorestab [[paste0(" target_residuals" ,j )]] <- unlist(lapply(prediction , function (x ) x [[" Residuals_target" ]][, j ]))
1249
-
1250
- if (options [[" benchmark" ]] != " none" ) {
1251
- scorestab [[paste0(" benchmark_residuals" ,j )]] <- unlist(lapply(prediction , function (x ) x [[" Residuals_benchmark" ]][, j ]))
1252
- }
1253
- }
1254
- } else {
1255
- for (j in indicator_names ) {
1256
- scorestab [[paste0(" actual" ,j )]] <- prediction [[" Actual" ]][[j ]]
1257
- scorestab [[paste0(" prediction" ,j )]] <- prediction [[" Predictions_target" ]][, j ]
1258
- scorestab [[paste0(" target_residuals" ,j )]] <- prediction [[" Residuals_target" ]][, j ]
1259
-
1260
- if (options [[" benchmark" ]] != " none" && options [[" benchmark" ]] != " all" ) {
1261
- scorestab [[paste0(" benchmark_residuals" ,j )]] <- prediction [[" Residuals_benchmark" ]][, j ]
1262
- }
1263
- }
1264
- }
1265
- }
1266
1210
}
1267
1211
1268
1212
# Additional Fit Measures Table
@@ -1977,34 +1921,48 @@ checkCSemModel <- function(model, availableVars) {
1977
1921
1978
1922
modelNames <- sapply(models , function (x ) x [[" name" ]])
1979
1923
modelNames <- gsub(" " , " _" , modelNames )
1980
- allNamesR <- c()
1924
+ colNamesR <- c()
1925
+
1981
1926
# loop over the models
1982
1927
for (i in seq_len(length(results ))) {
1983
- scores <- cSEM :: getConstructScores(results [[i ]])$ Construct_scores
1984
1928
1985
- # then loop over the scores
1986
- scoreNames <- colnames(scores )
1987
- for (ii in seq_len(ncol(scores ))) {
1929
+ if (options $ group != " " ) {
1930
+ scoresList <- cSEM :: getConstructScores(results [[i ]])
1931
+ scores <- lapply(scoresList , function (x ) x $ Construct_scores )
1932
+ groupLabs <- names(scoresList )
1933
+ facNames <- colnames(scores [[1 ]])
1934
+ colNamesR <- paste0(rep(groupLabs , each = ncol(scores [[1 ]])), " _" , " CS_" , facNames )
1935
+ } else {
1936
+ scores <- cSEM :: getConstructScores(results [[i ]])$ Construct_scores
1937
+ facNames <- colnames(scores )
1938
+ colNamesR <- paste0(" CS_" , facNames )
1939
+ scores <- list (scores )
1940
+ }
1988
1941
1989
- colNameR <- paste0(modelNames [i ], " _" , scoreNames [ii ])
1942
+ z <- 1
1943
+ for (ll in seq_len(length(scores ))) {
1944
+ for (ii in seq_len(ncol(scores [[ll ]]))) {
1990
1945
1991
- if (jaspBase ::: columnExists(colNameR ) && ! jaspBase ::: columnIsMine(colNameR )) {
1992
- .quitAnalysis(gettextf(" Column '%s' name already exists in the dataset" , colNameR ))
1993
- }
1946
+ colNameR <- colNamesR [z ]
1947
+ scoresTmp <- scores [[ll ]]
1948
+ if (jaspBase ::: columnExists(colNameR ) && ! jaspBase ::: columnIsMine(colNameR )) {
1949
+ .quitAnalysis(gettextf(" Column name %s already exists in the dataset" , colNameR ))
1950
+ }
1951
+
1952
+ container [[colNameR ]] <- jaspBase :: createJaspColumn(colNameR )
1953
+ container [[colNameR ]]$ setScale(scoresTmp [, ii ])
1994
1954
1995
- container [[colNameR ]] <- jaspBase :: createJaspColumn(colNameR )
1996
- container [[colNameR ]]$ setScale(scores [, ii ])
1955
+ z <- z + 1
1997
1956
1998
- # save the names to keep track of all names
1999
- allNamesR <- c(allNamesR , colNameR )
1957
+ }
2000
1958
}
2001
1959
}
2002
1960
2003
1961
jaspResults [[" addedScoresContainer" ]] <- container
2004
1962
2005
1963
# check if there are previous colNames that are not needed anymore and delete the cols
2006
1964
oldNames <- jaspResults [[" createdColumnNames" ]][[" object" ]]
2007
- newNames <- allNamesR
1965
+ newNames <- colNamesR [ 1 : z ]
2008
1966
if (! is.null(oldNames )) {
2009
1967
noMatch <- which(! (oldNames %in% newNames ))
2010
1968
if (length(noMatch ) > 0 ) {
@@ -2015,7 +1973,7 @@ checkCSemModel <- function(model, availableVars) {
2015
1973
}
2016
1974
2017
1975
# save the created col names
2018
- jaspResults [[" createdColumnNames" ]] <- createJaspState(allNamesR )
1976
+ jaspResults [[" createdColumnNames" ]] <- createJaspState(newNames )
2019
1977
2020
1978
2021
1979
return ()
0 commit comments