-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-testing-hypothesis-for-categorical-data.Rmd
1678 lines (1253 loc) · 55.4 KB
/
11-testing-hypothesis-for-categorical-data.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Testing Hypothesis for Categorical Data
[book](pdf/book11.pdf){target="_blank"}
[eStat YouTube Channel](https://www.youtube.com/channel/UCw2Rzl9A4rXMcT8ue8GH3IA){target="_blank"}
**CHAPTER OBJECTIVES**
The hypothesis tests that we have studied from Chapter 7 to Chapter 10
are for continuous data. In this chapter, we describe testing hypothesis
for categorical data.
Section 11.1 describes the goodness of fit test for the frequency table
of categorical data.
Section 11.2 describes the independence and homogeneity tests for the
contingence table of two categorical data.
:::
:::
## Goodness of Fit Test
::: presentation-video-link
[presentation](pdf/1101.pdf){.presentation-link target="_blank"}
[video](https://youtu.be/yMDHeJOkeU0){.video-link target="_blank"}
:::
::: mainTable
The frequency table of categorical data discussed in Chapter 4 counts
the frequency of possible values of a categorical variable. If this
frequency table is for sample data from a population, we are curious
what would be the frequency distribution of the population. The goodness
of fit test is a test on the hypothesis that the population follows a
particular distribution based on the sample frequency distribution. In
this section, we discuss the goodness of fit test for categorical
distributions (Section 11.1.1) and the goodness of fit test for
continuous distribution (Section 11.1.2).
:::
### Goodness of Fit Test for Categorical Distribution
::: mainTable
Consider the goodness of fit test for a categorical distribution using
the example below.
:::
::: mainTableGrey
**Example 11.1.1** The result of a survey of 150 people before a local
election to find out the approval ratings of three candidates is as
follows. Looking at this frequency table alone, it seems that A
candidate has a 40 percent approval rating, higher than the other
candidates. Based on this sample survey, perform the goodness of fit
test whether three candidates have the same approval rating or not.
Use『eStatU』with the 5% significance level.
::: textLeft
Table 11.1.1 Frequency table of survey result in an election.
:::
Candidate Number of Supporters Percent
----------- ---------------------- ---------
A 60 40.0%
B 50 33.3%
C 40 25.7%
Total 150 100%
**Answer**
Assume each of candidate A, B, and C's approval rating is
$p_1 , p_2 , p_3$ respectively. The hypothesis for this problem is as
follows:
::: textLeft
$\small H_0$ : The three candidates have the same approval rating.
(i.e.,$p_1 = p_2 = p_3 = \frac{1}{3}$ )
:::
::: textLeft
$\small H_1$ : The three candidates have different approval ratings.
:::
If the null hypothesis $\small H_0$ is true that the three candidates
have the same approval rating, each candidate will have 50 (=
150$\times \frac{1}{3}$ ) supporters out of total 150 people. It is
referred to as the 'expected frequency' of each candidate when
$\small H_0$ is true. For each candidate, the number of observed
supporters in the sample is called the 'observed frequency'. If
$\small H_0$ is true, the observed and expected number of supporters can
be summarized as the following table.
-----------------------------------------------------------------------
Candidate Observed frequency\ Expected frequency\
(denoted as $O_i$) (denoted as $E_i$)
----------------------- ----------------------- -----------------------
A $O_1 = 60$ $E_1 = 50$
B $O_2 = 50$ $E_2 = 50$
C $O_3 = 40$ $E_3 = 50$
Total 150 150
-----------------------------------------------------------------------
If $\small H_0$ is true, the observed frequency ($\small O_i$) and the
expected frequency ($\small E_i$) will coincide. Therefore, in order to
test the hypothesis, a statistic which uses the difference between
$\small O_i$ and $\small E_i$ is used. Specifically, the statistic to
test the hypotheses is as follows:
::: textLeft
${\chi}_{obs}^2 = \frac {(O_1 - E_1 )^2} {E_1} + \frac {(O_2 - E_2 ) ^2} {E_2} + \frac {(O_3 - E_3 )^2} {E_3}$
:::
If the observed value of this test statistic is close to zero, it can be
considered that $\small H_0$ is true, because $\small O_i$ is close to
$\small E_i$. If the observed value is large, $\small H_0$ will be
rejected. The question is, 'How large value of the test statistic would
be considered as the statistically significant one?' It can be shown
that this test statistic approximately follows the chi-square
distribution with $k-1$ degrees of freedom if the expected frequency is
large enough. Here $k$ is the number of categories (i.e., candidates) in
the table and it is 3 in this example. Therefore, the decision rule to
test the hypotheses is as follows:
::: textLeft
'If $\chi_{obs}^2 > \chi_{k-1; α}^2$ , reject $\small H_0$, else do not
reject $\small H_0$'
:::
The statistic $\chi_{obs}^2$ can be calculated as follows:
::: textLeft
${\chi}_{obs}^2 = \frac {(60 - 50)^2} {50} + \frac {(50 - 50 ) ^2} {50} + \frac {(40 - 50)^2} {50}$
:::
Since the significance level α is 5%, the critical value can be found
from the chi-square distribution as follows:
::: textLeft
$\chi_{k-1 ; α}^{2} = \chi_{3-1 ; 0.05}^{2} = \chi_{2 ; 0.05}^{2} = 5.991$
:::
Therefore, $\small H_0$ can not be rejected. In other words, although
the above sample frequency table shows that the approval ratings of the
three candidates differ, this difference does not provide sufficient
evidence to conclude that the three candidates have different approval
ratings.
Using each candidate's sample approval rating
$\hat p_1 = \frac{60}{150}$ = 0.40, $\hat p_2 = \frac{50}{150}$ = 0.33,
$\hat p_1 = \frac{40}{150}$ = 0.27, 95% confidence intervals for the
population proportion of each candidate's approval rating using the
formula (${{\hat p}} ± 1.96 \sqrt { \hat p (1- \hat p ) / n }$) (refer
Chapter 6.4) are as follows:
::: textLeft
A:
$\quad 0.40 \pm 1.96 \sqrt { \frac {0.40 \times 0.60} {150} } \quad \quad \Leftrightarrow \quad$
\[0.322, 0.478\]\
B:
$\quad 0.33 \pm 1.96 \sqrt { \frac {0.33 \times 0.67} {150} } \quad \quad \Leftrightarrow \quad$
\[0.255, 0.405\]\
C:
$\quad 0.27 \pm 1.96 \sqrt { \frac {0.27 \times 0.73} {150} } \quad \quad \Leftrightarrow \quad$
\[0.190, 0.330\]\
:::
The overlapping of the confidence intervals on the three candidates'
approval ratings does not mean that one candidate's approval rating is
completely different from the other.
In the Input box that appears by selecting the 'Goodness of Fit Test'
of 『eStatU』, enter the 'Observed Frequency' and 'Expected
Probability' data as shown in [Figure 11.1.1]{.figure-ref}. After entering the
data, select the significance level and click [Execute]{.button-ref} button to
calculate the 'Expected Frequency' and to see the result of the
chi-square test. Be sure that this chi-square goodness of fit test
should be applied when the expected frequency of each category is at
least 5.
<div>
<div>
<input class="qrBtn" onclick="window.open(addrStr[110])" src="QR/eStatU910_TestFit.svg" type="image"/>
</div>
<div>
![](Figure/Fig110101.png){.imgFig600400}
::: figText
[Figure 11.1.1]{.figure-ref} Goodness of fit test in 『eStatU』
:::
</div>
</div>
![](Figure/Fig110102.png){.imgFig600400}
::: figText
[Figure 11.1.2]{.figure-ref} 『eStatU』Chi-square Goodness of Fit Test
:::
:::
::: mainTable
Consider a categorical variable X which has number of possible values
and their probabilities are respectively. In other words, the
probability distribution for the categorical variable X is as follows:
$X$ $P(X = x)$
---------- ------------
$x_1$ $p_1$
$x_2$ $p_2$
$\cdots$ $\cdots$
$x_k$ $p_k$
Total 1
When random samples are collected from the population of the categorical
random variable X and their observed frequencies are
($O_1 , O_2 , ... , O_k$), the hypothesis to test the population
probability distribution of ($p_1 , p_2 , ... , p_k$) =
($p_{10} , p_{20} , ... , p_{k0}$) is as follows:
::: textLeft
$H_0$ : Data $O_1 , O_2 , ... , O_k$ are from the distribution
($p_1 , p_2 , ... , p_k$) = ($p_{10} , p_{20} , ... , p_{k0} )$\
$H_1$ : Data $O_1 , O_2 , ... , O_k$ are not from the distribution
($p_1 , p_2 , ... , p_k$) = ($p_{10} , p_{20} , ... , p_{k0} )$\
:::
If the total number of samples $n$ is large enough, the above hypothesis
can be tested using the following decision rule of the chi-square test
statistic. $$
\text{‘If } \chi_{obs}^{2} = \sum_{i=1}^{k} \frac { (O_{i} - E_{i} )^{2}} {E_{i}} > \chi_{k-m-1 ; α}^{2}, \text{ then reject } H_0 ’
$$ Here, ($E_1 , E_2 , ... , E_k$) =
($np_{10} , np_{20} , ... , np_{k0}$) are expected frequencies, $m$ is
the number of population parameters estimated from the sample data. In
[Example 11.1.1]{.example-ref}, since there was not a population parameter estimated
from the sample, $m$ = 0 .
:::
::: mainTableYellow
**Goodness of Fit Test**
Consider a categorical variable $X$ which has $k$ number of possible
values $x_1 , x_2 , ... , x_k$ and there probabilities are
$p_1 , p_2 , ... , p_k$ respectively. Let observed frequencies for each
value of $X$ from $n$ samples are ($O_1 , O_2 , ... , O_k$), expected
frequencies for each value of $X$ from $n$ samples are
$( E_1 , E_2 , ... , E_k )$ = $(np_{10} , np_{20} , ... , np_{k0} )$ and
the significance level is α.
**Hypothesis:**
::: textLeft
$H_0$ : Distribution of $O_1 , O_2 , ... , O_k$ follows
$(p_{10} , p_{20} , ... , p_{k0} )$\
$H_1$ : Distribution of $O_1 , O_2 , ... , O_k$ does not follow
$(p_{10} , p_{20} , ... , p_{k0} )$\
:::
**Decision Rule:**
::: textLeft
'If
$\chi_{obs}^{2} = \sum_{i=1}^{k} \frac { (O_{i} - E_{i} )^{2}} {E_{i}} > \chi_{k-m-1 ; α}^{2}$,
then reject $H_0$ '\
where $m$ is the number of population parameters estimated from the
samples.
:::
:::
::: mainTableYellow
**$\clubsuit$ In order to use the chi-square Goodness of Fit test, all
expected frequencies $E_i$ should be greater than 5.**
$\clubsuit$ A category which has an expected frequency less than 5 can
be merged with other category.
:::
::: mainTablePink
<div>
<div>
<input class="qrBtn" onclick="window.open(addrStr[110])" src="QR/eStatU910_TestFit.svg" type="image"/>
</div>
<div>
**Practice 11.1.1** Market shares of toothpaste A, B, C and D are known
to be 0.3, 0.6, 0.08, and 0.02 respectively. The result of a survey of
100 people for the toothpaste brands are as follows. Can you conclude
from these data that the known market share is incorrect? Use
『eStatU』. $\alpha$ = 0.05.
Brand Number of Customers
------- ---------------------
A 192
B 342
C 44
D 22
Total 600
</div>
</div>
:::
:::
:::
### Goodness of Fit Test for Continuous Distribution
::: presentation-video-link
[presentation](pdf/110102.pdf){.presentation-link target="_blank"}
[video](https://youtu.be/Zhdv1QwFfcE){.video-link target="_blank"}
:::
::: mainTable
The goodness of fit test for categorical data using the chi-square
distribution can also be used for continuous data. The following is an
example of the goodness of fit test in which data are derived from a
population of a normal distribution. The parametric statistical tests
from Chapter 6 to Chapter 9 require the assumption that the population
is normally distributed and the goodness of fit test in this section can
be used to test for normality.
:::
::: mainTableGrey
**Example 11.1.2** The age of 30 people who visited a library in the
morning is as follows. Test the hypothesis that the population is
normally distributed at the significance level of 5%.
::: textLeft
28 55 26 35 43 47 47 17 35 36 48 47 34 28 43
:::
::: textLeft
20 30 53 27 32 34 43 18 38 29 44 67 48 45 43
:::
::: textLeft
Ex ⇨ eBook ⇨ EX110102_AgeOfLibraryVisitor.csv
:::
**Answer**
Age is a continuous variable, but you can make a frequency distribution
by dividing possible values into intervals as we studied in histogram of
Chapter 3. It is called a categorization of the continuous data.
Let's find a frequency table which starts at the age of 10 with the
interval size of 10. The histogram of『eStat』makes this frequency table
easy to obtain.
If you enter the data as shown in [Figure 11.1.3]{.figure-ref}, click the histogram
icon and select Age from the variable selection box, then the histogram
as [Figure 11.1.4]{.figure-ref} will appear.
<div>
<div>
<input class="qrBtn" onclick="window.open(addrStr[34])" src="QR/EX110102.svg" type="image"/>
</div>
<div>
![](Figure/Fig110103.png){.imgFig600400}
::: figText
[Figure 11.1.3]{.figure-ref} Data input at『eStat』
:::
</div>
</div>
![](Figure/Fig110104.svg){.imgFig600400}
::: figText
[Figure 11.1.4]{.figure-ref} Default histogram of age
:::
If you specify 'start interval' as 10 and 'interval width' as 10 in
the options window below the histogram, the histogram of \<Figure
11.1.4\> is adjusted as [Figure 11.1.5]{.figure-ref}. If you click \[Frequency
Table\] button, the frequency table as shown in [Figure 11.1.6]{.figure-ref} will
appear in the Log Area. The designation of interval size can be
determined by a researcher.
![](Figure/Fig110105.png){.imgFig600400}
::: figText
[Figure 11.1.5]{.figure-ref} Adjusted histogram of age
:::
![](Figure/Fig110106.png){.imgFig600400}
::: figText
[Figure 11.1.6]{.figure-ref} Frequency table of the adjusted histogram
:::
Since the normal distribution is a continuous distribution defined at
$-\infty \lt x \lt \infty$ , the frequency table of [Figure 11.1.6]{.figure-ref}
can be written as follows:
::: textLeft
Table 11.1.2 Frequency table of age with adjusted interval
:::
Interval id Interval Observed frequency
------------- ----------------- --------------------
1 $X < 20$ 2
2 $20 \le X < 30$ 7
3 $30 \le X < 40$ 7
4 $40 \le X < 50$ 9
5 $50 \le X < 60$ 3
6 $X \ge 60$ 2
The frequency table of sample data as [Table 11.1.2]{.table-ref} can be used to test
the goodness of fit whether the sample data follows a normal
distribution using the chi-square distribution. The hypothesis of this
problem is as follows:
::: textLeft
$\small H_0$ Sample data follows a normal distribution
:::
::: textLeft
$\small H_1$ Sample data does not follow a normal distribution
:::
This hypothesis does not specify what a normal distribution is and
therefore, the population mean $\mu$ and the population variance
$\sigma^2$ should be estimated from sample data. Pressing the 'Basic
Statistics' icon on the main menu of『eStat』will display a table of
basic statistics in the Log Area, as shown in [Figure 11.1.7]{.figure-ref}. The
sample mean is 38.567 and the sample standard deviation is 12.982.
![](Figure/Fig110107.png){.imgFig600400}
::: figText
[Figure 11.1.7]{.figure-ref} Descriptive statistics of age
:::
Hence, the above hypothesis can be written in detail as follows:
::: textLeft
$\small H_0$ Sample data follows $N(38.567, 12.982^2 )$
:::
::: textLeft
$\small H_1$ Sample data does not follow $N(38.567, 12.982^2 )$
:::
In order to find the expected frequency of each interval when
$\small H_0$ is true, the expected probability of each interval is
calculated first using the normal distribution
$\small N(38.567, 12.982^2 )$ as follows. The normal distribution module
of 『eStatU』 makes it easy to calculate this probability of an
interval. At the normal distribution module of 『eStatU』, enter the
mean of 38.567 and the standard deviation of 12.982. Click the second
radio button of P(X \< x) type and enter 20, then press the [Execute]{.button-ref}
button to calculate the probability as shown in [Figure 11.1.8]{.figure-ref}.
::: textLeft
$P(X \lt 20) = P(Z \lt \frac {20-38.567} {12.982} ) = P(Z \lt -1.430)=0.075$
:::
<div>
<div>
<input class="qrBtn" onclick="window.open(addrStr[88])" src="QR/eStatU520_Normal.svg" type="image"/>
</div>
<div>
![](Figure/Fig110108.png){.imgFig600400}
::: figText
[Figure 11.1.8]{.figure-ref} Calculation of normal probability using『eStatU』
:::
</div>
</div>
Similarly you can calculate the following probabilities.
::: textLeft
$P(20 \le X \lt 30) = P(\frac{20-38.567}{12.982} \le Z \lt \frac {30-38.567}{12.982} ) = P(-1.430 \le Z \lt -0.660) = 0.178$
:::
::: textLeft
$P(30 \le X \lt 40) = P(\frac{30-38.567}{12.982} \le Z \lt \frac {40-38.567}{12.982} ) = P(-0.660 \le Z \lt 0.110) = 0.289$
:::
::: textLeft
$P(40 \le X \lt 50) = P(\frac{40-38.567}{12.982} \le Z \lt \frac {50-38.567}{12.982} ) = P( 0.110 \le Z \lt 0.881) = 0.267$
:::
::: textLeft
$P(50 \le X \lt 60) = P(\frac{50-38.567}{12.982} \le Z \lt \frac {60-38.567}{12.982} ) = P( 0.881 \le Z \lt 1.651) = 0.140$
:::
::: textLeft
$P( X \ge 60) = P( Z \ge \frac {60-38.567}{12.982} ) = P( Z \ge 1.651) = 0.049$
:::
Expected frequency can be calculated by multiplying the sample size of
30 to the expected probability of each interval obtained above. The
observed frequencies, expected probabilities, and expected frequencies
for each interval can be summarized as the following table.
::: textLeft
Table 11.1.3 Observed and expected frequencies of each interval of
distribution
:::
Interval id Interval Observed frequency Expected probability Expected frequency
------------- ----------------- -------------------- ---------------------- --------------------
1 $X < 20$ 2 0.075 2.25
2 $20 \le X < 30$ 7 0.178 5.34
3 $30 \le X < 40$ 7 0.289 8.67
4 $40 \le X < 50$ 9 0.267 8.01
5 $50 \le X < 60$ 3 0.140 4.20
6 $X \ge 60$ 2 0.049 1.47
Since the expected frequencies of the 1st and 6th interval are less than
5, the intervals should be combined with adjacent intervals for testing
the goodness of fit using the chi-square distribution as [Table 11.1.4]{.table-ref}.
The expected frequency of the last interval is still less than 5, but,
if we combine this interval, there are only three intervals, we
demonstrate the calculation as it is. Note that, due to computational
error, the sum of the expected probabilities may not be exactly equal to
1 and the sum of the expected frequencies may not be exactly 30 in Table
11.1.4.
::: textLeft
Table 11.1.4 Revised table after combining interval of small expected
frequency
:::
Interval id Interval Observed frequency Expected probability Expected frequency
------------- ----------------- -------------------- ---------------------- --------------------
1 $X < 30$ 9 0.253 7.59
2 $30 \le X < 40$ 7 0.289 8.67
3 $40 \le X < 50$ 9 0.267 8.01
4 $X \ge 50$ 5 0.189 5.67
Total 30 0.998 29.94
The test statistic for the goodness of fit test is as follows:
::: textLeft
$\chi_{obs}^{2} = \frac{(9-7.59)^{2}}{7.59} + \frac{(7-8.67)^{2}}{8.67} + \frac{(9-8.01)^{2}}{8.01} + \frac{(5-5.67)^{2}}{5.67} = 0.785$
:::
Since the number of intervals is 4, $k$ becomes 4, and $m$=2, because
two population parameters $\mu$ and $\sigma^2$ are estimated from the
sample data. Therefore, the critical value is as follows:
::: textLeft
$\chi_{k-m-1; α}^{2} = \chi_{4-2-1; 0.05}^{2} = \chi_{1; 0.05}^{2} = 3.841$
:::
The observed test statistic is less than the critical value, we can not
reject the null hypothesis that the sample data follows
$\small N(38.567, 12.982^2 )$ .
Test result can be verified using 'Goodness of Fit Test' in
『eStatU』. In the Input box that appears by selecting the 'Goodness of
Fit Test' module, enter the data for 'observation frequency' and
'expected probability' in [Table 11.1]{.table-ref}.4, as shown in [Figure 11.1.9]{.figure-ref}.
After entering the data, select the significance level and press the
[Execute]{.button-ref} button to calculate the 'expected frequency' and produce a
chi-square test result ([Figure 11.1.10]{.figure-ref}).
<div>
<div>
<input class="qrBtn" onclick="window.open(addrStr[110])" src="QR/eStatU910_TestFit.svg" type="image"/>
</div>
<div>
![](Figure/Fig110109.png){.imgFig600400}
::: figText
[Figure 11.1.9]{.figure-ref} Data input for goodness of fit test in 『eStatU』
:::
</div>
</div>
![](Figure/Fig110110.svg){.imgFig600400}
::: figText
[Figure 11.1.10]{.figure-ref} Chi-square goodness of fit test using『eStatU』
:::
:::
::: mainTablePink
<div>
<div>
<input class="qrBtn" onclick="window.open(addrStr[110])" src="QR/eStatU910_TestFit.svg" type="image"/>
</div>
<div>
**Practice 11.1.2** **(Otter length)**\
Data of 30 otter lengths can be found at the following location
of『eStat』.
::: textLeft
Ex ⇨ eBook ⇨ PR110102_OtterLength.csv
:::
Test the hypothesis that the population is normally distributed at the
significance level of 5% using『eStat』.
</div>
</div>
:::
::: mainTablePink
### Multiple Choice Exercise
Choose one answer and click Submit button
::: textL30M30
11.1 What tests do you need to investigate whether the sample data
follow a theoretical distribution?
:::
<form name="Q1">
<label><input name="item" type="radio" value="1"/> Goodness of fit test</label><br/>
<label><input name="item" type="radio" value="2"/> ndependence test</label><br/>
<label><input name="item" type="radio" value="3"/> Test for population proportion</label><br/>
<label><input name="item" type="radio" value="4"/> Test for two population means</label><br/>
<p>
<input onclick="radio(11,1,Q1)" type="button" value="Submit"/>
<input id="ansQ1" size="15" type="text"/>
</p></form>
::: textL30M30
11.2 In order to test whether sample data of a continuous variable
follow a distribution, what is the first necessary work for the goodness
of fit test?
:::
<form name="Q2">
<label><input name="item" type="radio" value="1"/> log transformation</label><br/>
<label><input name="item" type="radio" value="2"/> frequency distribution of interval</label><br/>
<label><input name="item" type="radio" value="3"/> [0,1] transformation </label><br/>
<label><input name="item" type="radio" value="4"/> frequency distribution</label><br/>
<p>
<input onclick="radio(11,2,Q2)" type="button" value="Submit"/>
<input id="ansQ2" size="15" type="text"/>
</p></form>
:::
:::
:::
## Testing Hypothesis for Contingency Table
::: presentation-video-link
[presentation](pdf/1102.pdf){.presentation-link target="_blank"}
[video](https://youtu.be/ywGsZrMH-Wo){.video-link target="_blank"}
:::
::: mainTable
The contingency table or cross table discussed in Chapter 4 was a table
that placed the possible values of two categorical variables in rows and
columns, respectively, and examined frequencies of each cell in which
the values of the two variables intersect. If this contingency table is
for sample data taken from a population, it is possible to predict what
would be the contingency table of the population. The test for the
contingency table is usually an analysis of the relation between two
categorical variables and it can be divided into the independence test
and homogeneity test according to the sampling method for obtaining the
data.
:::
### Independence Test
::: mainTable
The independence test of the contingency table is to investigate whether
two categorical variables are independent when samples are extracted
from one population. Consider the independence test with the following
example.
:::
::: mainTableGrey
**Example 11.2.1** In order to investigate whether college students who
are wearing glasses are independent by gender, a sample of 100 students
was collected and its contingency table was prepared as follows:
::: textLeft
Table 11.2.1 Wearing glasses by gender
:::
Wear Glasses No Glasses Total
------- -------------- ------------ -------
Men 40 10 50
Women 20 30 50
Total 60 40 100
::: textLeft
Ex ⇨ eBook ⇨ EX110201_GlassesByGender.csv.
:::
::: textL20M20
1\) Using『eStat』, draw a line graph of the use of eyeglasses by men
and women.
:::
::: textL20M20
2\) Test the hypothesis at 5% of the significance level to see if the
gender variable and the wearing of glasses are independent or related to
each other.
:::
::: textL20M20
3\) Check the result of the independence test using『eStatU』.
:::
**Answer**
::: textL20M20
1\) Enter data in 『eStat』 as shown in [Figure 11.2.1]{.figure-ref}.
<div>
<div>
<input class="qrBtn" onclick="window.open(addrStr[35])" src="QR/EX110201.svg" type="image"/>
</div>
<div>
![](Figure/Fig110201.png){.imgFig600400}
::: figText
[Figure 11.2.1]{.figure-ref} Data input
:::
</div>
</div>
:::
::: textL20
Select 'Line Graph' icon from the main menu. If you click variables
'Gender', 'Glasses', 'NoGlasses' one by one, then a line graph as shown
in [Figure 11.2.2]{.figure-ref} will appear in the Graph Area. If you look at the
line graph, you can see that the ratio of wearing glasses for men and
women are different. For men, there are many students who do not wear
glasses (80% of men) and for women, 60% of women do. In such cases, the
gender variable and the wearing of glasses are considered related. As
such, when two variables are related, two lines of the line graph
intersect to each other.
![](Figure/Fig110202.svg){.imgFig600400}
::: figText
[Figure 11.2.2]{.figure-ref} Line graph of wearing glasses by gender
:::
:::
::: textL20M20
2\) If two variables are not related (i.e., if the two variables are
independent of each other), the contingency table in [Table 11.2.1]{.table-ref} will
show that the proportion of wearing glasses by men or women is equal to
60% which is the proportion of all students wearing glasses. In other
words, if two variables are independent, the contingency table should be
as follows:
:::
::: textLeft
Table 11.2.2 Contingency table when gender and wearing glasses are
independent
:::
Wear Glasses No Glasses Total
------- -------------- ------------ -------
Men 30 20 50
Women 30 20 50
Total 60 40 100
::: textL20
If there is little difference between the observed contingency table and
the contingency table in the case of independence, two categorical
variables are said to be independent of each other. If the differences
are very large, two categorical variables are related to each other. The
independence test is a statistical method for determining that two
categorical variables of the population are independent of each other by
using the observed contingency table obtained from the sample. The
independent test uses the chi-square distribution and the hypothesis is
as follows:
:::
::: textLeft
$\small H_0 :$ Two variables of the contingency table are independent of
each other.
:::
::: textLeft
$\small H_1 :$ Two variables of the contingency table are related.
:::
::: textL20
The test statistic for testing this hypothesis utilizes the difference
between the observed frequency of the contingency table in the sample
and the expected frequency of the contingency table when two variables
are assumed to be independent which is similar to the goodness of fit
test. The test statistic in this example is as follows:
:::
::: textLeft
$\chi_{obs}^{2~} = \frac{(40-30)^{2}}{30} + \frac{(10-20)^{2}}{20} + \frac{(20-30)^{2}}{30} + \frac{(30-20)^{2}}{20}$
= 16.67
:::
::: textL20
This test statistic follows a chi-square distribution with $(r-1)(c-1)$
degrees of freedom where $r$ is the number of rows (number of possible
values of row variable) and $c$ is the number of columns (number of
possible values of column variable). Therefore, the decision rule to
test the hypothesis is as follows:
:::
::: textLeft
'If $\chi_{obs}^{2} > \chi_{(r-1)(c-1); α}^{2}$, then reject
$\small H_0$.'
:::
::: textL20
In this example, $\chi_{obs}^{2}$ = 16.67 is greater than the critical
value
$\chi_{(r-1)(c-1); α}^{2} = \chi_{(2-1)(2-1); 0.05}^{2} = \chi_{1; 0.05}^{2}$
= 3.841. Therefore, the null hypothesis that two variables are
independent each other is rejected and we conclude that the gender and
wearing glasses are related.
:::
::: textL20M20
3\) In the independence test of『eStatU』, enter data as shown in
[Figure 11.2.3]{.figure-ref} and press the [Execute]{.button-ref} button to display the result
of the chi-square test as shown in [Figure 11.2.4]{.figure-ref}.
:::
<div>
<div>
<input class="qrBtn" onclick="window.open(addrStr[111])" src="QR/eStatU920_TestIndependence.svg" type="image"/>
</div>
<div>
![](Figure/Fig110203.png){.imgFig600400}
::: figText
[Figure 11.2.3]{.figure-ref} 『eStatU』Test of Independence
:::
</div>
</div>
![](Figure/Fig110204.png){.imgFig600400}
::: figText
[Figure 11.2.4]{.figure-ref}『eStatU』Chi-square test of independence
:::
:::
::: mainTable
Assume that there are $r$ number of attributes of the variable $A$ such
as $A_1 , A_2 , ... , A_r$ , and $c$ number of attributes of the
variable $B$ such as $B_1 , B_2 , ... , B_c$. Let $p_{ij}$ denote the
probability of the cell of $A_i$ and $B_j$ attribute in the contingency
table of $A$ and $B$ as [Table 11.2.3]{.table-ref}. Here
$p_{i\cdot} = p_{i1} + p_{i2} + \cdots + p_{ic}$ denotes the probability
of $A_i$ and $p_{\cdot j} = p_{1j} + p_{2j} + \cdots + p_{rj}$ denotes
the probability of $B_j$.
:::
::: textLeft
Table 11.2.3 Notation of probabilities in $r \times c$ contingency table
:::
Variable A \| Variable B $B_1$ $B_2$ $\cdots$ $B_c$ Total
-------------------------- --------------- --------------- ---------- --------------- --------------
$A_1$ $p_{11}$ $p_{12}$ $\cdots$ $p_{1c}$ $p_{1\cdot}$
$A_2$ $p_{21}$ $p_{22}$ $\cdots$ $p_{2c}$ $p_{2\cdot}$
$\cdots$ $\cdots$ $\cdots$ $\cdots$ $\cdots$ $\cdots$
$A_r$ $p_{r1}$ $p_{r2}$ $\cdots$ $p_{rc}$ $p_{r\cdot}$
Total $p_{\cdot 1}$ $p_{\cdot 2}$ $\cdots$ $p_{\cdot c}$ 1
::: mainTable
If two events $A_i$ and $B_j$ are independent,
$P(A_{i} \cap B_{j} ) = P(A_{i})·P(B_{j} )$ and hence,
$p_{ij} = p_{i \cdot} p_{\cdot j}$. If two variables $A$ and $B$ are
independent, all $A_i$ and $B_j$ should satisfy the above property which
is called the independent test.
:::
::: textLeft
$H_0 :$ Variable $A$ and $B$ are independent.
:::
::: textLeft
$H_1 :$ Variable $A$ and $B$ are not independent.
:::
::: mainTable
In order to test whether two variables of the population are
independent, let us assume the observed frequencies, $O_{ij}$'s, of the
contingency table from $n$ samples are as follows:
:::
::: textLeft
Table 11.2.4 Observed frequency of contingency table
:::
Variable A \| Variable B $B_1$ $B_2$ $\cdots$ $B_c$ Total
-------------------------- --------------- --------------- ---------- --------------- --------------
$A_1$ $O_{11}$ $O_{12}$ $\cdots$ $O_{1c}$ $T_{1\cdot}$
$A_2$ $O_{21}$ $O_{22}$ $\cdots$ $O_{2c}$ $T_{2\cdot}$
$\cdots$ $\cdots$ $\cdots$ $\cdots$ $\cdots$ $\cdots$
$A_r$ $O_{r1}$ $O_{r2}$ $\cdots$ $O_{rc}$ $T_{r\cdot}$
Total $T_{\cdot 1}$ $T_{\cdot 2}$ $\cdots$ $T_{\cdot c}$ n
::: mainTable
If the null hypothesis $H_0 :$ is true, i.e., if two variables are
independent of each other, the expected frequency of the sample data
will be $n p_{i \cdot} p_{\cdot j}$. Since we do not know the population
$p_i$ and $p_j$, if we use the estimates of $\frac {T_{i \cdot}}{n}$ and
$\frac {T_{\cdot j}}{n}$, then the estimate of the expected frequency,
$E_{ij}$, is as follows: $$