Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Contingency Table #217

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rprofile
FBartos marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
96 changes: 53 additions & 43 deletions R/contingencytables.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ ContingencyTablesInternal <- function(jaspResults, dataset, options, ...) {

# Output Tables
.crossTabMain <- function(jaspResults, dataset, options, analyses, ready) {

if (!(options$countsObserved || options$countsExpected || options$percentagesRow || options$percentagesColumn ||
options$percentagesTotal || options$residualsUnstandardized || options$residualsPearson || options$residualsStandardized))
return()

for (i in 1:nrow(analyses)){
analysis <- analyses[i,]
analysisContainer <- jaspResults[[.crossTabCreateContainerName(analysis)]]
Expand All @@ -130,12 +135,12 @@ ContingencyTablesInternal <- function(jaspResults, dataset, options, ...) {

# Create table
crossTabMain <- createJaspTable(title = gettext("Contingency Tables"))
crossTabMain$dependOn(c("countsExpected", "percentagesRow", "percentagesColumn",
crossTabMain$dependOn(c("countsExpected", "countsObserved", "tableMargin", "percentagesRow", "percentagesColumn",
"percentagesTotal", "rowOrder", "columnOrder", "residualsUnstandardized",
"residualsPearson", "residualsStandardized"))
crossTabMain$showSpecifiedColumnsOnly <- TRUE
crossTabMain$position <- 1
#

.crossTabLayersColumns(crossTabMain, analysis)

colTitleHere <- analysis$rows
Expand All @@ -146,35 +151,35 @@ ContingencyTablesInternal <- function(jaspResults, dataset, options, ...) {

counts.fp <- .crossTabCountsFp(dataset, options)

if (options$countsExpected || options$percentagesRow || options$percentagesColumn ||
options$percentagesTotal || options$residualsUnstandardized || options$residualsPearson ||
options$residualsStandardized)
crossTabMain$addColumnInfo(name = "type[counts]", title = "", type = "string")
if (options$countsExpected) crossTabMain$addColumnInfo(name = "type[expected]", title = "", type = "string")
if (options$percentagesRow) crossTabMain$addColumnInfo(name = "type[row.proportions]", title = "", type = "string")
if (options$percentagesColumn) crossTabMain$addColumnInfo(name = "type[col.proportions]", title = "", type = "string")
if (options$percentagesTotal) crossTabMain$addColumnInfo(name = "type[total.proportions]", title = "", type = "string")
if (options$residualsUnstandardized) crossTabMain$addColumnInfo(name = "type[unstandardized.residuals]",title = "", type = "string")
if (options$residualsPearson) crossTabMain$addColumnInfo(name = "type[pearson.residuals]", title = "", type = "string")
if (options$residualsStandardized) crossTabMain$addColumnInfo(name = "type[standardized.residuals]", title = "", type = "string")
if (sum(options$countsObserved, options$countsExpected, options$percentagesRow, options$percentagesColumn,
options$percentagesTotal, options$residualsUnstandardized, options$residualsPearson, options$residualsStandardized) > 1) {
if (options$countsObserved) crossTabMain$addColumnInfo(name = "type[counts]", title = "", type = "string")
if (options$countsExpected) crossTabMain$addColumnInfo(name = "type[expected]", title = "", type = "string")
if (options$percentagesRow) crossTabMain$addColumnInfo(name = "type[row.proportions]", title = "", type = "string")
if (options$percentagesColumn) crossTabMain$addColumnInfo(name = "type[col.proportions]", title = "", type = "string")
if (options$percentagesTotal) crossTabMain$addColumnInfo(name = "type[total.proportions]", title = "", type = "string")
if (options$residualsUnstandardized) crossTabMain$addColumnInfo(name = "type[unstandardized.residuals]",title = "", type = "string")
if (options$residualsPearson) crossTabMain$addColumnInfo(name = "type[pearson.residuals]", title = "", type = "string")
if (options$residualsStandardized) crossTabMain$addColumnInfo(name = "type[standardized.residuals]", title = "", type = "string")
}

.crossTabMainOvertitle(dataset, options, crossTabMain, analysis, counts.fp)

# Totals columns
totalTitle <- gettext("Total")
if (counts.fp || options$countsExpected || options$percentagesRow || options$percentagesColumn ||
options$percentagesTotal || options$residualsUnstandardized || options$residualsPearson ||
options$residualsStandardized) {
crossTabMain$addColumnInfo(name = "total[counts]", title = totalTitle, type = "number", format = "sf:4;dp:2")
if (options$countsExpected) crossTabMain$addColumnInfo(name = "total[expected]", title = totalTitle, type = "number", format = "sf:4;dp:2")
if (options$percentagesRow) crossTabMain$addColumnInfo(name = "total[row.proportions]", title = totalTitle, type = "number", format = "dp:1;pc")
if (options$percentagesColumn) crossTabMain$addColumnInfo(name = "total[col.proportions]", title = totalTitle, type = "number", format = "dp:1;pc")
if (options$percentagesTotal) crossTabMain$addColumnInfo(name = "total[total.proportions]", title = totalTitle, type = "number", format = "dp:1;pc")
if (options$residualsUnstandardized) crossTabMain$addColumnInfo(name = "total[unstandardized.residuals]", title = totalTitle, type = "number", format = "sf:4;dp:2")
if (options$residualsPearson) crossTabMain$addColumnInfo(name = "total[pearson.residuals]", title = totalTitle, type = "number", format = "sf:4;dp:2")
if (options$residualsStandardized) crossTabMain$addColumnInfo(name = "total[standardized.residuals]", title = totalTitle, type = "number", format = "sf:4;dp:2")
} else
crossTabMain$addColumnInfo(name = "total[counts]", title = totalTitle, type = "integer")

if (options$tableMargin && (counts.fp || options$countsExpected || options$percentagesRow || options$percentagesColumn ||
options$percentagesTotal || options$residualsUnstandardized || options$residualsPearson || options$residualsStandardized)) {
if (options$countsObserved) crossTabMain$addColumnInfo(name = "total[counts]", title = totalTitle, type = "number", format = "sf:4;dp:2")
if (options$countsExpected) crossTabMain$addColumnInfo(name = "total[expected]", title = totalTitle, type = "number", format = "sf:4;dp:2")
if (options$percentagesRow) crossTabMain$addColumnInfo(name = "total[row.proportions]", title = totalTitle, type = "number", format = "dp:1;pc")
if (options$percentagesColumn) crossTabMain$addColumnInfo(name = "total[col.proportions]", title = totalTitle, type = "number", format = "dp:1;pc")
if (options$percentagesTotal) crossTabMain$addColumnInfo(name = "total[total.proportions]", title = totalTitle, type = "number", format = "dp:1;pc")
if (options$residualsUnstandardized) crossTabMain$addColumnInfo(name = "total[unstandardized.residuals]", title = totalTitle, type = "number", format = "sf:4;dp:2")
if (options$residualsPearson) crossTabMain$addColumnInfo(name = "total[pearson.residuals]", title = totalTitle, type = "number", format = "sf:4;dp:2")
if (options$residualsStandardized) crossTabMain$addColumnInfo(name = "total[standardized.residuals]", title = totalTitle, type = "number", format = "sf:4;dp:2")
} else if (options$tableMargin)
if (options$countsObserved) crossTabMain$addColumnInfo(name = "total[counts]", title = totalTitle, type = "integer")

analysisContainer[["crossTabMain"]] <- crossTabMain
analysis <- as.list(analysis)
Expand Down Expand Up @@ -413,14 +418,14 @@ ContingencyTablesInternal <- function(jaspResults, dataset, options, ...) {
pr.type <- "number"
}

table$addColumnInfo(name = paste0(column.name,"[counts]"), title = myTitle, type = pr.type, format = pr.format, overtitle = overTitle)
if (options$countsExpected) table$addColumnInfo(name = paste0(column.name,"[expected]"), title = myTitle, type = "number", format = "sf:4;dp:2")
if (options$percentagesRow) table$addColumnInfo(name = paste0(column.name,"[row.proportions]"), title = myTitle, type = "number", format = "dp:1;pc")
if (options$percentagesColumn) table$addColumnInfo(name = paste0(column.name,"[col.proportions]"), title = myTitle, type = "number", format = "dp:1;pc")
if (options$percentagesTotal) table$addColumnInfo(name = paste0(column.name,"[total.proportions]"), title = myTitle, type = "number", format = "dp:1;pc")
if (options$residualsUnstandardized) table$addColumnInfo(name = paste0(column.name,"[unstandardized.residuals]"),title = myTitle, type = "number", format = "sf:4;dp:2")
if (options$residualsPearson) table$addColumnInfo(name = paste0(column.name,"[pearson.residuals]"), title = myTitle, type = "number", format = "sf:4;dp:2")
if (options$residualsStandardized) table$addColumnInfo(name = paste0(column.name,"[standardized.residuals]"), title = myTitle, type = "number", format = "sf:4;dp:2")
if (options$countsObserved) table$addColumnInfo(name = paste0(column.name,"[counts]"), title = myTitle, type = pr.type, format = pr.format, overtitle = overTitle)
if (options$countsExpected) table$addColumnInfo(name = paste0(column.name,"[expected]"), title = myTitle, type = "number", format = "sf:4;dp:2", overtitle = overTitle)
if (options$percentagesRow) table$addColumnInfo(name = paste0(column.name,"[row.proportions]"), title = myTitle, type = "number", format = "dp:1;pc", overtitle = overTitle)
if (options$percentagesColumn) table$addColumnInfo(name = paste0(column.name,"[col.proportions]"), title = myTitle, type = "number", format = "dp:1;pc", overtitle = overTitle)
if (options$percentagesTotal) table$addColumnInfo(name = paste0(column.name,"[total.proportions]"), title = myTitle, type = "number", format = "dp:1;pc", overtitle = overTitle)
if (options$residualsUnstandardized) table$addColumnInfo(name = paste0(column.name,"[unstandardized.residuals]"),title = myTitle, type = "number", format = "sf:4;dp:2", overtitle = overTitle)
if (options$residualsPearson) table$addColumnInfo(name = paste0(column.name,"[pearson.residuals]"), title = myTitle, type = "number", format = "sf:4;dp:2", overtitle = overTitle)
if (options$residualsStandardized) table$addColumnInfo(name = paste0(column.name,"[standardized.residuals]"), title = myTitle, type = "number", format = "sf:4;dp:2", overtitle = overTitle)
}
}

Expand Down Expand Up @@ -882,7 +887,8 @@ ContingencyTablesInternal <- function(jaspResults, dataset, options, ...) {
row[["total[counts]"]] <- sum
else row[["total[counts]"]] <- as.integer(sum)

row <- c(row.count, row)
if (options$countsObserved)
row <- c(row.count, row)

if (options$countsExpected)
row <- c(row, .crossTabCountsMatrixToRow(expected.matrix,
Expand Down Expand Up @@ -924,7 +930,7 @@ ContingencyTablesInternal <- function(jaspResults, dataset, options, ...) {
row[[var.name]] <- dimnames(counts.matrix)[[1]][j]
row <- .crossTabLayerNames(row, group)

if (j == 1 && !options$countsExpected && !options$percentagesRow &&
if (j == 1 && !options$countsObserved && !options$countsExpected && !options$percentagesRow &&
!options$percentagesCol && !options$percentagesTotal &&
!options$residualsUnstandardized && !options$residualsPearson && !options$residualsStandardized)
row[[".isNewGroup"]] <- TRUE
Expand All @@ -937,13 +943,15 @@ ContingencyTablesInternal <- function(jaspResults, dataset, options, ...) {
names(row) <- paste0(names(row),"[counts]")
sum <- sum(counts.matrix)

if(counts.fp || options$countsExpected || options$percentagesRow ||
if(options$tableMargin && (counts.fp || options$countsExpected || options$percentagesRow ||
options$percentagesColumn || options$percentagesTotal || options$residualsUnstandardized ||
options$residualsPearson || options$residualsStandardized)
options$residualsPearson || options$residualsStandardized))
row[["total[counts]"]] <- sum
else row[["total[counts]"]] <- as.integer(sum)
else if (options$tableMargin)
row[["total[counts]"]] <- as.integer(sum)

row <- c(row.count, row)
if (options$countsObserved)
row <- c(row.count, row)

if (options$countsExpected) {
expected <- .crossTabCountsColumnTotalsMatrixToRow(expected.matrix, counts.matrix, type = "expected")
Expand Down Expand Up @@ -973,13 +981,15 @@ ContingencyTablesInternal <- function(jaspResults, dataset, options, ...) {
if(var.name != "")
row[[var.name]] <- gettext("Total")

if (!(options$countsExpected || options$percentagesRow || options$percentagesCol ||
if (!(options$countsObserved || options$countsExpected || options$percentagesRow || options$percentagesCol ||
options$percentagesTotal || options$residualsUnstandardized || options$residualsPearson ||
options$residualsStandardized))
row[[".isNewGroup"]] <- TRUE

row <- .crossTabLayerNames(row, group)
rows[[length(rows) + 1]] <- row
if (options$tableMargin) {
row <- .crossTabLayerNames(row, group)
rows[[length(rows) + 1]] <- row
}
counts.rows <- c(counts.rows, rows)
}

Expand Down
4 changes: 4 additions & 0 deletions inst/qml/ContingencyTables.qml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Form
Group
{
title: qsTr("Counts")
CheckBox { name: "countsObserved"; label: qsTr("Observed"); checked: true }
CheckBox { name: "countsExpected"; label: qsTr("Expected") }
CheckBox
{
Expand Down Expand Up @@ -148,6 +149,9 @@ Form
CheckBox { name: "percentagesColumn"; label: qsTr("Column") }
CheckBox { name: "percentagesTotal"; label: qsTr("Total") }
}


CheckBox { name: "tableMargin"; label: qsTr("Table margin"); checked: true }
}

Section
Expand Down
4 changes: 3 additions & 1 deletion inst/qml/ContingencyTablesBayesian.qml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Form
Group
{
title: qsTr("Counts")
debug: true
CheckBox { name: "countsObserved"; label: qsTr("Observed"); checked: true }
CheckBox { name: "countsExpected"; label: qsTr("Expected") }
}

Expand All @@ -127,6 +127,8 @@ Form
CheckBox { name: "percentagesColumn"; label: qsTr("Column") }
CheckBox { name: "percentagesTotal"; label: qsTr("Total") }
}

CheckBox { name: "tableMargin"; label: qsTr("Table margin"); checked: true }
}

Section
Expand Down
7 changes: 7 additions & 0 deletions renv/.gitignore
FBartos marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library/
local/
cellar/
lock/
python/
sandbox/
staging/
Loading
Loading