-
Notifications
You must be signed in to change notification settings - Fork 7
/
12-SummaryQual.Rmd
1582 lines (1253 loc) · 55.7 KB
/
12-SummaryQual.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
# Summarising qualitative data {#SummariseQualData}
\index{Qualitative data!summarising|(}
<!-- Introductions; easier to separate by format -->
```{r, child = if (knitr::is_html_output()) {'./introductions/12-SummaryQual-HTML.Rmd'} else {'./introductions/12-SummaryQual-LaTeX.Rmd'}}
```
<!-- Define colours as appropriate -->
```{r, child = if (knitr::is_html_output()) {'./children/coloursHTML.Rmd'} else {'./children/coloursLaTeX.Rmd'}}
```
## Introduction {#SummaryQual-Intro}
Many quantitative research studies involve qualitative variables
Except for very small amounts of data, understanding the data is difficult without a summary.
As with quantitative data, qualitative data can be understood by knowing how often values of the variables appear.
This is called the *distribution* of the data (Def.\ \@ref(def:Distribution)).\index{Distribution!qualitative data}
The distribution can be displayed using a frequency table (Sect.\ \@ref(QualitativeTables)) or a graph (Sect.\ \@ref(QualitativeGraphs)).
Qualitative data can be summarised by finding modes or, for ordinal qualitative data, using medians (Sect.\ \@ref(SummariseDataQualitative)).
The distribution of qualitative data can be summarised numerically by computing proportions, percentages (Sect.\ \@ref(QualitativeProportionsPercentages)) or odds (Sect.\ \@ref(QualOdds)).
## Frequency tables for qualitative data {#QualitativeTables}
\index{Qualitative data!frequency table}
Qualitative data are typically collated in a *frequency table*.\index{Frequency table!qualitative data}
The rows (or the columns) should list the *levels* of the variable, and these should be *exhaustive* (cover all levels) and *mutually exclusive* (observations belong to only one level).\index{Levels}
The number of observations or the percentage of observations (or both) are then given for each level.
For *nominal* data, the levels of the variables can be displayed in alphabetical order, in order of size, by personal preference, or other way: use the order most likely to be useful to readers.
For *ordinal* data, the natural order of the levels should almost always be used.
::: {.example #AVstudy name="Opinions of AV vehicles"}
@pyrialakou2020perceptions surveyed $400$\ residents of Phoenix (Arizona) about their opinions of autonomous vehicles (AVs).
Demographic information (Table\ \@ref(tab:AVtable1)) and respondents' opinions of sharing roads with AVs (Table\ \@ref(tab:AVtable2)) were recorded.
The gender of the respondent is *nominal* (two levels), while the age group is *ordinal* (six levels).
The levels are shown in the rows.
The three questions about safety (Table\ \@ref(tab:AVtable2)) all yield *ordinal* responses (five levels, in columns).
:::
```{r}
AVtable1 <- array( dim = c(8, 2) )
rownames(AVtable1) <- c("Female",
"Male",
"$18$ to $24$",
"$25$ to $34$",
"$35$ to $44$",
"$45$ to $54$",
"$55$ to $64$",
"$65+$")
colnames(AVtable1) <- c("Number",
"Percentage")
AVtable1[1, ] <- c(204, 51)
AVtable1[2, ] <- c(196, 49)
AVtable1[3, ] <- c(52, 13)
AVtable1[4, ] <- c(76, 19)
AVtable1[5, ] <- c(76, 19)
AVtable1[6, ] <- c(72, 18)
AVtable1[7, ] <- c(56, 14)
AVtable1[8, ] <- c(68, 17)
###########
AVtable2 <- array( dim = c(10, 4) )
colnames(AVtable2) <- c("",
"Driving near an AV",
"Cycling near an AV",
"Walking near an AV")
#rownames(AVtable2) <- c("Unsafe",
# "Somewhat unsafe",
# "Neutral",
# "Somewhat safe",
# "Safe")
rownames(AVtable2) <- c("Unsafe", "",
"Somewhat unsafe", "",
"Neutral", "",
"Somewhat safe", "",
"Safe", "")
AVtable2[ c(1, 3, 5, 7, 9), 2] <- c(58, 79, 96, 97, 70)
AVtable2[ c(1, 3, 5, 7, 9), 3] <- c(77, 104, 87, 76, 56)
AVtable2[ c(1, 3, 5, 7, 9), 4] <- c(63, 86, 103, 82, 66)
AVtable2[ c(2, 4, 6, 8, 10), 2] <- round( AVtable2[ c(1, 3, 5, 7, 9), 2] / 400 * 100, 0)
AVtable2[ c(2, 4, 6, 8, 10), 3] <- round( AVtable2[ c(1, 3, 5, 7, 9), 3] / 400 * 100, 0)
AVtable2[ c(2, 4, 6, 8, 10), 4] <- round( AVtable2[ c(1, 3, 5, 7, 9), 4] / 400 * 100, 0)
AVtable2[, 1] <- c("$n$", "Percent",
"$n$", "Percent",
"$n$", "Percent",
"$n$", "Percent",
"$n$", "Percent")
AVdemographics <- AVtable1[, 1]
AVquestions <- as.numeric( AVtable2[ c(1, 3, 5, 7, 9), 2:4] )
dim(AVquestions) <- c(5, 3)
rownames(AVquestions) <- rownames(AVtable2)[ c(1, 3, 5, 7, 9)]
colnames(AVquestions) <- colnames(AVtable2)[2:4]
```
```{r AVtable1}
if (knitr::is_html_output()) {
knitr::kable(pad(AVtable1,
surroundMaths = TRUE,
targetLength = c(3, 2),
decDigits = 0),
format = "html",
longtable = FALSE,
escape = FALSE,
align = c("c", "c"),
booktabs = TRUE,
caption = "Demographic information for the AV data for $400$ respondents.") %>%
row_spec(0, bold = TRUE) %>%
pack_rows( "Gender",
start_row = 1,
end_row = 2) %>%
pack_rows( "Age group",
start_row = 3,
end_row = 8)
} else {
knitr::kable(pad(AVtable1,
surroundMaths = TRUE,
targetLength = c(3, 2),
decDigits = 0),
format = "latex",
longtable = FALSE,
escape = FALSE,
align = c("c", "c"),
booktabs = TRUE,
caption = "Demographic information for the AV data for $400$ respondents.") %>%
kable_styling(font_size = 8) %>%
row_spec(0, bold = TRUE) %>%
pack_rows( "Gender ($n = 400$)",
start_row = 1,
escape = FALSE,
end_row = 2) %>%
pack_rows( "Age group ($n = 400$)",
start_row = 3,
escape = FALSE,
end_row = 8)
}
```
```{r AVtable2}
AVtable2T <- t( AVtable2)
colnames(AVtable2T) <- AVtable2T[1, ]
AVtable2T <- AVtable2T[-1, ]
colnames(AVtable2T) <- rep( c("$n$", "\\%"), 5)
if (knitr::is_html_output()) {
knitr::kable(pad( AVtable2T,
surroundMaths = TRUE,
targetLength = c(2, 2, 3, 2, 3, 2, 2, 2, 2, 2),
decDigits = 0),
format = "html",
longtable = FALSE,
escape = FALSE,
align = "c", # Otherwise adds a space after five lines...
booktabs = TRUE,
caption = "Responses to three scenarios for the AV data for $400$ respondents (rows sum to $n = 400$).") %>%
row_spec(0, bold = TRUE) %>%
column_spec(1, bold = TRUE) %>%
add_header_above( c(" " = 1,
"Unsafe" = 2,
"unsafe" = 2,
"Neutral" = 2,
"safe" = 2,
"Safe" = 2),
align = "c",
line = TRUE,
bold = TRUE) %>%
add_header_above( c(" " = 1,
" " = 2,
"Somewhat" = 2,
" " = 2,
"Somewhat" = 2,
" " = 2),
align = "c",
line = FALSE,
bold = TRUE)
} else {
knitr::kable( pad( AVtable2T,
surroundMaths = TRUE,
targetLength = c(2, 2, 3, 2, 3, 2, 2, 2, 2, 2),
decDigits = 0),
format = "latex",
booktabs = TRUE,
caption = "Responses to three scenarios for the AV data for $400$ respondents (rows sum to $n = 400$).",
align = "c",
longtable = FALSE,
escape = FALSE) %>%
kable_styling(font_size = 8) %>%
row_spec(0, bold = TRUE) %>%
column_spec(1, bold = TRUE) %>%
add_header_above( c(" " = 1,
"Unsafe" = 2,
"unsafe" = 2,
"Neutral" = 2,
"safe" = 2,
"Safe" = 2),
align = "c",
line = TRUE,
bold = TRUE) %>%
add_header_above( c(" " = 1,
" " = 2,
"Somewhat" = 2,
" " = 2,
"Somewhat" = 2,
" " = 2),
align = "c",
line = FALSE,
bold = TRUE)
}
```
## Graphs for qualitative data {#QualitativeGraphs}
\index{Qualitative data!graphs}\index{Graphs!qualitative data}\index{Software output!graphs}
Three options for graphing qualitative data include:
* *Dot charts* (Sect.\ \@ref(DotChartsOneQual)):
usually a good choice.
* *Bar charts* (Sect,\ \@ref(BarCharts)):
usually a good choice.
* *Pie charts* (Sect.\ \@ref(PieCharts)):
only useful in special circumstances, and can be hard to interpret.
Sometimes these graphs are used for *discrete* quantitative data with a small number of possible options.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
The purpose of a graph is to display the information in the clearest, simplest possible way, to facilitate understanding the message(s) in the data.
:::
### Dot charts (qualitative data) {#DotChartsOneQual}
\index{Graphs!dot chart!one qualitative variable}
Dot charts indicate the counts (or corresponding percentages) in each level using dots (or some other symbol).
The levels can be on the horizontal or vertical axis, and the counts or percentages on the other.
Placing the levels on the vertical axis often makes for easier reading, and space for long labels.
::: {.example #DotPlotsQual name="Dot plots"}
For the AV study in Example\ \@ref(exm:AVstudy), a dot chart of the age group of respondents is shown in Fig.\ \@ref(fig:AVDotBarPie) (top left panel).
:::
```{r, AVDotBarPie, fig.align='center', fig.cap="The age group of respondents in the AV study. All graphs present the same data.", out.width='100%', fig.width=9, fig.height=5.25, fig.show='hold'}
par(mfrow = c(2, 2))
par( mar = c(6, 4, 4, 2) + 0.1 )
if( knitr::is_latex_output()) {
cols <- grey.colors(n = 6,
start = 0.15,
end = 0.9)
} else {
cols <- viridis(9)[3:8]
}
AVdemographics2 <- AVdemographics
AVdemographics2.names <- names(AVdemographics2)
AVdemographics2.names <- gsub(pattern = "\\$",
replacement = "",
x = AVdemographics2.names)
names(AVdemographics2) <- AVdemographics2.names
dotchart(AVdemographics2[3:8],
pch = 21,
labels = rep(" ", 6), # Otherwise, coloured and hard to read; reinstate after
xlim = c(0, 80),
xlab = "Numbers of respondents",
main = "Age group of respondents\nin the AV study",
bg = cols)
text(labels = AVdemographics2.names[3],
x = -5,
y = 1,
adj = 1)
text(labels = AVdemographics2.names[4],
x = -5,
y = 2,
adj = 1)
text(labels = AVdemographics2.names[5],
x = -5,
y = 3,
adj = 1)
text(labels = AVdemographics2.names[6],
x = -5,
y = 4,
adj = 1)
text(labels = AVdemographics2.names[7],
x = -5,
y = 5,
adj = 1)
text(labels = AVdemographics2.names[8],
x = -5,
y = 6,
adj = 1)
###
barplot(AVdemographics2[3:8],
las = 2,
ylim = c(0, 80),
ylab = "Numbers of respondents",
main = "Age group of respondents\nin the AV study",
col = cols)
###
par( mar = c(0.4, 0.4, 3.4, 0.4) + 0.1 )
pie(AVdemographics2[3:8],
main = "Age group of respondents\nin the AV study",
col = cols)
###
par( mar = c(0.1, 0.1, 3.4, 0.1) + 0.1 )
# Add some spaces so the labels do not overlap pie chart
names(AVdemographics2)[3] <- paste0(" ", names(AVdemographics2)[3])
names(AVdemographics2)[5] <- paste0(names(AVdemographics2)[5], " ")
names(AVdemographics2)[6] <- paste0(names(AVdemographics2)[6], " ")
names(AVdemographics2)[8] <- paste0(" ", names(AVdemographics2)[8] )
plotrix::pie3D( AVdemographics2[3:8],
main = "Age group of respondents\nin the AV study",
theta = pi/4,
labels = names(AVdemographics2[3:8]),
labelcex = 0.9,
col = cols)
```
For dot charts:
* place the qualitative variable on the horizontal or vertical axis (and label with the levels of the variable).
* use counts or percentages on the other axis.
* for nominal data, *think about the most helpful order* for the levels.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
The axis displaying the counts (or percentages) should *start from zero*, since the distance of the dots from the axis visually implies the frequency of those observations (see Example\ \@ref(exm:VerticalTruncation)).
:::
### Bar charts {#BarCharts}
\index{Graphs!bar chart}
Bar charts use bars to represent the number (or percentage) of observations in each level.
As with dot charts, the levels can be on the horizontal or vertical axis, but placing the level names on the vertical axis often makes for easier reading, and room for long labels.
::: {.example #BarchartQual name="Bar plots"}
For the AV study in Example\ \@ref(exm:AVstudy), a bar chart of the age group of respondents is shown in Fig.\ \@ref(fig:AVDotBarPie) (top right panel).
:::
For bar charts:
* place the qualitative variable on the horizontal or vertical axis (and label with the levels of the variable).
* use counts or percentages on the other axis.
* for nominal data, levels can be ordered any way: *think about the most helpful order*.
* bars have gaps between bars, as the bars represent distinct categories.
In contrast to bar charts, the bars in histograms are butted together (except when an interval has a count of zero), as the variable-axis usually represents a continuous numerical scale.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
The axis displaying the counts (or percentages) should *start from zero*, since the height of the bars visually implies the frequency of those observations (see Example\ \@ref(exm:VerticalTruncation)).
:::
### Pie charts {#PieCharts}
\index{Graphs!pie chart}
In pie charts, a circle is divided into segments proportional to the number in each level of the qualitative variable.
::: {.example #PieChartsQual name="Pie charts"}
For the AV study in Example\ \@ref(exm:AVstudy), a pie chart of the age group of respondents is shown in Fig.\ \@ref(fig:AVDotBarPie) (bottom left panel).
:::
Using pie charts may present challenges:
* Pie charts only work when graphing parts of a whole.
* Pie charts only work when *all* options are present ('exhaustive').
* Pie charts are difficult to use with levels having zero or small counts (see Example\ \@ref(fig:PieSmallCounts)).
* Pie charts are difficult to interpret when many categories are present.
* Pie charts are hard to read: humans compare *lengths* (bar and dot charts) better than *angles* (pie charts) [@data:Friel:Graphs].
::: {.example #PieUnsuitable name="Pie chart unsuitable"}
Consider studying the percentage of people who use Firefox, Chrome, and Safari as web browsers.
A pie chart is *not suitable* for displaying the data, as people can use more than one of these browsers (i.e., the options are not *mutually exclusive*) nor *exhaustive* (i.e., other options exist).
:::
### Comparing dot, bar and pie charts {#CompareBarPie}
\index{Graphs!bar chart!compared to other graphs}\index{Graphs!pie chart!compared to other graphs}\index{Graphs!dot chart!compared to other graphs}
Consider the pie chart in Fig.\ \@ref(fig:AVDotBarPie) (bottom left panel).
Determining *which* age groups have the fewest and the most respondents is hard.
The equivalent bar chart or dot chart makes the comparison easy: the youngest age group has the fewest respondents, while the\ $25$ to\ $34$ and\ $35$ to\ $44$ age groups have the most.
The *tilted* pie chart makes this comparison even harder (Fig.\ \@ref(fig:AVDotBarPie), bottom right panel).
Recall that the *purpose of a graph is to display the information in the clearest, simplest possible way, to facilitate understanding the message(s) in the data*.
A pie chart often makes the message hard to see [@siegrist1996use].\index{Graphs!pie chart!warnings}
<iframe src="https://learningapps.org/watch?v=pf4om4k5t22" style="border:0px;width:100%;height:500px" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
## Numerical summary: proportions and percentages {#QualitativeProportionsPercentages}
\index{Proportions}\index{Percentages}
Qualitative data can be summarised numerically by using the *proportion* or *percentage* of individuals in each level.
These can be given instead of, or with, the counts (Tables\ \@ref(tab:AVtable1) and\ \@ref(tab:AVtable2)).
::: {.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 (the parameter), denoted\ $p$, is estimated by a *sample* proportion (a statistic), denoted by\ $\hat{p}$.\index{Estimate}
::: {.pronounceBox .pronounce data-latex="{iconmonstr-microphone-7-240.png}"}
The symbol\ $\hat{p}$ is pronounced 'pee-hat', and refers to a *sample* proportion.
The caret above the\ $p$ is called a 'hat'.
:::
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
As always, only one possible sample is studied.
*Statistics* are estimates of *parameters*, and the value of the *statistic* is not the same for every possible *sample*.
:::
::: {.example #AVProportionsPercentages name="Proportions and percentages"}
Consider the AV data in Table\ \@ref(tab:AVtable1), summarising results from a sample of $n = 400$ respondents.
The *sample proportion* of respondents aged\ $25$ to\ $34$ is $76\div 400$, or\ $0.19$.
The *sample percentage* of respondents aged\ $25$ to\ $34$ is $0.19 \times 100$, or\ $19$%, as in the table.
:::
## Numerical summary: odds {#QualOdds}
\index{Odds}
For the AV data in Table\ \@ref(tab:AVtable1), the number of females is slightly larger than the number of males.
More specifically, the *ratio* of females to males is $204\div 196 = 1.04$; that is, there are\ $1.04$ *times* as many females as males.
This value of\ $1.04$ is the *odds* that a respondent is female.
An alternative interpretation is that there are $1.04\times 100 = 104$ females for every\ $100$ males.
While proportions and percentages are computed as the number of results of interest divided by the *total number*, the *odds* are computed as the number of results of interest divided by *the remaining number*.
::: {.definition #Odds name="Odds"}
The *odds* are the number (or proportion, or percentage) of results of interest, divided by the remaining number (or proportion, or percentage) of results:
$$
\text{Odds} = \frac{\text{Number of results of interest}}{\text{Remaining number of results}}
$$
or (equivalently)
$$
\text{Odds}
=
\frac{\text{Proportion of results of interest}}
{\text{Remaining proportion of results}}
=
\frac{\text{Percentage of results of interest}}
{\text{Remaining percentage of results}}.
$$
The *odds* are how many *times* the result of interest *occurs* compared to the number of times the results of interest does *not occur*.
:::
::: {.example #AVOddsMale name="Interpreting odds"}
The AV data (Table\ \@ref(tab:AVtable1)) includes\ $204$ females and $196$\ males.
The *odds* that a respondent is female is\ $1.04$.
The odds are greater than one, as there are more females than males.
Alternatively, there are\ $104$ females for every\ $100$ males.
The *odds* that a respondent is male is $196/204 = 0.96$; there are $0.96$\ *times* the number of males as females.
The odds are less than one, as there are fewer males than females.
Alternatively, there are $96$\ males for every\ $100$ females.
:::
When interpreting odds:
* odds *greater* than\ $1$ mean the event is *more* likely to happen than not.
* odds *equal to*\ $1$ mean the event is *equally likely* to happen as not.
* odds *less* than\ $1$ mean the event is *less* likely to happen than not.
::: {.example #AVOdds name="Odds and percentages"}
Consider the AV data in Table\ \@ref(tab:AVtable1), summarising results from a sample of $n = 400$ respondents.
The percentage of respondents aged\ $18$ to\ $24$ is $52/400\times 400 = 13$%.
The *odds* that a respondent is aged\ $18$ to\ $24$ is $52/(400 - 52) = 0.15$; that is, the odds that a respondent is aged\ $18$ to\ $24$ is\ $0.15$.
This means that the number of respondents aged\ $18$ to\ $24$ is $0.15$\ times (i.e., less) the number of respondents aged over\ $24$.
The *odds* that a respondent is aged\ $18$ to\ $54$ is $(52 + 76 + 76 + 72)/(56 + 68) = 2.23$; that is, the odds that a respondent is aged\ $18$ to\ $54$ is\ $2.23$.
This means that the number of respondents aged\ $18$ to\ $54$ is\ $2.23$ times (i.e., greater) the number of respondents aged\ $55$ or over.
:::
*Population* odds are almost always unknown.
Instead, the *population* odds (the parameter) is estimated by a *sample* odds (a statistic).
No symbol is commonly used to denote odds.
Take care: proportions and odds are similar, but are different ways of numerically summarising quantitative data (Fig.\ \@ref(fig:PropOdds)).
```{r PropOdds, fig.align="center", out.width='90%', fig.width=9, fig.height=2.25, fig.cap="Proportions (left) are the number of interest divided by the total number. Odds (right) are the number of interest divided by the rest."}
source("R/showPropOdds.R")
showPropOdds()
```
## Describing the distribution: modes and medians {#SummariseDataQualitative}
\index{Qualitative data!distribution}
Graphs are constructed to help readers understand the data, so any important features in the graph should be described.
One simple way is to identify the level (or levels) with the *most* observations.
This is called the *mode*.\index{Mode}
::: {.definition #Mode name="Mode"}
A *mode* is the level (or levels) of a qualitative variable with the most observations.
:::
::: {.example #OrdinalModes name="Modes" }
Consider the data in Tables\ \@ref(tab:AVtable1) and\ \@ref(tab:AVtable2):
* The *mode* for gender is 'Female' (with\ $204$ respondents, or\ $51$%).
* The *mode* age groups are $25$ to\ $34$ and $35$ to\ $44$ (each with $19$\ respondents, or\ $4.8$%).
* The *modal* response to the question about *driving* near AVs is 'Somewhat safe'.
* The *modal* response to the question about *cycling* near AVs is 'Somewhat unsafe'.
* The *modal* response to the question about *walking* near AVs is 'Neutral'.
:::
*Medians*\index{Median!qualitative ordinal data} can be found for *ordinal* data (but *not* nominal data), since ordinal data have levels with a natural order.
The *median* is the location of the middle response, when the levels from all individuals are placed in order.
The sample median estimates the unknown *population* median.
::: {.importantBox .important data-latex="{iconmonstr-warning-8-240.png}"}
Medians can be used to summarise *quantitative data* and *ordinal* data, but *never* nominal data.
:::
::: {.example #OrdinalMedians name="Medians"}
Consider the data in Tables\ \@ref(tab:AVtable1) and\ \@ref(tab:AVtable2).
'Gender' is *nominal* qualitative, so medians are not appropriate.
However, the other variables are *ordinal*, so medians could be used to describe each variable.
Since $n = 400$, the median response will be halfway between the location of the\ $200$th and\ $201$st response when ordered:
* the *median* age group is $35$ to\ $44$.
* the *median* response to the driving-near-AVs question is 'Neutral'.
* the *median* response to the cycling-near-AVs question is 'Neutral'.
* the *median* response to the walking-near-AVs question is 'Neutral'.
For each variable, ordered observations\ $200$ and\ $201$ fall into the indicated level.
:::
Importantly, all these numerical quantities are computed from a sample (i.e., are statistics; Def.\ \@ref(def:Statistic)), even though the whole population is of interest (i.e., the parameter; Def.\ \@ref(def:Parameter)).
Means (Sect.\ \@ref(Mean)) are generally not suitable for numerically summarising qualitative data.
However, *ordinal* data *may be* numerically summarised like quantitative data in *rare and very special circumstances*: when
* the levels are considered equally spaced; *and*
* assigning a number to each level is appropriate (for example, using a mid-point for numerical age groups).
We will not consider means for ordinal data further.
## Numerical summary tables {#QualSummaryTable}
\index{Qualitative data!summary tables}
Qualitative variables should be summarised in a table.
The table should include, as a minimum, numbers and percentages for each level.
While useful in other contexts (see Chap.\ \@ref(CompareQualData)), odds are usually not given in summary table.
Examples are shown in Tables\ \@ref(tab:AVtable1) and\ \@ref(tab:AVtable2), and in the next section.
## Example: water access {#WaterAccessQual}
@lopez2022farmers recorded data about access to water for three rural communities in Cameroon (see Sect.\ \@ref(WaterAccessQuant)).
Numerous qualitative variables are recorded; some are displayed in Fig.\ \@ref(fig:WaterAcessQual), and summarised in Table\ \@ref(tab:WaterAccessQual).
Notice that the levels of the two ordinal variables are displayed in their natural order.
The distance to the nearest water source is usually less than\ $1\kms$, and the wait is often over\ $15\mins$.
The most common water source is a bore ($68.6$%).
```{r WaterAccessQual}
data(WaterAccess)
WaterAccessTable2A <- array( dim = c(6, 3) )
findOdds <- function(x){
tablex <- table(x)
oddsTab <- array( dim = length(tablex))
for (i in 1:length(tablex)){
oddsTab[i] <- tablex[i] / sum(tablex[-i])
}
oddsTab
}
qualSummary <- function(x){
out <- cbind( pad2( t(t(table(x))),
targetLength = 2,
surroundMaths = TRUE),
pad2( t( t( table(x) ) / sum(table(x)) * 100),
decDigits = 1,
targetLength = 4,
surroundMaths = TRUE),
pad2( t( t(findOdds(x)) ),
decDigits = 2,
targetLength = 4,
surroundMaths = TRUE )
)
out
}
qualSummary <- function(x){
tab <- table(x)
out <- array( dim = length(tab))
# for (i in 1:length(tab)){
# out <- cbind( pad( t(t(tab[i])),
# targetLength = 2,
# surroundMaths = TRUE),
# pad( format( round(t( t( tab[i] ) / sum(tab) * 100), 1), nsmall = 1),
# targetLength = 4,
# surroundMaths = TRUE),
# pad( format( round( t( t(findOdds(x)[i]) ), 2), nsmall = 2),
# targetLength = 4,
# surroundMaths = TRUE )
# )
out <- cbind( pad( t(t(tab)),
targetLength = 2,
decDigits = 0,
surroundMaths = TRUE),
pad( t( t( tab ) / sum(tab) * 100),
targetLength = 4,
decDigits = 1,
surroundMaths = TRUE),
pad( t( t(findOdds(x)) ),
decDigits = 2,
targetLength = 4,
surroundMaths = TRUE )
)
# }
out
}
WaterAccessTable2A <- rbind(qualSummary(WaterAccess$SourceDistance),
qualSummary(WaterAccess$SourceQueueTime) )
rownames(WaterAccessTable2A) <- c("Under $100\\ms$",
"$100\\ms$\\ to $1000\\ms$",
"Over $1000\\ms$",
"Under $5\\mins$",
"$5$ to $15\\mins$",
"Over $15\\mins$")
colnames(WaterAccessTable2A) <- c("Number",
"\\%",
"Odds")
###
WaterAccessTable2B <- array( dim = c(4, 3) )
rownames(WaterAccessTable2B) <- c( levels(WaterAccess$WaterSource) )
WaterAccessTable2B <- qualSummary(WaterAccess$WaterSource)
colnames(WaterAccessTable2B) <- c("Number",
"\\%",
"Odds")
if( knitr::is_latex_output() ) {
T1 <- kable( WaterAccessTable2A,
format = "latex",
longtable = FALSE,
escape = FALSE,
col.names = colnames(WaterAccessTable2A),
valign = 't',
booktabs = TRUE,
align = c("c")) %>%
row_spec(0, bold = TRUE) %>%
pack_rows("Distance to water source ($n = 121$)",
start_row = 1,
end_row = 3,
escape = FALSE,
bold = TRUE) %>%
pack_rows("Wait time at water source ($n = 120$)",
start_row = 4,
end_row = 6,
escape = FALSE,
bold = TRUE)
T2 <- kable( WaterAccessTable2B,
format = "latex",
longtable = FALSE,
escape = FALSE,
col.names = colnames(WaterAccessTable2B),
valign = 't',
booktabs = TRUE,
align = c("c")) %>%
row_spec(0, bold = TRUE) %>%
pack_rows("Water source ($n = 121$)",
start_row = 1,
end_row = 4,
escape = FALSE,
bold = TRUE)
out <- knitr::kables(list(T1, T2),
format = "latex",
label = "WaterAccessQual",
caption = "Summarising some qualitative data in the water-access study. Left: the ordinal variables. Right: the nominal variable.") %>%
kable_styling(font_size = 8)
out2 <- prepareSideBySideTable(out,
gap = "\\qquad\\qquad")
out2
}
if( knitr::is_html_output() ) {
kable( rbind(WaterAccessTable2A,
WaterAccessTable2B),
format = "html",
longtable = FALSE,
col.names = colnames(WaterAccessTable2A),
booktabs = TRUE,
align = c("r"),
caption = "Summarising some qualitative data in the water-access study. Left: the ordinal variables. Right: the nominal variable.") %>%
kable_styling(full_width = FALSE) %>%
pack_rows("Distance to water source",
start_row = 1,
end_row = 3,
bold = TRUE) %>%
pack_rows("Wait time at water source",
start_row = 4,
end_row = 6,
bold = TRUE) %>%
pack_rows("Water source",
start_row = 7,
end_row = 10,
bold = TRUE)
}
```
```{r WaterAcessQual, fig.align="center", fig.cap="The distance to the water source (left), the wait time at the water source (centre), and the water sources (right) for the water-access study. (Some data are missing.)", out.width='100%', fig.width=6.25, fig.height=1.7}
par(mfrow = c(1, 3),
mar = c(3.75, 0.2, 4, 1.75) )
dotchart( as.numeric(table(WaterAccess$SourceDistance)),
labels = names(table(WaterAccess$SourceDistance)),
xlab = "Number",
ylab = "",
main = "Distance to\nwater source",
xlim = c(0, 60),
las = 2,
pch = 19)
dotchart( as.numeric(table(WaterAccess$SourceQueueTime)),
labels = names(table(WaterAccess$SourceQueueTime)),
xlab = "Number",
ylab = "",
main = "Queue wait at\nwater source",
xlim = c(0, 55),
las = 2,
pch = 19)
dotchart( as.numeric(table(WaterAccess$WaterSource)),
labels = names(table(WaterAccess$WaterSource)),
xlab = "Number",
ylab = "",
main = "Water sources",
xlim = c(0, 90),
las = 2,
pch = 19)
```
\index{Qualitative data!summarising|)}
## Chapter summary {#SummaryQual-Summary}
Qualitative data can be graphed with a dot chart, bar chart or pie chart (in special circumstances).
Qualitative data can be described using the mode or (for *ordinal* data only) a median.
Qualitative data can be numerically summarised using *proportions*, *percentages* or *odds*.
## Quick review questions {#SummaryQual-QuickReview}
Are the following statements *true* or *false*?
::: {.webex-check .webex-box}
1. Nominal data can be summarised using a median. \tightlist
`r if( knitr::is_html_output() ) {torf(answer = FALSE)}`
1. Ordinal data can be summarised using a mode.
`r if( knitr::is_html_output() ) {torf(answer = TRUE)}`
1. Odds are the ratio of how often a result of interest occurs, to how often it does *not* occur.
`r if( knitr::is_html_output() ) {torf(answer = TRUE)}`
1. Proportions and percentages are the same.
`r if( knitr::is_html_output() ) {torf(answer = FALSE)}`
:::
## Exercises {#SummariseQualDataExercises}
[Answers to odd-numbered exercises] are given at the end of the book.
`r if( knitr::is_latex_output() ) "\\captionsetup{font=small}"`
::: {.exercise #SpiderMonkeys}
A study of spider monkeys [@data:Chapman1990:SpiderMonkeys] examined the types of social groups present
`r if (knitr::is_latex_output()) {
'(Table\\ \\@ref(tab:SpiderMonkeysLATEX)).'
} else {
'(Table\\ \\@ref(tab:SpiderMonkeysHTML)).'
}`
Construct a suitable plot, and explain what the data reveal.
:::
```{r SpiderMonkeysData}
namesSM <- c("Solitary",
"All males",
"Female + no young",
"Mixed young",
"Mixed + no young",
"One female + offspring",
"Many females + offspring",
NA)
SpiderMonkeyTable <- array( dim = c(8, 2) )
SpiderMonkeyTable[, 2] <- c(8, 3, 2, 15, 1, 23, 48, NA)
SpiderMonkeyTable[, 1] <- namesSM
rownames(SpiderMonkeyTable) <- namesSM
```
```{r}
if( knitr::is_latex_output() ) {
T1 <- kable( pad( SpiderMonkeyTable[1:4,],
surroundMaths = TRUE,
targetLength = 2,
decDigits = 0),
format = "latex",
longtable = FALSE,
booktabs = TRUE,
escape = FALSE,
digits = 1,
align = c("r", "c"),
row.names = FALSE,
col.names = c("Social group",
"Number")) %>%
row_spec(row = 0,
bold = TRUE)
T2 <- kable( pad( SpiderMonkeyTable[5:8,],
surroundMaths = TRUE,
targetLength = 2,
decDigits = 0),
format = "latex",
longtable = FALSE,
booktabs = TRUE,
escape = FALSE,
digits = 1,
align = c("r", "c"),
row.names = FALSE,
col.names = c("Social group",
"Number")) %>%
row_spec(row = 0,
bold = TRUE)
out <- knitr::kables(list(T1, T2),
format = "latex",
label = "SpiderMonkeysLATEX",
caption = "Social groups for soldier monkeys.") %>%
kable_styling(font_size = 8)
out2 <- prepareSideBySideTable(out,
gap = "\\qquad")
out2
}
```
```{r SpiderMonkeysHTML}
if( knitr::is_html_output() ) {
kable( pad( SpiderMonkeyTable,
surroundMaths = TRUE,
targetLength = 2,
decDigits = 0),
format = "html",
longtable = FALSE,
booktabs = TRUE,
escape = FALSE,
digits = 1,
align = c("r", "c"),
row.names = FALSE,
col.names = c("Social group",
"Number"),
caption = "Social groups in spider monkeys.") %>%
kable_styling(full_width = FALSE) %>%
row_spec(row = 0,
bold = TRUE)
}
```
::: {.exercise #QualSummary}
@czarniecka2021consumer studied how Poles prepared and consumed coffee using a sample of $1\,500$\ Poles.
Some data are shown in Table\ \@ref(tab:CoffeePoles).
1. Classify the variables as quantitative, nominal or ordinal.
2. Sketch appropriate graphs for the three variables.
3. Summarise the three variables.
:::
```{r}
Coffee1 <- array(dim = c(5, 2))
Coffee1[, 2] <- c(1432, 687, 922, 994, 1196)
Coffee1[, 1] <- c("Home",
"Canteen",
"Cafe",
"Others' homes",
"Work")
T1 <- kable( pad(Coffee1,
surroundMaths = TRUE,
targetLength = 4,
decDigits = 0),
format = "latex",
longtable = FALSE,
booktabs = TRUE,
escape = FALSE,
valign = 't',
col.names = c("Where consumed", "$n$"),
align = c("r", "c") ) %>%