-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchapter5.html
More file actions
2703 lines (2633 loc) · 221 KB
/
chapter5.html
File metadata and controls
2703 lines (2633 loc) · 221 KB
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 5 Chi-square tests | Intermediate Statistics with R</title>
<meta name="description" content="Chapter 5 Chi-square tests | Intermediate Statistics with R" />
<meta name="generator" content="bookdown 0.28 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 5 Chi-square tests | Intermediate Statistics with R" />
<meta property="og:type" content="book" />
<meta name="github-repo" content="gpeterson406/Greenwood_Book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 5 Chi-square tests | Intermediate Statistics with R" />
<meta name="author" content="Mark C Greenwood" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="chapter4.html"/>
<link rel="next" href="chapter6.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<style type="text/css">
/* Used with Pandoc 2.11+ new --citeproc when CSL is used */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" href="toc.css" type="text/css" />
<link rel="stylesheet" href="font-awesome.min.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">Intermediate Statistics with R</a></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Cover</a></li>
<li class="chapter" data-level="" data-path="acknowledgments.html"><a href="acknowledgments.html"><i class="fa fa-check"></i>Acknowledgments</a></li>
<li class="chapter" data-level="1" data-path="chapter1.html"><a href="chapter1.html"><i class="fa fa-check"></i><b>1</b> Preface</a>
<ul>
<li class="chapter" data-level="1.1" data-path="chapter1.html"><a href="chapter1.html#section1-1"><i class="fa fa-check"></i><b>1.1</b> Overview of methods</a></li>
<li class="chapter" data-level="1.2" data-path="chapter1.html"><a href="chapter1.html#section1-2"><i class="fa fa-check"></i><b>1.2</b> Getting started in R</a></li>
<li class="chapter" data-level="1.3" data-path="chapter1.html"><a href="chapter1.html#section1-3"><i class="fa fa-check"></i><b>1.3</b> Basic summary statistics, histograms, and boxplots using R</a></li>
<li class="chapter" data-level="1.4" data-path="chapter1.html"><a href="chapter1.html#section1-4"><i class="fa fa-check"></i><b>1.4</b> R Markdown</a></li>
<li class="chapter" data-level="1.5" data-path="chapter1.html"><a href="chapter1.html#section1-5"><i class="fa fa-check"></i><b>1.5</b> Grammar of Graphics</a></li>
<li class="chapter" data-level="1.6" data-path="chapter1.html"><a href="chapter1.html#section1-6"><i class="fa fa-check"></i><b>1.6</b> Exiting RStudio</a></li>
<li class="chapter" data-level="1.7" data-path="chapter1.html"><a href="chapter1.html#section1-7"><i class="fa fa-check"></i><b>1.7</b> Chapter summary</a></li>
<li class="chapter" data-level="1.8" data-path="chapter1.html"><a href="chapter1.html#section1-8"><i class="fa fa-check"></i><b>1.8</b> Summary of important R code</a></li>
<li class="chapter" data-level="1.9" data-path="chapter1.html"><a href="chapter1.html#section1-9"><i class="fa fa-check"></i><b>1.9</b> Practice problems</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="chapter2.html"><a href="chapter2.html"><i class="fa fa-check"></i><b>2</b> (R)e-Introduction to statistics</a>
<ul>
<li class="chapter" data-level="2.1" data-path="chapter2.html"><a href="chapter2.html#section2-1"><i class="fa fa-check"></i><b>2.1</b> Data wrangling and density curves</a></li>
<li class="chapter" data-level="2.2" data-path="chapter2.html"><a href="chapter2.html#section2-2"><i class="fa fa-check"></i><b>2.2</b> Pirate-plots</a></li>
<li class="chapter" data-level="2.3" data-path="chapter2.html"><a href="chapter2.html#section2-3"><i class="fa fa-check"></i><b>2.3</b> Models, hypotheses, and permutations for the two sample mean situation</a></li>
<li class="chapter" data-level="2.4" data-path="chapter2.html"><a href="chapter2.html#section2-4"><i class="fa fa-check"></i><b>2.4</b> Permutation testing for the two sample mean situation</a></li>
<li class="chapter" data-level="2.5" data-path="chapter2.html"><a href="chapter2.html#section2-5"><i class="fa fa-check"></i><b>2.5</b> Hypothesis testing (general)</a></li>
<li class="chapter" data-level="2.6" data-path="chapter2.html"><a href="chapter2.html#section2-6"><i class="fa fa-check"></i><b>2.6</b> Connecting randomization (nonparametric) and parametric tests</a></li>
<li class="chapter" data-level="2.7" data-path="chapter2.html"><a href="chapter2.html#section2-7"><i class="fa fa-check"></i><b>2.7</b> Second example of permutation tests</a></li>
<li class="chapter" data-level="2.8" data-path="chapter2.html"><a href="chapter2.html#section2-8"><i class="fa fa-check"></i><b>2.8</b> Reproducibility Crisis: Moving beyond p < 0.05, publication bias, and multiple testing issues</a></li>
<li class="chapter" data-level="2.9" data-path="chapter2.html"><a href="chapter2.html#section2-9"><i class="fa fa-check"></i><b>2.9</b> Confidence intervals and bootstrapping</a></li>
<li class="chapter" data-level="2.10" data-path="chapter2.html"><a href="chapter2.html#section2-10"><i class="fa fa-check"></i><b>2.10</b> Bootstrap confidence intervals for difference in GPAs</a></li>
<li class="chapter" data-level="2.11" data-path="chapter2.html"><a href="chapter2.html#section2-11"><i class="fa fa-check"></i><b>2.11</b> Chapter summary</a></li>
<li class="chapter" data-level="2.12" data-path="chapter2.html"><a href="chapter2.html#section2-12"><i class="fa fa-check"></i><b>2.12</b> Summary of important R code</a></li>
<li class="chapter" data-level="2.13" data-path="chapter2.html"><a href="chapter2.html#section2-13"><i class="fa fa-check"></i><b>2.13</b> Practice problems</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="chapter3.html"><a href="chapter3.html"><i class="fa fa-check"></i><b>3</b> One-Way ANOVA</a>
<ul>
<li class="chapter" data-level="3.1" data-path="chapter3.html"><a href="chapter3.html#section3-1"><i class="fa fa-check"></i><b>3.1</b> Situation</a></li>
<li class="chapter" data-level="3.2" data-path="chapter3.html"><a href="chapter3.html#section3-2"><i class="fa fa-check"></i><b>3.2</b> Linear model for One-Way ANOVA (cell means and reference-coding)</a></li>
<li class="chapter" data-level="3.3" data-path="chapter3.html"><a href="chapter3.html#section3-3"><i class="fa fa-check"></i><b>3.3</b> One-Way ANOVA Sums of Squares, Mean Squares, and F-test</a></li>
<li class="chapter" data-level="3.4" data-path="chapter3.html"><a href="chapter3.html#section3-4"><i class="fa fa-check"></i><b>3.4</b> ANOVA model diagnostics including QQ-plots</a></li>
<li class="chapter" data-level="3.5" data-path="chapter3.html"><a href="chapter3.html#section3-5"><i class="fa fa-check"></i><b>3.5</b> Guinea pig tooth growth One-Way ANOVA example</a></li>
<li class="chapter" data-level="3.6" data-path="chapter3.html"><a href="chapter3.html#section3-6"><i class="fa fa-check"></i><b>3.6</b> Multiple (pair-wise) comparisons using Tukey’s HSD and the compact letter display</a></li>
<li class="chapter" data-level="3.7" data-path="chapter3.html"><a href="chapter3.html#section3-7"><i class="fa fa-check"></i><b>3.7</b> Pair-wise comparisons for the Overtake data</a></li>
<li class="chapter" data-level="3.8" data-path="chapter3.html"><a href="chapter3.html#section3-8"><i class="fa fa-check"></i><b>3.8</b> Chapter summary</a></li>
<li class="chapter" data-level="3.9" data-path="chapter3.html"><a href="chapter3.html#section3-9"><i class="fa fa-check"></i><b>3.9</b> Summary of important R code</a></li>
<li class="chapter" data-level="3.10" data-path="chapter3.html"><a href="chapter3.html#section3-10"><i class="fa fa-check"></i><b>3.10</b> Practice problems</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="chapter4.html"><a href="chapter4.html"><i class="fa fa-check"></i><b>4</b> Two-Way ANOVA</a>
<ul>
<li class="chapter" data-level="4.1" data-path="chapter4.html"><a href="chapter4.html#section4-1"><i class="fa fa-check"></i><b>4.1</b> Situation</a></li>
<li class="chapter" data-level="4.2" data-path="chapter4.html"><a href="chapter4.html#section4-2"><i class="fa fa-check"></i><b>4.2</b> Designing a two-way experiment and visualizing results</a></li>
<li class="chapter" data-level="4.3" data-path="chapter4.html"><a href="chapter4.html#section4-3"><i class="fa fa-check"></i><b>4.3</b> Two-Way ANOVA models and hypothesis tests</a></li>
<li class="chapter" data-level="4.4" data-path="chapter4.html"><a href="chapter4.html#section4-4"><i class="fa fa-check"></i><b>4.4</b> Guinea pig tooth growth analysis with Two-Way ANOVA</a></li>
<li class="chapter" data-level="4.5" data-path="chapter4.html"><a href="chapter4.html#section4-5"><i class="fa fa-check"></i><b>4.5</b> Observational study example: The Psychology of Debt</a></li>
<li class="chapter" data-level="4.6" data-path="chapter4.html"><a href="chapter4.html#section4-6"><i class="fa fa-check"></i><b>4.6</b> Pushing Two-Way ANOVA to the limit: Un-replicated designs and Estimability</a></li>
<li class="chapter" data-level="4.7" data-path="chapter4.html"><a href="chapter4.html#section4-7"><i class="fa fa-check"></i><b>4.7</b> Chapter summary</a></li>
<li class="chapter" data-level="4.8" data-path="chapter4.html"><a href="chapter4.html#section4-8"><i class="fa fa-check"></i><b>4.8</b> Summary of important R code</a></li>
<li class="chapter" data-level="4.9" data-path="chapter4.html"><a href="chapter4.html#section4-9"><i class="fa fa-check"></i><b>4.9</b> Practice problems</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="chapter5.html"><a href="chapter5.html"><i class="fa fa-check"></i><b>5</b> Chi-square tests</a>
<ul>
<li class="chapter" data-level="5.1" data-path="chapter5.html"><a href="chapter5.html#section5-1"><i class="fa fa-check"></i><b>5.1</b> Situation, contingency tables, and tableplots</a></li>
<li class="chapter" data-level="5.2" data-path="chapter5.html"><a href="chapter5.html#section5-2"><i class="fa fa-check"></i><b>5.2</b> Homogeneity test hypotheses</a></li>
<li class="chapter" data-level="5.3" data-path="chapter5.html"><a href="chapter5.html#section5-3"><i class="fa fa-check"></i><b>5.3</b> Independence test hypotheses</a></li>
<li class="chapter" data-level="5.4" data-path="chapter5.html"><a href="chapter5.html#section5-4"><i class="fa fa-check"></i><b>5.4</b> Models for R by C tables</a></li>
<li class="chapter" data-level="5.5" data-path="chapter5.html"><a href="chapter5.html#section5-5"><i class="fa fa-check"></i><b>5.5</b> Permutation tests for the <span class="math inline">\(X^2\)</span> statistic</a></li>
<li class="chapter" data-level="5.6" data-path="chapter5.html"><a href="chapter5.html#section5-6"><i class="fa fa-check"></i><b>5.6</b> Chi-square distribution for the <span class="math inline">\(X^2\)</span> statistic</a></li>
<li class="chapter" data-level="5.7" data-path="chapter5.html"><a href="chapter5.html#section5-7"><i class="fa fa-check"></i><b>5.7</b> Examining residuals for the source of differences</a></li>
<li class="chapter" data-level="5.8" data-path="chapter5.html"><a href="chapter5.html#section5-8"><i class="fa fa-check"></i><b>5.8</b> General protocol for <span class="math inline">\(X^2\)</span> tests</a></li>
<li class="chapter" data-level="5.9" data-path="chapter5.html"><a href="chapter5.html#section5-9"><i class="fa fa-check"></i><b>5.9</b> Political party and voting results: Complete analysis</a></li>
<li class="chapter" data-level="5.10" data-path="chapter5.html"><a href="chapter5.html#section5-10"><i class="fa fa-check"></i><b>5.10</b> Is cheating and lying related in students?</a></li>
<li class="chapter" data-level="5.11" data-path="chapter5.html"><a href="chapter5.html#section5-11"><i class="fa fa-check"></i><b>5.11</b> Analyzing a stratified random sample of California schools</a></li>
<li class="chapter" data-level="5.12" data-path="chapter5.html"><a href="chapter5.html#section5-12"><i class="fa fa-check"></i><b>5.12</b> Chapter summary</a></li>
<li class="chapter" data-level="5.13" data-path="chapter5.html"><a href="chapter5.html#section5-13"><i class="fa fa-check"></i><b>5.13</b> Summary of important R code</a></li>
<li class="chapter" data-level="5.14" data-path="chapter5.html"><a href="chapter5.html#section5-14"><i class="fa fa-check"></i><b>5.14</b> Practice problems</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="chapter6.html"><a href="chapter6.html"><i class="fa fa-check"></i><b>6</b> Correlation and Simple Linear Regression</a>
<ul>
<li class="chapter" data-level="6.1" data-path="chapter6.html"><a href="chapter6.html#section6-1"><i class="fa fa-check"></i><b>6.1</b> Relationships between two quantitative variables</a></li>
<li class="chapter" data-level="6.2" data-path="chapter6.html"><a href="chapter6.html#section6-2"><i class="fa fa-check"></i><b>6.2</b> Estimating the correlation coefficient</a></li>
<li class="chapter" data-level="6.3" data-path="chapter6.html"><a href="chapter6.html#section6-3"><i class="fa fa-check"></i><b>6.3</b> Relationships between variables by groups</a></li>
<li class="chapter" data-level="6.4" data-path="chapter6.html"><a href="chapter6.html#section6-4"><i class="fa fa-check"></i><b>6.4</b> Inference for the correlation coefficient</a></li>
<li class="chapter" data-level="6.5" data-path="chapter6.html"><a href="chapter6.html#section6-5"><i class="fa fa-check"></i><b>6.5</b> Are tree diameters related to tree heights?</a></li>
<li class="chapter" data-level="6.6" data-path="chapter6.html"><a href="chapter6.html#section6-6"><i class="fa fa-check"></i><b>6.6</b> Describing relationships with a regression model</a></li>
<li class="chapter" data-level="6.7" data-path="chapter6.html"><a href="chapter6.html#section6-7"><i class="fa fa-check"></i><b>6.7</b> Least Squares Estimation</a></li>
<li class="chapter" data-level="6.8" data-path="chapter6.html"><a href="chapter6.html#section6-8"><i class="fa fa-check"></i><b>6.8</b> Measuring the strength of regressions: R<sup>2</sup></a></li>
<li class="chapter" data-level="6.9" data-path="chapter6.html"><a href="chapter6.html#section6-9"><i class="fa fa-check"></i><b>6.9</b> Outliers: leverage and influence</a></li>
<li class="chapter" data-level="6.10" data-path="chapter6.html"><a href="chapter6.html#section6-10"><i class="fa fa-check"></i><b>6.10</b> Residual diagnostics – setting the stage for inference</a></li>
<li class="chapter" data-level="6.11" data-path="chapter6.html"><a href="chapter6.html#section6-11"><i class="fa fa-check"></i><b>6.11</b> Old Faithful discharge and waiting times</a></li>
<li class="chapter" data-level="6.12" data-path="chapter6.html"><a href="chapter6.html#section6-12"><i class="fa fa-check"></i><b>6.12</b> Chapter summary</a></li>
<li class="chapter" data-level="6.13" data-path="chapter6.html"><a href="chapter6.html#section6-13"><i class="fa fa-check"></i><b>6.13</b> Summary of important R code</a></li>
<li class="chapter" data-level="6.14" data-path="chapter6.html"><a href="chapter6.html#section6-14"><i class="fa fa-check"></i><b>6.14</b> Practice problems</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="chapter7.html"><a href="chapter7.html"><i class="fa fa-check"></i><b>7</b> Simple linear regression inference</a>
<ul>
<li class="chapter" data-level="7.1" data-path="chapter7.html"><a href="chapter7.html#section7-1"><i class="fa fa-check"></i><b>7.1</b> Model</a></li>
<li class="chapter" data-level="7.2" data-path="chapter7.html"><a href="chapter7.html#section7-2"><i class="fa fa-check"></i><b>7.2</b> Confidence interval and hypothesis tests for the slope and intercept</a></li>
<li class="chapter" data-level="7.3" data-path="chapter7.html"><a href="chapter7.html#section7-3"><i class="fa fa-check"></i><b>7.3</b> Bozeman temperature trend</a></li>
<li class="chapter" data-level="7.4" data-path="chapter7.html"><a href="chapter7.html#section7-4"><i class="fa fa-check"></i><b>7.4</b> Randomization-based inferences for the slope coefficient</a></li>
<li class="chapter" data-level="7.5" data-path="chapter7.html"><a href="chapter7.html#section7-5"><i class="fa fa-check"></i><b>7.5</b> Transformations part I: Linearizing relationships</a></li>
<li class="chapter" data-level="7.6" data-path="chapter7.html"><a href="chapter7.html#section7-6"><i class="fa fa-check"></i><b>7.6</b> Transformations part II: Impacts on SLR interpretations: log(y), log(x), & both log(y) & log(x)</a></li>
<li class="chapter" data-level="7.7" data-path="chapter7.html"><a href="chapter7.html#section7-7"><i class="fa fa-check"></i><b>7.7</b> Confidence interval for the mean and prediction intervals for a new observation</a></li>
<li class="chapter" data-level="7.8" data-path="chapter7.html"><a href="chapter7.html#section7-8"><i class="fa fa-check"></i><b>7.8</b> Chapter summary</a></li>
<li class="chapter" data-level="7.9" data-path="chapter7.html"><a href="chapter7.html#section7-9"><i class="fa fa-check"></i><b>7.9</b> Summary of important R code</a></li>
<li class="chapter" data-level="7.10" data-path="chapter7.html"><a href="chapter7.html#section7-10"><i class="fa fa-check"></i><b>7.10</b> Practice problems</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="chapter8.html"><a href="chapter8.html"><i class="fa fa-check"></i><b>8</b> Multiple linear regression</a>
<ul>
<li class="chapter" data-level="8.1" data-path="chapter8.html"><a href="chapter8.html#section8-1"><i class="fa fa-check"></i><b>8.1</b> Going from SLR to MLR</a></li>
<li class="chapter" data-level="8.2" data-path="chapter8.html"><a href="chapter8.html#section8-2"><i class="fa fa-check"></i><b>8.2</b> Validity conditions in MLR</a></li>
<li class="chapter" data-level="8.3" data-path="chapter8.html"><a href="chapter8.html#section8-3"><i class="fa fa-check"></i><b>8.3</b> Interpretation of MLR terms</a></li>
<li class="chapter" data-level="8.4" data-path="chapter8.html"><a href="chapter8.html#section8-4"><i class="fa fa-check"></i><b>8.4</b> Comparing multiple regression models</a></li>
<li class="chapter" data-level="8.5" data-path="chapter8.html"><a href="chapter8.html#section8-5"><i class="fa fa-check"></i><b>8.5</b> General recommendations for MLR interpretations and VIFs</a></li>
<li class="chapter" data-level="8.6" data-path="chapter8.html"><a href="chapter8.html#section8-6"><i class="fa fa-check"></i><b>8.6</b> MLR inference: Parameter inferences using the t-distribution</a></li>
<li class="chapter" data-level="8.7" data-path="chapter8.html"><a href="chapter8.html#section8-7"><i class="fa fa-check"></i><b>8.7</b> Overall F-test in multiple linear regression</a></li>
<li class="chapter" data-level="8.8" data-path="chapter8.html"><a href="chapter8.html#section8-8"><i class="fa fa-check"></i><b>8.8</b> Case study: First year college GPA and SATs</a></li>
<li class="chapter" data-level="8.9" data-path="chapter8.html"><a href="chapter8.html#section8-9"><i class="fa fa-check"></i><b>8.9</b> Different intercepts for different groups: MLR with indicator variables</a></li>
<li class="chapter" data-level="8.10" data-path="chapter8.html"><a href="chapter8.html#section8-10"><i class="fa fa-check"></i><b>8.10</b> Additive MLR with more than two groups: Headache example</a></li>
<li class="chapter" data-level="8.11" data-path="chapter8.html"><a href="chapter8.html#section8-11"><i class="fa fa-check"></i><b>8.11</b> Different slopes and different intercepts</a></li>
<li class="chapter" data-level="8.12" data-path="chapter8.html"><a href="chapter8.html#section8-12"><i class="fa fa-check"></i><b>8.12</b> F-tests for MLR models with quantitative and categorical variables and interactions</a></li>
<li class="chapter" data-level="8.13" data-path="chapter8.html"><a href="chapter8.html#section8-13"><i class="fa fa-check"></i><b>8.13</b> AICs for model selection</a></li>
<li class="chapter" data-level="8.14" data-path="chapter8.html"><a href="chapter8.html#section8-14"><i class="fa fa-check"></i><b>8.14</b> Case study: Forced expiratory volume model selection using AICs</a></li>
<li class="chapter" data-level="8.15" data-path="chapter8.html"><a href="chapter8.html#section8-15"><i class="fa fa-check"></i><b>8.15</b> Chapter summary</a></li>
<li class="chapter" data-level="8.16" data-path="chapter8.html"><a href="chapter8.html#section8-16"><i class="fa fa-check"></i><b>8.16</b> Summary of important R code</a></li>
<li class="chapter" data-level="8.17" data-path="chapter8.html"><a href="chapter8.html#section8-17"><i class="fa fa-check"></i><b>8.17</b> Practice problems</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="chapter9.html"><a href="chapter9.html"><i class="fa fa-check"></i><b>9</b> Case studies</a>
<ul>
<li class="chapter" data-level="9.1" data-path="chapter9.html"><a href="chapter9.html#section9-1"><i class="fa fa-check"></i><b>9.1</b> Overview of material covered</a></li>
<li class="chapter" data-level="9.2" data-path="chapter9.html"><a href="chapter9.html#section9-2"><i class="fa fa-check"></i><b>9.2</b> The impact of simulated chronic nitrogen deposition on the biomass and N2-fixation activity of two boreal feather moss–cyanobacteria associations</a></li>
<li class="chapter" data-level="9.3" data-path="chapter9.html"><a href="chapter9.html#section9-3"><i class="fa fa-check"></i><b>9.3</b> Ants learn to rely on more informative attributes during decision-making</a></li>
<li class="chapter" data-level="9.4" data-path="chapter9.html"><a href="chapter9.html#section9-4"><i class="fa fa-check"></i><b>9.4</b> Multi-variate models are essential for understanding vertebrate diversification in deep time</a></li>
<li class="chapter" data-level="9.5" data-path="chapter9.html"><a href="chapter9.html#section9-5"><i class="fa fa-check"></i><b>9.5</b> What do didgeridoos really do about sleepiness?</a></li>
<li class="chapter" data-level="9.6" data-path="chapter9.html"><a href="chapter9.html#section9-6"><i class="fa fa-check"></i><b>9.6</b> General summary</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Intermediate Statistics with R</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="chapter5" class="section level1 hasAnchor" number="5">
<h1><span class="header-section-number">Chapter 5</span> Chi-square tests<a href="chapter5.html#chapter5" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<div id="section5-1" class="section level2 hasAnchor" number="5.1">
<h2><span class="header-section-number">5.1</span> Situation, contingency tables, and tableplots<a href="chapter5.html#section5-1" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>In this chapter, the focus shifts briefly from analyzing quantitative
response variables to methods
for handling categorical response variables. This is important because in some
situations it is not possible to measure the response variable quantitatively.
For example, we will analyze the results from a clinical trial where the results
for the subjects were measured as one of three categories: <em>no improvement</em>,
<em>some improvement</em>, and <em>marked improvement</em>. While that type of response
could be treated as
numerical, coded possibly as 1, 2, and 3, it would be difficult to assume that
the responses such as those follow a normal distribution since they are
<strong><em>discrete</em></strong> (not continuous, measured at whole number values only) and,
more importantly,
the difference between <em>no improvement</em> and <em>some improvement</em> is not
necessarily the same as the difference between <em>some</em> and <em>marked improvement</em>.
If it is treated numerically, then the differences between levels are assumed to be the same
unless a different coding scheme is used (say 1, 2, and 5). It is better to
treat this type of responses as being in one of the three categories and use
statistical methods that don’t make unreasonable and arbitrary assumptions about what the
numerical coding might mean. The study being performed here involved subjects
randomly assigned to either a treatment or a placebo (control) group and we
want to address research questions similar to those considered in
Chapters <a href="chapter2.html#chapter2">2</a> and <a href="chapter3.html#chapter3">3</a> –
assessing differences in a response variable among two or more groups. With quantitative
responses, the differences in the distributions are parameterized via the means
of the groups and we used linear models. With
categorical responses, the focus is on the probabilities of getting responses in
each category and whether they differ among the groups.</p>
<p>We start with some useful summary techniques, both numerical and graphical,
applied to some examples of
studies these methods can be used to analyze. Graphical techniques provide
opportunities for assessing specific patterns in variables, relationships
between variables, and for generally understanding the responses obtained.
There are many different types of plots and each can elucidate certain features
of data. The <em>tableplot</em>, briefly introduced<a href="#fn98" class="footnote-ref" id="fnref98"><sup>98</sup></a> in Chapter <a href="chapter4.html#chapter4">4</a>, is a great and often fun starting point for working with data sets that contain categorical variables. We will start here with using it to help us
understand some aspects of the results from a double-blind randomized clinical
trial investigating a treatment for rheumatoid arthritis.
These data are available
in the <code>Arthritis</code> data set available in the <code>vcd</code> package <span class="citation">(<a href="#ref-R-vcd" role="doc-biblioref">D. Meyer, Zeileis, and Hornik 2022</a>)</span>.
There were <span class="math inline">\(n = 84\)</span> subjects, with some demographic
information recorded
along with the <code>Treatment</code> status (<em>Treated</em>, <em>Placebo</em>) and whether the
patients’ arthritis symptoms <code>Improved</code> (with levels of <em>None</em>, <em>Some</em>,
and <em>Marked</em>). When using <code>tableplot</code>, we may
not want to display everything in the tibble and can just select some
of the variables. We use <code>Treatment</code>, <code>Improved</code>, <code>Gender</code>, and <code>Age</code>
in the <code>select = ...</code> option with a <code>c()</code> and commas between the names of
the variables we want to display as shown below. The first one in the list is also the one that
the data are sorted on and is what we want here – to start with sorting observations based on <code>Treatment</code> status.</p>
<div class="sourceCode" id="cb404"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb404-1"><a href="chapter5.html#cb404-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(vcd)</span>
<span id="cb404-2"><a href="chapter5.html#cb404-2" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(Arthritis) <span class="co">#Double-blind clinical trial with treatment and control groups</span></span>
<span id="cb404-3"><a href="chapter5.html#cb404-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tibble)</span>
<span id="cb404-4"><a href="chapter5.html#cb404-4" aria-hidden="true" tabindex="-1"></a>Arthritis <span class="ot"><-</span> <span class="fu">as_tibble</span>(Arthritis)</span>
<span id="cb404-5"><a href="chapter5.html#cb404-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Homogeneity example</span></span>
<span id="cb404-6"><a href="chapter5.html#cb404-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tabplot)</span>
<span id="cb404-7"><a href="chapter5.html#cb404-7" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(RColorBrewer)</span>
<span id="cb404-8"><a href="chapter5.html#cb404-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Options needed to (sometimes) prevent errors on PC</span></span>
<span id="cb404-9"><a href="chapter5.html#cb404-9" aria-hidden="true" tabindex="-1"></a><span class="co"># options(ffbatchbytes = 1024^2 * 128); options(ffmaxbytes = 1024^2 * 128 * 32) </span></span>
<span id="cb404-10"><a href="chapter5.html#cb404-10" aria-hidden="true" tabindex="-1"></a><span class="fu">tableplot</span>(Arthritis, <span class="at">select =</span> <span class="fu">c</span>(Treatment, Improved, Sex, Age), <span class="at">pals =</span> <span class="fu">list</span>(<span class="st">"BrBG"</span>), </span>
<span id="cb404-11"><a href="chapter5.html#cb404-11" aria-hidden="true" tabindex="-1"></a> <span class="at">sample =</span> F, <span class="at">colorNA_num =</span> <span class="st">"orange"</span>, <span class="at">numMode =</span> <span class="st">"MB-ML"</span>)</span></code></pre></div>
<div class="figure" style="text-align: center"><span style="display:block;" id="fig:Figure5-1"></span>
<img src="05-chiSquaredTests_files/figure-html/Figure5-1-1.png" alt="Tableplot of the arthritis data set." width="75%" />
<p class="caption">
Figure 5.1: Tableplot of the arthritis data set.
</p>
</div>
<p>The first thing we can gather from Figure <a href="chapter5.html#fig:Figure5-1">5.1</a> is that there
are no red cells so there were no missing
observations in the data set. Missing observations regularly arise in real
studies when observations are not obtained for many different reasons and it is
always good to check for missing data issues – this plot provides a quick visual
method for doing that check. Primarily we are interested in whether the
treatment led to a different pattern (or rates) of improvement responses. There
seems to be more light (<em>Marked</em>) improvement responses in the treatment
group and more dark (<em>None</em>) responses in the placebo group.
This sort of plot also helps us to simultaneously consider the role of other
variables in the observed responses. You can see the sex of each subject in the
vertical panel for <code>Sex</code> and it seems
that there is a relatively balanced mix of males and females in the
treatment/placebo groups. Quantitative variables are also displayed with
horizontal bars corresponding to the responses (the x-axis provides the units of the responses, here in years). From the panel for
<code>Age</code>, we can see that the ages of subjects ranged from the 20s to 70s
and that there is no clear difference in
the ages between the treated and placebo groups. If, for example, all the male
subjects had ended up being randomized into the treatment group, then we might
have worried about whether sex and treatment were confounded and whether any
differences in the responses might be due to sex instead of the treatment. The
random assignment of treatment/placebo to the subjects appears to have been
successful here in generating a mix of ages and sexes among the
two treatment groups<a href="#fn99" class="footnote-ref" id="fnref99"><sup>99</sup></a>. The main benefit of this sort of plot is the ability to
visualize more than two categorical variables simultaneously. But now we want
to focus more directly on the researchers’ main question – does the treatment
lead to different improvement outcomes than the placebo?</p>
<p>To directly assess the effects of the treatment, we
want to display just the two variables of interest. <strong><em>Stacked bar charts</em></strong>
provide a method of displaying the response patterns (in <code>Improved</code>) across
the levels of a predictor variable (<code>Treatment</code>) by displaying a bar for each
predictor variable level and the proportions of responses in each category of
the response in each of those groups. If the placebo is as effective as the
treatment, then we would expect similar proportions of responses in each
improvement category. A difference in the effectiveness would manifest in
different proportions in the different improvement categories between <em>Treated</em>
and <em>Placebo</em>. To get information in this direction, we start with
obtaining the counts in each combination of categories using the <code>tally</code>
function to generate contingency tables.
<strong><em>Contingency tables</em></strong> with
<strong><em>R</em></strong> rows and <strong><em>C</em></strong> columns (called <strong><em>R by C tables</em></strong>) summarize
the counts of observations in each combination of the explanatory and
response variables.
In these data, there are <span class="math inline">\(R = 2\)</span> rows and <span class="math inline">\(C = 3\)</span> columns
making a <span class="math inline">\(2\times 3\)</span> table – note that you do not count the row
and column for the “Totals” in defining the size of the table. In the table,
there seems to be many more <em>Marked</em> improvement responses (21 vs 7) and
fewer <em>None</em> responses (13 vs 29) in the treated group compared to the
placebo group.</p>
<div class="sourceCode" id="cb405"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb405-1"><a href="chapter5.html#cb405-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(mosaic)</span>
<span id="cb405-2"><a href="chapter5.html#cb405-2" aria-hidden="true" tabindex="-1"></a><span class="fu">tally</span>(<span class="sc">~</span> Treatment <span class="sc">+</span> Improved, <span class="at">data =</span> Arthritis, <span class="at">margins =</span> T)</span></code></pre></div>
<pre><code>## Improved
## Treatment None Some Marked Total
## Placebo 29 7 7 43
## Treated 13 7 21 41
## Total 42 14 28 84</code></pre>
<p>Using the <code>tally</code> function with <code>~ x + y</code> provides a contingency table with
the <code>x</code> variable on the rows and the <code>y</code> variable on the columns, with
<code>margins = T</code> as an option so we can obtain the totals along the rows,
columns, and table total of <span class="math inline">\(N = 84\)</span>.
In general, contingency tables contain
the counts <span class="math inline">\(n_{rc}\)</span> in the <span class="math inline">\(r^{th}\)</span> row and <span class="math inline">\(c^{th}\)</span> column where
<span class="math inline">\(r = 1,\ldots,R\)</span> and <span class="math inline">\(c = 1,\ldots,C\)</span>. We can also define the <strong><em>row totals</em></strong>
as the sum across the columns of the counts in row <span class="math inline">\(r\)</span> as</p>
<p><span class="math display">\[\mathbf{n_{r\bullet}} = \Sigma^C_{c = 1}n_{rc},\]</span></p>
<p>the <strong><em>column totals</em></strong> as the sum across the rows for the counts in column <span class="math inline">\(c\)</span> as</p>
<p><span class="math display">\[\mathbf{n_{\bullet c}} = \Sigma^R_{r = 1}n_{rc},\]</span></p>
<p>and the <strong><em>table total</em></strong> as</p>
<p><span class="math display">\[\mathbf{N} = \Sigma^R_{r = 1}\mathbf{n_{r\bullet}} = \Sigma^C_{c = 1}\mathbf{n_{\bullet c}}
= \Sigma^R_{r = 1}\Sigma^C_{c = 1}\mathbf{n_{rc}}.\]</span></p>
<p>We’ll need these quantities to do some calculations in a bit. A generic
contingency table with added row, column,
and table totals just like the previous result from the <code>tally</code>
function is provided in Table <a href="chapter5.html#tab:Table5-1">5.1</a>.
</p>
<div style="page-break-after: always;"></div>
<table>
<caption><span id="tab:Table5-1">Table 5.1: </span> General notation for counts in an <em>R</em> by <em>C</em> contingency table.</caption>
<colgroup>
<col width="7%" />
<col width="17%" />
<col width="17%" />
<col width="17%" />
<col width="5%" />
<col width="17%" />
<col width="17%" />
</colgroup>
<thead>
<tr class="header">
<th align="center"> </th>
<th align="center"><strong>Response Level 1</strong></th>
<th align="center"><strong>Response Level 2</strong></th>
<th align="center"><strong>Response Level 3</strong></th>
<th align="center"><strong>…</strong></th>
<th align="center"><strong>Response Level C</strong></th>
<th align="center"><strong>Totals</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="center"><strong>Group 1</strong></td>
<td align="center"><span class="math inline">\(n_{11}\)</span></td>
<td align="center"><span class="math inline">\(n_{12}\)</span></td>
<td align="center"><span class="math inline">\(n_{13}\)</span></td>
<td align="center">…</td>
<td align="center"><span class="math inline">\(n_{1C}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{n_{1 \bullet}}\)</span></td>
</tr>
<tr class="even">
<td align="center"><strong>Group 2</strong></td>
<td align="center"><span class="math inline">\(n_{21}\)</span></td>
<td align="center"><span class="math inline">\(n_{22}\)</span></td>
<td align="center"><span class="math inline">\(n_{23}\)</span></td>
<td align="center">…</td>
<td align="center"><span class="math inline">\(n_{2C}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{n_{2 \bullet}}\)</span></td>
</tr>
<tr class="odd">
<td align="center"><strong>…</strong></td>
<td align="center">…</td>
<td align="center">…</td>
<td align="center">…</td>
<td align="center">…</td>
<td align="center">…</td>
<td align="center"><strong>…</strong></td>
</tr>
<tr class="even">
<td align="center"><strong>Group R</strong></td>
<td align="center"><span class="math inline">\(n_{R1}\)</span></td>
<td align="center"><span class="math inline">\(n_{R2}\)</span></td>
<td align="center"><span class="math inline">\(n_{R3}\)</span></td>
<td align="center">…</td>
<td align="center"><span class="math inline">\(n_{RC}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{n_{R \bullet}}\)</span></td>
</tr>
<tr class="odd">
<td align="center"><strong>Totals</strong></td>
<td align="center"><span class="math inline">\(\boldsymbol{n_{\bullet 1}}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{n_{\bullet 2}}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{n_{\bullet 3}}\)</span></td>
<td align="center">…</td>
<td align="center"><span class="math inline">\(\boldsymbol{n_{\bullet C}}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{N}\)</span></td>
</tr>
</tbody>
</table>
<p>Comparing counts from the contingency table is useful, but comparing proportions
in each category is better, especially when the sample sizes in the levels of
the explanatory variable differ. Switching the formula used in the <code>tally</code>
function formula to <code>~ y | x</code> and adding the <code>format = "proportion"</code>
option provides the proportions in the response categories conditional on the
category of the predictor (these are
called <strong><em>conditional proportions</em></strong> or the <strong><em>conditional distribution</em></strong> of,
here, <em>Improved</em> on <em>Treatment</em>)<a href="#fn100" class="footnote-ref" id="fnref100"><sup>100</sup></a>.
Note that they sum to 1.0 in each level of x, <em>placebo</em> or <em>treated</em>:</p>
<div class="sourceCode" id="cb407"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb407-1"><a href="chapter5.html#cb407-1" aria-hidden="true" tabindex="-1"></a><span class="fu">tally</span>(<span class="sc">~</span> Improved <span class="sc">|</span> Treatment, <span class="at">data =</span> Arthritis, <span class="at">format =</span> <span class="st">"proportion"</span>, <span class="at">margins =</span> T)</span></code></pre></div>
<pre><code>## Treatment
## Improved Placebo Treated
## None 0.6744186 0.3170732
## Some 0.1627907 0.1707317
## Marked 0.1627907 0.5121951
## Total 1.0000000 1.0000000</code></pre>
<p>This version of the <code>tally</code> result switches the variables between the rows and columns from the
first summary of the data but the single
“Total” row makes it clear to read the proportions down the columns in this
version of the table.
In this application, it shows how the proportions seem to be different among categories of <em>Improvement</em> between the placebo and treatment groups. This matches the previous thoughts on
these data, but now a difference of marked improvement of 16% vs 51% is more
clearly a big difference. We can also display this result using a
<strong><em>stacked bar chart</em></strong><a href="#fn101" class="footnote-ref" id="fnref101"><sup>101</sup></a> that displays the same information using the <code>plot</code>
function with a <code>y ~ x</code> formula:</p>
<div class="sourceCode" id="cb409"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb409-1"><a href="chapter5.html#cb409-1" aria-hidden="true" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mai =</span> <span class="fu">c</span>(<span class="fl">1.5</span>,<span class="fl">1.5</span>,<span class="fl">0.82</span>,<span class="fl">0.42</span>), <span class="co">#Adds extra space to bottom and left margin,</span></span>
<span id="cb409-2"><a href="chapter5.html#cb409-2" aria-hidden="true" tabindex="-1"></a> <span class="at">las =</span> <span class="dv">2</span>, <span class="co">#Rotates text labels, optional code</span></span>
<span id="cb409-3"><a href="chapter5.html#cb409-3" aria-hidden="true" tabindex="-1"></a> <span class="at">mgp =</span> <span class="fu">c</span>(<span class="dv">6</span>,<span class="dv">1</span>,<span class="dv">0</span>)) <span class="co">#Adds space to labels, order is axis label, tick label, tick mark</span></span>
<span id="cb409-4"><a href="chapter5.html#cb409-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(Improved <span class="sc">~</span> Treatment, <span class="at">data =</span> Arthritis,</span>
<span id="cb409-5"><a href="chapter5.html#cb409-5" aria-hidden="true" tabindex="-1"></a> <span class="at">main =</span> <span class="st">"Stacked Bar Chart of Arthritis Data"</span>)</span></code></pre></div>
<div class="figure" style="text-align: center"><span style="display:block;" id="fig:Figure5-2"></span>
<img src="05-chiSquaredTests_files/figure-html/Figure5-2-1.png" alt="Stacked bar chart of Arthritis data. The left bar is for the Placebo group and the right bar is for the Treated group. The width of the bars is based on relative size of each group and the portion of the total height of each shaded area is the proportion of that group in each category. The lightest shading is for “none”, medium shading for “some”, and the darkest shading for “marked”, as labeled on the y-axis." width="75%" />
<p class="caption">
Figure 5.2: Stacked bar chart of Arthritis data. The left bar is for the Placebo group and the right bar is for the Treated group. The width of the bars is based on relative size of each group and the portion of the total height of each shaded area is the proportion of that group in each category. The lightest shading is for “none”, medium shading for “some”, and the darkest shading for “marked”, as labeled on the y-axis.
</p>
</div>
<p>The stacked bar chart in Figure <a href="chapter5.html#fig:Figure5-2">5.2</a> displays the previous
conditional proportions for the groups, with
the same relatively clear difference between the groups persisting. If you run
the <code>plot</code> function with variables that are
coded numerically, it will make a very different looking graph (R is smart!) so
again be careful that you are instructing R to treat your variables as
categorical if they really are categorical. R is powerful but can’t read your
mind!</p>
<p>In this chapter, we analyze data collected in two different fashions and
modify the hypotheses to
reflect the differences in the data collection processes, choosing either
between what are called Homogeneity and Independence tests. The previous
situation where levels of a treatment are randomly assigned to the subjects in
a study describes the situation for what is called a <strong><em>Homogeneity Test</em></strong>.
Homogeneity also applies when random samples are taken from each population of
interest to generate the observations in each group of the explanatory variable based on the population groups.
These sorts of situations resemble many of the examples from
Chapter <a href="chapter3.html#chapter3">3</a> where treatments were assigned to subjects. The other
situation considered is where a
single sample is collected to represent a population and then a contingency
table is formed based on responses on two categorical variables.
When one
sample is collected and analyzed using a contingency table, the appropriate
analysis is called a Chi-square test of <strong><em>Independence</em></strong> or <strong><em>Association</em></strong>.
In this
situation, it is not necessary to have variables that are clearly classified
as explanatory or response although it is certainly possible. Data that often
align with Independence testing are collected using surveys of
subjects randomly selected from a single, large population. An example,
analyzed below, involves a survey of voters and whether their party affiliation
is related to who they voted for – the Republican, Democrat, or other
candidate. There is clearly an explanatory variable of the <em>Party affiliation</em>
but a single large sample was taken from the population of all likely voters
so the Independence test needs to be applied.
Another example where Independence is appropriate involves a study of student
cheating behavior. Again, a single sample was taken from the population of
students at a university and this determines that it will be an Independence
test. Students responded to questions about lying to get out of turning in a
paper and/or taking an exam (<em>none</em>, <em>either</em>, or <em>both</em>) and copying on an
exam and/or turning in a paper written by
someone else (<em>neither</em>, <em>either</em>, or <em>both</em>). In this situation, it is not
clear which variable is response or explanatory (which should explain the other) and it does not matter
with the Independence testing framework.
Figure <a href="chapter5.html#fig:Figure5-3">5.3</a> contains
a diagram of the
data collection processes and can help you to identify the appropriate analysis
situation.</p>
<div class="figure" style="text-align: center"><span style="display:block;" id="fig:Figure5-3"></span>
<img src="chapter5_files/image027.png" alt="Diagram of the scenarios involved in Homogeneity and Independence tests. Homogeneity testing involves R random samples or subjects assigned to R groups. Independence testing involves a single random sample and measurements on two categorical variables." width="75%" />
<p class="caption">
Figure 5.3: Diagram of the scenarios involved in Homogeneity and Independence tests. Homogeneity testing involves R random samples or subjects assigned to R groups. Independence testing involves a single random sample and measurements on two categorical variables.
</p>
</div>
<p>You will discover that the test statistics are the same for both methods,
which can create some desire
to assume that the differences in the data collection don’t matter. In
Homogeneity designs, the sample size in each group
<span class="math inline">\((\mathbf{n_{1\bullet}},\mathbf{n_{2\bullet},\ldots,\mathbf{n_{R\bullet}}})\)</span>
is fixed (researcher chooses the size of each group).
In Independence situations, the total sample size <span class="math inline">\(\mathbf{N}\)</span> is
fixed but all the <span class="math inline">\(\mathbf{n_{r\bullet}}\text{'s}\)</span> are random (we need the data set to know how many are in each group).
These
differences impact the graphs, hypotheses, and conclusions used even though
the test statistics and p-values are calculated the same way – so we only
need to learn one test statistic to handle the two situations, but we need
to make sure we know which we’re doing!</p>
</div>
<div id="section5-2" class="section level2 hasAnchor" number="5.2">
<h2><span class="header-section-number">5.2</span> Homogeneity test hypotheses<a href="chapter5.html#section5-2" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>If we define some additional notation, we can then define hypotheses that allow us
to assess evidence related to whether the treatment “matters” in Homogeneity
situations.
This situation is similar to what we did in the One-Way ANOVA (Chapter <a href="chapter3.html#chapter3">3</a>)
situation with quantitative responses but the parameters now
relate to proportions in the response variable categories across the groups.
First we can define the conditional population proportions in level <span class="math inline">\(c\)</span> (column
<span class="math inline">\(c = 1,\ldots,C\)</span>) of group <span class="math inline">\(r\)</span> (row <span class="math inline">\(r = 1,\ldots,R\)</span>) as <span class="math inline">\(p_{rc}\)</span>.
Table <a href="chapter5.html#tab:Table5-2">5.2</a> shows the proportions, noting that the proportions
in each row sum to 1 since they are conditional on the group of
interest. A <strong><em>transposed</em></strong> (rows and columns flipped) version of this table is
produced by the <code>tally</code> function if you use the formula <code>~ y | x</code>.
</p>
<table>
<caption><span id="tab:Table5-2">Table 5.2: </span> Table of conditional proportions in the Homogeneity testing scenario.</caption>
<colgroup>
<col width="8%" />
<col width="18%" />
<col width="18%" />
<col width="18%" />
<col width="5%" />
<col width="18%" />
<col width="12%" />
</colgroup>
<thead>
<tr class="header">
<th align="center"> </th>
<th align="center"><strong>Response Level 1</strong></th>
<th align="center"><strong>Response Level 2</strong></th>
<th align="center"><strong>Response Level 3</strong></th>
<th align="center"><strong>…</strong></th>
<th align="center"><strong>Response Level C</strong></th>
<th align="center"><strong>Totals</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="center"><strong>Group 1</strong></td>
<td align="center"><span class="math inline">\(p_{11}\)</span></td>
<td align="center"><span class="math inline">\(p_{12}\)</span></td>
<td align="center"><span class="math inline">\(p_{13}\)</span></td>
<td align="center">…</td>
<td align="center"><span class="math inline">\(p_{1C}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{1.0}\)</span></td>
</tr>
<tr class="even">
<td align="center"><strong>Group 2</strong></td>
<td align="center"><span class="math inline">\(p_{21}\)</span></td>
<td align="center"><span class="math inline">\(p_{22}\)</span></td>
<td align="center"><span class="math inline">\(p_{23}\)</span></td>
<td align="center">…</td>
<td align="center"><span class="math inline">\(p_{2C}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{1.0}\)</span></td>
</tr>
<tr class="odd">
<td align="center"><strong>…</strong></td>
<td align="center">…</td>
<td align="center">…</td>
<td align="center">…</td>
<td align="center">…</td>
<td align="center">…</td>
<td align="center"><strong>…</strong></td>
</tr>
<tr class="even">
<td align="center"><strong>Group R</strong></td>
<td align="center"><span class="math inline">\(p_{R1}\)</span></td>
<td align="center"><span class="math inline">\(p_{R2}\)</span></td>
<td align="center"><span class="math inline">\(p_{R3}\)</span></td>
<td align="center">…</td>
<td align="center"><span class="math inline">\(p_{RC}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{1.0}\)</span></td>
</tr>
<tr class="odd">
<td align="center"><strong>Totals</strong></td>
<td align="center"><span class="math inline">\(\boldsymbol{p_{\bullet 1}}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{n_{\bullet 2}}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{p_{\bullet 3}}\)</span></td>
<td align="center">…</td>
<td align="center"><span class="math inline">\(\boldsymbol{p_{\bullet C}}\)</span></td>
<td align="center"><span class="math inline">\(\boldsymbol{1.0}\)</span></td>
</tr>
</tbody>
</table>
<p>In the Homogeneity situation, the null hypothesis is that the distributions are the same in all
the <span class="math inline">\(R\)</span> populations.
This means that the null hypothesis is:</p>
<!-- \newpage -->
<p><span class="math display">\[\begin{array}{rl}
\mathbf{H_0:}\ & \mathbf{p_{11} = p_{21} = \ldots = p_{R1}} \textbf{ and } \mathbf{p_{12} = p_{22} = \ldots = p_{R2}} \textbf{ and } \mathbf{p_{13} = p_{23} = \ldots = p_{R3}} \\
& \textbf{ and } \mathbf{\ldots} \textbf{ and }\mathbf{p_{1C} = p_{2C} = \ldots = p_{RC}}. \\
\end{array}\]</span></p>
<!-- \newpage -->
<p>If all the groups are the same, then they all have the same conditional proportions and we can
more simply write the null hypothesis as:</p>
<p><span class="math display">\[\mathbf{H_0:(p_{r1},p_{r2},\ldots,p_{rC}) = (p_1,p_2,\ldots,p_C)} \textbf{ for all } \mathbf{r}.\]</span></p>
<p>In other words, the pattern of proportions across the columns are <strong>the same for all the</strong>
<span class="math inline">\(\mathbf{R}\)</span> <strong>groups</strong>. The alternative is that there is some difference in the proportions
of at least one
response category for at least one group. In slightly more gentle and easier to
reproduce words, equivalently, we can say:</p>
<ul>
<li><span class="math inline">\(\mathbf{H_0:}\)</span> <strong>The population distributions of the responses for variable</strong> <span class="math inline">\(\mathbf{y}\)</span>
<strong>are the same across the</strong> <span class="math inline">\(\mathbf{R}\)</span> <strong>groups</strong>.</li>
</ul>
<p>The alternative hypothesis is then:</p>
<ul>
<li><span class="math inline">\(\mathbf{H_A:}\)</span> <strong>The population distributions of the responses for variable</strong> <span class="math inline">\(\mathbf{y}\)</span>
<strong>are NOT ALL the same across the</strong> <span class="math inline">\(\mathbf{R}\)</span> <strong>groups</strong>.</li>
</ul>
<p>To make this concrete, consider what the proportions could look like if they satisfied
the null hypothesis for the <em>Arthritis</em> example, as displayed in Figure <a href="chapter5.html#fig:Figure5-4">5.4</a>. Stacked bar charts provide a natural way to visualize the null hypothesis (equal distributions) to compare to the observed proportions in the observed data. <strong>Stacked bar charts are the appropriate visual display to present the summarized data in homogeneity test situations.</strong></p>
<div class="figure" style="text-align: center"><span style="display:block;" id="fig:Figure5-4"></span>
<img src="05-chiSquaredTests_files/figure-html/Figure5-4-1.png" alt="Stacked bar chart of one way that the Arthritis proportions could have been if the null hypothesis had been true." width="75%" />
<p class="caption">
Figure 5.4: Stacked bar chart of one way that the Arthritis proportions could have been if the null hypothesis had been true.
</p>
</div>
<p>Note that the proportions in the different response categories do not need to be the
same just that the distribution needs
to be the same across the groups. The null hypothesis does <em>not</em>
require that all three response categories (<em>none</em>, <em>some</em>, <em>marked</em>) be equally
likely. It assumes
that whatever the distribution of proportions is across these three levels of the response that
there is no difference in that distribution between the explanatory variable
(here treated/placebo) groups. Figure <a href="chapter5.html#fig:Figure5-4">5.4</a> shows an example of a
situation where
the null hypothesis is true and the distributions of responses across the
groups look the same but the proportions for <em>none</em>, <em>some</em> and <em>marked</em> are
not all equally likely. That
situation satisfies the null hypothesis. Compare this plot to the one for the
real data set in Figure <a href="chapter5.html#fig:Figure5-2">5.2</a>. It looks like there might be
some differences in
the responses between the treated and placebo groups as that plot looks much
different from this one, but we will need a test statistic and a p-value to
fully address the evidence relative to the previous null hypothesis.</p>
</div>
<div id="section5-3" class="section level2 hasAnchor" number="5.3">
<h2><span class="header-section-number">5.3</span> Independence test hypotheses<a href="chapter5.html#section5-3" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>When we take a single random sample of size <span class="math inline">\(N\)</span> and make a contingency
table, our inferences relate to whether there is a relationship or
<strong><em>association</em></strong> (that they are not independent) between the variables.
This is related to whether the distributions of proportions match
across rows in the table but is a more general question since we do not
need to determine a variable to condition on, one that takes on the role
of an explanatory variable, from the two variables of interest. In general,
the hypotheses for an Independence test for variables <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> are:
</p>
<ul>
<li><p><span class="math inline">\(\mathbf{H_0}\)</span>: <strong>There is no relationship between</strong> <span class="math inline">\(\mathbf{x}\)</span> <strong>and</strong>
<span class="math inline">\(\mathbf{y}\)</span> <strong>in the population.</strong></p>
<ul>
<li>Or: <span class="math inline">\(H_0\)</span>: <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> are independent in the population.</li>
</ul></li>
<li><p><span class="math inline">\(\mathbf{H_A}\)</span>: <strong>There is a relationship between</strong> <span class="math inline">\(\mathbf{x}\)</span> <strong>and</strong>
<span class="math inline">\(\mathbf{y}\)</span> <strong>in the population.</strong></p>
<ul>
<li>Or: <span class="math inline">\(H_A\)</span>: <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> are dependent in the population.</li>
</ul></li>
</ul>
<p>To illustrate a test of independence, consider an example involving
data from a national random sample
taken prior to the 2000 U.S. elections from the data set <code>election</code>
from the package <code>poLCA</code> (<span class="citation">Linzer and Lewis. (<a href="#ref-R-poLCA" role="doc-biblioref">2022</a>)</span>, <span class="citation">Linzer and Lewis (<a href="#ref-Linzer2011" role="doc-biblioref">2011</a>)</span>). Each respondent’s
democratic-republican partisan identification was collected,
provided in the <code>PARTY</code> variable for measurements on a seven-point
scale from (1) <em>Strong Democrat</em>, (2) <em>Weak Democrat</em>,
(3) <em>Independent-Democrat</em>, (4) <em>Independent-Independent</em>,
(5) <em>Independent-Republican</em>, (6) <em>Weak Republican</em>, to
(7) <em>Strong Republican</em>. The <code>VOTEF</code> variable that is created below
will contain the candidate that the participants voted for (the data set was
originally coded with 1, 2, and 3 for the candidates and we replaced those
<code>levels</code> with the candidate names). The contingency table shows some expected
results, that individuals with strong party affiliations tend to vote for the
party nominee with strong support for Gore in the Democrats
(<code>PARTY</code> = 1 and 2) and strong support for Bush in the Republicans
(<code>PARTY</code> = 6 and 7). As always, we want to support our explorations with
statistical inferences, here with the potential to extend inferences to
the overall population of
voters. The inferences in an independence test are related to whether there is a
relationship between the two variables in the population.
A <strong><em>relationship</em></strong> between variables occurs when knowing the level of
one variable for a person,
say that they voted for Gore, informs the types of responses that you would
expect for that person, here that they are likely affiliated with the Democratic
Party. When there is no relationship (the null hypothesis here), knowing the
level of one variable is not informative about the level of the other variable.</p>
<div class="sourceCode" id="cb410"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb410-1"><a href="chapter5.html#cb410-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(poLCA)</span>
<span id="cb410-2"><a href="chapter5.html#cb410-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 2000 Survey - use package = "" because other data sets in R have same name</span></span>
<span id="cb410-3"><a href="chapter5.html#cb410-3" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(election, <span class="at">package =</span> <span class="st">"poLCA"</span>) </span>
<span id="cb410-4"><a href="chapter5.html#cb410-4" aria-hidden="true" tabindex="-1"></a>election <span class="ot"><-</span> <span class="fu">as_tibble</span>(election)</span>
<span id="cb410-5"><a href="chapter5.html#cb410-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Subset variables and remove missing values</span></span>
<span id="cb410-6"><a href="chapter5.html#cb410-6" aria-hidden="true" tabindex="-1"></a>election2 <span class="ot"><-</span> election <span class="sc">%>%</span> </span>
<span id="cb410-7"><a href="chapter5.html#cb410-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(PARTY, VOTE3) <span class="sc">%>%</span></span>
<span id="cb410-8"><a href="chapter5.html#cb410-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">VOTEF =</span> <span class="fu">factor</span>(VOTE3)) <span class="sc">%>%</span></span>
<span id="cb410-9"><a href="chapter5.html#cb410-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">drop_na</span>()</span>
<span id="cb410-10"><a href="chapter5.html#cb410-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb410-11"><a href="chapter5.html#cb410-11" aria-hidden="true" tabindex="-1"></a><span class="fu">levels</span>(election2<span class="sc">$</span>VOTEF) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Gore"</span>, <span class="st">"Bush"</span>, <span class="st">"Other"</span>) <span class="co">#Replace 1,2,3 with meaningful names</span></span>
<span id="cb410-12"><a href="chapter5.html#cb410-12" aria-hidden="true" tabindex="-1"></a><span class="fu">levels</span>(election2<span class="sc">$</span>VOTEF) <span class="co">#Check new names of levels in VOTEF</span></span></code></pre></div>
<pre><code>## [1] "Gore" "Bush" "Other"</code></pre>
<div class="sourceCode" id="cb412"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb412-1"><a href="chapter5.html#cb412-1" aria-hidden="true" tabindex="-1"></a>electable <span class="ot"><-</span> <span class="fu">tally</span>(<span class="sc">~</span> PARTY <span class="sc">+</span> VOTEF, <span class="at">data =</span> election2) <span class="co">#Contingency table</span></span></code></pre></div>
<div style="page-break-after: always;"></div>
<div class="sourceCode" id="cb413"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb413-1"><a href="chapter5.html#cb413-1" aria-hidden="true" tabindex="-1"></a>electable</span></code></pre></div>
<pre><code>## VOTEF
## PARTY Gore Bush Other
## 1 238 6 2
## 2 151 18 1
## 3 113 31 13
## 4 37 37 11
## 5 21 124 12
## 6 20 121 2
## 7 3 189 1</code></pre>
<p>The hypotheses for an Independence/Association Test here are:</p>
<ul>
<li><p><span class="math inline">\(H_0\)</span>: There is no relationship between party affiliation and voting
status in the population.</p>
<ul>
<li>Or: <span class="math inline">\(H_0\)</span>: Party affiliation and voting status are independent in the
population.</li>
</ul></li>
<li><p><span class="math inline">\(H_A\)</span>: There is a relationship between party affiliation and voting
status in the population.</p>
<ul>
<li>Or: <span class="math inline">\(H_A\)</span>: Party affiliation and voting status are dependent in the
population.</li>
</ul></li>
</ul>
<p>You could also write these hypotheses with the variables switched and
that is also perfectly acceptable. Because
these hypotheses are ambivalent about the choice of a variable as an “x” or a
“y”, the summaries of results should be consistent with that idea. We should
not calculate conditional proportions or make stacked bar charts since they
imply a directional relationship from x to y (or results for y conditional on
the levels of x) that might be hard to justify. Our summaries in these
situations are the contingency table (<code>tally(~ var1 + var2, data = DATASETNAME)</code>)
and a new graph called a <strong><em>mosaic plot</em></strong> (using the <code>mosaicplot</code>
function).
</p>
<p>Mosaic plots display a box for each cell count whose area corresponds
to the proportion of the <em>total</em> data set that is in that cell
<span class="math inline">\((n_{rc}/\mathbf{N})\)</span>. In some cases, the bars can be short or narrow
if proportions of the total are small and the labels can be
hard to read but the same bars or a single line exist for each category of the
variables in all rows and columns. The mosaic plot makes it easy to identify
the most common combination of categories. For example, in
Figure <a href="chapter5.html#fig:Figure5-5">5.5</a> the <em>Gore</em> and <code>PARTY</code> = 1 (<em>Strong Democrat</em>)
box in the top segment under column 1 of the plot has the largest area
so is the highest proportion of the total. Similarly, the middle segment
on the right for the <code>PARTY</code> category 7s corresponds to the <em>Bush</em>
voters who were a 7 (<em>Strong Republican</em>). Knowing that the
middle box in each column is for Bush voters is a little difficult as “Other”
and “Bush” overlap each other in the y-axis labeling but it is easy enough to
sort out the story here if we have briefly explored the contingency table. We
can also get information about the variable used to make the
columns as the width
of the columns is proportional to the number of subjects in each
<code>PARTY</code> category in this plot. There were relatively few 4s
(<em>Independent-Independent</em> responses) in total in the data set.
Also, the <em>Other</em> category was the highest proportion of any
vote-getter in the
<code>PARTY</code> = 4 column but there were actually slightly more
<em>Other</em> votes out of the total in the 3s (<em>Independent-Democrat</em>)
party affiliation. Comparing the size of the 4s & <em>Other</em> segment
with the 3s & <em>Other</em> segment, one should conclude that the 3s & <em>Other</em>
segment is a slightly larger portion of the total data set. There is
generally a gradient of decreasing/increasing voting rates for the
two main party candidates
across the party affiliations, but there are a few exceptions. For
example, the
proportion of <em>Gore</em> voters goes up slightly between the <code>PARTY</code>
affiliations of 5s and 6s – as the voters become more strongly republican. To
have evidence of a relationship, there just needs to be a pattern of variation
across the plot of some sort but it does not need to follow such an easily
described pattern, especially when the categorical variables do not contain
natural ordering.</p>
<div style="page-break-after: always;"></div>
<p>The mosaic plots are best made on the tables created by the <code>tally</code>
function from a table that just contains the counts (<strong>no totals</strong>):
</p>
<div class="sourceCode" id="cb415"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb415-1"><a href="chapter5.html#cb415-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Makes a mosaic plot where areas are related to the proportion of</span></span>
<span id="cb415-2"><a href="chapter5.html#cb415-2" aria-hidden="true" tabindex="-1"></a><span class="co"># the total in the table</span></span>
<span id="cb415-3"><a href="chapter5.html#cb415-3" aria-hidden="true" tabindex="-1"></a><span class="fu">mosaicplot</span>(electable, <span class="at">main =</span> <span class="st">"Mosaic plot of observed results"</span>) </span></code></pre></div>
<div class="figure" style="text-align: center"><span style="display:block;" id="fig:Figure5-5"></span>
<img src="05-chiSquaredTests_files/figure-html/Figure5-5-1.png" alt="Mosaic plot of the 2000 election data comparing party affiliation and voting results." width="75%" />
<p class="caption">
Figure 5.5: Mosaic plot of the 2000 election data comparing party affiliation and voting results.
</p>
</div>
<p>In general, the results here are not too surprising as the respondents
became more heavily republican,
they voted for Bush and the same pattern occurs as you look at more democratic
respondents. As the voters leaned towards being independent, the proportion
voting for “Other” increased. So it certainly seems that there is some sort of
relationship between party affiliation and voting status. As always, it is good
to compare the observed results to what we would expect if the null hypothesis
is true. Figure <a href="chapter5.html#fig:Figure5-6">5.6</a> assumes that the null
hypothesis is true and shows the variation
in the proportions in each category in the columns and variation in the
proportions across the rows, but displays no relationship between
<code>PARTY</code> and <code>VOTEF</code>. Essentially, the pattern down a
column is the same for all
the columns or vice-versa for the rows. The way to think of “no relationship”
here would involve considering whether knowing the party level could help you
predict the voting response and that is not the case in
Figure <a href="chapter5.html#fig:Figure5-6">5.6</a> but was in certain places in
Figure <a href="chapter5.html#fig:Figure5-5">5.5</a>.</p>
<div class="figure" style="text-align: center"><span style="display:block;" id="fig:Figure5-6"></span>
<img src="05-chiSquaredTests_files/figure-html/Figure5-6-1.png" alt="Mosaic plot of what the 2000 election data would look like if the null hypothesis of no relationship were true." width="75%" />
<p class="caption">
Figure 5.6: Mosaic plot of what the 2000 election data would look like if the null hypothesis of no relationship were true.
</p>
</div>
</div>
<div id="section5-4" class="section level2 hasAnchor" number="5.4">
<h2><span class="header-section-number">5.4</span> Models for R by C tables<a href="chapter5.html#section5-4" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>This section is very short in this chapter because we really do not use any
“models” in this Chapter. There are some complicated
statistical models that can be employed in these situations, but they are
beyond the scope of this book. What we do have in this situation is our
original data summary in the form of a contingency table, graphs of the results
like those seen above, a hypothesis test and p-value (presented below), and
some post-test plots that we can use to understand the “source” of any evidence
we found in the test.</p>
</div>
<div id="section5-5" class="section level2 hasAnchor" number="5.5">
<h2><span class="header-section-number">5.5</span> Permutation tests for the <span class="math inline">\(X^2\)</span> statistic<a href="chapter5.html#section5-5" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>In order to assess the evidence against our null hypotheses of
no difference in distributions or no
relationship between the variables, we need to define a test
statistic and find
its distribution under the null hypothesis. The test statistic
used with both
types of tests is called the <span class="math inline">\(\mathbf{X^2}\)</span> <strong><em>statistic</em></strong>
(we want to call the statistic X-square not Chi-square). The statistic
compares the
observed counts in the contingency table to the <strong><em>expected counts</em></strong>
under the null hypothesis, with large differences between what
we observed and what we
expect under the null leading to evidence against the null hypothesis. To help
this statistic to follow a named parametric distribution and provide some
insights into sources of interesting differences from the null hypothesis, we <strong><em>standardize</em></strong><a href="#fn102" class="footnote-ref" id="fnref102"><sup>102</sup></a>
the difference between the observed and expected counts by the square-root of
the expected count.
The <span class="math inline">\(\mathbf{X^2}\)</span> <strong><em>statistic</em></strong> is based on
the sum of squared standardized differences,</p>
<p><span class="math display">\[\boldsymbol{X^2 = \Sigma^{RC}_{i = 1}\left(\frac{Observed_i-Expected_i}
{\sqrt{Expected_i}}\right)^2},\]</span></p>
<p>which is the sum over all (<span class="math inline">\(R\)</span> times <span class="math inline">\(C\)</span>) cells in the contingency
table of the square of the difference between observed and expected
cell counts divided by the square root of the
expected cell count. To calculate this test statistic, it useful to start with
a table of expected cell counts to go with our contingency table of observed
counts. The expected cell counts are easiest to understand in the
homogeneity situation but are calculated the same in either scenario.</p>
<p>The idea underlying finding the <strong><em>expected cell counts</em></strong> is to find
how many observations we would expect in category <span class="math inline">\(c\)</span> given the sample
size in that group, <span class="math inline">\(\mathbf{n_{r\bullet}}\)</span>, if the null hypothesis is true.
Under the null hypothesis across all <span class="math inline">\(R\)</span> groups
the conditional probabilities in each response category must be the
same. Consider Figure <a href="chapter5.html#fig:Figure5-7">5.7</a> where, under the null