#set document(title: "11.2 Chi-Square Goodness of Fit", author: "Kathryn Kozak") #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")) == 11.2#h(0.6em)Chi-Square Goodness of Fit In probability, you calculated probabilities using both experimental and theoretical methods. There are times when it is important to determine how well the experimental values match the theoretical values. An example of this is if you wish to verify if a die is fair. To determine if observed values fit the expected values, you want to see if the difference between observed values and expected values is large enough to say that the test statistic is unlikely to happen if you assume that the observed values fit the expected values. The test statistic in this case is also the chi-square. The process is the same as for the chi-square test for independence. === Hypothesis Test for Goodness of Fit Test + State the null and alternative hypotheses and the level of significance #linebreak() #math.equation(block: false, alt: "H sub o")[$H_(o)$]: The data are consistent with a specific distribution #linebreak() #math.equation(block: false, alt: "H sub A")[$H_(A)$]: The data are not consistent with a specific distribution #linebreak() Also, state your #math.equation(block: false, alt: "α")[$α$] level here. + State and check the assumptions for the hypothesis test + A random sample is taken. + Expected frequencies for each cell are greater than or equal to 5 (The expected frequencies, #emph[E], will be calculated later, and this assumption means #math.equation(block: false, alt: "E greater than or equal to 5")[$E ≥ 5$]). + Find the test statistic and p-value #linebreak() Finding the test statistic involves several steps. First the data is collected and counted, and then it is organized into a table (in a table each entry is called a cell). These values are known as the observed frequencies, which the symbol for an observed frequency is #emph[O]. The table is made up of k entries. The total number of observed frequencies is #emph[n]. The expected frequencies are calculated by multiplying the probability of each entry, #emph[p], times #emph[n]. #math.equation(block: true, alt: "Expected frequency( entry i close parenthesis equals E equals n to the power * p")[$"Expected frequency( entry " i \) = E = n^(*) p$] ==== Test Statistic#strong[:] #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)$] where #emph[O] is the observed frequency and #emph[E] is the expected frequency. Again, the test statistic involves squaring the differences, so the test statistics are all positive. Thus a chi-squared test for goodness of fit is always right tailed. p-value: Using the TI-83/84: #math.equation(block: false, alt: "χ cdf open parenthesis lower limit, 1 E 99 , d f close parenthesis")[$χ " cdf " ( " lower limit, " 1 E 99 , d f )$] Using R: #math.equation(block: false, alt: "1 minus pchisq open parenthesis χ squared , d f close parenthesis")[$1 − " pchisq " ( χ^(2) , d f )$] Where the degrees of freedom is #emph[df] = #emph[k] - 1 4. Conclusion This is where you write reject #math.equation(block: false, alt: "H sub o")[$H_(o)$] or fail to reject #math.equation(block: false, alt: "H sub o")[$H_(o)$]. The rule is: if the p-value \< #math.equation(block: false, alt: "α")[$α$], then reject #math.equation(block: false, alt: "H sub o")[$H_(o)$]. If the p-value #math.equation(block: false, alt: "greater than or equal to α")[$≥ α$], then fail to reject #math.equation(block: false, alt: "H sub o")[$H_(o)$], 5. Interpretation This is where you interpret in real world terms the conclusion to the test. The conclusion for a hypothesis test is that you either have enough evidence to show #math.equation(block: false, alt: "H sub A")[$H_(A)$] is true, or you do not have enough evidence to show #math.equation(block: false, alt: "H sub A")[$H_(A)$] is true. #examplebox("Example 1")[goodness of fit test using the formula][ Suppose you have a die that you are curious if it is fair or not. If it is fair then the proportion for each value should be the same. You need to find the observed frequencies and to accomplish this you roll the die 500 times and count how often each side comes up. The data is in Example #math.equation(block: false, alt: "1")[$1$]. Do the data show that the die is fair? Test at the 5% level. #figure(table( columns: 8, align: left, inset: 6pt, table.header([Die values], [1], [2], [3], [4], [5], [6], [Total]), [Observed Frequency], [78], [87], [87], [76], [85], [87], [100], )) #solutionbox[ 1. State the null and alternative hypotheses and the level of significance #math.equation(block: false, alt: "H sub o")[$H_(o)$]: The observed frequencies are consistent with the distribution for fair die (the die is fair) #math.equation(block: false, alt: "H sub A")[$H_(A)$]: The observed frequencies are not consistent with the distribution for fair die (the die is not fair) #math.equation(block: false, alt: "α")[$α$] = 0.05 2. State and check the assumptions for the hypothesis test + A random sample is taken since each throw of a die is a random event. + Expected frequencies for each cell are greater than or equal to 5. See step 3. 3. Find the test statistic and p-value First you need to find the probability of rolling each side of the die. The sample space for rolling a die is {1, 2, 3, 4, 5, 6}. Since you are assuming that the die is fair, then #math.equation(block: false, alt: "P open parenthesis 1 close parenthesis equals P open parenthesis 2 close parenthesis equals P open parenthesis 3 close parenthesis equals P open parenthesis 4 close parenthesis equals P open parenthesis 5 close parenthesis equals P open parenthesis 6 close parenthesis equals the fraction 1 over 6")[$P ( 1 ) = P ( 2 ) = P ( 3 ) = P ( 4 ) = P ( 5 ) = P ( 6 ) = frac(1, 6)$]. Now you can find the expected frequency for each side of the die. Since all the probabilities are the same, then each expected frequency is the same. #math.equation(block: true, alt: "Expected Frequency equals E equals n to the power * p equals 500 * the fraction 1 over 6 approximately equals 83.33")[$"Expected Frequency" = E = n^(*) p = 500 * frac(1, 6) ≈ 83.33$] Test Statistic: It is easier to calculate the test statistic using a table. #figure(table( columns: 5, align: left, inset: 6pt, table.header([#emph[O]], [#emph[E]], [#emph[O-E]], [#math.equation(block: false, alt: "open parenthesis O minus E close parenthesis squared")[$( O − E )^(2)$]], [#math.equation(block: false, alt: "the fraction open parenthesis O minus E close parenthesis squared over E")[$frac(( O − E )^(2), E)$]]), [78], [83.33], [-5.22], [28.4089], [0.340920437], [87], [83.33], [3.67], [13.4689], [0.161633265], [87], [83.33], [3.67], [13.4689], [0.161633265], [76], [83.33], [-7.33], [53.7289], [0.644772591], [85], [83.33], [1.67], [2.7889], [0.033468139], [87], [83.33], [3.67], [13.4689], [0.161633265], [Total], [], [0.02], [], [#math.equation(block: false, alt: "χ squared approximately equals 1.504060962")[$χ^(2) ≈ 1.504060962$]], )) The test statistic is #math.equation(block: false, alt: "χ squared approximately equals 1.504060962")[$χ^(2) ≈ 1.504060962$] The degrees of freedom are #emph[df] = #emph[k - 1 = 6 - 1 = 5] Using TI-83/84: #math.equation(block: false, alt: "p minus value equals χ squared cdf open parenthesis 1.50406096 , 1 E 99 , 5 close parenthesis approximately equals 0.913")[$p − "value" = χ^(2) op("cdf") ( 1.50406096 , 1 E 99 , 5 ) ≈ 0.913$] Using R: #math.equation(block: false, alt: "p minus value equals 1 minus pchisq open parenthesis 1.50406096 , 5 close parenthesis approximately equals 0.9126007")[$p − "value" = 1 − " pchisq " ( 1.50406096 , 5 ) ≈ 0.9126007$] pchisq(1.50406096, 5) 4. Conclusion Fail to reject #math.equation(block: false, alt: "H sub o")[$H_(o)$] since the p-value is greater than 0.05. 5. Interpretation There is not enough evidence to show that the die is not consistent with the distribution for a fair die. There is not enough evidence to show that the die is not fair. ] ] #examplebox("Example 2")[goodness of fit test using technology][ Suppose you have a die that you are curious if it is fair or not. If it is fair then the proportion for each value should be the same. You need to find the observed frequencies and to accomplish this you roll the die 500 times and count how often each side comes up. The data is in Example #math.equation(block: false, alt: "1")[$1$]. Do the data show that the die is fair? Test at the 5% level. #solutionbox[ 1. State the null and alternative hypotheses and the level of significance #math.equation(block: false, alt: "H sub o")[$H_(o)$]: The observed frequencies are consistent with the distribution for fair die (the die is fair) #math.equation(block: false, alt: "H sub A")[$H_(A)$]: The observed frequencies are not consistent with the distribution for fair die (the die is not fair) #math.equation(block: false, alt: "α")[$α$] = 0.05 2. State and check the assumptions for the hypothesis test + A random sample is taken since each throw of a die is a random event. + Expected frequencies for each cell are greater than or equal to 5. See step 3. 3. Find the test statistic and p-value Using the TI-83/84 calculator: #strong[Using the TI-83:] To use the TI-83 calculator to compute the test statistic, you must first put the data into the calculator. Type the observed frequencies into L1 and the expected frequencies into L2. Then you will need to go to L3, arrow up onto the name, and type in #math.equation(block: false, alt: "open parenthesis L 1 minus L 2 close parenthesis to the power ∧ 2 / L 2")[$( L 1 − L 2 )^(∧) 2 / L 2$]. Now you use 1-Var Stats L3 to find the total. See #emph[Figure #math.equation(block: false, alt: "1")[$1$]]for the initial setup, #emph[Figure 11.2].2 for the results of that calculation, and #emph[Figure #math.equation(block: false, alt: "3")[$3$]] for the result of the 1-Var Stats L3. The total is the chi-square value, #math.equation(block: false, alt: "χ squared equals ∑ x approximately equals 1.50406")[$χ^(2) = ∑ x ≈ 1.50406$]. The p-value is found using #math.equation(block: false, alt: "p minus value equals χ squared cdf open parenthesis 1.50406096 , 1 E 99 , 5 close parenthesis approximately equals 0.913")[$p − "value" = χ^(2) op("cdf") ( 1.50406096 , 1 E 99 , 5 ) ≈ 0.913$], where the degrees of freedom is #emph[df] = #emph[k] - 1 = 6 - 1 = 5. #strong[Using the TI-84:] To run the test on the TI-84, type the observed frequencies into L1 and the expected frequencies into L2, then go into STAT, move over to TEST and choose #math.equation(block: false, alt: "χ squared")[$χ^(2)$] GOF-Test from the list. The setup for the test is in #emph[Figure #math.equation(block: false, alt: "4")[$4$]]. Once you press ENTER on Calculate you will see the results in #emph[Figure #math.equation(block: false, alt: "5")[$5$]]. The test statistic is #math.equation(block: false, alt: "χ squared approximately equals 1.504060962")[$χ^(2) ≈ 1.504060962$] The #emph[p-value]#math.equation(block: false, alt: "approximately equals 0.913")[$≈ 0.913$] The CNTRB represent the #math.equation(block: false, alt: "the fraction open parenthesis O minus E close parenthesis squared over E")[$frac(( O − E )^(2), E)$] for each die value. You can see the values by pressing the right arrow. Using R: Type in the observed frequencies. Call it something like observed. #linebreak() observed\<- c(type in data with commas in between) #linebreak() Type in the probabilities that you are comparing to the observed frequencies. #linebreak() Call it something like null.probs. #linebreak() null.probs \<- c(type in probabilities with commas in between) #linebreak() chisq.test(observed, p=null.probs) – the command for the hypothesis test #linebreak() For this example (Note since you are looking to see if the die is fair, then the probability of each side of a fair die coming up is 1/6.) #linebreak() observed\<-c(78, 87, 87, 76, 85, 87) #linebreak() null.probs\<-c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6) #linebreak() chisq.test(observed, p=null.probs) observed \<- c(78, 87, 87, 76, 85, 87) null.probs \<- c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6) chisq.test(observed, p=null.probs) Output: #linebreak() Chi-squared test for given probabilities data: observed #linebreak() X-squared = 1.504, df = 5, p-value = 0.9126 The test statistic is #math.equation(block: false, alt: "χ squared equals 1.504")[$χ^(2) = 1.504$] and the p-value = 0.9126. 4. Conclusion Fail to reject #math.equation(block: false, alt: "H sub o")[$H_(o)$] since the p-value is greater than 0.05. 5. Interpretation There is not enough evidence to show that the die is not consistent with the distribution for a fair die. There is not enough evidence to show that the die is not fair. ] ] === Homework #notebox("Your Turn", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ In each problem show all steps of the hypothesis test. If some of the assumptions are not met, note that the results of the test may not be correct and then continue the process of the hypothesis test. + According to the M&M candy company, the expected proportion can be found in Example #math.equation(block: false, alt: "3")[$3$]. In addition, the table contains the number of M&M’s of each color that were found in a case of candy (Madison, 2013). At the 5% level, do the observed frequencies support the claim of M&M? #figure(table( columns: 8, align: left, inset: 6pt, table.header([], [Blue], [Brown], [Green], [Orange], [Red], [Yellow], [Total]), [Observed Frequencies], [481], [371], [483], [544], [372], [369], [2620], [Expected Proportion], [0.24], [0.13], [0.16], [0.20], [0.13], [0.14], [], )) + Eyeglassomatic manufactures eyeglasses for different retailers. They test to see how many defective lenses they made the time period of January 1 to March 31. Example #math.equation(block: false, alt: "4")[$4$] gives the defect and the number of defects. Do the data support the notion that each defect type occurs in the same proportion? Test at the 10% level. #figure(table( columns: 2, align: left, inset: 6pt, table.header([Defect type], [Number of defects]), [Scratch], [5865], [Right shaped - small], [4613], [Flaked], [1992], [Wrong axis], [1838], [Chamfer wrong], [1596], [Crazing, cracks], [1546], [Wrong shape], [1485], [Wrong PD], [1398], [Spots and bubbles], [1371], [Wrong height], [1130], [Right shape - big], [1105], [Lost in lab], [976], [Spots/bubble - intern], [976], )) + On occasion, medical studies need to model the proportion of the population that has a disease and compare that to observed frequencies of the disease actually occurring. Suppose the end-stage renal failure in south-west Wales was collected for different age groups. Do the data in Example #math.equation(block: false, alt: "5")[$5$] show that the observed frequencies are in agreement with proportion of people in each age group (Boyle, Flowerdew & Williams, 1997)? Test at the 1% level. #figure(table( columns: 7, align: left, inset: 6pt, table.header([Age Group], [16-29], [30-44], [45-59], [60-75], [75+], [Total]), [Observed Frequency], [32], [66], [132], [218], [91], [539], [Expected Proportion], [0.23], [0.25], [0.22], [0.21], [0.09], [], )) + In Africa in 2011, the number of deaths of a female from cardiovascular disease for different age groups are in Table 6 ("Global health observatory," 2013). In addition, the proportion of deaths of females from all causes for the same age groups are also in Table 6. Do the data show that the death from cardiovascular disease are in the same proportion as all deaths for the different age groups? Test at the 5% level. #figure(table( columns: 6, align: left, inset: 6pt, table.header([Age], [5-14], [15-29], [30-49], [50-69], [Total]), [Cardiovascular Frequency], [9], [16], [56], [433], [513], [All Cause Proportion], [0.10], [0.12], [0.26], [0.52], [], )) + In Australia in 1995, there was a question of whether indigenous people are more likely to die in prison than non-indigenous people. To figure out, the data in Table 7 was collected. ("Aboriginal deaths in," 2013). Do the data show that indigenous people die in the same proportion as non-indigenous people? Test at the 1% level. #figure(table( columns: 4, align: left, inset: 6pt, table.header([], [Prisoner Dies], [Prisoner Did Not Die], [Total]), [Indigenous Prisoner Frequency], [17], [2890], [2907], [Frequency of Non-Indigenous Prisoner], [42], [14459], [14501], )) + A project conducted by the Australian Federal Office of Road Safety asked people many questions about their cars. One question was the reason that a person chooses a given car, and that data is in Table 8 ("Car preferences," 2013). #figure(table( columns: 6, align: left, inset: 6pt, table.header([Safety], [Reliability], [Cost], [Performance], [Comfort], [Looks]), [84], [62], [46], [34], [47], [27], )) Answer For all hypothesis tests, just the conclusion is given. See solutions for the entire answer. 1. Reject Ho 3. Reject Ho 5. Reject Ho ]