One-way ANOVA and Chi-square test (the two-variable tab) — build .117

One-way ANOVA and Chi-square test (the two-variable tab) — build .117

Tests ▾ → One-way ANOVA and Tests ▾ → Chi-square test open the two-variable tab in Compare means or Crosstab mode. Code: the isTwo block of renderVals and twoSpec, twoLevels, twoTest, twoMore, ctOrdered in src/app/component.js; the console printouts are meansStataText / meansRText in src/analysis-suite.js; groupsOf, anova, wDiff, crosstab and chi2Of in src/app/engine.mjs.

Compare means

  • Every group with a case counts. A group with one case adds a between-groups degree of freedom and nothing within, as Stata’s oneway and R’s aov treat it. Its SD shows as —, and a note names it. Before .117 such groups were dropped silently, which could change the F test completely.
  • Two groups need two cases in each. Without a weight the test is Welch’s t (ttest y, by(g) unequal, t.test(y ~ factor(g))).
  • Two groups with a weight: the difference in weighted means, from a regression on a group dummy with these weights and robust (HC1) standard errors, df = n − 2. This is what Stata’s regress y i.g [pw=w] and R’s lmtest::coeftest(lm(y ~ factor(g), weights = w), vcov. = sandwich::vcovHC(m, type = "HC1")) report. A string group variable is encoded first in the Stata command.
  • Three or more groups with a weight: weighted group means and sums of squares; the df count rows. This is R’s summary(aov(y ~ factor(g), weights = w)) (raw weights) and Stata’s oneway y g [aw=w] (weights rescaled to sum to N, so its sums of squares differ by that factor and F is the same). The console prints each one in its own units. Bartlett’s test is left out with a weight.
  • More tests (three or more groups, unweighted, at least two cases in each): Welch’s ANOVA (oneway.test), Levene’s test centred on the median (car::leveneTest’s default, Stata’s robvar W50), and Tukey’s HSD for every pair (TukeyHSD, Stata pwmean …, mcompare(tukey)), up to 15 groups.
  • Notes under the result: one-case groups; what the weight does; rows with no usable weight (missing or ≤ 0), which are left out; and an outcome holding a typical missing-value code (−99, 98, 999 …) that is far from its other values and not set in Settings → Missing codes.
  • The logged test follows the groups the page used after the IF bar and the subgroup. If a subgroup leaves two groups, the page and the console both run the two-group test.
  • R commands use factor(g). Without it, a labelled .dta group would be fitted as one slope.

Crosstab and chi-square

  • Categories come from complete pairs. A category seen only where the other variable is missing is not a row. Rows or columns with no cases are left out of the test, so they add no degrees of freedom.
  • The test uses the full table. The display keeps the largest 40 columns and 25 rows (the rest under “other”). When it is capped, the chi-square still runs on every category, and a note says so (up to 250,000 cells).
  • Fisher’s exact test is added to a 2 × 2 table with an expected count below 5 (unweighted). The page then reports it instead of the chi-square. The commands add exact (Stata) and fisher.test(tb) (R).
  • The R command drops empty rows and columns (tb[rowSums(tb) > 0, colSums(tb) > 0]), as Stata’s tabulate does.
  • With a weight, the weights are rescaled to the number of observations for the test (as before), and rows with no usable weight are counted in the note.

Smaller changes

  • The X / Y labels follow the roles after ⇄ Swap (“X — OUTCOME” when the numeric variable is on X).
  • Mode and sort buttons say whether they are on (aria-pressed). The X and Y pickers have labels.
  • Column headers show the whole category name.
  • Copy for Word is titled with the test (One-way ANOVA, Compare means (t-test), Crosstab and chi-square test). Before, every test was titled “Correlation”.

Validation

tests/audit117.test.mjs checks against R 4.3:

  • the one-case group (aov);
  • weighted ANOVA (aov with weights, oneway [aw]);
  • Welch’s ANOVA, Levene’s test and Tukey’s HSD;
  • the weighted two-group test (coeftest with HC1);
  • the empty row, Fisher’s exact test, and a 44-column table.