#set document(title: "10.2 Goodness of Fit Test", author: "Rachel Webb") #set page(width: 8.5in, height: auto, margin: 1in) #import "@preview/cetz:0.5.2" #set text(font: ("STIX Two Text", "Libertinus Serif", "New Computer Modern"), size: 10.5pt, lang: "en") #show math.equation: set text(font: ("STIX Two Math", "New Computer Modern Math")) #set par(justify: true, leading: 0.62em, spacing: 0.9em) #set enum(spacing: 1.1em) // room between list items so tall inline fractions don't collide #set list(spacing: 1.1em) #set table(stroke: 0.5pt + rgb("#c7ccd3")) #let BLUE = rgb("#183B6F") // brand navy — section bars + example/solution labels (white on navy 11.09:1) #let ORANGE = rgb("#A94509") // brand primary-700 — AA-safe deep orange for TEXT (5.93:1 on white; raw brand #F37021 is 2.94:1 and must never carry text) #let RED = rgb("#DC2626") // brand error-600 #let GREEN = rgb("#059669") // brand success-600 (decoration only; small green text uses green-text #007942) #show heading.where(level: 1): it => block(width: 100%, above: 0pt, below: 16pt, fill: gradient.linear(BLUE, rgb("#2C5AA0")), inset: (x: 14pt, y: 12pt), radius: 3pt, text(fill: white, weight: "bold", size: 19pt, it.body)) #show heading.where(level: 2): it => block(width: 100%, above: 18pt, below: 10pt, fill: BLUE, inset: (x: 10pt, y: 6pt), radius: 2pt, text(fill: white, weight: "bold", size: 12pt, it.body)) #show heading.where(level: 3): it => text(fill: ORANGE, weight: "bold", size: 12.5pt, it.body) #show heading.where(level: 4): it => text(fill: BLUE, weight: "bold", size: 10.5pt, it.body) #let examplebox(label, title, body) = block(width: 100%, breakable: true, fill: rgb("#EFF1F5"), stroke: 0.5pt + rgb("#CFDDF0"), radius: 4pt, inset: 10pt, above: 12pt, below: 12pt)[ #block(below: 6pt)[#box(fill: BLUE, inset: (x: 6pt, y: 2pt), radius: 2pt, text(fill: white, weight: "bold", size: 8.5pt, label)) #h(0.4em) #strong[#title]] #body] // rail = decorative left rule (raw brand token); labelcolor = AA-safe label text shade #let notebox(label, rail, labelcolor, tint, body) = block(width: 100%, breakable: true, fill: tint, stroke: (left: 3pt + rail), inset: (left: 10pt, rest: 8pt), radius: (right: 4pt), above: 11pt, below: 11pt)[ #text(fill: labelcolor, weight: "bold", size: 7.5pt, tracking: 0.5pt)[#upper(label)] #linebreak() #body] #let solutionbox(body) = block(above: 4pt, below: 8pt)[ #text(fill: BLUE, weight: "bold", size: 8.5pt)[Solution] #linebreak() #body] #let figph(msg) = block(width: 100%, height: 60pt, fill: rgb("#f6f7f9"), stroke: (paint: rgb("#c7ccd3"), dash: "dashed"), radius: 4pt, inset: 10pt)[ #align(center + horizon, text(fill: rgb("#889"), style: "italic", size: 9pt, msg))] // Standardize inlined figure sizes: measure the natural CeTZ canvas, then scale to a // consistent envelope (aspect-aware; see build_typst.py FIG_* constants). Unlike the // print preamble, dimensions are FLOORED: in an editor a user can trim a figure to a // degenerate 1-D shape (a bare line), and w/h or tw/w would then divide by zero. #let _STD_W = 3.5 #let _WIDE_W = 5.6 #let _MAX_H = 3.4 #let _ASPECT_WIDE = 2.2 #let _UPSCALE_MAX = 1.15 #let stdfig(body) = context { let m = measure(body) let w = calc.max(m.width / 1in, 0.01) let h = calc.max(m.height / 1in, 0.01) let tw = if w / h > _ASPECT_WIDE { _WIDE_W } else { _STD_W } let s = calc.min(tw / w, _MAX_H / h, _UPSCALE_MAX) align(center, box(scale(x: s * 100%, y: s * 100%, reflow: true, body))) } #show figure: set block(breakable: false) #set figure(gap: 8pt) #show figure.caption: set text(size: 8.5pt, fill: rgb("#555")) == 10.2#h(0.6em)Goodness of Fit Test The #math.equation(block: false, alt: "χ squared")[$χ^(2)$] #strong[goodness-of-fit] #strong[test]can be used to test the distribution of three or more proportions within a single population. #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[#math.equation(block: false, alt: "χ squared")[$χ^(2)$] goodness-of-fit test] The #math.equation(block: false, alt: "χ squared")[$χ^(2)$]-test is a statistical test for testing the goodness-of-fit of a variable. It can be used when the data are obtained from a random sample and when the expected frequency (E) from each category is 5 or more. The formula for the #math.equation(block: false, alt: "χ squared")[$χ^(2)$] -test statistic is: #math.equation(block: true, alt: "χ squared equals ∑ the fraction open parenthesis o minus E close parenthesis squared over E")[$χ^(2) = ∑ frac(( o − E )^(2), E)$] Use a right-tailed #math.equation(block: false, alt: "χ squared")[$χ^(2)$] -distribution with #math.equation(block: false, alt: "df equals k minus 1")[$"df" = k − 1$] where #math.equation(block: false, alt: "k")[$k$] = the number of categories. with - #math.equation(block: false, alt: "O")[$O$] = the observed frequency (what was observed in the sample) and - #math.equation(block: false, alt: "E")[$E$] = the expected frequency (based on #math.equation(block: false, alt: "H sub 0")[$H_(0)$] and the sample size). - #math.equation(block: false, alt: "H sub 0 : p sub 1 equals p sub 0 , p sub 2 equals p sub 0 , ⋯ , p sub k equals p sub 0")[$H_(0) : p_(1) = p_(0) , p_(2) = p_(0) , ⋯ , p_(k) = p_(0)$] - #math.equation(block: false, alt: "H sub 1 :")[$H_(1) :$] At least one proportion is different. ] #examplebox("Example 1")[][ An instructor claims that their students’ grade distribution is different than the department’s grade distribution. The department’s grades have the following proportion of students who get A’s is 35%, B’s is 23%, C’s is 25%, D’s is 10% and F’s is 7% in introductory statistics courses. For a sample of 250 introductory statistics students with this instructor, there were 80 A’s, 50 B’s, 58 C’s, 38 D’s, and 24 F’s. Test the instructor’s claim at the 5% level of significance. #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Run the grade-distribution test the way the TI-84 does] Opens the Chi-Square panel in Goodness of Fit mode with the observed counts already in L1 and the expected counts 250 · 0.35 = 87.5, 57.5, 62.5, 25 and 17.5 in L2 - the same two lists the TI-84 steps below ask you to type. Press Calculate for df = 4 and p = 0.0253; the statistic reads 11.119404 because the panel adds the five unrounded terms, while the book's 11.1195 adds its own 4-decimal table row. Now move a few students from the D column to the C column and recalculate: the D term is carrying most of the statistic. - χ² GOF-Test on the 250 grades: χ² = 11.1195, df = 4, p = 0.0253 ] #solutionbox[ This is a test for three or more proportions within a single population, so use the goodness-of-fit test. We will always use a right-tailed χ 2 -test. The hypotheses for this example would be: #math.equation(block: true, alt: "H sub 0 : p sub A equals 0.35 , p sub B equals 0.23 , p sub C equals 0.25 , p sub D equals 0.10 , p sub F equals 0.07")[$H_(0) : p_(A) = 0.35 , p_(B) = 0.23 , p_(C) = 0.25 , p_(D) = 0.10 , p_(F) = 0.07$] #math.equation(block: false, alt: "H sub 1 :")[$H_(1) :$] At least one proportion is different. Even though there is an inequality in #math.equation(block: false, alt: "H sub 1")[$H_(1)$], the goodness-of-fit test is always a right-tailed test. This is because we are testing to see if there is a large variation between the observed versus the expected values. If the variance between the observed and expected values is large, then there is a difference in the proportions. \# Where does the chi-square curve come from, and why only the right tail? \# Draw 5000 samples of n = 250 in which the instructor's grades DO follow the \# department's proportions, and compute the goodness-of-fit statistic each time. p0 \<- c(.35, .23, .25, .10, .07) \# H0 is true in every one of these samples E \<- 250 \* p0 \# 87.5 57.5 62.5 25 17.5 set.seed(10) X2 \<- replicate(5000, sum((rmultinom(1, 250, p0) - E)^2 / E)) hist(X2, breaks = 40, freq = FALSE, xlab = "chi-square statistic", main = "5000 goodness-of-fit statistics, all simulated under H0") curve(dchisq(x, df = 4), add = TRUE, lwd = 2) \# df = k - 1 = 4 abline(v = 9.4877, lty = 2) \# the book's critical value mean(X2 \> 9.4877) \# about 0.05 -- only a LARGE statistic is surprising Also note that we do not write the alternative hypothesis as #math.equation(block: false, alt: "p sub A not equal to 0.35 , p sub B not equal to 0.23 , p sub C not equal to 0.25 , p sub D not equal to 0.10 , p sub F not equal to 0.07")[$p_(A) ≠ 0.35 , p_(B) ≠ 0.23 , p_(C) ≠ 0.25 , p_(D) ≠ 0.10 , p_(F) ≠ 0.07$] since it could be that any one of these proportions is different. All of the proportions not being equal to their hypothesized values is just one possible case. There are #math.equation(block: false, alt: "k equals 5")[$k = 5$] categories that we are comparing: A’s, B’s, C’s, D’s and F’s. The observed counts are the actual number of A’s, B’s, C’s, D’s and F’s from the sample. We must compute the expected count for each of the five categories. Find the expected counts by multiplying the expected proportion of A’s, B’s, C’s, D’s and F’s by the sample size. It will be helpful to make a table to organize the work. #figure(figph[Table organizing the goodness-of-fit work by grade: observed counts are 80 A's, 50 B's, 58 C's, 38 D's, and 24 F's; expected counts are 0.35\*250 = 87.5, 0.23\*250 = 57.5, 0.25\*250 = 62.5, 0.10\*250 = 25, and 0.07\*250 = 17.5; and the (O−E)²/E values are 0.6429, 0.9738, 0.324, 6.76, and 2.4143.], alt: "Table organizing the goodness-of-fit work by grade: observed counts are 80 A's, 50 B's, 58 C's, 38 D's, and 24 F's; expected counts are 0.35*250 = 87.5, 0.23*250 = 57.5, 0.25*250 = 62.5, 0.10*250 = 25, and 0.07*250 = 17.5; and the (O−E)²/E values are 0.6429, 0.9738, 0.324, 6.76, and 2.4143.", caption: none) The test statistic is the sum of this last row: #math.equation(block: false, alt: "χ squared equals ∑ the fraction open parenthesis O minus E close parenthesis squared over E equals 0.6429 plus 0.9783 plus 0.324 plus 6.76 plus 2.4143 equals 11.1195")[$χ^(2) = ∑ frac(( O − E )^(2), E) = 0.6429 + 0.9783 + 0.324 + 6.76 + 2.4143 = 11.1195$] The critical value for a right-tailed #math.equation(block: false, alt: "χ squared")[$χ^(2)$]-test with #math.equation(block: false, alt: "df equals k minus 1 equals 5 minus 1 equals 4")[$"df" = k − 1 = 5 − 1 = 4$] is found by finding the area in the #math.equation(block: false, alt: "χ squared")[$χ^(2)$] -distribution using your calculator or Excel. Use #math.equation(block: false, alt: "α equals 0.05")[$α = 0.05$] area in the right-tail, to get the critical value of #math.equation(block: false, alt: "χ sub α squared")[$χ_(α)^(2)$] =CHISQ.INV.RT(0.05,4) = 9.4877. Draw and label the curve as shown in Figure 10-4. #figure(figph[Right-skewed chi-square curve with the right tail shaded green beyond the critical value 9.4877; a blue vertical line marks the test statistic 11.1195 inside the shaded rejection region.], alt: "Right-skewed chi-square curve with the right tail shaded green beyond the critical value 9.4877; a blue vertical line marks the test statistic 11.1195 inside the shaded rejection region.", caption: [Figure 10-4: Values of #math.equation(block: false, alt: "χ sub α squared")[$χ_(α)^(2)$] and #math.equation(block: false, alt: "χ squared")[$χ^(2)$] on a #math.equation(block: false, alt: "χ squared")[$χ^(2)$]-test with #math.equation(block: false, alt: "df equals 4")[$"df" = 4$] and #math.equation(block: false, alt: "α equals 0.05")[$α = 0.05$].]) The test statistic of #math.equation(block: false, alt: "χ squared equals 11.1195 greater than χ sub α squared equals 9.4877")[$χ^(2) = 11.1195 > χ_(α)^(2) = 9.4877$] and is in the rejection area, so our decision is: Reject #math.equation(block: false, alt: "H sub 0")[$H_(0)$]. There is sufficient evidence to support the claim that the proportion of students who get A’s, B’s, C’s, D’s and F’s in introductory statistics courses for this instructor is different than the department’s proportions of 35%, 23%, 25%, 10% and 7% respectively. If we were asked to find the p-value, you would just find the area to right of the test statistic (always a right-tailed test) using your calculator or Excel =CHISQ.DIST.RT(11.1195,4) = 0.0253. #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Both Excel formulas on one chi-square curve] The first link is =CHISQ.DIST.RT(11.1195,4): the Distributions panel opens on df = 4 with the right-tail area above the test statistic already shaded, giving p = 0.0253. The second is =CHISQ.INV.RT(0.05,4), entered as the left-tail area 0.95, giving the critical value 9.4877. Both say reject. Drag the boundary below 9.4877 and watch the tail area climb past α = 0.05. - p-value: P(χ² \> 11.1195) = 0.0253 with df = 4 - Critical value χ² with df = 4, left area 0.95 = 9.4877 ] #figure(figph[TI-84 calculator screen showing the command χ²cdf(11.1195,1E99,4) returning .0252533235.], alt: "TI-84 calculator screen showing the command χ²cdf(11.1195,1E99,4) returning .0252533235.", caption: none) This gives a p-value = 0.0252 which is less than #math.equation(block: false, alt: "α equals 0.05")[$α = 0.05$], therefore reject #math.equation(block: false, alt: "H sub 0")[$H_(0)$]. You can use the GOF shortcut function on your calculator to get a p-value; see directions below. If you get the program from your instructor or the website for your TI-83, you can also have the calculator find the #math.equation(block: false, alt: "the fraction open parenthesis O minus E close parenthesis squared over E")[$frac(( O − E )^(2), E)$] values. The TI-84 and 89 already does this. #strong[TI-84:] Note: For the TI-83 download a GOF program from #link("http://MostlyHarmlessStatistics.com")[http://MostlyHarmlessStatistics.com]. Only newer TI-84 operating systems have a calculator shortcut key for GOF. Use the same GOF program as for the TI-83 if your 84 does not have the #math.equation(block: false, alt: "χ squared")[$χ^(2)$] GOF-Test. Before you start, write down your observed and expected values. Select Stat, then Calc. Type in the observed values into list 1, and the expected values into list 2. Select Stat, then Tests. Go down to option D: #math.equation(block: false, alt: "χ squared")[$χ^(2)$] GOF-Test. Choose L#sub[1] for the Observed category and L#sub[2] for the Expected category, type in your degrees of freedom (#math.equation(block: false, alt: "df equals k minus 1")[$"df" = k − 1$]), and then select Calculate. The calculator returns the #math.equation(block: false, alt: "χ squared")[$χ^(2)$] -test statistic and the p-value. Use the right arrow to see the rest of the #math.equation(block: false, alt: "the fraction open parenthesis O minus E close parenthesis squared over E")[$frac(( O − E )^(2), E)$] values. #figure(figph[Four TI-84 screens: lists L1 and L2 holding the observed counts 80, 50, 58, 38, 24 and expected counts 87.5, 57.5, 62.5, 25, 17.5; the TESTS menu with D:χ²GOF-Test selected; the inputs Observed:L1, Expected:L2, df:4; and the output χ²=11.1194037, p=.0252543538, df=4 with CNTRB starting at .642857.], alt: "Four TI-84 screens: lists L1 and L2 holding the observed counts 80, 50, 58, 38, 24 and expected counts 87.5, 57.5, 62.5, 25, 17.5; the TESTS menu with D:χ²GOF-Test selected; the inputs Observed:L1, Expected:L2, df:4; and the output χ²=11.1194037, p=.0252543538, df=4 with CNTRB starting at .642857.", caption: none) #strong[TI-89:] Go to the \[Apps\] Stat/List Editor, then type in the observed values into list 1, and the expected values into list 2. Press \[2#super[nd]\] then F6 \[Tests\], then select 7: Chi-2GOF. Type in the list names and the degrees of freedom (#math.equation(block: false, alt: "df equals k minus 1")[$"df" = k − 1$]). Then press the \[ENTER\] key to calculate. The calculator returns the #math.equation(block: false, alt: "χ squared")[$χ^(2)$]-test statistic and the p-value. The #math.equation(block: false, alt: "the fraction open parenthesis O minus E close parenthesis squared over E")[$frac(( O − E )^(2), E)$] values are stored in the comp list. #figure(figph[Four TI-89 screens for the chi-square goodness-of-fit test: the Tests menu with 7:Chi2 GOF selected, the inputs Observed List list1 and Expected List list2 with df 4, the results Chi-2 = 11.1194 with P Value = .025254, and the comp list of (O−E)²/E values .64286, .97826, .324, 6.76, and 2.4143.], alt: "Four TI-89 screens for the chi-square goodness-of-fit test: the Tests menu with 7:Chi2 GOF selected, the inputs Observed List list1 and Expected List list2 with df 4, the results Chi-2 = 11.1194 with P Value = .025254, and the comp list of (O−E)²/E values .64286, .97826, .324, 6.76, and 2.4143.", caption: none) ] ] #examplebox("Example 2")[][ A research company is looking to see if the proportion of consumers who purchase a cereal is different based on shelf placement. They have four locations: Bottom Shelf, Middle Shelf, Top Shelf, and Aisle End Shelf. Test to see whether there is a preference among the four shelf placements. Use the p-value method with #math.equation(block: false, alt: "α equals 0.05")[$α = 0.05$]. #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[A p-value that just misses α] The first link seeds the four shelf sales in L1 and the equal-preference expected count 240/4 = 60 four times in L2; press Calculate for χ² = 7.8 with df = 3 and p = 0.05033. The second link shows that same p-value as the area right of 7.8 on the df = 3 curve. Move one sale from the Aisle End to the Bottom shelf and recalculate - a single count flips this borderline decision. - χ² GOF-Test on the four shelves: χ² = 7.8, df = 3, p = 0.05033 - p-value: P(χ² \> 7.8) = 0.05033 with df = 3 ] #figure(figph[Table of observed cereal purchases by shelf placement: Bottom 45, Middle 67, Top 55, and End 73.], alt: "Table of observed cereal purchases by shelf placement: Bottom 45, Middle 67, Top 55, and End 73.", caption: none) #solutionbox[ The hypotheses can be written as a sentence or as proportions. If you use proportions, note that there are no percentages given. We would expect that each shelf placement be the same if there was no preference. There are 4 categories, so #math.equation(block: false, alt: "p sub 0 equals the fraction 1 over 4 equals 0.25")[$p_(0) = frac(1, 4) = 0.25$] or 25% for each placement. - #math.equation(block: false, alt: "H sub 0 : p sub B equals 0.25 , p sub M equals 0.25 , p sub T equals 0.25 , p sub E equals 0.25")[$H_(0) : p_(B) = 0.25 , p_(M) = 0.25 , p_(T) = 0.25 , p_(E) = 0.25$] - #math.equation(block: false, alt: "H sub 1 :")[$H_(1) :$] At least one proportion is different. It is also acceptable to write the hypotheses as a sentence. - #math.equation(block: false, alt: "H sub 0 :")[$H_(0) :$] Proportion of cereal sales is equally distributed across the four shelf placements. - #math.equation(block: false, alt: "H sub 1 :")[$H_(1) :$] Proportion of cereal sales is not equally distributed across the four shelf placements. Find the expected values. Total all the observed values to get the sample size: #math.equation(block: false, alt: "n equals 45 plus 67 plus 55 plus 73 equals 240")[$n = 45 + 67 + 55 + 73 = 240$]. Then take the sample size and divide by 4 to get #math.equation(block: false, alt: "the fraction 240 over 4 equals 60")[$frac(240, 4) = 60$]. The expected value for each group is 60. Compute the test statistic: #math.equation(block: true, alt: "χ squared equals ∑ the fraction open parenthesis O minus E close parenthesis squared over E equals the fraction open parenthesis 45 minus 60 close parenthesis squared over 60 plus the fraction open parenthesis 67 minus 60 close parenthesis squared over 60 plus the fraction open parenthesis 55 minus 60 close parenthesis squared over 60 plus the fraction open parenthesis 73 minus 60 close parenthesis squared over 60; equals 3.75 plus 0.816667 plus 0.416667 plus 2.816667 equals 7.8")[$χ^(2) = ∑ frac(( O − E )^(2), E) = frac(( 45 − 60 )^(2), 60) + frac(( 67 − 60 )^(2), 60) + frac(( 55 − 60 )^(2), 60) + frac(( 73 − 60 )^(2), 60) \ = 3.75 + 0.816667 + 0.416667 + 2.816667 = 7.8$] Check your work using technology and find the p-value. The degrees of freedom are the number of groups minus one: #math.equation(block: false, alt: "df equals k minus 1 equals 3")[$"df" = k − 1 = 3$]. #figure(figph[Four TI-84 screens for the goodness-of-fit test: lists L1 and L2 with observed counts 45, 67, 55, 73 and expected counts of 60 each, the TESTS menu with D:χ²GOF-Test selected, the inputs Observed:L1, Expected:L2, df:3, and the output χ²=7.8, p=.050331098, df=3.], alt: "Four TI-84 screens for the goodness-of-fit test: lists L1 and L2 with observed counts 45, 67, 55, 73 and expected counts of 60 each, the TESTS menu with D:χ²GOF-Test selected, the inputs Observed:L1, Expected:L2, df:3, and the output χ²=7.8, p=.050331098, df=3.", caption: none) You can scroll right by selecting the right arrow button to see the rest of the contribution values. In Excel the p-value is found by the formula =CHISQ.DIST.RT(7.8,3). On the TI-Calculator, use the #math.equation(block: false, alt: "χ squared")[$χ^(2)$] GOF-Test shortcut. The p-value = 0.05033 which is larger than #math.equation(block: false, alt: "α equals 0.05")[$α = 0.05$]; therefore, do not reject #math.equation(block: false, alt: "H sub 0")[$H_(0)$]. There is not enough evidence to support the claim that cereal shelf placement makes a statistically significant difference in the proportion of sales at the 5% level of significance. ] ]