-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_scores.html
2196 lines (2154 loc) · 150 KB
/
standard_scores.html
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 xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.6.1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>16 Ranks, Quantiles and Standard Scores – Resampling statistics</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { display: inline-block; 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;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
/* CSS for citations */
div.csl-bib-body { }
div.csl-entry {
clear: both;
margin-bottom: 0em;
}
.hanging-indent 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>
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<link href="./inference_ideas.html" rel="next">
<link href="./monte_carlo.html" rel="prev">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "sidebar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "start",
"type": "textbox",
"limit": 50,
"keyboard-shortcut": [
"f",
"/",
"s"
],
"show-item-context": false,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-text-placeholder": "",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
}
}</script>
<script type="text/javascript">
$(document).ready(function() {
$("table").addClass('lightable-paper lightable-striped lightable-hover')
});
</script>
<script src="site_libs/kePrint-0.0.1/kePrint.js"></script>
<link href="site_libs/lightable-0.0.1/lightable.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
<script type="text/javascript">
const typesetMath = (el) => {
if (window.MathJax) {
// MathJax Typeset
window.MathJax.typeset([el]);
} else if (window.katex) {
// KaTeX Render
var mathElements = el.getElementsByClassName("math");
var macros = [];
for (var i = 0; i < mathElements.length; i++) {
var texText = mathElements[i].firstChild;
if (mathElements[i].tagName == "SPAN") {
window.katex.render(texText.data, mathElements[i], {
displayMode: mathElements[i].classList.contains('display'),
throwOnError: false,
macros: macros,
fleqn: false
});
}
}
}
}
window.Quarto = {
typesetMath
};
</script>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="font-awesome.min.css">
</head>
<body class="nav-sidebar floating">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="quarto-secondary-nav">
<div class="container-fluid d-flex">
<button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" role="button" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<i class="bi bi-layout-text-sidebar-reverse"></i>
</button>
<nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./standard_scores.html"><span class="chapter-number">16</span> <span class="chapter-title">Ranks, Quantiles and Standard Scores</span></a></li></ol></nav>
<a class="flex-grow-1" role="navigation" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
</a>
<button type="button" class="btn quarto-search-button" aria-label="Search" onclick="window.quartoOpenSearch();">
<i class="bi bi-search"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation floating overflow-auto">
<div class="pt-lg-2 mt-2 text-left sidebar-header">
<div class="sidebar-title mb-0 py-0">
<a href="./">Resampling statistics</a>
</div>
</div>
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./index.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">R version</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./preface_third.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Preface to the third edition</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./preface_second.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Preface to the second edition</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./intro.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">1</span> <span class="chapter-title">Introduction</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./resampling_method.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">2</span> <span class="chapter-title">The resampling method</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./what_is_probability.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">3</span> <span class="chapter-title">What is probability?</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./about_technology.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">4</span> <span class="chapter-title">Introducing R and the Jupyter notebook</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./resampling_with_code.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">5</span> <span class="chapter-title">Resampling with code</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./resampling_with_code2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">6</span> <span class="chapter-title">More resampling with code</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./sampling_tools.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">7</span> <span class="chapter-title">Tools for samples and sampling</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_1a.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">8</span> <span class="chapter-title">Probability Theory, Part 1</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_1b.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">9</span> <span class="chapter-title">Probability Theory Part I (continued)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./more_sampling_tools.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">10</span> <span class="chapter-title">Two puzzles and more tools</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_2_compound.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">11</span> <span class="chapter-title">Probability Theory, Part 2: Compound Probability</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_3.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">12</span> <span class="chapter-title">Probability Theory, Part 3</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_4_finite.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">13</span> <span class="chapter-title">Probability Theory, Part 4: Estimating Probabilities from Finite Universes</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./sampling_variability.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">14</span> <span class="chapter-title">On Variability in Sampling</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./monte_carlo.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">15</span> <span class="chapter-title">The Procedures of Monte Carlo Simulation (and Resampling)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./standard_scores.html" class="sidebar-item-text sidebar-link active">
<span class="menu-text"><span class="chapter-number">16</span> <span class="chapter-title">Ranks, Quantiles and Standard Scores</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./inference_ideas.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">17</span> <span class="chapter-title">The Basic Ideas in Statistical Inference</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./inference_intro.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">18</span> <span class="chapter-title">Introduction to Statistical Inference</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./point_estimation.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">19</span> <span class="chapter-title">Point Estimation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./framing_questions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">20</span> <span class="chapter-title">Framing Statistical Questions</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_counts_1.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">21</span> <span class="chapter-title">Hypothesis-Testing with Counted Data, Part 1</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./significance.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">22</span> <span class="chapter-title">The Concept of Statistical Significance in Testing Hypotheses</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_counts_2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">23</span> <span class="chapter-title">The Statistics of Hypothesis-Testing with Counted Data, Part 2</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_measured.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">24</span> <span class="chapter-title">The Statistics of Hypothesis-Testing With Measured Data</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_procedures.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">25</span> <span class="chapter-title">General Procedures for Testing Hypotheses</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./confidence_1.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">26</span> <span class="chapter-title">Confidence Intervals, Part 1: Assessing the Accuracy of Samples</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./confidence_2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">27</span> <span class="chapter-title">Confidence Intervals, Part 2: The Two Approaches to Estimating Confidence Intervals</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./reliability_average.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">28</span> <span class="chapter-title">Some Last Words About the Reliability of Sample Averages</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./correlation_causation.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">29</span> <span class="chapter-title">Correlation and Causation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./how_big_sample.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">30</span> <span class="chapter-title">How Large a Sample?</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./bayes_simulation.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">31</span> <span class="chapter-title">Bayesian Analysis by Simulation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./references.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">References</span></a>
</div>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" role="navigation" aria-expanded="true">
<span class="menu-text">Appendices</span></a>
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" role="navigation" aria-expanded="true" aria-label="Toggle section">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-1" class="collapse list-unstyled sidebar-section depth1 show">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./exercise_solutions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">A</span> <span class="chapter-title">Exercise Solutions</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./technical_note.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">B</span> <span class="chapter-title">Technical Note to the Professional Reader</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./acknowlegements.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">C</span> <span class="chapter-title">Acknowledgements</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./code_topics.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">D</span> <span class="chapter-title">Code topics</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./errors_suggestions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">E</span> <span class="chapter-title">Errors and suggestions</span></span></a>
</div>
</li>
</ul>
</li>
</ul>
</div>
</nav>
<div id="quarto-sidebar-glass" class="quarto-sidebar-collapse-item" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item"></div>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#household-income-and-congressional-districts" id="toc-household-income-and-congressional-districts" class="nav-link active" data-scroll-target="#household-income-and-congressional-districts"><span class="header-section-number">16.1</span> Household income and congressional districts</a>
<ul class="collapse">
<li><a href="#sec-csv-format" id="toc-sec-csv-format" class="nav-link" data-scroll-target="#sec-csv-format"><span class="header-section-number">16.1.1</span> Comma-separated-values (CSV) format</a></li>
<li><a href="#introducing-r-data-frames" id="toc-introducing-r-data-frames" class="nav-link" data-scroll-target="#introducing-r-data-frames"><span class="header-section-number">16.1.2</span> Introducing R data frames</a></li>
<li><a href="#incomes-and-ranks" id="toc-incomes-and-ranks" class="nav-link" data-scroll-target="#incomes-and-ranks"><span class="header-section-number">16.1.3</span> Incomes and Ranks</a></li>
<li><a href="#calculating-ranks" id="toc-calculating-ranks" class="nav-link" data-scroll-target="#calculating-ranks"><span class="header-section-number">16.1.4</span> Calculating ranks</a></li>
</ul></li>
<li><a href="#comparing-two-values-in-the-district-income-data" id="toc-comparing-two-values-in-the-district-income-data" class="nav-link" data-scroll-target="#comparing-two-values-in-the-district-income-data"><span class="header-section-number">16.2</span> Comparing two values in the district income data</a></li>
<li><a href="#comparing-values-with-ranks-and-quantile-positions" id="toc-comparing-values-with-ranks-and-quantile-positions" class="nav-link" data-scroll-target="#comparing-values-with-ranks-and-quantile-positions"><span class="header-section-number">16.3</span> Comparing values with ranks and quantile positions</a></li>
<li><a href="#unusual-values-compared-to-the-distribution" id="toc-unusual-values-compared-to-the-distribution" class="nav-link" data-scroll-target="#unusual-values-compared-to-the-distribution"><span class="header-section-number">16.4</span> Unusual values compared to the distribution</a></li>
<li><a href="#on-deviations" id="toc-on-deviations" class="nav-link" data-scroll-target="#on-deviations"><span class="header-section-number">16.5</span> On deviations</a></li>
<li><a href="#the-mean-absolute-deviation" id="toc-the-mean-absolute-deviation" class="nav-link" data-scroll-target="#the-mean-absolute-deviation"><span class="header-section-number">16.6</span> The mean absolute deviation</a></li>
<li><a href="#sec-standard-deviation" id="toc-sec-standard-deviation" class="nav-link" data-scroll-target="#sec-standard-deviation"><span class="header-section-number">16.7</span> The standard deviation</a>
<ul class="collapse">
<li><a href="#sec-squared-arrays" id="toc-sec-squared-arrays" class="nav-link" data-scroll-target="#sec-squared-arrays"><span class="header-section-number">16.7.1</span> Squares of values and vectors</a></li>
<li><a href="#calculating-the-standard-deviation" id="toc-calculating-the-standard-deviation" class="nav-link" data-scroll-target="#calculating-the-standard-deviation"><span class="header-section-number">16.7.2</span> Calculating the standard deviation</a></li>
</ul></li>
<li><a href="#standard-scores" id="toc-standard-scores" class="nav-link" data-scroll-target="#standard-scores"><span class="header-section-number">16.8</span> Standard scores</a></li>
<li><a href="#sec-standard-scores" id="toc-sec-standard-scores" class="nav-link" data-scroll-target="#sec-standard-scores"><span class="header-section-number">16.9</span> Standard scores to compare values on different scales</a></li>
<li><a href="#conclusion" id="toc-conclusion" class="nav-link" data-scroll-target="#conclusion"><span class="header-section-number">16.10</span> Conclusion</a></li>
</ul>
</nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title"><span class="chapter-number">16</span> <span class="chapter-title">Ranks, Quantiles and Standard Scores</span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p>Imagine we have a set of measures, in some particular units. We may want some way to see quickly how these measures compare to one another, and how they may compare to other measures, in different units.</p>
<p>Ranks are one way of having an implicit comparison between values.<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a> Is the value large in terms of the other values (with high rank) — or is it small (low rank)?</p>
<p>We can convert ranks to quantile positions. Quantile positions are values from 0 through 1 that are closer to 1 for high rank values, and closer to 0 for low rank values. The smallest value (and the value with the lowest rank) will have quantile position 0, the largest value (highest rank) will have quantile position 1. Each value in the data has a rank, and a corresponding quantile position. We can also look at the <em>value</em> corresponding to each quantile position, and these are the <em>quantile values</em>, usually called simply — <em>quantiles</em>. You will see what we mean later in the chapter.</p>
<p>Ranks and quantile positions give an idea whether the measure is high or low compared to the other values, but they do not immediately tell us whether the measure is exceptional or unusual. To do that, we may want to ask whether the measure falls outside the typical range of values — that is, how the measure compares to the <em>distribution</em> of values. One common way of doing this is to re-express the measures (values) as <em>standard scores</em>, where the standard score for a particular value tells you how far the value is from the <em>center</em> of the distribution, in terms of the typical <em>spread</em> of the distribution. (We will say more about what we mean by “typical” later.) Standard values are particularly useful to allow us to <em>compare</em> different types of measures on a standard scale. They translate the units of measurement into <em>standard</em> and comparable units.</p>
<section id="household-income-and-congressional-districts" class="level2" data-number="16.1">
<h2 data-number="16.1" class="anchored" data-anchor-id="household-income-and-congressional-districts"><span class="header-section-number">16.1</span> Household income and congressional districts</h2>
<p>Democratic <a href="https://en.wikipedia.org/wiki/Marcy_Kaptur">congresswoman Marcy Kaptur</a> has represented the 9th district of Ohio since 1983. Ohio’s 9th district is relatively working class, and the Democratic party has, traditionally, represented people with lower income. However, Kaptur has pointed out that this pattern appears to be changing; more of the high-income congressional districts now lean Democrat, and the Republican party is now more likely to represent lower-income districts. The French economist <a href="https://en.wikipedia.org/wiki/Thomas_Piketty">Thomas Piketty</a> has described this phenomenon across several Western countries. Voters for left parties are now more likely to be highly educated and wealthy. He terms this shift “Brahmin Left Vs Merchant Right” <span class="citation" data-cites="piketty2018brahmin">(<a href="references.html#ref-piketty2018brahmin" role="doc-biblioref">Piketty 2018</a>)</span>. The data below come from a <a href="https://s3.documentcloud.org/documents/23726766/rep-kaptur-district-chart.pdf">table Kaptur prepared</a> that shows this pattern in the 2023 US congress. The table lists the top 20 districts by the median income of the households in that district, along with their representatives and their party.<a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a></p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="tbl-district_income" class="lightable-paper lightable-striped lightable-hover quarto-float quarto-figure quarto-figure-center anchored" data-quarto-postprocess="true" style="font-family: "Arial Narrow", arial, helvetica, sans-serif; width: auto !important; margin-left: auto; margin-right: auto;">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-district_income-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table 16.1: 20 most wealthy 2023 Congressional districts by household income
</figcaption>
<div aria-describedby="tbl-district_income-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="lightable-paper lightable-striped lightable-hover caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Ascending_Rank</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">District</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Median Income</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Representative</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Party</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">422</td>
<td style="text-align: right;">422</td>
<td style="text-align: left;">MD-3</td>
<td style="text-align: right;">114804</td>
<td style="text-align: left;">J. Sarbanes</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="even">
<td style="text-align: left;">423</td>
<td style="text-align: right;">423</td>
<td style="text-align: left;">MA-5</td>
<td style="text-align: right;">115618</td>
<td style="text-align: left;">K. Clark</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="odd">
<td style="text-align: left;">424</td>
<td style="text-align: right;">424</td>
<td style="text-align: left;">NY-12</td>
<td style="text-align: right;">116070</td>
<td style="text-align: left;">J. Nadler</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="even">
<td style="text-align: left;">425</td>
<td style="text-align: right;">425</td>
<td style="text-align: left;">VA-8</td>
<td style="text-align: right;">116332</td>
<td style="text-align: left;">D. Beyer</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="odd">
<td style="text-align: left;">426</td>
<td style="text-align: right;">426</td>
<td style="text-align: left;">MD-5</td>
<td style="text-align: right;">117049</td>
<td style="text-align: left;">S. Hoyer</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="even">
<td style="text-align: left;">427</td>
<td style="text-align: right;">427</td>
<td style="text-align: left;">NJ-11</td>
<td style="text-align: right;">117198</td>
<td style="text-align: left;">M. Sherrill</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="odd">
<td style="text-align: left;">428</td>
<td style="text-align: right;">428</td>
<td style="text-align: left;">NY-3</td>
<td style="text-align: right;">119185</td>
<td style="text-align: left;">G. Santos</td>
<td style="text-align: left;">Republican</td>
</tr>
<tr class="even">
<td style="text-align: left;">429</td>
<td style="text-align: right;">429</td>
<td style="text-align: left;">CA-14</td>
<td style="text-align: right;">119209</td>
<td style="text-align: left;">E. Swalwell</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="odd">
<td style="text-align: left;">430</td>
<td style="text-align: right;">430</td>
<td style="text-align: left;">NJ-7</td>
<td style="text-align: right;">119567</td>
<td style="text-align: left;">T. Kean</td>
<td style="text-align: left;">Republican</td>
</tr>
<tr class="even">
<td style="text-align: left;">431</td>
<td style="text-align: right;">431</td>
<td style="text-align: left;">NY-1</td>
<td style="text-align: right;">120031</td>
<td style="text-align: left;">N. LaLota</td>
<td style="text-align: left;">Republican</td>
</tr>
<tr class="odd">
<td style="text-align: left;">432</td>
<td style="text-align: right;">432</td>
<td style="text-align: left;">WA-1</td>
<td style="text-align: right;">120671</td>
<td style="text-align: left;">S. DelBene</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="even">
<td style="text-align: left;">433</td>
<td style="text-align: right;">433</td>
<td style="text-align: left;">MD-8</td>
<td style="text-align: right;">120948</td>
<td style="text-align: left;">J. Raskin</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="odd">
<td style="text-align: left;">434</td>
<td style="text-align: right;">434</td>
<td style="text-align: left;">NY-4</td>
<td style="text-align: right;">121979</td>
<td style="text-align: left;">A. D’Esposito</td>
<td style="text-align: left;">Republican</td>
</tr>
<tr class="even">
<td style="text-align: left;">435</td>
<td style="text-align: right;">435</td>
<td style="text-align: left;">CA-11</td>
<td style="text-align: right;">124456</td>
<td style="text-align: left;">N. Pelosi</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="odd">
<td style="text-align: left;">436</td>
<td style="text-align: right;">436</td>
<td style="text-align: left;">CA-15</td>
<td style="text-align: right;">125855</td>
<td style="text-align: left;">K. Mullin</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="even">
<td style="text-align: left;">437</td>
<td style="text-align: right;">437</td>
<td style="text-align: left;">CA-10</td>
<td style="text-align: right;">135150</td>
<td style="text-align: left;">M. DeSaulnier</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="odd">
<td style="text-align: left;">438</td>
<td style="text-align: right;">438</td>
<td style="text-align: left;">VA-11</td>
<td style="text-align: right;">139003</td>
<td style="text-align: left;">G. Connolly</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="even">
<td style="text-align: left;">439</td>
<td style="text-align: right;">439</td>
<td style="text-align: left;">VA-10</td>
<td style="text-align: right;">140815</td>
<td style="text-align: left;">J. Wexton</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="odd">
<td style="text-align: left;">440</td>
<td style="text-align: right;">440</td>
<td style="text-align: left;">CA-16</td>
<td style="text-align: right;">150720</td>
<td style="text-align: left;">A. Eshoo</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="even">
<td style="text-align: left;">441</td>
<td style="text-align: right;">441</td>
<td style="text-align: left;">CA-17</td>
<td style="text-align: right;">157049</td>
<td style="text-align: left;">R. Khanna</td>
<td style="text-align: left;">Democrat</td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
</div>
</div>
<p>You may notice right away that many of the 20 richest districts have Democratic Party representatives.</p>
<p>In fact, if we look at all 441 congressional districts in Kaptur’s table, we find a large difference in the average median household income for Democrat and Republican districts; the Democrat districts are, on average, about 14% richer (<a href="#tbl-party_means" class="quarto-xref">Table <span>16.2</span></a>).</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="tbl-party_means" class="lightable-paper lightable-striped lightable-hover quarto-float quarto-figure quarto-figure-center anchored" data-quarto-postprocess="true" style="font-family: "Arial Narrow", arial, helvetica, sans-serif; width: auto !important; margin-left: auto; margin-right: auto;">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-party_means-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table 16.2: Means for median household income by party
</figcaption>
<div aria-describedby="tbl-party_means-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="lightable-paper lightable-striped lightable-hover caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Mean of median household income</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Democrat</td>
<td style="text-align: right;">$76,933</td>
</tr>
<tr class="even">
<td style="text-align: left;">Republican</td>
<td style="text-align: right;">$67,474</td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
</div>
</div>
<p>Next we are going to tip our hand, and show how we got these data. In previous chapters, we had chunks like this in which we enter the values we will analyze. These values come from the example we introduced in <a href="probability_theory_3.html#sec-public-liquor" class="quarto-xref"><span>Section 12.15</span></a>:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Liquor prices for US states with private market.</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>priv <span class="ot"><-</span> <span class="fu">c</span>(<span class="fl">4.82</span>, <span class="fl">5.29</span>, <span class="fl">4.89</span>, <span class="fl">4.95</span>, <span class="fl">4.55</span>, <span class="fl">4.90</span>, <span class="fl">5.25</span>, <span class="fl">5.30</span>, <span class="fl">4.29</span>, <span class="fl">4.85</span>, <span class="fl">4.54</span>,</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> <span class="fl">4.75</span>, <span class="fl">4.85</span>, <span class="fl">4.85</span>, <span class="fl">4.50</span>, <span class="fl">4.75</span>, <span class="fl">4.79</span>, <span class="fl">4.85</span>, <span class="fl">4.79</span>, <span class="fl">4.95</span>, <span class="fl">4.95</span>, <span class="fl">4.75</span>,</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="fl">5.20</span>, <span class="fl">5.10</span>, <span class="fl">4.80</span>, <span class="fl">4.29</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Now we have 441 values to enter, and it is time to introduce R’s standard tools for loading data.</p>
<section id="sec-csv-format" class="level3" data-number="16.1.1">
<h3 data-number="16.1.1" class="anchored" data-anchor-id="sec-csv-format"><span class="header-section-number">16.1.1</span> Comma-separated-values (CSV) format</h3>
<p>The data we will load is in a file on disk called <code>data/congress_2023.csv</code>. These are data from Kaptur’s table in a comma-separated-values (CSV) format file. We refer to this file with its <em>filename</em>, that starts with the directory (folder) containing the file — <code>data/</code> — followed by the name of the file (<code>congress_2023.csv</code>), giving a filename of <code>data/congress_2023.csv</code>.</p>
<p>The <em>CSV format</em> is a very simple text format for storing table data. Usually, the first line of the CSV file contains the column names of the table, and the rest of the lines contain the row values. As the name suggests, commas (<code>,</code>) separate the column names in the first line, and the row values in the following lines. If you opened the <code>data/congress_2023.csv</code> file in some editor, such as Notepad on Windows or TextEdit on Mac, you would find that the first few lines looked like this:</p>
<div class="cell" data-layout-align="center">
<pre><code>Ascending_Rank,District,Median_Income,Representative,Party
1,PR-At Large,22237,J. González-Colón,Republican
2,AS-At Large,28352,A. Coleman,Republican
3,MP-At Large,31362,G. Sablan,Democrat
4,KY-5,37910,H. Rogers,Republican
5,MS-2,37933,B. G. Thompson,Democrat</code></pre>
</div>
<p>In the code that follows, we will <em>read</em> the values from the CSV file directly into R.</p>
<div id="nte-download-links" class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note 16.1: Download links with data files
</div>
</div>
<div class="callout-body-container callout-body">
<p>Up till now, the Download links for notebook files point to a single file — the <em>notebook</em>. The <span class="r">RStudio notebook file has extension <code>.Rmd</code></span>.</p>
<p>From now on, some examples involve reading a data file. In order to run the notebook, you need the notebook file (with extension<span class="r"><code>.Rmd</code></span>) as well as the data file. For those cases, the download link points to a <code>.zip</code> file containing the notebook file and the data file. To run the example on your computer, download the <code>.zip</code> file, extract the contents, and then open the notebook file using RStudio.</p>
</div>
</div>
<!---
End of python block
-->
</section>
<section id="introducing-r-data-frames" class="level3 r" data-number="16.1.2">
<h3 data-number="16.1.2" class="anchored" data-anchor-id="introducing-r-data-frames"><span class="header-section-number">16.1.2</span> Introducing R data frames</h3>
<div id="nte-intro_data_frames" class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note 16.2: Notebook: Introducing data frames
</div>
</div>
<div class="callout-body-container callout-body">
<div class="nb-links">
<p><a class="notebook-link" href="notebooks/intro_data_frames.zip">Download zip with notebook + data file</a> <a class="interact-button" href="./interact/lab/index.html?path=intro_data_frames.ipynb">Interact</a></p>
</div>
</div>
</div>
<div class="nb-start" name="intro_data_frames" title="Introducing data frames">
</div>
<p>R is a data analysis language, so, as you would expect, it is particularly good at loading data files, and presenting them to us as a useful table-like structure, called a <em>data frame</em>.</p>
<p>We start by using R to load our data file. R has a special function to do this, called <code>read.csv</code>.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>district_income <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">'data/congress_2023.csv'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We have thus far done many operations that returned R <em>vectors</em>. <code>read.csv</code> returns a new type of value, called a <em>data frame</em>:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">class</span>(district_income)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "data.frame"</code></pre>
</div>
</div>
<p>A data frame is R’s own way of representing a table, with columns and rows. You can think of it as R’s version of a spreadsheet. Data frames are a fundamental type in R, and there are many functions that operate on them. Among them is the function <code>head</code> which selects (by default) the first six rows of whatever you send it. Here we select the first six rows of the data frame.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the first six rows in the data frame</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(district_income)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Ascending_Rank District Median_Income Representative Party
1 1 PR-At Large 22237 J. González-Colón Republican
2 2 AS-At Large 28352 A. Coleman Republican
3 3 MP-At Large 31362 G. Sablan Democrat
4 4 KY-5 37910 H. Rogers Republican
5 5 MS-2 37933 B. G. Thompson Democrat
6 6 NY-15 40319 R. Torres Democrat</code></pre>
</div>
</div>
<p>The data are in income order, sorted lowest to highest, so the first five districts are those with the lowest household income.</p>
<p>We are particularly interested in the column named <code>Median_Income</code>.</p>
<p>You can fetch columns of data from a data frame by using R’s <code>$</code> syntax. The <code>$</code> syntax means “fetch the thing named on the right of the <code>$</code> attached to the value given to the left of the <code>$</code>”.</p>
<p>So, to get the data for the <code>Median_Income</code> column, we can write:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Use $ syntax to get a column of data from a data frame.</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="co"># "fetch the Median_Income thing from district_income".</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>incomes <span class="ot">=</span> district_income<span class="sc">$</span>Median_Income</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a><span class="co"># The thing that comes back is our familiar R vector.</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the first five values, by indexing with a slice.</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a>incomes[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 22237 28352 31362 37910 37933</code></pre>
</div>
</div>
<div class="nb-end">
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
End of notebook: Introducing data frames
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>intro_data_frames</code> starts at <a href="#nte-intro_data_frames" class="quarto-xref">Note <span>16.2</span></a>.</p>
</div>
</div>
<!---
End of notebook.
-->
</section>
<!---
End of R block
-->
<section id="incomes-and-ranks" class="level3" data-number="16.1.3">
<h3 data-number="16.1.3" class="anchored" data-anchor-id="incomes-and-ranks"><span class="header-section-number">16.1.3</span> Incomes and Ranks</h3>
<p>We now have the <code>incomes</code> values as a vector.</p>
<p>There are 441 values in the whole vector, one of each congressional district:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span>(incomes)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 441</code></pre>
</div>
</div>
<p>While we are at it, let us also get the values from the <code>Ascending_Rank</code> column, with the same procedure. These are ranks from low to high, meaning 1 is the lowest median income, and 441 is the highest median income.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>lo_to_hi_ranks <span class="ot"><-</span> district_income<span class="sc">$</span>Ascending_Rank</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the first five values, by indexing with a slice.</span></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a>lo_to_hi_ranks[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3 4 5</code></pre>
</div>
</div>
<p>In our case, the <span class="r">data frame</span> has the <code>Ascending_Rank</code> column with the ranks we need, but if we need the ranks and we don’t have them, we can calculate them using the <span class="r"><code>rank</code> function</span>.</p>
</section>
<section id="calculating-ranks" class="level3" data-number="16.1.4">
<h3 data-number="16.1.4" class="anchored" data-anchor-id="calculating-ranks"><span class="header-section-number">16.1.4</span> Calculating ranks</h3>
<p>As you might expect, <span class="r"><code>rank</code></span> accepts a vector as an input argument. Let’s say that there are <span class="r"><code>n <- length(data)</code></span> values in the vector that we pass to <span class="r"><code>rank</code></span>. The function returns a vector, length <span class="math inline">\(n\)</span>, where the elements are the ranks of each corresponding element in the input <code>data</code> vector. A rank value of 1 corresponds the lowest value in <code>data</code> (closest to negative infinity), and a rank of <span class="math inline">\(n\)</span> corresponds to the highest value (closest to positive infinity).</p>
<p>Here’s an example <code>data</code> vector to show how <span class="r"><code>rank</code></span> works.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="co"># The data.</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">c</span>(<span class="dv">3</span>, <span class="sc">-</span><span class="dv">1</span>, <span class="dv">5</span>, <span class="sc">-</span><span class="dv">2</span>)</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Corresponding ranks for the data.</span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="fu">rank</span>(data)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3 2 4 1</code></pre>
</div>
</div>
<p>We can use <span class="r"><code>rank</code></span> to recalculate the ranks for the congressional median household income values.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Recalculate the ranks.</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>recalculated_ranks <span class="ot"><-</span> <span class="fu">rank</span>(incomes)</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the first 5 ranks.</span></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a>recalculated_ranks[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3 4 5</code></pre>
</div>
</div>
</section>
</section>
<section id="comparing-two-values-in-the-district-income-data" class="level2" data-number="16.2">
<h2 data-number="16.2" class="anchored" data-anchor-id="comparing-two-values-in-the-district-income-data"><span class="header-section-number">16.2</span> Comparing two values in the district income data</h2>
<p>Let us say that we have taken an interest in two particular members of Congress: the Speaker of the House of Representatives, Republican <a href="https://en.wikipedia.org/wiki/Kevin_McCarthy">Kevin McCarthy</a>, and the progressive activist and Democrat <a href="https://en.wikipedia.org/wiki/Alexandria_Ocasio-Cortez">Alexandria Ocasio-Cortez</a>. We will refer to both using their initials: KM for Kevin Owen McCarthy and AOC for Alexandra Ocasio-Cortez.</p>
<p>By scrolling through the CSV file, or (in our case) using some simple <span class="r">R</span> code that we won’t cover now, we find the rows corresponding to McCarthy (KM) and Ocasio-Cortez (AOC) — <a href="#tbl-rows-of-interest" class="quarto-xref">Table <span>16.3</span></a>.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="tbl-rows-of-interest" class="lightable-paper lightable-striped lightable-hover quarto-float quarto-figure quarto-figure-center anchored" data-quarto-postprocess="true" style="font-family: "Arial Narrow", arial, helvetica, sans-serif; width: auto !important; margin-left: auto; margin-right: auto;">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-rows-of-interest-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table 16.3: Rows for Kevin McCarthy and Alexandra Ocasio-Cortez
</figcaption>
<div aria-describedby="tbl-rows-of-interest-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="lightable-paper lightable-striped lightable-hover caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">Ascending_Rank</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">District</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Median Income</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Representative</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Party</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">81</td>
<td style="text-align: left;">NY-14</td>
<td style="text-align: right;">56129</td>
<td style="text-align: left;">A. Ocasio-Cortez</td>
<td style="text-align: left;">Democrat</td>
</tr>
<tr class="even">
<td style="text-align: right;">295</td>
<td style="text-align: left;">CA-20</td>
<td style="text-align: right;">77205</td>
<td style="text-align: left;">K. McCarthy</td>
<td style="text-align: left;">Republican</td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
</div>
</div>
<p>The rows show the <em>rank</em> of each congressional district in terms of median household income. The districts are ordered by this rank, so we can get their respective <em>indices</em> (positions) in the <code>incomes</code> vector from their rank. </p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Rank of McCarthy's district in terms of median household income.</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>km_rank <span class="ot">=</span> <span class="dv">295</span></span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Index (position) of McCarthy's value in the "incomes" vector.</span></span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a><span class="co"># This is the same as the rank.</span></span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a>km_index <span class="ot">=</span> km_rank</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Now we have the index (position) of KM’s value, we can find the household income for his district from the <code>incomes</code> vector:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the median household income from McCarthy's district</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a><span class="co"># by indexing into the "incomes" vector:</span></span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a>km_income <span class="ot"><-</span> incomes[km_index]</span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a>km_income</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 77205</code></pre>
</div>
</div>
<p>Here is the corresponding index and <code>incomes</code> value for AOC:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Index (position) of AOC's value in the "incomes" array.</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>aoc_rank <span class="ot">=</span> <span class="dv">81</span></span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>aoc_index <span class="ot">=</span> aoc_rank</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the median household income from AOC's district</span></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a><span class="co"># by indexing into the "incomes" array:</span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a>aoc_income <span class="ot"><-</span> incomes[aoc_index]</span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a>aoc_income</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 56129</code></pre>
</div>
</div>
<p>Notice that we fetch the same value for median household income from <code>incomes</code> as you see in the corresponding rows.</p>
</section>
<section id="comparing-values-with-ranks-and-quantile-positions" class="level2" data-number="16.3">
<h2 data-number="16.3" class="anchored" data-anchor-id="comparing-values-with-ranks-and-quantile-positions"><span class="header-section-number">16.3</span> Comparing values with ranks and quantile positions</h2>
<p>We have KM’s and AOC’s district median household income values, but our next question might be — how unusual are these values?</p>
<p>Of course, it depends what we mean by <em>unusual</em>. We might mean, are they greater or smaller than most of the other values?</p>
<p>One way of answering that question is simply looking at the rank of the values. If the rank is lower than <span class="math inline">\(\frac{441}{2} = 220.5\)</span> then this is a district with lower income than most districts. If it is greater than <span class="math inline">\(220.5\)</span> then it has higher income than most districts. We see that KM’s district, with rank <code>r get_var('km_rank')</code>, is wealthier than most, whereas AOC’s district (rank <code>r get_var('aoc_rank')</code>) is poorer than most.</p>
<p>But we can’t interpret the ranks without remembering that there are 441 values, so — for example - a rank of 81 represents a relatively low value, whereas one of 295 is relatively high.</p>
<p>We would like some scale that tells us immediately whether this is a relatively low or a relatively high value, without having to remembering how many values there are.</p>
<p>This is a good use for <em>quantile positions</em> (QPs). The QP of a value tells you where the value ranks relative to the other values, on a scale from <span class="math inline">\(0\)</span> through <span class="math inline">\(1\)</span>. A QP of <span class="math inline">\(0\)</span> tells you this is the lowest-ranking value, and a QP of <span class="math inline">\(1\)</span> tells you this is the highest-ranking value.</p>
<p>We can calculate the QP for each rank. Think of the low-to-high ranks as being a line starting at 1 (the lowest rank — for the lowest median income) and going up to 441 (the highest rank — for the highest median income).</p>
<p>The QP corresponding to any particular rank tells you how far along this line the rank is. Notice that the length of the line is the distance from the first to the last value, so 441 - 1 = 440.</p>
<p>So, if the rank was <span class="math inline">\(1\)</span>, then the value is at the start of the line. It has got <span class="math inline">\(\frac{0}{440}\)</span> of the way along the line, and the QP is <span class="math inline">\(0\)</span>. If the rank is <span class="math inline">\(441\)</span>, the value is at the end of the line, it has got <span class="math inline">\(\frac{440}{440}\)</span> of the way along the line and the QP is <span class="math inline">\(1\)</span>.</p>
<p>Now consider the rank of <span class="math inline">\(100\)</span>. It has got <span class="math inline">\(\frac{(100 - 1)}{440}\)</span> of the way along the line, and the QP position is 0.22.</p>
<p>More generally, we can translate the high-to-low ranks to QPs with:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Length of the line defining quantile positions.</span></span>