4.3 - Chi-squared goodness of fit

4.3 - Chi-squared goodness of fit

A fitted model will rarely reproduce every observed frequency exactly. You will measure the overall discrepancy, choose degrees of freedom after grouping and parameter estimation, and use an upper-tail chi-squared test to judge whether the data give evidence against the model.

Comparing observed and expected counts

How large must a difference between observed and expected counts be before we doubt a probability model? A difference of five is more striking against an expected count of five than against an expected count of five hundred.

The chi-squared statistic measures relative discrepancies across the whole table. Squaring stops positive and negative differences cancelling; dividing each squared difference by its expected count adjusts for the scale of that class.

Chi-squared statistic

After combining classes so that every expected frequency is at least 55, calculate

X2=∑i(Oi−Ei)2Ei.X^2=\sum_i\frac{(O_i-E_i)^2}{E_i}.

Use counts from NN independent observations, with the same exhaustive, non-overlapping classes for the observed and expected rows. Every EiE_i is positive, and both rows total NN.

This statistic is not in the formula booklet: learn it. The booklet supplies chi-squared critical values.

The statistic is nonnegative. It is zero precisely when every observed count matches its expected count. A larger value indicates a greater discrepancy, but we need degrees of freedom before judging how unusual it is.

Worked example 1: small discrepancies

Question. Calculate the chi-squared statistic for the following complete table.

ClassABC
Observed181827271515
Expected202025251515

Plan. Find each squared discrepancy divided by its expected count, then add.

X2=(18−20)220+(27−25)225+(15−15)215=0.20+0.16+0=0.36.\begin{aligned} X^2&=\frac{(18-20)^2}{20}+\frac{(27-25)^2}{25} +\frac{(15-15)^2}{15}\\ &=0.20+0.16+0=0.36. \end{aligned}

Answer. X2=0.36X^2=0.36.

Check. Expanding the numerator gives

∑i(Oi−Ei)2Ei=∑iOi2Ei−2∑iOi+∑iEi=∑iOi2Ei−N.\sum_i\frac{(O_i-E_i)^2}{E_i} =\sum_i\frac{O_i^2}{E_i}-2\sum_iO_i+\sum_iE_i =\sum_i\frac{O_i^2}{E_i}-N.

Here 182/20+272/25+152/15−60=0.3618^2/20+27^2/25+15^2/15-60=0.36, confirming the total by a different calculation.

Move five observations from C to B, keeping the expected counts and sample size fixed. This creates larger discrepancies without changing the model.

Worked example 2: larger discrepancies

Question. Calculate the chi-squared statistic when the observed row becomes 18,32,1018,32,10, with expected row 20,25,1520,25,15.

Plan. Recalculate all three contributions using the unchanged expected denominators.

X2=(−2)220+7225+(−5)215=0.20+1.96+1.666666…=3.826666….\begin{aligned} X^2&=\frac{(-2)^2}{20}+\frac{7^2}{25}+\frac{(-5)^2}{15}\\ &=0.20+1.96+1.666666\ldots\\ &=3.826666\ldots. \end{aligned}

Answer. X2=3.83X^2=3.83 to three significant figures.

Check. The alternative calculation gives 182/20+322/25+102/15−60=3.826666…18^2/20+32^2/25+10^2/15-60=3.826666\ldots. The larger result also agrees with the increased discrepancies in B and C.

Keep unrounded expected counts and contributions until the final sum. If classes were combined, calculate contributions from the combined table, rather than adding contributions calculated before grouping.

Watch out

The denominator is EiE_i, not OiO_i. An observed zero is allowed: if Oi=0O_i=0, its contribution is EiE_i, provided the expected-count condition is satisfied.

Calculate a new table's total discrepancy.

Degrees of freedom

The same statistic can be unusual for one table and quite ordinary for another, because the number of independent discrepancies matters.

With three classes and a fixed sample size, choosing two observed counts determines the third: it must make the total NN. That leaves two independent counts. Fitting a model parameter from these same data uses further information, reducing the degrees of freedom again.

Goodness-of-fit degrees of freedom

For kk classes remaining after combining, with mm independent model parameters estimated from the sample being tested,

ν=k−1−m.\nu=k-1-m.

Under the null model, the statistic is approximately distributed as χν2\chi^2_\nu, subject to the expected-count and sampling conditions. A usable test needs ν>0\nu>0.

The degrees-of-freedom rule is not in the formula booklet: learn it.

The subtraction of one accounts for the fixed total. A parameter given numerically in the hypothesis is not estimated from this sample, so it does not add to mm. Nor does the known sample size count as an estimated parameter.

Worked example 3: a fully specified model

Question. A goodness-of-fit test uses the specified model Po(2)\mathrm{Po}(2) for 100100 independent observations. Its final classes are 0,1,2,3,≥40,1,2,3,\ge4, with all expected counts at least 55. Find the degrees of freedom.

Plan. Count the final classes, then count parameters estimated from this sample.

k=5,m=0,ν=5−1−0=4.k=5, \qquad m=0, \qquad\nu=5-1-0=4.

The value 22 was specified, so there is no fitted Poisson parameter to subtract.

Answer. Use χ42\chi^2_4.

Check. Once four of the five class counts are known, the fixed total determines the last. No sample estimate imposes an additional fitted-model constraint, leaving four degrees of freedom.

Keep the same final classes, but change the hypothesis so that its Poisson mean must be estimated. This changes the degrees of freedom even if the fitted numerical mean happens to be two.

Worked example 4: estimating the Poisson mean

Question. For a Poisson goodness-of-fit test, λ\lambda is estimated from the sample mean as λ^=2\hat\lambda=2. The final classes are again 0,1,2,3,≥40,1,2,3,\ge4, with all expected counts at least 55. Find the degrees of freedom.

Plan. Subtract one for the total and one for the fitted mean.

k=5,m=1,ν=5−1−1=3.k=5, \qquad m=1, \qquad\nu=5-1-1=3.

Answer. Use χ32\chi^2_3.

Check. Compared with the fully specified model, one additional parameter has been chosen to agree with the sample. The degrees of freedom fall by exactly one, from four to three.

For B(n,p)\mathrm B(n,p) with known nn and fitted pp, subtract one fitted parameter. For a normal model with both mean and standard deviation estimated from this sample, subtract two. If both were specified in advance, subtract neither. Count what has actually been estimated, rather than counting symbols in the distribution's name.

Watch out

Seven original classes reduced to five final classes give k=5k=5, not 77. If grouping leaves no positive degrees of freedom, the stated chi-squared procedure cannot give a valid test; do not restore small classes merely to obtain a positive number.

Use both grouping and parameter counting in this check.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account