Skip to content

[jaspFrequencies] Fix crash in Multinomial Test when Descriptives checkbox is...#310

Open
sisyphus-jasp wants to merge 1 commit into
jasp-stats:masterfrom
sisyphus-jasp:fix-sisyphus-1772767397
Open

[jaspFrequencies] Fix crash in Multinomial Test when Descriptives checkbox is...#310
sisyphus-jasp wants to merge 1 commit into
jasp-stats:masterfrom
sisyphus-jasp:fix-sisyphus-1772767397

Conversation

@sisyphus-jasp

Copy link
Copy Markdown

Summary

Fixes: https://github.com/jasp-stats/INTERNAL-jasp/issues/3109

Root cause

  • R/multinomialtest.R .chisquareTest() called chisq.test() even when only one observed factor level was present.
  • chisq.test() then errored ('x' must at least have 2 elements), causing fatal termination when descriptives output requested that result path.

What changed

  • Added a guard in .chisquareTest() for length(dataTable) < 2.
  • For this edge case, code now skips chisq.test() and returns chisq-like placeholder results (observed, expected, X-squared=NA, df=NA, p.value=NA) plus a translated warning message.
  • Valid inputs (length(dataTable) >= 2) keep the original code path unchanged.

Why

  • Prevent fatal crash while preserving output rendering for descriptives/main table paths.
  • Provide explicit user-facing note: chi-square needs at least two levels with observations.

Caveats / reviewer checks

  • Reproduction in this container used synthetic data frame (single-level debInf) because provided upgrade .jasp fixture lacked internal.sqlite in this environment.
  • Full testAll() run after rebuild: [ FAIL 0 | WARN 9 | SKIP 3 | PASS 110 ].
  • Exact crash scenario now returns status: complete with note instead of fatal error.
Implementation Plan

Root cause

  • In R/multinomialtest.R, .chisquareTest() calls chisq.test() even when table(factorVariable) has only one element.
  • chisq.test() errors ('x' must at least have 2 elements), and this bubbles out from .multinomialDescriptivesTable() (because .chisquareTest() is called before its local try()), causing fatal termination.

Proposed changes

  • File: R/multinomialtest.R
  • Function: .chisquareTest()
  • Add a guard after dataTable <- table(factorVariable):
    • If length(dataTable) < 2, skip chisq.test() and return chisq-like placeholder result objects per hypothesis.
    • Each placeholder object will include observed, expected, statistic (X-squared = NA_real_), parameter (df = NA_real_), p.value = NA_real_, and warn with a translated message.
  • Keep existing code path unchanged when length(dataTable) >= 2.

Expected test impact

  • Normal multinomial cases unchanged.
  • Edge case with one observed level no longer crashes; outputs stay renderable with NA inferential stats and warning footnotes.

Fix crash in Multinomial Test when Descriptives checkbox is checked by adding proper error handling for debInf values

Attachment Descriptions

The following images were attached to the issue. They have been converted to text descriptions — the original image files are NOT available in the workspace. Do not reference screenshot filenames in your output.

Image

The screenshot shows JASP’s Frequencies module using the Multinomial Test analysis (analysis title: “Copy of Multinomial Test”), and the run has failed with a red error panel.

Error text (transcribed exactly)

This analysis terminated unexpectedly.

Error in chisq.test(x = dataTable, p = h, rescale.p = TRUE, simulate.p.value = FALSE): 'x' must at least have 2 elements

Stack trace
doTryCatch(return(expr), name, parentenv, handler)

withCallingHandlers(expr = analysis(jaspResults = jaspResults, dataset = dataset, options = options), error = .addStackTrace)

analysis(jaspResults = jaspResults, dataset = dataset, options = options)

.multinomialDescriptivesTable(jaspResults, dataset, options, ready)

.chisquareTest(jaspResults, dataset, options)

lapply(hyps, function(h) {
    csr <- warn <- NULL
    csr <- withCallingHandlers(chisq.test(x = dataTable, p = h, rescale.p = TRUE, simulate.p.value = FALSE), warning = function(w) warn <<- w$message)
    csr[["warn"]] <- warn
    return(csr)
})

FUN(X[[i]], ...)

withCallingHandlers(chisq.test(x = dataTable, p = h, rescale.p = TRUE, simulate.p.value = FALSE), warning = function(w) warn <<- w$message)

chisq.test(x = dataTable, p = h, rescale.p = TRUE, simulate.p.value = FALSE)

stop("'x' must at least have 2 elements'")

To receive assistance with this problem, please report the message above at: https://jasp-stats.org/bug-reports

UI state and selected options

The screenshot shows:

  • Analyses list on the left:

    • Binomial Test (collapsed)
    • Multinomial Test (collapsed)
    • Copy of Multinomial Test (expanded and active)
  • Variable assignment area:

    • Factor: debInf is assigned.
    • Counts: empty.
    • Expected Counts: empty.
  • Test Values section:

    • Radio selected: Equal proportions (multinomial test).
    • Radio unselected: Custom expected proportions (χ² test).
  • Additional Statistics section:

    • Descriptives: checked.
    • Confidence interval: checked, value set to 95 %.
    • Vovk-Sellke maximum p-ratio: unchecked.
  • Display section:

    • Counts: selected.
    • Proportions: unselected.
  • Plots section:

    • Descriptives plot: checked.
    • Confidence interval: checked, value 95 %.

Visible output elements

  • Main output panel title: Copy of Multinomial Test.
  • Large red error/warning box with the full stack trace.
  • Faintly visible output content behind the error panel (likely table rows), but no usable statistical result is shown due to termination.

Reproduction-relevant state

The failure occurs with a multinomial test where only one factor variable (debInf) is assigned and both Counts and Expected Counts fields are empty, while descriptive statistics and descriptive plot options (with confidence intervals) are enabled. The error indicates the internal chisq.test input x has fewer than 2 elements.

Test Results

Test Run Result
Baseline (pre-fix) [ FAIL 0 | WARN 9 | SKIP 3 | PASS 110 ]
Post-fix [ FAIL 0 | WARN 9 | SKIP 3 | PASS 110 ]
Upstream CI 0af3785 -- CI: passing

Automated Code Review

Approved after 1 review iteration(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant