MerQuR+Statistics Glossary

Statistics Glossary → Chi-Square Goodness of Fit

Chi-Square Goodness of Fitanalysis

A test of whether the observed distribution of a single categorical variable fits a pre-specified distribution (equal proportions, theoretical ratios, population percentages).

Concept

Sometimes the question is not about the relationship between two variables but whether one variable's distribution matches an expectation: is a die loaded, does the sample's age profile resemble the population's, do the offspring of a cross appear in the ratio predicted by inheritance? The goodness-of-fit test compares the observed category counts with this expectation.

The expected count in each category is the total number of observations times that category's expected proportion: E = n·p. The test adds up the differences between observed and expected counts: χ² = Σ (O − E)² / E, with degrees of freedom one less than the number of categories (k − 1). The bigger the differences, the bigger χ² and the smaller the p value.

Observations must be independent and expected counts must not be too small (usually 5 or more per category); if they are small, the exact multinomial test or a Monte Carlo p is used. A non-significant result does not prove that the data are 'the same' as the expectation, only that no clear departure was found. When the result is significant, the residuals show which categories depart and Cohen's w shows how large the departure is.

When to use it

  • When comparing the distribution of one categorical variable with theoretical or known proportions (e.g. equal preference, Mendelian ratios, census percentages).
  • With small expected counts use the exact multinomial test or a Monte Carlo p instead of the chi-square approximation; with only two categories use the binomial test.
  • If the question is about the association between two categorical variables use the chi-square test of independence; if it is whether a continuous measurement follows a given distribution (e.g. normal), tests such as Shapiro-Wilk or Kolmogorov-Smirnov are appropriate.

An everyday example

Based on last summer's sales, an ice-cream seller expects chocolate, vanilla, strawberry and lemon to take 40%, 30%, 20% and 10% of sales. Of the first 200 cones sold this summer, 70 are chocolate, 65 vanilla, 45 strawberry and 20 lemon; the expected counts are 80, 60, 40 and 20. The goodness-of-fit test says whether these departures can be explained by chance; here χ² ≈ 2.29 with 3 degrees of freedom, so p is about 0.51 and there is no evidence that customers' tastes have clearly changed since last year.

Parameters in MerQur

Column

Concept: The single categorical variable whose distribution is tested. The number of categories (k) sets the test's degrees of freedom (k − 1); each observation must fall into exactly one category.

Everyday example: In the ice-cream example each row is one cone sold and the column holds its flavour (chocolate, vanilla, strawberry, lemon). With four flavours there are 3 degrees of freedom.

What it does: Selects the categorical variable whose distribution is tested; the set of categories, n, and df = k - 1 come from this column.

Effect on the result: Determines all results.

On the validation data: hsb2 race (equal proportions) X²(3) = 242.44; ses X²(2) = 18.97, p = .00008. Default example: race, expected 10 / 10 / 10 / 70, X²(3) = 5.0286, p = .1697.

Expected proportions

Concept: The distribution stated by the null hypothesis: the share of the total each category is expected to take. The proportions should come from theory or an outside source, not from the data; if none are given, all categories are assumed equal.

Everyday example: The ice-cream seller takes the 40%, 30%, 20% and 10% proportions from last summer's sales. Without them, the test would answer 'are the four flavours equally popular?' and expect 50 cones of each.

What it does: The category probabilities under H₀. Empty = equal proportions. The "name=proportion" form maps by category name; a plain number list maps to a fixed order (numeric codes ascending, text alphabetical). The proportions are divided by their sum.

Effect on the result: Because the expected frequencies are E_k = n·p_k, X², p, and the residuals change.

On the validation data: race empty: 242.44; 10 / 10 / 10 / 70: 5.0286; the same proportions written with names or as 1, 1, 1, 7 also give 5.0286 (the proportions are rescaled to sum to 1). Five-category example (40, 20, 20, 15, 5): 9.9901 (p = .0406). To avoid order confusion, writing with names is preferable.

→ Null hypothesis (H₀) and test value

Effect: Cohen w and V (checkbox)

Concept: An effect size showing how far the observed distribution departs from the expected one, independently of sample size. For Cohen's w, about 0.1 is a small departure, 0.3 medium and 0.5 large.

Everyday example: In the ice-cream example w = √(2.29/200) ≈ 0.11, a small departure. If the same proportions were seen in 2000 cones, p would become very small but w would still be 0.11.

What it does: w = √(X²/n), V = √(X²/(n(k - 1))) (Cohen, 1988; w: .1 small, .3 medium, .5 large).

Effect on the result: Extra row only.

On the validation data: hsb2 race w = .1586, V = .0915; Color w = .1776; five-category example w = .1765.

→ Cramér's V and Cohen's w

G-test (checkbox)

Concept: Makes the likelihood-ratio statistic (G²) the main test instead of Pearson's chi-square. In large samples the two give similar results; with small expected counts Pearson's usually follows the chi-square distribution more closely.

Everyday example: In the 200-cone sample G² and Pearson's χ² are almost the same. Had the seller decided on only 30 cones, the two statistics could have drifted further apart.

What it does: The likelihood ratio G² = 2 Σ O ln(O/E) becomes the main statistic instead of the Pearson X².

Effect on the result: The statistic and p change; df is the same. With small expected frequencies Pearson generally fits the chi-square distribution better (Agresti, 2013).

On the validation data: hsb2 race G² = 5.7755, p = .1231 (Pearson 5.0286, .1697); Color G² = 9.0176, p = .0607 (Pearson p = .1008).

→ G-test (likelihood-ratio chi-square, G²)

Standardized residuals (checkbox)

Concept: The difference between observed and expected counts in each category, divided by the square root of the expected count. When the overall test is significant or borderline, it shows which categories the departure comes from; values beyond about 2 stand out.

Everyday example: For chocolate, 70 observed against 80 expected gives a residual of (70 − 80)/√80 ≈ −1.12: chocolate sold a little less than expected, but the shortfall is not remarkable on its own.

What it does: Pearson residuals (O - E)/√E; |r| > 2 is marked with a star.

Effect on the result: Extra table only.

On the validation data: race: Asian -2.01*, the others +0.89, +0.00, +0.42.

→ Standardized and adjusted residuals (contingency tables)

Weight / frequency column

Concept: When the data are a summary table with one row per category rather than raw data with one row per observation, this column gives how many times each category was observed.

Everyday example: Instead of entering 200 cones one by one, the seller can write a four-row table: 'chocolate 70', 'vanilla 65', 'strawberry 45', 'lemon 20'. With the count column chosen as the weight, the result matches the raw data.

What it does: Gives the count of each row in a summary table.

Effect on the result: The frequencies become sums of weights.

On the validation data: Color Region 1 with Count 7.7602; without weights each row counts as 1 (2.7179, p = .606). Summary + weight and the expanded data give exactly the same result. MerQur does not warn about a wrong column choice.

→ Frequency weight (summary-table data)

Confidence Level (%90, %95, %99, Other/custom 80.0-99.9)

Concept: Sets the decision threshold (α = 1 − confidence level): if p is below it, the hypothesis that the observed distribution fits the expected one is rejected. The statistic and p value do not change.

Everyday example: If the seller only wants to change the ordering plan on strong evidence, they choose 99% confidence. With p ≈ 0.51, the decision is the same whichever level is chosen.

What it does: α = 1 - level. This analysis has no confidence interval; it determines only the decision and the marks in the category tests.

Effect on the result: The statistic and p do not change; the decision can change. The custom value is active only when "Other" is selected.

On the validation data: race p = .1697: "not rejected" at 90%, 95%, 99%; "rejected" at 80.0 (α = .20). Five-category example p = .0406: rejected at 90%, not rejected at 99%.

→ Confidence level and confidence interval

Exact / Monte Carlo p

Concept: Computes the p value directly instead of using the chi-square approximation. The exact test sums the probabilities of all outcomes as likely as or less likely than the observed one when the expected proportions are true; the Monte Carlo method generates thousands of random samples from these proportions and counts how many are as extreme. These are preferred when expected counts are small.

Everyday example: If the seller sold only 25 cones on the first day of the season, the expected count for lemon falls to 2.5 and the chi-square approximation becomes unreliable. The exact p is then a sounder basis for the decision.

What it does: Exact multinomial p (probability ordering: sum of P(x) <= P(observed)) or Monte Carlo (B = 10000, seed 12345). The selected p is the basis of the main p and the decision; the asymptotic p is written separately.

Effect on the result: The main p and the decision change; the exact p is also computed for large tables (n = 200, k = 4).

On the validation data: Five-category example (observed 89 / 37 / 30 / 28 / 2; proportions 40 / 20 / 20 / 15 / 5): exact p = .0238 (probability ordering; asymptotic p = .0406; exact p with χ² ordering = .0404). The value .30997 belongs to the three-category ABC example (20 / 15 / 25, equal proportions; asymptotic p = .2865). hsb2 race 1-3: exact p = .0786 (probability ordering). MC p = .1686 (within 3 MC SE).

→ Exact test and exact p-value

Category-wise tests (None, Holm, Bonferroni)

Concept: The overall test only says 'the distribution does not fit'; category-wise tests compare each category with its own expected proportion. Because several tests are run, the Holm or Bonferroni correction controls the risk of false alarms.

Everyday example: Had the overall test been significant, the seller would want to know which flavour departs from expectation. Category-wise tests might show, for example, that only vanilla sold more than expected while the others fit.

What it does: For each category, the two-sided exact binomial test O_k ~ Bin(n, p_k) and the adjusted residual (O - E)/√(E(1 - p_k)); Holm or Bonferroni adjustment.

Effect on the result: Extra section only.

On the validation data: race Asian p = .0331, Holm .1325; Bonferroni .1325. The category p values come from the exact two-sided binomial test.

→ Multiple comparisons and the familywise error rate (FWER)

Category range

Concept: When categories are stored as integer codes, sets the range of codes that enter the test. The key point is that categories never observed can still enter with a zero count; otherwise only observed categories are tested and the degrees of freedom change.

Everyday example: If the seller's list has five flavours coded 1-5 but the fifth (pistachio) never sold, choosing the range 1-5 brings pistachio into the test with zero sales. That folds the question 'why does pistachio never sell?' into the result.

What it does: Lower and upper bounds for integer codes; all codes in the range (unobserved ones as 0) enter the test, the others are dropped. Also applied when a weight column is selected.

Effect on the result: n, k, X², and p change.

On the validation data: race "1, 3": n = 55, X²(2) = 4.8364, p = .0891; "0, 5" including unobserved 0 and 5: X²(5) = 463.66. In the five-category example with weights, range 1-4: n = 184, X²(3) = 54.5652.

Example on real data

Data: UCLA OARC hsb2 (High School and Beyond, n = 200)

Variables: column race (ethnicity coded 1-4), expected proportions 10, 10, 10, 70 (the hypothesis of the UCLA example)

  • Question: Does the ethnicity distribution of the sample (1 Hispanic, 2 Asian, 3 African American, 4 white) fit the hypothesised proportions 10% / 10% / 10% / 70%?
  • Assumption: The minimum expected count is 20.00; it is above 5 in all cells, so the chi-square approximation is valid.
  • Result: χ²(3) = 5.03, p = 0.170. The null hypothesis cannot be rejected: the observed distribution is compatible with the hypothesised proportions.
  • Descriptive: The largest deviation is in the Asian group (observed 11, expected 20); in the other groups the observed and expected frequencies are close.
  • APA: χ²(3, N = 200) = 5.03, p = .170.

Related concepts

Null hypothesis (H₀) and test valueMonte Carlo p-valueCramér's V and Cohen's wG-test (likelihood-ratio chi-square, G²)Standardized and adjusted residuals (contingency tables)Frequency weight (summary-table data)Exact test and exact p-valueMultiple comparisons and the familywise error rate (FWER)

The numbers in the examples are MerQur's output on independent, publicly available validation data; see the MerQur Technical Note for details.