Skip to content

Commit 674d59f

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

File tree

6 files changed

+140
-406
lines changed

6 files changed

+140
-406
lines changed

R/plssem.R

+33-75
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
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))
23-
2421
options <- .plsSemPrepOpts(options)
2522

2623
# Read data, check if ready
@@ -1046,8 +1043,8 @@ checkCSemModel <- function(model, availableVars) {
10461043
}
10471044
}
10481045

1049-
.plsSemPredictionTables <- function(fit, name, parent, modelContainer, options, ready) {
10501046

1047+
.plsSemPredictionTables <- function(fit, name, parent, modelContainer, options, ready) {
10511048

10521049
if (is.null(name)) {
10531050
predictcont <- parent
@@ -1114,7 +1111,7 @@ checkCSemModel <- function(model, availableVars) {
11141111
if("MAXVAR" %in% benchmarks)
11151112
metricstab$addColumnInfo(name = "rmseMAXVAR", title = gettext("MAXVAR RMSE"), type = "number")
11161113

1117-
metricstab$addColumnInfo(name = "q2", title = gettext("Target Q2 prediction"), type = "number")
1114+
metricstab$addColumnInfo(name = "q2", title = gettext("Target Q2 Prediction"), type = "number")
11181115

11191116
predictcont[["metrics"]] <- metricstab
11201117

@@ -1140,12 +1137,12 @@ checkCSemModel <- function(model, availableVars) {
11401137
progressbarTick()
11411138
prediction_list[i] <- prediction
11421139
}
1143-
}
1144-
else if (options[["benchmark"]] == "none") {
1140+
} else if (options[["benchmark"]] == "none") {
11451141
prediction <- try(cSEM::predict(fit, .handle_inadmissibles = "ignore", .cv_folds = options[["kFolds"]], .r = options[["repetitions"]]))
11461142
} else {
11471143
prediction <- try(cSEM::predict(fit, .handle_inadmissibles = "ignore", .benchmark = benchmarks, .cv_folds = options[["kFolds"]], .r = options[["repetitions"]]))
11481144
}
1145+
11491146
if (isTryError(prediction)) {
11501147
err <- .extractErrorMessage(prediction)
11511148
if(grepl("attempt to set 'colnames'", err))
@@ -1207,62 +1204,9 @@ checkCSemModel <- function(model, availableVars) {
12071204
}
12081205
}
12091206

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-
}
12331207

12341208
if (!is.null(name)) parent[[name]] <- predictcont
12351209

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-
}
12661210
}
12671211

12681212
# Additional Fit Measures Table
@@ -1977,34 +1921,48 @@ checkCSemModel <- function(model, availableVars) {
19771921

19781922
modelNames <- sapply(models, function(x) x[["name"]])
19791923
modelNames <- gsub(" ", "_", modelNames)
1980-
allNamesR <- c()
1924+
colNamesR <- c()
1925+
19811926
# loop over the models
19821927
for (i in seq_len(length(results))) {
1983-
scores <- cSEM::getConstructScores(results[[i]])$Construct_scores
19841928

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+
}
19881941

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]]))) {
19901945

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])
19941954

1995-
container[[colNameR]] <- jaspBase::createJaspColumn(colNameR)
1996-
container[[colNameR]]$setScale(scores[, ii])
1955+
z <- z + 1
19971956

1998-
# save the names to keep track of all names
1999-
allNamesR <- c(allNamesR, colNameR)
1957+
}
20001958
}
20011959
}
20021960

20031961
jaspResults[["addedScoresContainer"]] <- container
20041962

20051963
# check if there are previous colNames that are not needed anymore and delete the cols
20061964
oldNames <- jaspResults[["createdColumnNames"]][["object"]]
2007-
newNames <- allNamesR
1965+
newNames <- colNamesR[1:z]
20081966
if (!is.null(oldNames)) {
20091967
noMatch <- which(!(oldNames %in% newNames))
20101968
if (length(noMatch) > 0) {
@@ -2015,7 +1973,7 @@ checkCSemModel <- function(model, availableVars) {
20151973
}
20161974

20171975
# save the created col names
2018-
jaspResults[["createdColumnNames"]] <- createJaspState(allNamesR)
1976+
jaspResults[["createdColumnNames"]] <- createJaspState(newNames)
20191977

20201978

20211979
return()

inst/Description.qml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Description
77
title : qsTr("SEM")
88
description: qsTr("Evaluate latent data structures with Yves Rosseel’s lavaan program")
99
icon: "sem-latreg.svg"
10-
version : "0.19.2"
10+
version : "0.19.3"
1111
author: "JASP Team"
1212
maintainer: "JASP Team <[email protected]>"
1313
website: "https://github.com/jasp-stats/jaspSem/"

inst/Upgrades.qml

+2
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,6 @@ Upgrades
445445
ChangeRename { from: "eq_lvcovariances"; to: "equalLatentCovariance" }
446446
ChangeRename { from: "group.partial"; to: "freeParameters" }
447447
}
448+
449+
448450
}

inst/qml/PLSSEM.qml

+2-2
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
}
@@ -216,8 +218,6 @@ Form
216218
RadioButton { value: "MAXVAR"; label: qsTr("MAXVAR") }
217219
RadioButton { value: "all"; label: qsTr("All") }
218220
}
219-
220-
CheckBox { name: "predictedScore"; label: qsTr("Show predicted scores"); enabled: prediction.checked}
221221
}
222222
}
223223
}

renv.lock

+6-6
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)