forked from PeterKDunn/SRM-Textbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-Numerical-Qual.Rmd
executable file
·1565 lines (1231 loc) · 60.5 KB
/
14-Numerical-Qual.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
# Numerical summaries: qualitative data {#NumericalQual}
<!-- Introductions; easier to separate by format -->
```{r, child = if (knitr::is_html_output()) {'./introductions/14-Numerical-Qual-HTML.Rmd'} else {'./introductions/14-Numerical-Qual-LaTeX.Rmd'}}
```
## Modes, medians and means {#QualIntro}
Because qualitative data has [*levels*](#def:Levels), all qualitative data (nominal; ordinal) can be numerically summarised by *counting* the number of observations in each level (or computing the percentages in each level).
The **mode** is the category with the most observations.
::: {.definition #Mode name="Mode"}
A *mode* is a level (or category) of a qualitative variable with the most observations.
:::
Very few ways exist to summarise *nominal* data apart from the mode.
However, *ordinal* data can be numerically summarised in ways that nominal data cannot be, since *ordinal* data has levels with a natural order.
Ordinal qualitative data, but *not* nominal qualitative data, can be summarised using *medians*.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
Medians can be used to summarise *quantitative data* and *ordinal* data, but *never* for nominal data.
:::
::: {.example #OrdinalMedians name="Modes and medians" }
In a study of the taste of bread with varying salt and fibre content [@gkebski2019impact], researchers recorded information from the 300 subjects (Table\ \@ref(tab:Bread)), including gender, place of residence, and the subjects' responses to the statement 'Rolls with lower salt content taste worse than regular ones', on a five-point ordinal scale from 'Strongly Agree' to 'Strongly Disagree'.
'Gender' is *nominal* qualitative; all other variables are *ordinal* qualitative.
The mode could be used to summarise each variable:
* The two *modes* for gender are 'females' and 'males' (150 respondents each).
* The *mode* residence is 'City with > 100,000 residents' (130 responses).
* The *mode* response to the statement is 'Agree' (84 responses).
Medians are not appropriate for 'Gender' (it is nominal).
For the other variables, the medians are located at ordered observation $(300 + 1) / 2 = 150.5$, or half-way between ordered observations 150 and 151:
* The *median* residence is 'City 20,000 to 100,000 residents'.
* The *median* response to the statement is 'Neutral'.
:::
```{r Bread}
BreadTable <- array( dim = c(11, 3))
colnames(BreadTable) <- c("",
"Number",
"Percentage")
BreadTable[, 1] <- c("Female",
"Male",
"Rural",
"City up to 20,000 residents",
"City 20,000 to 100,000 residents",
"City > 100,000 residents",
"Strongly agree",
"Agree",
"Neutral",
"Disagree","Strongly disagree"
)
BreadTable[, 2] <- c(150,
150,
49,
38,
83,
130,
30,
84,
78,
66,
42)
BreadTable[, 3] <- c(50,
50,
16,
13,
28,
43,
10,
28,
26,
22,
14)
if( knitr::is_latex_output() ) {
knitr::kable(BreadTable,
format = "latex",
longtable = FALSE,
booktabs = TRUE,
align = "r",
linesep = c("", "\\addlinespace", "", "", "", "\\addlinespace", "", "", "", ""),
caption = "The bread-tasting data") %>%
kable_styling(font_size = 10) %>%
pack_rows("Gender", 1, 2) %>%
pack_rows("Place of residence", 3, 6) %>%
pack_rows("Response to statement", 7, 11) %>%
row_spec(0, bold = TRUE)
}
if( knitr::is_html_output() ) {
knitr::kable(BreadTable,
format = "html",
longtable = FALSE,
booktabs = TRUE,
align = "r",
caption = "The bread-tasting data")%>%
#kable_styling(font_size = 10) %>%
pack_rows("Gender", 1, 2) %>%
pack_rows("Place of residence", 3, 6) %>%
pack_rows("Response to statement", 7, 11) %>%
row_spec(0, bold = TRUE)
}
```
Means are not suitable for numerically summarising qualitative data.
However, *ordinal* data *may be* [numerically summarised like quantitative data](#NumericalQuant) in *special* circumstances: only when
* the categories are considered 'equally spaced'; *and*
* assigning a number to each category is appropriate (perhaps using a mid-point for numerical groups).
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
Means can be used to summarise *quantitative data*, *ordinal* data in very special circumstances, but *never* for nominal data.
:::
::: {.example #OrdinalMeans name="Means of ordinal data"}
Consider again the data in Table\ \@ref(tab:Bread).
Means are **not** suitable for summarising 'Gender' since it is nominal.
Means are **not** suitable for summarising 'Place of residence' either, as the categories are *not* equally spaced, the first level is text ('Rural'), and the final category is open-ended (so a mid-point for that level is not appropriate).
The mean response to the statement *may* be sensible *only if* the levels are considered equally spaced (i.e., the distances between successive points on the five-point scale are considered equal).
Then, assigning **1** to 'Strongly agree' up to **5** for 'Strongly disagree', the *mean* response is $3.02$ (effectively, 'Neutral').
:::
## Proportions and percentages {#ProportionsPercentages}
Qualitative data, as in Table\ \@ref(tab:Bread), can also be summarised using *proportions* or *percentages*.
These can be given instead of, or with, the counts (as in Table\ \@ref(tab:Bread)).
::: {.definition #Proportion name="Proportion"}
A *proportion* is a fraction out of a total, and is a number between 0 and 1.
:::
::: {.definition #Percentage name="Percentages"}
A *percentage* is a proportion, multiplied by 100.
In this context, percentages are numbers between 0% and 100%.
:::
*Population* proportions are almost always unknown.
Instead, the population proportion is estimated by the *sample* proportion, denoted by $\hat{p}$.
In this context, the unknown parameter is $p$, the population proportion, and the statistic is $\hat{p}$, the sample proportion.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
As always, only one of the many possible samples is studied.
*Statistics* are estimates of *parameters*, and the value of the *statistic* is not the same for every possible *sample*.
:::
::: {.pronounceBox .pronounce data-latex="{iconmonstr-microphone-7-240.png}"}
The symbol $\hat{p}$ is pronounced 'pee-hat', and refers to the *sample* proportion.
:::
::: {.example #ProportionsPercentages name="Proportions and percentages"}
Consider again the data in Table\ \@ref(tab:Bread), summarising results from a sample of $n = 300$ respondents.
The *sample proportion* of respondents from a rural location is $30/300$, or $0.1$.
The *sample percentage* of respondents from a rural location is $30/300\times 100$, or $10$%.
:::
## Two-way tables {#PercentagesKStones}
### Introduction {#PropPercentages-Intro}
Commonly, qualitative data are collected for multiple variable, and summarised using tabular summaries (Sect.\ \@ref(GraphNeeded)).
When two qualitative variables are classified using a *two-way table*, the data can be numerically summarised using [*percentages*](#def:Percentage) or [*odds*](#def:Odds), which we study using an example.
<div style="float:right; width: 222x; border: 1px; padding:10px">
<img src="Illustrations/surgery-3034133_640.jpg" width="200px"/>
</div>
:::{.example name="Two-way tables"}
A medical study [@data:Charig:stones] compared two treatments for kidney stones to determine which was better.
Data were collected from $700$ UK patients, on two qualitative variables:
* the treatment method ('A' or 'B'): The explanatory variable.
* the result ('success' or 'failure' of the procedure): The response variable.
Both variables are *qualitative* with two *levels*.
Each treatment was used on $350$ patients.
Treatment\ A was used from 1972--1980, and Treatment B\ from 1980--1985; that is, *treatments were not randomly* allocated, and so *confounding* may be present.
For this reason, the researchers also recorded the *size* of the kidney stone ('small' or 'large') as one possible confounding variable.
Firstly, consider just the *small stones* [@julious1994confounding], displayed in the two-way table in Table\ \@ref(tab:KS-Small).
:::
The data in Table\ \@ref(tab:KS-Small) can be numerically summarised by computing *proportions* or *percentages*.
These can be computed as parts of the *total number* (Sect.\ \@ref(OverallPercentage)), by *row totals* (Sect. \@ref(RowPercentages)), or by *column totals* (Sect.\ \@ref(ColumnPercentages)).
(ref:KStonesNumbersSmall) **Numbers** for **small** kidney stones
```{r KS-Small}
data(KStones)
KS.small <- xtabs( Counts ~ Method + Result,
data = subset(KStones, Size == "Small"))[, c(2, 1)]
KS.small2 <- cbind(KS.small,
"Total" = rowSums(KS.small))
KS.small2.full <- rbind( KS.small2,
"Total" = colSums(KS.small2) )
if( knitr::is_latex_output() ) {
kable(KS.small2.full,
format = "latex",
longtable = FALSE,
booktabs = TRUE,
digits = 0,
align = c("r", "r", "r", "r"),
col.names = c("Success",
"Failure",
"Total"),
caption = "(ref:KStonesNumbersSmall)"
) %>%
row_spec(0, bold = TRUE) %>%
row_spec(3, bold = TRUE) %>%
row_spec(2, hline_after = TRUE) %>%
kable_styling(full_width = FALSE) %>%
kable_styling(font_size = 10) %>%
column_spec(column = 4,
bold = TRUE)
}
if( knitr::is_html_output() ) {
kable(KS.small2.full,
format = "html",
longtable = FALSE,
booktabs = TRUE,
digits = 0,
align = c("r", "r", "r", "r"),
col.names = c("Success",
"Failure",
"Total"),
caption = "(ref:KStonesNumbersSmall)"
) %>%
kable_styling(full_width = FALSE) %>%
column_spec(column = 4,
bold = TRUE) %>%
row_spec(3,
bold = TRUE)
}
```
### Overall proportions and percentages {#OverallPercentage}
From Table\ \@ref(tab:KS-Small), the overall *sample proportion* of successes (denoted $\hat{p}$) is:
\begin{align*}
\hat{p}
& =
\frac{\text{Overall number of successes}}{\text{Overall number of procedures performed}}\\
& =
\frac{81 + 234}{6 + 81 + 36 + 234} = 0.882.
\end{align*}
The *sample* proportion of successful procedures for *small* kidney stones is $\hat{p} = 0.882$ (and the *sample* percentage is $88.2$%).
The proportion could also be expressed as a *percentage*, by multiplying the proportion by $100$: $0.882 \times 100 = 88.2\%$.
The sample *percentage* of successful procedures for *small* kidney stones is $88.2$%.
The sample *proportion* and sample *percentage* are both *statistics*.
<div style="float:right; width: 222x; border: 1px; padding:10px">
<img src="Pics/iconmonstr-brick-4-240.png" width="50px"/>
</div>
### Row proportions and percentages {#RowPercentages}
For the small kidney stones (Table\ \@ref(tab:KS-Small)), the *row proportions*
`r if (knitr::is_latex_output()) {
'(Table\\ \\@ref(tab:KidneyRowColLATEX), left table)'
} else {
'(Table\\ \\@ref(tab:KS-Small-rowPC)'
}`
give the proportion of successes for each *Method*, since the rows represent the counts for Methods\ A and\ B.
*Row* proportions allow the proportions *within the rows* (i.e., for each Method) to be compared:
* $81 \div 87 = 0.931$ (or $93.1$%) of operations in the sample were successful for Method\ A; and
* $0.867$ (or $86.7$%) of operations were successful in the sample for Method\ B.
This suggests that, for small kidney stones, Method\ A is slightly more successful ($93.1$%) than Method\ B ($86.7$%) in the *sample*.
(ref:KStonesRowPercentSmall) **Row percentages** for **small** kidney stones (from Table\ \@ref(tab:KS-Small))
```{r KS-Small-rowPC}
KS.small.rowPC <- prop.table(KS.small,
margin = 1) * 100
KS.small.rowPC2 <- cbind(KS.small.rowPC,
"Total" = c(100, 100) )
if( knitr::is_html_output() ) {
kable(KS.small.rowPC2,
format = "html",
longtable = FALSE,
booktabs = TRUE,
digits = 1,
align = c("r", "r", "r", "r"),
col.names = c("Success",
"Failure",
"Total"),
caption = "(ref:KStonesRowPercentSmall)") %>%
kable_styling(full_width = FALSE) %>%
column_spec(column = 4,
bold = TRUE)
}
```
### Column proportions and percentages {#ColumnPercentages}
For the *small* kidney stones (Table\ \@ref(tab:KS-Small)), the *column proportions*
`r if (knitr::is_latex_output()) {
'(Table\\ \\@ref(tab:KidneyRowColLATEX), right table)'
} else {
'(Table\\ \\@ref(tab:KS-Small-colPC)'
}`
give the proportion of successes within each column (i.e., for successes and for failures), since the columns contain the procedure results.
*Column* proportions allow the proportions (or percentages) within *columns* to be compared:
* $81 \div (81 + 234) = 0.257$ (or $25.7$%) of all *successful* operations were from Method\ A; and
* $0.143$ (or $14.3$%) of *failures* were from Method\ A.
Row percentages seems more intuitive than column percentages here: they compare the success percentage for each treatment method.
(ref:KStonesColPercentSmall) **Column percentages** for **small** kidney stones (from Table\ \@ref(tab:KS-Small))
```{r KS-Small-colPC}
KS.small.colPC <- prop.table(KS.small,
margin = 2) * 100
KS.small.colPC2 <- rbind(KS.small.colPC,
"Total" = c(100, 100) )
if( knitr::is_html_output() ) {
kable(KS.small.colPC2,
format = "html",
longtable = FALSE,
digits = 1,
booktabs = TRUE,
align = c("r", "r", "r"),
col.names = c("Success",
"Failure"),
caption = "(ref:KStonesColPercentSmall)") %>%
kable_styling(full_width = FALSE) %>%
row_spec(row = 3,
bold = TRUE)
}
```
(ref:KStonesRowColPercentSmall) **Small** kidney stones: left: **Row percentages**; right, **column percentages** (from Table\ \@ref(tab:KS-Small)).
```{r}
KS.small.rowPC <- prop.table(KS.small,
margin = 1) * 100
KS.small.rowPC <- round(KS.small.rowPC, 1)
KS.small.rowPC2 <- cbind(KS.small.rowPC,
"Total" = c("\\textbf{100.0}", "\\textbf{100.0}") )
KS.small.colPC <- prop.table(KS.small,
margin = 2) * 100
KS.small.colPC2 <- rbind(KS.small.colPC,
"Total" = c(100, 100) )
KS.small.colPC <- round(KS.small.colPC, 1)
if( knitr::is_latex_output() ) {
T1 <- kable(KS.small.rowPC2,
format ="latex",
longtable = FALSE,
booktabs = TRUE,
escape = FALSE,
digits = 1,
align = c("r", "r", "r", "r"),
col.names = c("Success",
"Failure",
"Total")) %>%
row_spec(0, bold = TRUE)
T2 <- kable(KS.small.colPC2,
format = "latex",
longtable = FALSE,
digits = 1,
booktabs = TRUE,
align = c("r", "r", "r"),
col.names = c("Success",
"Failure")) %>%
row_spec(0, bold = TRUE) %>%
row_spec(3, bold = TRUE)
out <- knitr::kables(list(T1, T2),
format = "latex",
label = "KidneyRowColLATEX",
caption = "(ref:KStonesRowColPercentSmall)") %>%
kable_styling(font_size = 10)
out2 <- prepareSideBySideTable(out,
numberOfTables = 2,
gap = "\\qquad\\qquad\\qquad")
out2
}
```
### Example: large kidney stones {#KidneyExample}
<div style="float:right; width: 222x; border: 1px; padding:10px">
<img src="Illustrations/surgery-3034133_640.jpg" width="200px"/>
</div>
The data in Table\ \@ref(tab:KS-Small) are for *small* kidney stones.
Data were also recorded for the *large* kidney stones
`r if (knitr::is_latex_output()) {
'(Table\\ \\@ref(tab:KStonesNumbersLargeAll), left table).'
} else {
'(Table\\ \\@ref(tab:KS-Large)).'
}`
As for small kidney stones, the *success proportions* can be computed for large kidney stones for Methods\ A and\ B (i.e., row percentages):
* Method\ A: Success proportion for large kidney stones: $192/263 = 0.730$, or $73.0$%; and
* Method\ B: Success proportion for large kidney stones: $55/80 = 0688$, or $68.8$%.
For large kidney stones, then, *Method\ A* has a higher success proportion than Method\ B, just as with the small kidney stones.
(ref:KStonesNumbersLarge) **Numbers** for **large** kidney stones
```{r KS-Large}
KS.large <- xtabs( Counts ~ Method + Result,
data = subset(KStones, Size == "Large"))[, c(2, 1)]
KS.large2 <- cbind(KS.large,
"Total" = rowSums(KS.large))
if( knitr::is_html_output() ) {
kable(KS.large2,
format = "html",
longtable = FALSE,
booktabs = TRUE,
digits = 0,
align = c("r", "r", "r", "r"),
col.names = c("Success",
"Failure",
"Total"),
caption = "(ref:KStonesNumbersLarge)") %>%
column_spec(column = 4,
bold = TRUE)
}
```
So... could the data for small (Table\ \@ref(tab:KS-Small)) and large kidney stones
`r if (knitr::is_latex_output()) {
'(Table\\ \\@ref(tab:KStonesNumbersLargeAll), left table)'
} else {
'(Table\\ \\@ref(tab:KS-Large))'
}`
be combined, to produce a single two-way table of just Method and Result
`r if (knitr::is_latex_output()) {
'(Table\\ \\@ref(tab:KStonesNumbersLargeAll), right table),'
} else {
'(Table\\ \\@ref(tab:KSAll)),'
}`
ignoring size?
(ref:KStonesNumbersAll) **Numbers** for **all** kidney stones combined, ignoring the size of the kidney stone
```{r KSAll}
KS.all <- KS.small + KS.large
KS.all2 <- cbind(KS.all,
"Total" = rowSums(KS.all))
if( knitr::is_html_output() ) {
kable(KS.all2,
format = "html",
longtable = FALSE,
booktabs = TRUE,
digits = 0,
align = c("r", "r", "r", "r"),
col.names = c("Success",
"Failure",
"Total"),
caption = "(ref:KStonesNumbersAll)") %>%
column_spec(column = 4,
bold = TRUE)
}
```
(ref:KStonesNumbersLargeAll) The kidney stones data: left: **Numbers** for **large** stones only; right: **numbers** for **all** kidney stones combined, ignoring the size of the kidney stone
```{r}
if( knitr::is_latex_output() ) {
T1 <- kable(KS.large2,
format ="latex",
longtable = FALSE,
booktabs = TRUE,
escape = FALSE,
digits = 1,
align = c("r", "r", "r"),
col.names = c("Success",
"Failure",
"Total")) %>%
row_spec(0, bold = TRUE)
T2 <- kable(KS.all2,
format = "latex",
longtable = FALSE,
digits = 1,
booktabs = TRUE,
align = c("r", "r", "r"),
col.names = c("Success",
"Failure",
"Total")) %>%
row_spec(0, bold = TRUE)
out <- knitr::kables(list(T1, T2),
format = "latex",
label = "KStonesNumbersLargeAll",
caption = "(ref:KStonesNumbersLargeAll)") %>%
kable_styling(font_size = 10)
out2 <- prepareSideBySideTable(out,
numberOfTables = 2,
gap = "\\qquad\\qquad\\qquad")
out2
}
```
::: {.thinkBox .think data-latex="{iconmonstr-light-bulb-2-240.png}"}
Compute the success proportions for Method\ A and Method\ B when *small* and *large* stones are combined\label{thinkBox:AllStonesSuccess}
`r if (knitr::is_latex_output()) {
'(Table\\ \\@ref(tab:KStonesNumbersLargeAll), right table):'
} else {
'(Table\\ \\@ref(tab:KSAll)):'
}`\vspace{-2ex}
* For small and large stones combined, the success proportion with Method\ A is: ______
* For small and large stones combined, the success proportion with Method\ B is: ______
Which method has the higher success proportion for all stones combined?
`r if (knitr::is_latex_output()) '<!--'`
`r webexercises::hide()`
Method\ A has a higher success proportion ($273/350 = 0.780$) than Method\ B ($289/350 = 0.826$), for all kidney stones combined.
`r webexercises::unhide()`
`r if (knitr::is_latex_output()) '-->'`
:::
To summarise:
* *Method\ A* is better for small stones ($93.1$% vs $86.7$%);
* *Method\ A* is better for large stones ($73.0$% vs $68.8$%); but
* *Method\ B* is better when all stones are combined ($78.0$% vs $82.6$%)...
That seems strange...
Method\ A performs better for small stones, and for large kidney stones... but Method\ B performs better when the size is unknown (i.e., ignoring size).
The *size of the stone* is a *confounding variable* (Fig.\ \@ref(fig:SimpsonRulesStones)), as it is associated with the method (small stones are treated more often with Method\ B) *and* with the result (small stones have a greater success proportion).
Treating the small and large stones separately is a form of *blocking* (Sect.\ \@ref(ObsManagingConfounding)).
This confounding could have been avoided by randomly allocating a treatment method to patients.
However, random allocation was not possible in this study, so the researchers used a different method to manage confounding: *recording* the size of the kidney stones (and also the age and sex of the patient); see Sect.\ \@ref(ObsManagingConfoundingAnalysis).
In this example, incorporating information about a potential confounder (the size of the kidney stone) is important, otherwise the wrong (opposite) conclusion is reached: Method\ B would be considered better if the size of the stones was ignored, when the better method really is Method\ A.
This is called
`r if (knitr::is_latex_output()) {
"*Simpson's paradox*."
} else {
"[*Simpson's paradox*](https://en.wikipedia.org/wiki/Simpson%27s_paradox)."
}`
If the size of the kidney stone had not been recorded, size would be a *lurking variable*, and the incorrect conclusion would have been reached.
```{r SimpsonRulesStones, fig.cap="The size of the stones is related to both the success percentage and the method", fig.align="center", fig.height=3, out.width='50%'}
par( mar = c(0.05, 0.05, 0.05, 0.05))
openplotmat()
pos <- array(NA, dim = c(3, 2))
pos[1, ] <- c(0.25, 0.25) # Success
pos[2, ] <- c(0.75, 0.25) # Method
pos[3, ] <- c(0.5, 0.65) # Size
straightarrow(from = pos[2,],
to = pos[1,],
lty = 1,
lwd = 2)
straightarrow(from = pos[3,],
to = pos[1,],
lty=2,
lcol="grey")
straightarrow(from = pos[3,],
to = pos[2,],
lty = 2,
lcol = "grey")
textrect( pos[1,],
lab = "Success",
radx = 0.07,
rady = 0.075,
shadow.size = 0,
box.col = ResponseColour,
lcol = ResponseColour)
textrect( pos[2,],
lab = "Method",
radx = 0.07,
rady = 0.075,
shadow.size = 0,
box.col = ExplanatoryColour,
lcol = ExplanatoryColour)
textrect( pos[3,],
lab = "Size",
radx = 0.07,
rady = 0.075,
shadow.size = 0,
box.col = ExtraneousColour,
lcol = ExtraneousColour)
```
## Odds {#QualOdds}
Consider again the *small* kidney stone data (Table\ \@ref(tab:KS-Small)).
For *Method\ A*, the sample contains $81$ successes and $6$ failures.
Another way to numerically summarise this information is to see that there are $81\div 6 = 13.5$ *times* as many successes than failures in the sample.
In other words, for small kidney stones, the *odds* of success for Method\ A is $13.5$ (in the sample).
The sample odds is a *statistic*, and the unknown population odds is a *parameter*.
::: {.definition #Odds name="Odds"}
The *odds* are the number (or proportion, or percentage) of times that an event *happens*, divided by the number (or proportion, or percentage) of times that the event does *not happen*:
\[
\text{Odds} = \frac{\text{Number of times event happens}}{\text{Number of times event doesn't happen}}
\]
or (equivalently)
\[
\text{Odds}
=
\frac{\text{Proportion of times event happens}}
{\text{Proportion of times event doesn't happen}}.
\]
The *odds* show how many *times* an event *happens* compared to the event *not happening*.
:::
<div style="float:right; width: 222x; border: 1px; padding:10px">
<img src="Pics/iconmonstr-ruler-16-240.png" width="50px"/>
</div>
When computing odds, the count of interest is divided by the *remaining number*, whereas in proportions and percentages the relevant number is divided by the *total number* relevant to the context.
Software often works with odds rather than percentages (for good reasons that we will not delve into).
Understanding *how* software computes the odds is important.
::: {.softwareBox .software data-latex="{iconmonstr-laptop-4-240.png}"}
In a two-by-two table, the odds computed by jamovi or SPSS can be interpreted in *either* of these ways (i.e., both are correct)
\vspace{-2ex}
* Row\ 1 counts to Row\ 2 counts; *or*
* Column\ 1 counts to Column\ 2 counts.
\vspace{-2ex}
*Both ways are correct*, but one often makes more sense that the other.
Based on Table\ \@ref(tab:KS-Small), the odds for the Methods computed by software will compare Method\ A compared to Method\ B (not Method\ B to Method\ A).
:::
::: {.example #InterpretingOdds name="Interpreting odds"}
For the *small* kidney stone data, the odds of a success for Method\ A is $81\div6 = 13.5$.
This can be interpreted as:
* There are $13.5$ *times* as many successes as failures (in the sample); or
* There are $13.5\times 100 = 1350$ successes for every $100$ failures (in the sample).
Either way, successes are *far* more common than failures, for small kidney stones using Method\ A.
:::
::: {.thinkBox .think data-latex="{iconmonstr-light-bulb-2-240.png}"}
What are the odds of a *failure* for Method\ A?
How is this value interpreted?\label{thinkBox:InterpretOdds}
`r if (knitr::is_latex_output()) '<!--'`
`r webexercises::hide()`
The odds: $6\div81 = 0.0741$.
For every $100$ successes, expect about $0.0741\times 100 = 7.4$ failures.
`r webexercises::unhide()`
`r if (knitr::is_latex_output()) '-->'`
:::
::: {.example #OddsEG name="Odds"}
Suppose that 67% of students at a particular university were female.
The *population* odds of finding a female is $67 / (100 - 67) = 2.03$: about twice as many students are females compared to non-females.
Alternatively, there are about $100\times 2.03 = 203$ females to every $100$ non-females.
Suppose one specific class had $18$ females and $5$ non-females.
The *sample* odds of finding a female in this class is $18/5 = 3.60$.
Another class had $16$ females and $9$ non-females.
The *sample* odds of finding a female in this class is $16/9 = 1.79$.
:::
::: {.example #ComputingOdds name="Computing odds"}
Consider again the **small** kidney stone data (Table\ \@ref(tab:KS-Small)).
The odds of a success using *Method\ B* is $234/36 = 6.52$.
Working with the corresponding proportions or percentages
`r if (knitr::is_latex_output()) {
'(Table\\ \\@ref(tab:KidneyRowColLATEX), left table)'
} else {
'(Table\\ \\@ref(tab:KS-Small-rowPC)'
}`
rather than the numbers gives the same value: $86.7/13.3 = 6.52$.
:::
<iframe src="https://learningapps.org/watch?v=pxf5194ek22" style="border:0px;width:100%;height:500px" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
Take care interpreting odds:
* odds are *greater* than $1$: the event is *more* likely to happen than to not happen.
* odds are *equal to* $1$: the event is just as likely to happen as it is to not happen.
* odds are *less* than $1$: the event is *less* likely to happen than to not happen.
::: {.thinkBox .think data-latex="{iconmonstr-light-bulb-2-240.png}"}
What are the odds of rolling a 2 on a die?\label{thinkBox:Roll2}
`r if( knitr::is_html_output()) {
longmcq( c(
"About 0.1666",
"6",
"0.5",
"1/6",
answer = "0.2") )}`
:::
## Comparing odds: odds ratios {#OdddsRatios}
To summarise the *small* kidney stone data (Table\ \@ref(tab:KS-Small)):
* *Method\ A*: the odds of success are $13.5$ ($13.5$ *times* as many successes as failures).
* *Method\ B*: the odds of success are $6.5$ ($6.5$ *times* as many successes as failures).
The odds of success for Method\ A and Method\ B are very different.
In the sample, the odds of success for Method\ A is many *times* greater than for Method\ B.
In fact, in the sample, the odds of success for Method\ A is $13.5\div 6.5 = 2.08$ *times* the odds of a success for Method\ B.
This value is the *odds ratio* (OR).
The sample odds ratio is a *statistic*, and the population odds ratio is a *parameter*.
<div style="float:right; width: 222x; border: 1px; padding:10px">
<img src="Pics/iconmonstr-cursor-21-240.png" width="50px"/>
</div>
::: {.definition #OddsRatio name="Odds Ratio (OR)"}
The *odds ratio* is the ratio of the odds of an event in one group, compared to the odds of the *same* event in a *different* group:
\[
\text{Odds ratio} =
\frac{\text{Odds of an event in Group A}}
{\text{Odds of the same event in Group B}}.
\]
:::
::: {.softwareBox .software data-latex="{iconmonstr-laptop-4-240.png}"}
Understanding how software computes the odds ratio is important for understanding the output.
jamovi and SPSS output can be interpreted in *either* of these ways (i.e., both are correct):
\vspace{-2ex}
* The *odds* compare Row\ 1 counts to Row\ 2 counts, for both columns.
The *odds ratio* then compares the Column\ 1 odds to the Column\ 2 odds.
* The *odds* compare Column\ 1 counts to Column\ 2 counts.
The *odds ratio* then compares the Row\ 1 odds to the Row\ 2 odds.
\vspace{-2ex}
Odds and odds ratios are computed with the *first row* and *first column* values on the *top* of the fraction.
:::
The OR compares the odds of the same event in two different groups.
This means that a $2\times 2$ table can be summarised using one number: the odds ratio (OR).
Take care interpreting odds ratios (or ORs):
* odds ratio is *greater than 1*:
the odds of the event is *greater* for the group in the top of the division compared to the group in the bottom of the division.
* odds ratio is *equal to 1*:
the odds of the event is the *same* for both groups (in the top and the bottom of the division).
* odds ratio is *less than 1*:
the odds of the event is *less* for the group in the top of the division compared to the group in the bottom of the division.
`r if (knitr::is_html_output()){
'The following short video may help explain some of these concepts:'
}`
<div style="text-align:center;">
```{r}
htmltools::tags$video(src ="./videos/oddsratios.mp4",
width="550",
controls="controls",
loop="loop",
style="padding:5px; border: 2px solid gray;")
```
</div>
<iframe src="https://learningapps.org/watch?v=pcyn538fj22" style="border:0px;width:100%;height:500px" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
## Observing relationships {#OddsObserveRelationships}
For the *small* kidney stone data, the odds of a success for Method\ A ($13.5$) is different than the odds of a successes for Method\ B ($6.52$), in the *sample*.
Is there a difference in the *population* odds?
Broadly, two possible reasons exist to explain the differences *in the sample odds*:
* The *population odds* are the same for Method\ A and Method\ B, but the *sample odds* are difference simply because of sample obained.
After all, we have just one of the many possible samples, and every sample is likely to be different; the *sample* we studied just happened to show a difference.
That is, *sampling variation* explains the *difference in the sample odds*.
* The *population odds* are different for Method\ A and Method\ B, and so the *sample odds* are difference, simply reflects this difference between the *population* odds.
The difficulty, of course, is knowing which of these two reasons ('hypotheses') is the most likely reason for the difference between the sample odds.
This question is of prime importance (after all, it answers the RQ), and is addressed at length later in this book.
## Example: skipping breakfast {#Skipping-Breakfast}
<div style="float:right; width: 222x; border: 1px; padding:10px">
<img src="Illustrations/pexels-daria-shevtsova-949067.jpg" width="200px"/>
</div>
The data in Table\ \@ref(tab:SkipBreakfast) come from a study of Iranian children aged 6--18 years old [@data:kelishadi2017:snack].
From this table:
* The *proportion* of females who skipped breakfast is $\hat{p}_F = 2\,383/6\,640 = 0.359$;
* The *proportion* of males who skipped breakfast is $\hat{p}_M = 1\,944/6\,846 = 0.284$.
Alternatively,
* $\text{Odds}(\text{Skips breakfast, among F}) = 2\,383/4\,257 = 0.5598$;
* $\text{Odds}(\text{Skips breakfast, among M}) = 1\,944/4\,902 = 0.3966$.
(Notice that the number on *top* of the fraction in each case is from the *first* column.)
So, about $55.98$ females *skip* breakfast for every $100$ females who *do eat* breakfast (i.e., Column\ 1).
The *odds ratio* (OR) comparing the odds of skipping breakfast, comparing females (Row\ 1) to males (Row\ 2), is
\begin{align*}
\text{OR}
&= \frac{\text{Odds}(\text{Skipping breakfast, for females})}{\text{Odds}(\text{Skipping breakfast, for males})}\\
&= \frac{0.5598}{0.3966}
= 1.41.
\end{align*}
(Notice that the number of *top* of the fraction is the odds computed from the *first* row.)
The odds of females skipping breakfast are $1.41$ *times* the odds of males skipping breakfast.
The information can be summarised numerically (Table\ \@ref(tab:BreakSum)).
```{r SkipBreakfast}
Counts <- c(2383,
1944,
4257,
4902)
Gender <- c("Females",
"Males",
"Females",
"Males")
Breakfast <- c("Skip",
"Skip",
"Doesn't skip",
"Doesn't skip")
Brek <- xtabs(Counts ~ Gender + Breakfast)[, c(2, 1)]
Brek2 <- cbind(Brek, "Total" = rowSums(Brek))
if( knitr::is_latex_output() ) {
kable(Brek2,
format = "latex",
longtable = FALSE,
booktabs = TRUE,
align = c("r", "r", "r"),
digits = 0,
col.names = c("Skips breakfast",
"Doesn't skip breakfast",
"Total"),
caption = "The number of Iranian children aged 6 to 18 who skip and do not skip breakfast" ) %>%
row_spec(0, bold = TRUE) %>%
kable_styling(font_size = 10) %>%
column_spec(column = 4,
bold = TRUE)
}
if( knitr::is_html_output() ) {
out <- kable(Brek2,
format = "html",
longtable = FALSE,
booktabs = TRUE,
align = c("r", "r", "r"),
digits = 0,
col.names = c("Skips breakfast",
"Doesn't skip breakfast",
"Total"),
caption = "The number of Iranian children aged 6 to 18 who skip and do not skip breakfast" )
if ( knitr::is_html_output()) {
column_spec(out,
column = 4,
bold = TRUE)
} else {
out
}
}
```
```{r BreakSum}
Brek.Sum <- array( NA,
dim = c(3, 3))
rownames(Brek.Sum) <- c("Females",
"Males",
"Odds ratio")
Brek.Sum[1, 1] <- Brek[1, 1] / sum(Brek[1,]) * 100
Brek.Sum[2, 1] <- Brek[2, 1] / sum(Brek[2,]) * 100
Brek.Sum[1, 2] <- Brek[1, 1] /Brek[1, 2]
Brek.Sum[2, 2] <- Brek[2, 1] /Brek[2, 2]
Brek.Sum[3, 2] <- Brek.Sum[1, 2] / Brek.Sum[2, 2]
Brek.Sum[1, 3] <- sum(Brek[1, ])
Brek.Sum[2, 3] <- sum(Brek[2, ])
if( knitr::is_latex_output() ) {
kable(Brek.Sum,
format = "latex",
longtable = FALSE,
booktabs = TRUE,
align = c("r", "r", "r"),
digits = c(1, 3),
col.names = c("Percentage",