Skip to content

Commit e311099

Browse files
committed
fix saving construct scores bug
1 parent 4485a9f commit e311099

File tree

4 files changed

+141
-350
lines changed

4 files changed

+141
-350
lines changed

R/plssem.R

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
PLSSEMInternal <- function(jaspResults, dataset, options, ...) {
1919
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/.")
2020

21-
sink(file="~/Downloads/log.txt")
22-
on.exit(sink(NULL))
21+
# sink(file="~/Downloads/log.txt")
22+
# on.exit(sink(NULL))
2323

2424
options <- .plsSemPrepOpts(options)
2525

@@ -1140,12 +1140,12 @@ checkCSemModel <- function(model, availableVars) {
11401140
progressbarTick()
11411141
prediction_list[i] <- prediction
11421142
}
1143-
}
1144-
else if (options[["benchmark"]] == "none") {
1143+
} else if (options[["benchmark"]] == "none") {
11451144
prediction <- try(cSEM::predict(fit, .handle_inadmissibles = "ignore", .cv_folds = options[["kFolds"]], .r = options[["repetitions"]]))
11461145
} else {
11471146
prediction <- try(cSEM::predict(fit, .handle_inadmissibles = "ignore", .benchmark = benchmarks, .cv_folds = options[["kFolds"]], .r = options[["repetitions"]]))
11481147
}
1148+
11491149
if (isTryError(prediction)) {
11501150
err <- .extractErrorMessage(prediction)
11511151
if(grepl("attempt to set 'colnames'", err))
@@ -1215,9 +1215,10 @@ checkCSemModel <- function(model, availableVars) {
12151215
if (options[["group"]] != "") {
12161216
scorestab$addColumnInfo(name = "group", title = gettext("Group"), type = "string", combine = TRUE)
12171217
group_names <- names(prediction)
1218-
indicator_names <- names(prediction[[group_names[1]]][["Actual"]])
1218+
indicator_names <- names(prediction[[group_names[1]]][["Actual"]][[1]])
12191219
} else {
1220-
indicator_names <- names(prediction[["Actual"]])
1220+
indicator_names <- names(prediction[["Actual"]][[1]])
1221+
# Actual list has as many datasets as Repetitions and the indicator names are columns names for each dataset
12211222
}
12221223
for (j in indicator_names) {
12231224
scorestab$addColumnInfo(name = paste0("actual", j), title = gettext("Actual scores"), type = "number", overtitle = gettext(j))
@@ -1241,7 +1242,7 @@ checkCSemModel <- function(model, availableVars) {
12411242
group_i <- rep(i, length(prediction[[i]][["Actual"]][[indicator_names[1]]]))
12421243
group_list <- c(group_list, group_i)
12431244
}
1244-
scorestab[["group"]] <- group_list
1245+
scorestab[["group"]] <- group_list
12451246
for (j in indicator_names) {
12461247
scorestab[[paste0("actual",j)]] <- unlist(lapply(prediction, function(x) x[["Actual"]][[j]]))
12471248
scorestab[[paste0("prediction",j)]] <- unlist(lapply(prediction, function(x) x[["Predictions_target"]][, j]))
@@ -1977,34 +1978,48 @@ checkCSemModel <- function(model, availableVars) {
19771978

19781979
modelNames <- sapply(models, function(x) x[["name"]])
19791980
modelNames <- gsub(" ", "_", modelNames)
1980-
allNamesR <- c()
1981+
colNamesR <- c()
1982+
19811983
# loop over the models
19821984
for (i in seq_len(length(results))) {
1983-
scores <- cSEM::getConstructScores(results[[i]])$Construct_scores
19841985

1985-
# then loop over the scores
1986-
scoreNames <- colnames(scores)
1987-
for (ii in seq_len(ncol(scores))) {
1986+
if (options$group != "") {
1987+
scoresList <- cSEM::getConstructScores(results[[i]])
1988+
scores <- lapply(scoresList, function(x) x$Construct_scores)
1989+
groupLabs <- names(scoresList)
1990+
facNames <- colnames(scores[[1]])
1991+
colNamesR <- paste0(rep(groupLabs, each = ncol(scores[[1]])), "_", "CS_", facNames)
1992+
} else {
1993+
scores <- cSEM::getConstructScores(results[[i]])$Construct_scores
1994+
facNames <- colnames(scores)
1995+
colNamesR <- paste0("CS_", facNames)
1996+
scores <- list(scores)
1997+
}
19881998

1989-
colNameR <- paste0(modelNames[i], "_", scoreNames[ii])
1999+
z <- 1
2000+
for (ll in seq_len(length(scores))) {
2001+
for (ii in seq_len(ncol(scores[[ll]]))) {
19902002

1991-
if (jaspBase:::columnExists(colNameR) && !jaspBase:::columnIsMine(colNameR)) {
1992-
.quitAnalysis(gettextf("Column '%s' name already exists in the dataset", colNameR))
1993-
}
2003+
colNameR <- colNamesR[z]
2004+
scoresTmp <- scores[[ll]]
2005+
if (jaspBase:::columnExists(colNameR) && !jaspBase:::columnIsMine(colNameR)) {
2006+
.quitAnalysis(gettextf("Column name %s already exists in the dataset", colNameR))
2007+
}
19942008

1995-
container[[colNameR]] <- jaspBase::createJaspColumn(colNameR)
1996-
container[[colNameR]]$setScale(scores[, ii])
2009+
container[[colNameR]] <- jaspBase::createJaspColumn(colNameR)
2010+
container[[colNameR]]$setScale(scoresTmp[, ii])
19972011

1998-
# save the names to keep track of all names
1999-
allNamesR <- c(allNamesR, colNameR)
2012+
z <- z + 1
2013+
2014+
}
20002015
}
20012016
}
20022017

20032018
jaspResults[["addedScoresContainer"]] <- container
20042019

20052020
# check if there are previous colNames that are not needed anymore and delete the cols
20062021
oldNames <- jaspResults[["createdColumnNames"]][["object"]]
2007-
newNames <- allNamesR
2022+
newNames <- colNamesR[1:z]
20082023
if (!is.null(oldNames)) {
20092024
noMatch <- which(!(oldNames %in% newNames))
20102025
if (length(noMatch) > 0) {
@@ -2015,7 +2030,7 @@ checkCSemModel <- function(model, availableVars) {
20152030
}
20162031

20172032
# save the created col names
2018-
jaspResults[["createdColumnNames"]] <- createJaspState(allNamesR)
2033+
jaspResults[["createdColumnNames"]] <- createJaspState(newNames)
20192034

20202035

20212036
return()

inst/qml/PLSSEM.qml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Form
3030
id: models
3131
name: "models"
3232
maximumItems: 1
33+
newItemName: qsTr("Model")
34+
optionKey: "name"
3335

3436
content: TextArea { name: "syntax"; width: models.width; textType: JASP.TextTypeCSem; showLineNumber: true }
3537
}

renv.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,11 +1103,11 @@
11031103
"Version": "0.19.2",
11041104
"Source": "GitHub",
11051105
"RemoteType": "github",
1106+
"RemoteHost": "api.github.com",
11061107
"RemoteUsername": "jasp-stats",
11071108
"RemoteRepo": "jaspBase",
11081109
"RemoteRef": "master",
1109-
"RemoteSha": "67652e03f135b8d9b7c42e353a0fe02b9fea0929",
1110-
"RemoteHost": "api.github.com",
1110+
"RemoteSha": "cf7c84b877e1af958dd0d5b228d9a81831002bdd",
11111111
"Requirements": [
11121112
"R6",
11131113
"Rcpp",
@@ -1136,18 +1136,18 @@
11361136
"systemfonts",
11371137
"withr"
11381138
],
1139-
"Hash": "54ff30fbbe384057dea12677bdb0de3f"
1139+
"Hash": "98b18ec61f0bd447d543612016111494"
11401140
},
11411141
"jaspGraphs": {
11421142
"Package": "jaspGraphs",
11431143
"Version": "0.19.2",
11441144
"Source": "GitHub",
11451145
"RemoteType": "github",
1146+
"RemoteHost": "api.github.com",
11461147
"RemoteUsername": "jasp-stats",
11471148
"RemoteRepo": "jaspGraphs",
11481149
"RemoteRef": "master",
1149-
"RemoteSha": "797e38a84746e9aa0f456439b8f00f756164d82c",
1150-
"RemoteHost": "api.github.com",
1150+
"RemoteSha": "9bd1fdafc6bf6a04451f982e6c6dd9002524dbcb",
11511151
"Requirements": [
11521152
"R6",
11531153
"RColorBrewer",
@@ -1160,7 +1160,7 @@
11601160
"scales",
11611161
"viridisLite"
11621162
],
1163-
"Hash": "35093128cffc6280d0ec4183a851a9d9"
1163+
"Hash": "1eacba557c12ef472e122cc9445cb134"
11641164
},
11651165
"jaspTools": {
11661166
"Package": "jaspTools",

0 commit comments

Comments
 (0)