Repository navigation
Expand file tree
/
Copy path03_DescriptiveStatistics.html
More file actions
2137 lines (2064 loc) · 203 KB
/
Copy path03_DescriptiveStatistics.html
File metadata and controls
2137 lines (2064 loc) · 203 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.5.43">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Topic 3: Descriptive Statistics for Numerical and Categorical Data</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; }
}
</style>
<script src="03_DescriptiveStatistics_files/libs/clipboard/clipboard.min.js"></script>
<script src="03_DescriptiveStatistics_files/libs/quarto-html/quarto.js"></script>
<script src="03_DescriptiveStatistics_files/libs/quarto-html/popper.min.js"></script>
<script src="03_DescriptiveStatistics_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="03_DescriptiveStatistics_files/libs/quarto-html/anchor.min.js"></script>
<link href="03_DescriptiveStatistics_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="03_DescriptiveStatistics_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="03_DescriptiveStatistics_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="03_DescriptiveStatistics_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="03_DescriptiveStatistics_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="03_DescriptiveStatistics_files/libs/quarto-contrib/live-runtime/live-runtime.js" type="module"></script>
<link href="03_DescriptiveStatistics_files/libs/quarto-contrib/live-runtime/live-runtime.css" rel="stylesheet">
<script type="module" src="03_DescriptiveStatistics_files/libs/quarto-ojs/quarto-ojs-runtime.js"></script>
<link href="03_DescriptiveStatistics_files/libs/quarto-ojs/quarto-ojs.css" rel="stylesheet">
<script src="03_DescriptiveStatistics_files/libs/kePrint-0.0.1/kePrint.js"></script>
<link href="03_DescriptiveStatistics_files/libs/lightable-0.0.1/lightable.css" rel="stylesheet">
<style>
.btn-exercise-solution { display: none !important; }
</style>
<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>
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Topic 3: Descriptive Statistics for Numerical and Categorical Data</h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<div class="cell" data-edit="false">
<div>
<div id="webr-1" class="exercise-cell">
</div>
<script type="webr-1-contents">
eyJjb2RlIjoid2Vicjo6aW5zdGFsbChcImdyYWRldGhpc1wiLCBxdWlldCA9IFRSVUUpXG5saWJyYXJ5KGdyYWRldGhpcylcbm9wdGlvbnMod2Vici5leGVyY2lzZS5jaGVja2VyID0gZnVuY3Rpb24oXG4gIGxhYmVsLCB1c2VyX2NvZGUsIHNvbHV0aW9uX2NvZGUsIGNoZWNrX2NvZGUsIGVudmlyX3Jlc3VsdCwgZXZhbHVhdGVfcmVzdWx0LFxuICBlbnZpcl9wcmVwLCBsYXN0X3ZhbHVlLCBlbmdpbmUsIHN0YWdlLCAuLi5cbikge1xuICBpZiAoaXMubnVsbChjaGVja19jb2RlKSkge1xuICAgICMgTm8gZ3JhZGluZyBjb2RlLCBzbyBqdXN0IHNraXAgZ3JhZGluZ1xuICAgIGludmlzaWJsZShOVUxMKVxuICB9IGVsc2UgaWYgKGlzLm51bGwobGFiZWwpKSB7XG4gICAgbGlzdChcbiAgICAgIGNvcnJlY3QgPSBGQUxTRSxcbiAgICAgIHR5cGUgPSBcIndhcm5pbmdcIixcbiAgICAgIG1lc3NhZ2UgPSBcIkFsbCBleGVyY2lzZXMgbXVzdCBoYXZlIGEgbGFiZWwuXCJcbiAgICApXG4gIH0gZWxzZSBpZiAoaXMubnVsbChzb2x1dGlvbl9jb2RlKSkge1xuICAgIGxpc3QoXG4gICAgICBjb3JyZWN0ID0gRkFMU0UsXG4gICAgICB0eXBlID0gXCJ3YXJuaW5nXCIsXG4gICAgICBtZXNzYWdlID0gaHRtbHRvb2xzOjp0YWdzJGRpdihcbiAgICAgICAgaHRtbHRvb2xzOjp0YWdzJHAoXCJBIHByb2JsZW0gb2NjdXJyZWQgZ3JhZGluZyB0aGlzIGV4ZXJjaXNlLlwiKSxcbiAgICAgICAgaHRtbHRvb2xzOjp0YWdzJHAoXG4gICAgICAgICAgXCJObyBzb2x1dGlvbiBjb2RlIHdhcyBmb3VuZC4gTm90ZSB0aGF0IGdyYWRpbmcgZXhlcmNpc2VzIHVzaW5nIHRoZSBcIixcbiAgICAgICAgICBodG1sdG9vbHM6OnRhZ3MkY29kZShcImdyYWRldGhpc1wiKSxcbiAgICAgICAgICBcInBhY2thZ2UgcmVxdWlyZXMgYSBtb2RlbCBzb2x1dGlvbiB0byBiZSBpbmNsdWRlZCBpbiB0aGUgZG9jdW1lbnQuXCJcbiAgICAgICAgKVxuICAgICAgKVxuICAgIClcbiAgfSBlbHNlIHtcbiAgICBncmFkZXRoaXM6OmdyYWRldGhpc19leGVyY2lzZV9jaGVja2VyKFxuICAgICAgbGFiZWwgPSBsYWJlbCwgc29sdXRpb25fY29kZSA9IHNvbHV0aW9uX2NvZGUsIHVzZXJfY29kZSA9IHVzZXJfY29kZSxcbiAgICAgIGNoZWNrX2NvZGUgPSBjaGVja19jb2RlLCBlbnZpcl9yZXN1bHQgPSBlbnZpcl9yZXN1bHQsXG4gICAgICBldmFsdWF0ZV9yZXN1bHQgPSBldmFsdWF0ZV9yZXN1bHQsIGVudmlyX3ByZXAgPSBlbnZpcl9wcmVwLFxuICAgICAgbGFzdF92YWx1ZSA9IGxhc3RfdmFsdWUsIHN0YWdlID0gc3RhZ2UsIGVuZ2luZSA9IGVuZ2luZSlcbiAgfVxufSkiLCJhdHRyIjp7ImV2YWwiOnRydWUsIm91dHB1dCI6ZmFsc2UsInBlcnNpc3QiOnRydWUsImVkaXQiOmZhbHNlfX0=
</script>
</div>
</div>
<div class="cell" data-edit="false" data-define="ok_response">
<div>
<div id="webr-2" class="exercise-cell">
</div>
<script type="webr-2-contents">
eyJjb2RlIjoiXG5saWJyYXJ5KGh0bWx0b29scylcbm9rX3Jlc3BvbnNlIDwtIGZ1bmN0aW9uKHJlc3BvbnNlLCBuKSB7XG4gIGlmIChpcy5uYShyZXNwb25zZSkpIGRpdihIVE1MKFwiWW91IGhhdmVuJ3QgYW5zd2VyZWQgeWV0LlwiKSwgc3R5bGUgPSBcImNvbG9yOiBwdXJwbGVcIilcbiAgZWxzZSBpZiAocmVzcG9uc2UgPT0gbikgZGl2KEhUTUwoXCJDb3JyZWN0IOKck1wiKSwgc3R5bGUgPSBcImNvbG9yOiBncmVlblwiKVxuICBlbHNlIGRpdihIVE1MKFwiSW5jb3JyZWN0IOKcl1wiKSwgc3R5bGUgPSBcImNvbG9yOiByZWRcIilcbn0iLCJhdHRyIjp7InBlcnNpc3QiOnRydWUsImV2YWwiOnRydWUsIm91dHB1dCI6ZmFsc2UsImRlZmluZSI6WyJva19yZXNwb25zZSJdLCJlZGl0IjpmYWxzZX19
</script>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code hidden" id="cb1" data-startfrom="40" data-source-offset="0"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 39;"><span id="cb1-40"><a href="#cb1-40" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">ok_checkbox</span>(response<span class="op">,</span> n) {</span>
<span id="cb1-41"><a href="#cb1-41" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="op">!</span>response <span class="op">||</span> response<span class="op">.</span><span class="at">length</span> <span class="op">===</span> <span class="dv">0</span>)</span>
<span id="cb1-42"><a href="#cb1-42" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="fu">html</span><span class="vs">`<span style="color:purple">You haven't answered yet.</span>`</span><span class="op">;</span></span>
<span id="cb1-43"><a href="#cb1-43" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (response<span class="op">.</span><span class="fu">toString</span>() <span class="op">===</span> n)</span>
<span id="cb1-44"><a href="#cb1-44" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="fu">html</span><span class="vs">`<span style="color:green">Correct ✓</span>`</span><span class="op">;</span></span>
<span id="cb1-45"><a href="#cb1-45" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="fu">html</span><span class="vs">`<span style="color:red">Not Yet! ✗</span>`</span><span class="op">;</span></span>
<span id="cb1-46"><a href="#cb1-46" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-1" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell" data-message="false" data-results="false">
<div class="sourceCode cell-code hidden" id="cb2" data-startfrom="54" data-source-offset="0"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 53;"><span id="cb2-54"><a href="#cb2-54" aria-hidden="true" tabindex="-1"></a>mutable ok_response <span class="op">=</span> (response<span class="op">,</span> n) <span class="kw">=></span> { <span class="cf">return</span> <span class="fu">html</span><span class="vs">`Loading...`</span> }<span class="op">;</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-display">
<div id="ojs-cell-2" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell" data-context="setup">
<div>
<div id="webr-3" class="exercise-cell">
</div>
<script type="webr-3-contents">
eyJjb2RlIjoiXG5saWJyYXJ5KGRwbHlyKVxubGlicmFyeShnZ3Bsb3QyKVxubGlicmFyeShqc29ubGl0ZSlcbmxpYnJhcnkoYmFzZTY0ZW5jKVxuXG4jIEluaXRpYWxpc2UgZXhlcmNpc2UgcmVzdWx0cyBpbiB0aGUgZ2xvYmFsIGVudmlyb25tZW50XG5leGVyY2lzZV9yZXN1bHRzIDwtIGxpc3QoXG4gIG5vdGVib29rICAgICAgICAgID0gXCJUb3BpYyAzOiBEZXNjcmlwdGl2ZSBTdGF0aXN0aWNzXCIsXG4gIG1lYW5fc2FtcGxlX29uZSAgID0gXCJ1bmF0dGVtcHRlZFwiLFxuICBtZWFuX3NhbXBsZV90aHJlZSA9IFwidW5hdHRlbXB0ZWRcIixcbiAgbXlfZGF0YSAgICAgICAgICAgPSBcInVuYXR0ZW1wdGVkXCJcbilcblxuc2V0LnNlZWQoMjQxKVxuXG5kaWFtb25kcyA8LSBkaWFtb25kc1xuZGlhbW9uZHMgPC0gZGF0YS5mcmFtZShkaWFtb25kcylcblxuZGlhbW9uZHMkcmVnaW9uIDwtIHNhbXBsZShjKDEsMiwzLDQsNSksIHByb2IgPSBjKDAuMTEsIC4wMiwgMC42NSwgMC4yMCwgLjAyKSwgbnJvdyhkaWFtb25kcyksIHJlcGxhY2UgPSBUUlVFKVxuZGlhbW9uZHMkcmVnaW9uIDwtIGFzLmZhY3RvcihkaWFtb25kcyRyZWdpb24pXG5cbnNhbXBsZXMgPC0gZGF0YS5mcmFtZShcbiAgU2FtcGxlX09uZSAgID0gZGlhbW9uZHMkY2FyYXRbc2FtcGxlKDE6bnJvdyhkaWFtb25kcyksIDgsIHJlcGxhY2UgPSBGQUxTRSldLFxuICBTYW1wbGVfVHdvICAgPSBkaWFtb25kcyRjYXJhdFtzYW1wbGUoMTpucm93KGRpYW1vbmRzKSwgOCwgcmVwbGFjZSA9IEZBTFNFKV0sXG4gIFNhbXBsZV9UaHJlZSA9IGMoZGlhbW9uZHMkY2FyYXRbc2FtcGxlKDE6bnJvdyhkaWFtb25kcyksIDcsIHJlcGxhY2UgPSBGQUxTRSldLCAxMClcbilcblxuY3V0U2FtcGxlcyA8LSBkYXRhLmZyYW1lKFxuICBTYW1wbGVfT25lICAgPSBkaWFtb25kcyRjdXRbc2FtcGxlKDE6bnJvdyhkaWFtb25kcyksIDgsIHJlcGxhY2UgPSBGQUxTRSldLFxuICBTYW1wbGVfVHdvICAgPSBkaWFtb25kcyRjdXRbc2FtcGxlKDE6bnJvdyhkaWFtb25kcyksIDgsIHJlcGxhY2UgPSBGQUxTRSldLFxuICBTYW1wbGVfVGhyZWUgPSBkaWFtb25kcyRjdXRbc2FtcGxlKDE6bnJvdyhkaWFtb25kcyksIDgsIHJlcGxhY2UgPSBGQUxTRSldXG4pIiwiYXR0ciI6eyJwZXJzaXN0Ijp0cnVlLCJlZGl0IjpmYWxzZSwiZXZhbCI6dHJ1ZSwib3V0cHV0IjpmYWxzZSwiZWNobyI6ZmFsc2UsImNvbnRleHQiOiJzZXR1cCJ9fQ==
</script>
</div>
</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">
About
</div>
</div>
<div class="callout-body-container callout-body">
<p>This activity builds off of what we learned in the Topic 1 and Topic 2 activities. We’ll learn how to use R to analyse numeric and categorical data. We discuss and implement measures of central tendency and spread (including inquiry into robustness in the presence of outliers) for numerical data, and measures of frequency for categorical data.</p>
<div class="cell">
<div class="sourceCode cell-code hidden" id="cb3" data-startfrom="132" data-source-offset="0"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 131;"><span id="cb3-132"><a href="#cb3-132" aria-hidden="true" tabindex="-1"></a>{</span>
<span id="cb3-133"><a href="#cb3-133" aria-hidden="true" tabindex="-1"></a> <span class="kw">const</span> btn <span class="op">=</span> <span class="fu">html</span><span class="vs">`<button style="</span></span>
<span id="cb3-134"><a href="#cb3-134" aria-hidden="true" tabindex="-1"></a><span class="vs"> background: #436b95;</span></span>
<span id="cb3-135"><a href="#cb3-135" aria-hidden="true" tabindex="-1"></a><span class="vs"> color: white;</span></span>
<span id="cb3-136"><a href="#cb3-136" aria-hidden="true" tabindex="-1"></a><span class="vs"> border: none;</span></span>
<span id="cb3-137"><a href="#cb3-137" aria-hidden="true" tabindex="-1"></a><span class="vs"> padding: 8px 16px;</span></span>
<span id="cb3-138"><a href="#cb3-138" aria-hidden="true" tabindex="-1"></a><span class="vs"> border-radius: 4px;</span></span>
<span id="cb3-139"><a href="#cb3-139" aria-hidden="true" tabindex="-1"></a><span class="vs"> cursor: pointer;</span></span>
<span id="cb3-140"><a href="#cb3-140" aria-hidden="true" tabindex="-1"></a><span class="vs"> font-size: 0.9em;</span></span>
<span id="cb3-141"><a href="#cb3-141" aria-hidden="true" tabindex="-1"></a><span class="vs"> ">🔄 Reset My Responses</button>`</span><span class="op">;</span></span>
<span id="cb3-142"><a href="#cb3-142" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-143"><a href="#cb3-143" aria-hidden="true" tabindex="-1"></a> btn<span class="op">.</span><span class="at">onclick</span> <span class="op">=</span> () <span class="kw">=></span> {</span>
<span id="cb3-144"><a href="#cb3-144" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">confirm</span>(<span class="st">'Reset all responses for this activity? This cannot be undone.'</span>)) {</span>
<span id="cb3-145"><a href="#cb3-145" aria-hidden="true" tabindex="-1"></a> <span class="bu">Object</span><span class="op">.</span><span class="fu">keys</span>(localStorage)</span>
<span id="cb3-146"><a href="#cb3-146" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span><span class="fu">filter</span>(k <span class="kw">=></span> k<span class="op">.</span><span class="fu">endsWith</span>(<span class="st">'_selected_t3'</span>) <span class="op">||</span> k<span class="op">.</span><span class="fu">endsWith</span>(<span class="st">'_correct_t3'</span>) <span class="op">||</span> k<span class="op">.</span><span class="fu">endsWith</span>(<span class="st">'_result_t3'</span>))</span>
<span id="cb3-147"><a href="#cb3-147" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span><span class="fu">forEach</span>(k <span class="kw">=></span> localStorage<span class="op">.</span><span class="fu">removeItem</span>(k))<span class="op">;</span></span>
<span id="cb3-148"><a href="#cb3-148" aria-hidden="true" tabindex="-1"></a> location<span class="op">.</span><span class="fu">reload</span>()<span class="op">;</span></span>
<span id="cb3-149"><a href="#cb3-149" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb3-150"><a href="#cb3-150" aria-hidden="true" tabindex="-1"></a> }<span class="op">;</span></span>
<span id="cb3-151"><a href="#cb3-151" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-152"><a href="#cb3-152" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> btn<span class="op">;</span></span>
<span id="cb3-153"><a href="#cb3-153" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-3" data-nodetype="expression">
</div>
</div>
</div>
<p><em>Note.</em> The button above resets multiple choice and checkbox questions. Currently, resetting code cells must be done manually via hitting the <em>Start Over</em> button on each individual interactive cell.</p>
</div>
</div>
<section id="descriptive-statistics" class="level2">
<h2 class="anchored" data-anchor-id="descriptive-statistics">Descriptive Statistics</h2>
<p><strong>Workbook Objectives:</strong> This workbook covers the following objectives.</p>
<ul>
<li>Compute and discuss appropriate summaries for both numerical and categorical data</li>
<li>Regarding numerical variables, discuss the difference between mean and median as well as standard deviation and inter-quartile range. Identify when each measure is appropriate.</li>
<li>Compute summary statistics both by hand and with the use of R</li>
</ul>
<p><strong>Important Reminders:</strong> The following previously mastered material is necessary for success through this workbook.</p>
<ul>
<li>A variable is <em>numerical</em> if a summary statistic such as the mean has a meaningful interpretation.</li>
<li>A variable is <em>categorical</em> if it serves to sort observations into different groups (categories).</li>
<li>The unique values which a variable takes are called the <em>levels</em> of that variable.</li>
</ul>
</section>
<section id="summarizing-numerical-quantitative-data" class="level2">
<h2 class="anchored" data-anchor-id="summarizing-numerical-quantitative-data">Summarizing Numerical (Quantitative) Data</h2>
<p><strong>Recall</strong>: Variables for which computation of measures like the mean (average) or standard deviation are meaningful are numerical variables.</p>
<section id="measuring-central-tendency" class="level3">
<h3 class="anchored" data-anchor-id="measuring-central-tendency">Measuring Central Tendency</h3>
<p><strong>Measures of Central Tendency (Averages)</strong>: The mean and median both attempt to measure the <em>center</em> of a dataset.</p>
<ul>
<li><p>The <strong>mean</strong> of a set of observations is the traditional <em>average</em>. We typically denote the mean by <span class="math inline">\(\bar{x}\)</span> (or <span class="math inline">\(\mu\)</span> in the case of population-level data) and it is computed as follows: <span class="math display">\[\bar{x} = \frac{\displaystyle{\sum_{i=1}^{n}{x_i}}}{n} = \frac{x_1+x_2+x_3+\cdots+x_n}{n}\]</span></p></li>
<li><p>The <strong>median</strong> is the <em>middle value</em> for a set of observations. To compute the median, list the numbers in ascending order and find the number or numbers in the middle of the list. In the case that there is a single middle number, that is the median. In the case where there are two middle numbers, we take the <em>mean</em> of those two.</p></li>
</ul>
<p><strong>Example</strong>: The following are three random samples of carat values from the <em>diamonds</em> dataset.</p>
<div class="cell">
<div class="cell-output-display">
<table class="table table-hover table-striped caption-top table-sm small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">Sample_One</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Sample_Two</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Sample_Three</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.41</td>
<td style="text-align: right;">2.68</td>
<td style="text-align: right;">1.05</td>
</tr>
<tr class="even">
<td style="text-align: right;">1.01</td>
<td style="text-align: right;">1.01</td>
<td style="text-align: right;">0.31</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1.22</td>
<td style="text-align: right;">0.26</td>
<td style="text-align: right;">0.31</td>
</tr>
<tr class="even">
<td style="text-align: right;">1.02</td>
<td style="text-align: right;">0.90</td>
<td style="text-align: right;">0.30</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.30</td>
<td style="text-align: right;">0.43</td>
<td style="text-align: right;">0.40</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.30</td>
<td style="text-align: right;">0.53</td>
<td style="text-align: right;">1.50</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1.00</td>
<td style="text-align: right;">2.00</td>
<td style="text-align: right;">2.27</td>
</tr>
<tr class="even">
<td style="text-align: right;">1.17</td>
<td style="text-align: right;">0.33</td>
<td style="text-align: right;">10.00</td>
</tr>
</tbody>
</table>
</div>
</div>
<ol type="1">
<li>Use the code block below to compute the mean of <code>Sample_One</code>.</li>
</ol>
<div class="cell" data-exercise="mean-sample-one">
<div>
<div id="webr-4" class="exercise-cell">
</div>
<script type="webr-4-contents">
eyJjb2RlIjoiX19fX19fIiwiYXR0ciI6eyJldmFsIjp0cnVlLCJlZGl0Ijp0cnVlLCJwZXJzaXN0Ijp0cnVlLCJleGVyY2lzZSI6Im1lYW4tc2FtcGxlLW9uZSJ9fQ==
</script>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="mean-sample-one">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 1
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Manually add up the carat values in Sample One, and then divide by the total number of diamonds in the sample.</p>
<div class="cell">
<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></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="mean-sample-one">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 2
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Manually add up the carat values in Sample One, and then divide by the total number of diamonds in the sample. Fill in the blanks.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>(___ <span class="sc">+</span> ___ <span class="sc">+</span> ___ <span class="sc">+</span> ___ <span class="sc">+</span> ___ <span class="sc">+</span> ___ <span class="sc">+</span> ___ <span class="sc">+</span> ___) <span class="sc">/</span> ___</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="mean-sample-one">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 3 (Solved)
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Manually add up the carat values in Sample One, and then divide by the total number of diamonds in the sample.</p>
<div class="cell">
<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="fl">0.41</span> <span class="sc">+</span> <span class="fl">1.01</span> <span class="sc">+</span> <span class="fl">1.22</span> <span class="sc">+</span> <span class="fl">1.02</span> <span class="sc">+</span> <span class="fl">0.30</span> <span class="sc">+</span> <span class="fl">0.30</span> <span class="sc">+</span> <span class="fl">1.00</span> <span class="sc">+</span> <span class="fl">1.17</span>) <span class="sc">/</span> <span class="dv">8</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell" data-exercise="mean-sample-one" data-solution="true">
<div class="webr-ojs-exercise exercise-solution d-none" data-exercise="mean-sample-one">
<code id="interpolate-6" class="solution-code d-none">
(0.41 + 1.01 + 1.22 + 1.02 + 0.30 + 0.30 + 1.00 + 1.17) / 8</code>
<div class="sourceCode cell-code" id="interpolate-7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="interpolate-7-1"><a href="#interpolate-7-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="interpolate-7-2"><a href="#interpolate-7-2" aria-hidden="true" tabindex="-1"></a>(<span class="fl">0.41</span> <span class="sc">+</span> <span class="fl">1.01</span> <span class="sc">+</span> <span class="fl">1.22</span> <span class="sc">+</span> <span class="fl">1.02</span> <span class="sc">+</span> <span class="fl">0.30</span> <span class="sc">+</span> <span class="fl">0.30</span> <span class="sc">+</span> <span class="fl">1.00</span> <span class="sc">+</span> <span class="fl">1.17</span>) <span class="sc">/</span> <span class="dv">8</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
<div class="cell" data-exercise="mean-sample-one" data-check="true">
<script type="exercise-check-mean-sample-one-contents">
eyJjb2RlIjoiXG5ncmFkZXRoaXM6OmdyYWRlX3RoaXMoe1xuICBpZiAoLnJlc3VsdCA9PSAuc29sdXRpb24pIHtcbiAgICBleGVyY2lzZV9yZXN1bHRzJG1lYW5fc2FtcGxlX29uZSA8PC0gXCJwYXNzXCJcbiAgICBwYXNzKHJhbmRvbV9wcmFpc2UoKSlcbiAgfVxuICBleGVyY2lzZV9yZXN1bHRzJG1lYW5fc2FtcGxlX29uZSA8PC0gXCJmYWlsXCJcbiAgZmFpbChyYW5kb21fZW5jb3VyYWdlbWVudCgpKVxufSkiLCJhdHRyIjp7ImNoZWNrIjp0cnVlLCJwZXJzaXN0Ijp0cnVlLCJleGVyY2lzZSI6Im1lYW4tc2FtcGxlLW9uZSIsImV2YWwiOnRydWUsImVkaXQiOnRydWV9fQ==
</script>
</div>
<p>In R we can easily compute the means and medians for our samples or for the entire data set! You might remember from our most recent notebook that the <code>$</code> operator can be used to access an entire column of a data frame. I’ve stored the samples in a data frame called <code>samples</code>. R includes a function <code>mean()</code> for computing the mean of a list of numbers and a function <code>median()</code> for computing the median. The following methods both compute the <code>mean()</code> of <code>Sample_Two</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(samples<span class="sc">$</span>Sample_Two)</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.0175</code></pre>
</div>
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>samples <span class="sc">|></span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">avg_carat_wt =</span> <span class="fu">mean</span>(Sample_Two))</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> avg_carat_wt
1 1.0175</code></pre>
</div>
</div>
<p>While the second method involves more typing, it is much more readable. Getting used to using the pipe operator (<code>|></code>) will allow us to chain data manipulation operations together, resulting in very readable analyses.</p>
<ol start="2" type="1">
<li>Use the code block below so that it computes the mean of <code>Sample_Three</code>. You may use whichever method you prefer here.</li>
</ol>
<div class="cell" data-exercise="mean-sample-three">
<div>
<div id="webr-9" class="exercise-cell">
</div>
<script type="webr-9-contents">
eyJjb2RlIjoiX19fX19fIiwiYXR0ciI6eyJldmFsIjp0cnVlLCJlZGl0Ijp0cnVlLCJwZXJzaXN0Ijp0cnVlLCJleGVyY2lzZSI6Im1lYW4tc2FtcGxlLXRocmVlIn19
</script>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="mean-sample-three">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 1
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-5" class="callout-5-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Start with the code below (copied from our earlier code chunk) and update it to compute the mean of <code>Sample_Three</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>samples <span class="sc">|></span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">avg_carat_wt =</span> <span class="fu">mean</span>(Sample_Two))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="mean-sample-three">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 2
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-6" class="callout-6-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Start with the code below (copied from our earlier code chunk) and update it to compute the mean of <code>Sample_Three</code>.</p>
<div class="cell">
<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>samples <span class="sc">|></span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">avg_carat_wt =</span> <span class="fu">mean</span>(___))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="mean-sample-three">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-7-contents" aria-controls="callout-7" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 3 (Solved)
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-7" class="callout-7-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Start with the code below (copied from our earlier code chunk) and update it to compute the mean of <code>Sample_Three</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>samples <span class="sc">|></span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">avg_carat_wt =</span> <span class="fu">mean</span>(Sample_Three))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell" data-exercise="mean-sample-three" data-solution="true">
<div class="webr-ojs-exercise exercise-solution d-none" data-exercise="mean-sample-three">
<code id="interpolate-11" class="solution-code d-none">
samples |>
summarize(avg_carat_wt = mean(Sample_Three))</code>
<div class="sourceCode cell-code" id="interpolate-12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="interpolate-12-1"><a href="#interpolate-12-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="interpolate-12-2"><a href="#interpolate-12-2" aria-hidden="true" tabindex="-1"></a>samples <span class="sc">|></span></span>
<span id="interpolate-12-3"><a href="#interpolate-12-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">avg_carat_wt =</span> <span class="fu">mean</span>(Sample_Three))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
<div class="cell" data-exercise="mean-sample-three" data-check="true">
<script type="exercise-check-mean-sample-three-contents">
eyJjb2RlIjoiXG5ncmFkZXRoaXM6OmdyYWRlX3RoaXMoe1xuICBpZiAoLnJlc3VsdCA9PSAuc29sdXRpb24pIHtcbiAgICBleGVyY2lzZV9yZXN1bHRzJG1lYW5fc2FtcGxlX3RocmVlIDw8LSBcInBhc3NcIlxuICAgIHBhc3MocmFuZG9tX3ByYWlzZSgpKVxuICB9XG4gIGV4ZXJjaXNlX3Jlc3VsdHMkbWVhbl9zYW1wbGVfdGhyZWUgPDwtIFwiZmFpbFwiXG4gIGZhaWwocmFuZG9tX2VuY291cmFnZW1lbnQoKSlcbn0pIiwiYXR0ciI6eyJjaGVjayI6dHJ1ZSwicGVyc2lzdCI6dHJ1ZSwiZXhlcmNpc2UiOiJtZWFuLXNhbXBsZS10aHJlZSIsImV2YWwiOnRydWUsImVkaXQiOnRydWV9fQ==
</script>
</div>
<ol start="3" type="1">
<li>Use the <code>median()</code> function and the code block below to compute the median of each of the samples and then answer the question that follows.</li>
</ol>
<div class="cell" data-exercise="find-medians" data-caption="Ungraded, Exploratory Only">
<div>
<div id="webr-14" class="exercise-cell">
</div>
<script type="webr-14-contents">
eyJjb2RlIjoiX19fX19fIiwiYXR0ciI6eyJjYXB0aW9uIjoiVW5ncmFkZWQsIEV4cGxvcmF0b3J5IE9ubHkiLCJwZXJzaXN0Ijp0cnVlLCJleGVyY2lzZSI6ImZpbmQtbWVkaWFucyIsImV2YWwiOnRydWUsImVkaXQiOnRydWV9fQ==
</script>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="find-medians">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-8-contents" aria-controls="callout-8" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 1
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-8" class="callout-8-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Start with the copied code again, and update it to compute the median instead of the mean.</p>
<div class="cell">
<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>samples <span class="sc">|></span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">avg_carat_wt =</span> <span class="fu">mean</span>(Sample_Two))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="find-medians">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-9-contents" aria-controls="callout-9" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 2
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-9" class="callout-9-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Start with the copied code again, and update it to compute the median instead of the mean.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>samples <span class="sc">|></span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">avg_carat_wt =</span> <span class="fu">___</span>(Sample_Two))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="find-medians">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-10-contents" aria-controls="callout-10" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 3
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-10" class="callout-10-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Power move! The <code>summarize()</code> function can compute multiple summary statistics at once. Update the code below to compute all three medians at once.</p>
<div class="cell">
<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>samples <span class="sc">|></span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> <span class="at">avg_carat_wt_samp1 =</span> <span class="fu">mean</span>(Sample_One),</span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a> <span class="at">avg_carat_wt_samp2 =</span> <span class="fu">mean</span>(Sample_Two),</span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a> <span class="at">avg_carat_wt_samp3 =</span> <span class="fu">mean</span>(Sample_Three)</span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="find-medians">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-11-contents" aria-controls="callout-11" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 4
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-11" class="callout-11-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Power move! The <code>summarize()</code> function can compute multiple summary statistics at once. Update the code below to compute all three medians at once.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>samples <span class="sc">|></span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(</span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="at">median_carat_wt_samp1 =</span> <span class="fu">___</span>(Sample_One),</span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> <span class="at">median_carat_wt_samp2 =</span> <span class="fu">___</span>(Sample_Two),</span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a> <span class="at">median_carat_wt_samp3 =</span> <span class="fu">___</span>(Sample_Three)</span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="find-medians">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-12-contents" aria-controls="callout-12" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 5 (Solved)
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-12" class="callout-12-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Power move! The <code>summarize()</code> function can compute multiple summary statistics at once. Update the code below to compute all three medians at once.</p>
<div class="cell">
<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>samples <span class="sc">|></span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a> <span class="at">median_carat_wt_samp1 =</span> <span class="fu">median</span>(Sample_One),</span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a> <span class="at">median_carat_wt_samp2 =</span> <span class="fu">median</span>(Sample_Two),</span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a> <span class="at">median_carat_wt_samp3 =</span> <span class="fu">median</span>(Sample_Three)</span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-caution 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">
Check Your Understanding: Measures of Center
</div>
</div>
<div class="callout-body-container callout-body">
<p>What does your work above tell you about the mean and median as measures of central tendency?</p>
<div class="cell" data-message="false" data-results="false">
<div class="sourceCode cell-code hidden" id="cb19" data-startfrom="486" data-source-offset="-1"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 485;"><span id="cb19-486"><a href="#cb19-486" aria-hidden="true" tabindex="-1"></a>viewof q1 <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">radio</span>(</span>
<span id="cb19-487"><a href="#cb19-487" aria-hidden="true" tabindex="-1"></a> <span class="kw">new</span> <span class="bu">Map</span>([</span>
<span id="cb19-488"><a href="#cb19-488" aria-hidden="true" tabindex="-1"></a> [<span class="st">"The mean is generally larger than the median."</span><span class="op">,</span> <span class="dv">1</span>]<span class="op">,</span></span>
<span id="cb19-489"><a href="#cb19-489" aria-hidden="true" tabindex="-1"></a> [<span class="st">"The mean is generally smaller than the median."</span><span class="op">,</span> <span class="dv">2</span>]<span class="op">,</span></span>
<span id="cb19-490"><a href="#cb19-490" aria-hidden="true" tabindex="-1"></a> [<span class="st">"The mean is usually close to the median."</span><span class="op">,</span> <span class="dv">3</span>]<span class="op">,</span></span>
<span id="cb19-491"><a href="#cb19-491" aria-hidden="true" tabindex="-1"></a> [<span class="st">"The mean is more strongly distorted by outliers (unusually large or small observed values) than the median is."</span><span class="op">,</span> <span class="dv">4</span>]</span>
<span id="cb19-492"><a href="#cb19-492" aria-hidden="true" tabindex="-1"></a> ])<span class="op">,</span></span>
<span id="cb19-493"><a href="#cb19-493" aria-hidden="true" tabindex="-1"></a> {<span class="dt">value</span><span class="op">:</span> <span class="bu">JSON</span><span class="op">.</span><span class="fu">parse</span>(localStorage<span class="op">.</span><span class="fu">getItem</span>(<span class="st">"q1_selected_t3"</span>) <span class="op">??</span> <span class="st">"null"</span>)}</span>
<span id="cb19-494"><a href="#cb19-494" aria-hidden="true" tabindex="-1"></a>)<span class="op">;</span></span>
<span id="cb19-495"><a href="#cb19-495" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-496"><a href="#cb19-496" aria-hidden="true" tabindex="-1"></a>{</span>
<span id="cb19-497"><a href="#cb19-497" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-498"><a href="#cb19-498" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q1_selected_t3"</span><span class="op">,</span> <span class="bu">JSON</span><span class="op">.</span><span class="fu">stringify</span>(q1))<span class="op">;</span></span>
<span id="cb19-499"><a href="#cb19-499" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q1_correct_t3"</span><span class="op">,</span> <span class="st">"4"</span>)<span class="op">;</span></span>
<span id="cb19-500"><a href="#cb19-500" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q1_result_t3"</span><span class="op">,</span> q1 <span class="op">===</span> <span class="kw">null</span> <span class="op">?</span> <span class="st">"unattempted"</span> <span class="op">:</span> (q1 <span class="op">==</span> <span class="dv">4</span> <span class="op">?</span> <span class="st">"correct"</span> <span class="op">:</span> <span class="st">"incorrect"</span>))<span class="op">;</span></span>
<span id="cb19-501"><a href="#cb19-501" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-502"><a href="#cb19-502" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-1" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-2" data-nodetype="expression">
</div>
</div>
</div>
<div class="sourceCode cell-code hidden" id="cb20" data-startfrom="504" data-source-offset="-635"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 503;"><span id="cb20-504"><a href="#cb20-504" aria-hidden="true" tabindex="-1"></a><span class="fu">ok_response</span>(q1<span class="op">,</span> <span class="st">"4"</span>)<span class="op">;</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-display">
<div>
<div id="ojs-cell-4-3" data-nodetype="expression">
</div>
</div>
</div>
</div>
</div>
</div>
<ol start="4" type="1">
<li>In our first notebook we saw that we can use sample data to make generalizations about populations for which the sample is representative. Answer the following questions with this in mind.</li>
</ol>
<div class="callout callout-style-default callout-caution 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">
Check Your Understanding: Sampling and Validity I
</div>
</div>
<div class="callout-body-container callout-body">
<p>Assuming that the samples of diamonds were taken randomly, which of the following claims is justified?</p>
<div class="cell">
<div class="sourceCode cell-code hidden" id="cb21" data-startfrom="519" data-source-offset="-1"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 518;"><span id="cb21-519"><a href="#cb21-519" aria-hidden="true" tabindex="-1"></a>viewof q2 <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">radio</span>(</span>
<span id="cb21-520"><a href="#cb21-520" aria-hidden="true" tabindex="-1"></a> <span class="kw">new</span> <span class="bu">Map</span>([</span>
<span id="cb21-521"><a href="#cb21-521" aria-hidden="true" tabindex="-1"></a> [<span class="st">"The mean from Sample_Three is too high."</span><span class="op">,</span> <span class="dv">1</span>]<span class="op">,</span></span>
<span id="cb21-522"><a href="#cb21-522" aria-hidden="true" tabindex="-1"></a> [<span class="st">"The mean from Sample_One is too low."</span><span class="op">,</span> <span class="dv">2</span>]<span class="op">,</span></span>
<span id="cb21-523"><a href="#cb21-523" aria-hidden="true" tabindex="-1"></a> [<span class="st">"Each of the sample means provides an estimate for the population mean."</span><span class="op">,</span> <span class="dv">3</span>]<span class="op">,</span></span>
<span id="cb21-524"><a href="#cb21-524" aria-hidden="true" tabindex="-1"></a> [<span class="st">"The population mean is equal to the sample means."</span><span class="op">,</span> <span class="dv">4</span>]</span>
<span id="cb21-525"><a href="#cb21-525" aria-hidden="true" tabindex="-1"></a> ])<span class="op">,</span></span>
<span id="cb21-526"><a href="#cb21-526" aria-hidden="true" tabindex="-1"></a> {<span class="dt">value</span><span class="op">:</span> <span class="bu">JSON</span><span class="op">.</span><span class="fu">parse</span>(localStorage<span class="op">.</span><span class="fu">getItem</span>(<span class="st">"q2_selected_t3"</span>) <span class="op">??</span> <span class="st">"null"</span>)}</span>
<span id="cb21-527"><a href="#cb21-527" aria-hidden="true" tabindex="-1"></a>)<span class="op">;</span></span>
<span id="cb21-528"><a href="#cb21-528" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-529"><a href="#cb21-529" aria-hidden="true" tabindex="-1"></a>{</span>
<span id="cb21-530"><a href="#cb21-530" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q2_selected_t3"</span><span class="op">,</span> <span class="bu">JSON</span><span class="op">.</span><span class="fu">stringify</span>(q2))<span class="op">;</span></span>
<span id="cb21-531"><a href="#cb21-531" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q2_correct_t3"</span><span class="op">,</span> <span class="st">"3"</span>)<span class="op">;</span></span>
<span id="cb21-532"><a href="#cb21-532" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q2_result_t3"</span><span class="op">,</span> q2 <span class="op">===</span> <span class="kw">null</span> <span class="op">?</span> <span class="st">"unattempted"</span> <span class="op">:</span> (q2 <span class="op">==</span> <span class="dv">3</span> <span class="op">?</span> <span class="st">"correct"</span> <span class="op">:</span> <span class="st">"incorrect"</span>))<span class="op">;</span></span>
<span id="cb21-533"><a href="#cb21-533" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-534"><a href="#cb21-534" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-5-1" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-5-2" data-nodetype="expression">
</div>
</div>
</div>
<div class="sourceCode cell-code hidden" id="cb22" data-startfrom="536" data-source-offset="-587"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 535;"><span id="cb22-536"><a href="#cb22-536" aria-hidden="true" tabindex="-1"></a><span class="fu">ok_response</span>(q2<span class="op">,</span> <span class="st">"3"</span>)<span class="op">;</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-display">
<div>
<div id="ojs-cell-5-3" data-nodetype="expression">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-caution 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">
Check Your Understanding: Sampling and Validity II
</div>
</div>
<div class="callout-body-container callout-body">
<p>Which of the following would result in improved estimates for the true mean carat weight of diamonds in our population? Select all that apply.</p>
<div class="cell">
<div class="sourceCode cell-code hidden" id="cb23" data-startfrom="549" data-source-offset="-1"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 548;"><span id="cb23-549"><a href="#cb23-549" aria-hidden="true" tabindex="-1"></a>viewof q3 <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">checkbox</span>(</span>
<span id="cb23-550"><a href="#cb23-550" aria-hidden="true" tabindex="-1"></a> <span class="kw">new</span> <span class="bu">Map</span>([</span>
<span id="cb23-551"><a href="#cb23-551" aria-hidden="true" tabindex="-1"></a> [<span class="st">"Take the mean of the sample means."</span><span class="op">,</span> <span class="dv">1</span>]<span class="op">,</span></span>
<span id="cb23-552"><a href="#cb23-552" aria-hidden="true" tabindex="-1"></a> [<span class="st">"Combine the three samples into a single sample of 24 diamonds (rather than three separate samples of 8), and take its mean."</span><span class="op">,</span> <span class="dv">2</span>]<span class="op">,</span></span>
<span id="cb23-553"><a href="#cb23-553" aria-hidden="true" tabindex="-1"></a> [<span class="st">"Take samples using smaller sample sizes."</span><span class="op">,</span> <span class="dv">3</span>]<span class="op">,</span></span>
<span id="cb23-554"><a href="#cb23-554" aria-hidden="true" tabindex="-1"></a> [<span class="st">"Stratify the diamonds according to price (low, medium, and high) and take a random sample of diamonds of each price category."</span><span class="op">,</span> <span class="dv">4</span>]<span class="op">,</span></span>
<span id="cb23-555"><a href="#cb23-555" aria-hidden="true" tabindex="-1"></a> [<span class="st">"Take samples including more diamonds."</span><span class="op">,</span> <span class="dv">5</span>]</span>
<span id="cb23-556"><a href="#cb23-556" aria-hidden="true" tabindex="-1"></a> ])<span class="op">,</span></span>
<span id="cb23-557"><a href="#cb23-557" aria-hidden="true" tabindex="-1"></a> {<span class="dt">value</span><span class="op">:</span> <span class="bu">JSON</span><span class="op">.</span><span class="fu">parse</span>(localStorage<span class="op">.</span><span class="fu">getItem</span>(<span class="st">"q3_selected_t3"</span>) <span class="op">??</span> <span class="st">"[]"</span>) <span class="op">??</span> []}</span>
<span id="cb23-558"><a href="#cb23-558" aria-hidden="true" tabindex="-1"></a>)<span class="op">;</span></span>
<span id="cb23-559"><a href="#cb23-559" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-560"><a href="#cb23-560" aria-hidden="true" tabindex="-1"></a>{</span>
<span id="cb23-561"><a href="#cb23-561" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-562"><a href="#cb23-562" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q3_selected_t3"</span><span class="op">,</span> <span class="bu">JSON</span><span class="op">.</span><span class="fu">stringify</span>(q3))<span class="op">;</span></span>
<span id="cb23-563"><a href="#cb23-563" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q3_correct_t3"</span><span class="op">,</span> <span class="st">"1,2,5"</span>)<span class="op">;</span></span>
<span id="cb23-564"><a href="#cb23-564" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q3_result_t3"</span><span class="op">,</span> (<span class="op">!</span>q3 <span class="op">||</span> q3<span class="op">.</span><span class="at">length</span> <span class="op">===</span> <span class="dv">0</span>) <span class="op">?</span> <span class="st">"unattempted"</span> <span class="op">:</span> (q3<span class="op">.</span><span class="fu">toString</span>() <span class="op">===</span> <span class="st">"1,2,5"</span> <span class="op">?</span> <span class="st">"correct"</span> <span class="op">:</span> <span class="st">"incorrect"</span>))<span class="op">;</span></span>
<span id="cb23-565"><a href="#cb23-565" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-566"><a href="#cb23-566" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-6-1" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-6-2" data-nodetype="expression">
</div>
</div>
</div>
<div class="sourceCode cell-code hidden" id="cb24" data-startfrom="568" data-source-offset="-810"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 567;"><span id="cb24-568"><a href="#cb24-568" aria-hidden="true" tabindex="-1"></a><span class="fu">ok_checkbox</span>(q3<span class="op">,</span> <span class="st">"1,2,5"</span>)<span class="op">;</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-display">
<div>
<div id="ojs-cell-6-3" data-nodetype="expression">
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="aside-defining-our-own-data" class="level3">
<h3 class="anchored" data-anchor-id="aside-defining-our-own-data">Aside: Defining Our Own Data</h3>
<p>For data which is not already known to R (i.e. data which is not part of a data frame), we can still use R to quickly perform computations. Consider the distributions of doors knocked on by two political campaign workers last week (Monday – Friday): <span class="math inline">\(\begin{array}{lcl} \text{Worker A} & : & 23,~24,~25,~26,~27\\ \text{Worker B} & : & 0,~15,~25,~35,~50\end{array}\)</span>. We do this below with the help of the <code>c()</code> function in R, which can be used to create lists of values.</p>
<p>The following code block finds the mean and median for <code>Worker A</code> — execute the code block to find the mean and median. Once you’ve done this for <code>Worker A</code>, add two lines to the bottom of the code block so that it also finds the mean and median for <code>Worker B</code>.</p>
<div class="cell" data-exercise="my-data">
<div>
<div id="webr-15" class="exercise-cell">
</div>
<script type="webr-15-contents">
eyJjb2RlIjoibWVhbihjKDIzLCAyNCwgMjUsIDI2LCAyNykpXG5tZWRpYW4oYygyMywgMjQsIDI1LCAyNiwgMjcpKSIsImF0dHIiOnsiZXZhbCI6dHJ1ZSwiZWRpdCI6dHJ1ZSwicGVyc2lzdCI6dHJ1ZSwiZXhlcmNpc2UiOiJteS1kYXRhIn19
</script>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="my-data">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-16-contents" aria-controls="callout-16" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 1
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-16" class="callout-16-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Copy and paste the two existing lines and replace the data inside of <code>c()</code> to reflect Worker B in the last two lines.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(<span class="fu">c</span>(<span class="dv">23</span>, <span class="dv">24</span>, <span class="dv">25</span>, <span class="dv">26</span>, <span class="dv">27</span>))</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="fu">median</span>(<span class="fu">c</span>(<span class="dv">23</span>, <span class="dv">24</span>, <span class="dv">25</span>, <span class="dv">26</span>, <span class="dv">27</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="my-data">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-17-contents" aria-controls="callout-17" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 1
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-17" class="callout-17-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Copy and paste the two existing lines and replace the data inside of <code>c()</code> to reflect Worker B in the last two lines.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Worker A</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(<span class="fu">c</span>(<span class="dv">23</span>, <span class="dv">24</span>, <span class="dv">25</span>, <span class="dv">26</span>, <span class="dv">27</span>))</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="fu">median</span>(<span class="fu">c</span>(<span class="dv">23</span>, <span class="dv">24</span>, <span class="dv">25</span>, <span class="dv">26</span>, <span class="dv">27</span>))</span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Worker B</span></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(<span class="fu">c</span>(___))</span>
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></a><span class="fu">median</span>(<span class="fu">c</span>(___))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="hint webr-ojs-exercise exercise-hint d-none" data-exercise="my-data">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-18-contents" aria-controls="callout-18" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Hint 3 (Solved)
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-18" class="callout-18-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>Copy and paste the two existing lines and replace the data inside of <code>c()</code> to reflect Worker B in the last two lines.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Worker A</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(<span class="fu">c</span>(<span class="dv">23</span>, <span class="dv">24</span>, <span class="dv">25</span>, <span class="dv">26</span>, <span class="dv">27</span>))</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a><span class="fu">median</span>(<span class="fu">c</span>(<span class="dv">23</span>, <span class="dv">24</span>, <span class="dv">25</span>, <span class="dv">26</span>, <span class="dv">27</span>))</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Worker B</span></span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(<span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">15</span>, <span class="dv">25</span>, <span class="dv">35</span>, <span class="dv">50</span>))</span>
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a><span class="fu">median</span>(<span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">15</span>, <span class="dv">25</span>, <span class="dv">35</span>, <span class="dv">50</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="cell" data-exercise="my-data" data-solution="true">
<div class="webr-ojs-exercise exercise-solution d-none" data-exercise="my-data">
<code id="interpolate-17" class="solution-code d-none">
mean(c(23, 24, 25, 26, 27))
median(c(23, 24, 25, 26, 27))
mean(c(0, 15, 25, 35, 50))
median(c(0, 15, 25, 35, 50))</code>
<div class="sourceCode cell-code" id="interpolate-18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="interpolate-18-1"><a href="#interpolate-18-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="interpolate-18-2"><a href="#interpolate-18-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(<span class="fu">c</span>(<span class="dv">23</span>, <span class="dv">24</span>, <span class="dv">25</span>, <span class="dv">26</span>, <span class="dv">27</span>))</span>
<span id="interpolate-18-3"><a href="#interpolate-18-3" aria-hidden="true" tabindex="-1"></a><span class="fu">median</span>(<span class="fu">c</span>(<span class="dv">23</span>, <span class="dv">24</span>, <span class="dv">25</span>, <span class="dv">26</span>, <span class="dv">27</span>))</span>
<span id="interpolate-18-4"><a href="#interpolate-18-4" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(<span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">15</span>, <span class="dv">25</span>, <span class="dv">35</span>, <span class="dv">50</span>))</span>
<span id="interpolate-18-5"><a href="#interpolate-18-5" aria-hidden="true" tabindex="-1"></a><span class="fu">median</span>(<span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">15</span>, <span class="dv">25</span>, <span class="dv">35</span>, <span class="dv">50</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
<div class="cell" data-exercise="my-data" data-check="true">
<script type="exercise-check-my-data-contents">
eyJjb2RlIjoiXG5leGVyY2lzZV9yZXN1bHRzJG15X2RhdGEgPDwtIFwiZmFpbFwiXG5cbmdyYWRldGhpczo6Z3JhZGVfdGhpcyh7XG4gIGZlZWRiYWNrIDwtIGdyYWRldGhpczo6Y29kZV9mZWVkYmFjaygpXG4gIGlmIChpcy5udWxsKGZlZWRiYWNrKSkge1xuICAgIGV4ZXJjaXNlX3Jlc3VsdHMkbXlfZGF0YSA8PC0gXCJwYXNzXCJcbiAgICBwYXNzKHJhbmRvbV9wcmFpc2UoKSlcbiAgfVxuICBleGVyY2lzZV9yZXN1bHRzJG15X2RhdGEgPDwtIFwiZmFpbFwiXG4gIGZhaWwocmFuZG9tX2VuY291cmFnZW1lbnQoKSlcbn0pIiwiYXR0ciI6eyJjaGVjayI6dHJ1ZSwicGVyc2lzdCI6dHJ1ZSwiZXhlcmNpc2UiOiJteS1kYXRhIiwiZXZhbCI6dHJ1ZSwiZWRpdCI6dHJ1ZX19
</script>
</div>
<ol start="5" type="1">
<li>Use your explorations of the means and medians for the campaign workers to answer the following question.</li>
</ol>
<div class="callout callout-style-default callout-caution 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">
Check Your Understanding: Measures of Center
</div>
</div>
<div class="callout-body-container callout-body">
<p>Which of the following are correct? Select all that apply.</p>
<div class="cell">
<div class="sourceCode cell-code hidden" id="cb28" data-startfrom="683" data-source-offset="-1"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 682;"><span id="cb28-683"><a href="#cb28-683" aria-hidden="true" tabindex="-1"></a>viewof q4 <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">checkbox</span>(</span>
<span id="cb28-684"><a href="#cb28-684" aria-hidden="true" tabindex="-1"></a> <span class="kw">new</span> <span class="bu">Map</span>([</span>
<span id="cb28-685"><a href="#cb28-685" aria-hidden="true" tabindex="-1"></a> [<span class="st">"Worker A and Worker B had the same mean and median number of doors knocked."</span><span class="op">,</span> <span class="dv">1</span>]<span class="op">,</span></span>
<span id="cb28-686"><a href="#cb28-686" aria-hidden="true" tabindex="-1"></a> [<span class="st">"Worker A and Worker B had identical door-knocking strategies."</span><span class="op">,</span> <span class="dv">2</span>]<span class="op">,</span></span>
<span id="cb28-687"><a href="#cb28-687" aria-hidden="true" tabindex="-1"></a> [<span class="st">"Measures of center alone are not enough to describe numerical data."</span><span class="op">,</span> <span class="dv">3</span>]</span>
<span id="cb28-688"><a href="#cb28-688" aria-hidden="true" tabindex="-1"></a> ])<span class="op">,</span></span>
<span id="cb28-689"><a href="#cb28-689" aria-hidden="true" tabindex="-1"></a> {<span class="dt">value</span><span class="op">:</span> <span class="bu">JSON</span><span class="op">.</span><span class="fu">parse</span>(localStorage<span class="op">.</span><span class="fu">getItem</span>(<span class="st">"q4_selected_t3"</span>) <span class="op">??</span> <span class="st">"[]"</span>) <span class="op">??</span> []}</span>
<span id="cb28-690"><a href="#cb28-690" aria-hidden="true" tabindex="-1"></a>)<span class="op">;</span></span>
<span id="cb28-691"><a href="#cb28-691" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-692"><a href="#cb28-692" aria-hidden="true" tabindex="-1"></a>{</span>
<span id="cb28-693"><a href="#cb28-693" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q4_selected_t3"</span><span class="op">,</span> <span class="bu">JSON</span><span class="op">.</span><span class="fu">stringify</span>(q4))<span class="op">;</span></span>
<span id="cb28-694"><a href="#cb28-694" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q4_correct_t3"</span><span class="op">,</span> <span class="st">"1,3"</span>)<span class="op">;</span></span>
<span id="cb28-695"><a href="#cb28-695" aria-hidden="true" tabindex="-1"></a> localStorage<span class="op">.</span><span class="fu">setItem</span>(<span class="st">"q4_result_t3"</span><span class="op">,</span> (<span class="op">!</span>q4 <span class="op">||</span> q4<span class="op">.</span><span class="at">length</span> <span class="op">===</span> <span class="dv">0</span>) <span class="op">?</span> <span class="st">"unattempted"</span> <span class="op">:</span> (q4<span class="op">.</span><span class="fu">toString</span>() <span class="op">===</span> <span class="st">"1,3"</span> <span class="op">?</span> <span class="st">"correct"</span> <span class="op">:</span> <span class="st">"incorrect"</span>))<span class="op">;</span></span>
<span id="cb28-696"><a href="#cb28-696" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb28-697"><a href="#cb28-697" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-7-1" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-7-2" data-nodetype="expression">
</div>
</div>
</div>
<div class="sourceCode cell-code hidden" id="cb29" data-startfrom="699" data-source-offset="-623"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 698;"><span id="cb29-699"><a href="#cb29-699" aria-hidden="true" tabindex="-1"></a><span class="fu">ok_checkbox</span>(q4<span class="op">,</span> <span class="st">"1,3"</span>)<span class="op">;</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-display">
<div>
<div id="ojs-cell-7-3" data-nodetype="expression">
</div>
</div>
</div>
</div>
</div>
</div>
</section>
</section>
<section id="measuring-spread" class="level2">
<h2 class="anchored" data-anchor-id="measuring-spread">Measuring Spread</h2>
<p><strong>Measures of Variability</strong>: Clearly, the center of a dataset doesn’t tell the entire story. Our two political campaign workers obviously have very different door-knocking strategies but both have a mean (and median) of 25 doors per day. We should also measure the <em>spread</em> of data.</p>
<p>The <em>standard deviation</em> of a set of observations is denoted by <span class="math inline">\(s\)</span> (or <span class="math inline">\(\sigma\)</span> in the case of population-level data) and is computed as follows: <span class="math display">\[s = \sqrt{\frac{\displaystyle{\sum_{i=1}^{n}{\left(x_i-\bar{x}\right)^2}}}{n-1}}\]</span></p>
<p>We should also note that if you are certain that you are working with population-level data, then the denominator used to compute the standard deviation should be changed to <span class="math inline">\(N\)</span> (the population size). We can do this because there is no uncertainty in estimating the population standard deviation if we have records from every element of the population.</p>
<p><strong>Explaining the Standard Deviation Formula</strong>: The standard deviation seeks to measure an “average deviation” from the mean.</p>
<ul>
<li>If we don’t look too closely at the formula, we can see the summation symbol <span class="math inline">\(\left(\sum\right)\)</span> as well as division (by just about the number of values we’ve added up). That’s almost like an average!</li>
<li>What are we averaging? The quantity <span class="math inline">\(\left(x - \bar{x}\right)\)</span> denotes an observed value’s deviation from the mean. We shouldn’t average these values though, because since the mean sits in the <em>center</em> of the data and we would have deviations above the mean (positive) “cancelling out” deviations below the mean (negative).</li>
<li>We square the deviations which has two effects: (i) all of the squared deviations are now non-negative, so that no cancellation can occur, and (ii) large deviations from the mean carry a larger weight in measuring the standard deviation.</li>
<li>Since we squared the deviations before computing the “average”, the units of measure are no longer comparable to the original units that the variable was measured in — the units are square units now. This is why we see the large square root as the last piece of the formula — taking the square root brings us back to the original units.</li>
</ul>
<p>The <em>inter-quartile range</em> (IQR) of a set of observations measures the spread of the “middle 50 percent” of the observations. The IQR is the distance between <span class="math inline">\(Q_1\)</span> (the 25th percentile) and <span class="math inline">\(Q_3\)</span> (the 75th percentile).</p>
<ul>
<li>Note that the median of a set of observations splits the set into two halves: an upper half and a lower half. The median of the lower half is called the <em>first quartile</em> (<span class="math inline">\(Q_1\)</span>) while the median of the upper half is called the <em>third quartile</em> (<span class="math inline">\(Q_3\)</span>). The interquartile range is the distance between <span class="math inline">\(Q_1\)</span> and <span class="math inline">\(Q_3\)</span>. That is, <span class="math display">\[IQR = Q_3 - Q_1\]</span></li>
</ul>
<ol start="6" type="1">
<li>Check your intuition about the standard deviation and interquartile range by answering the questions below.</li>
</ol>
<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">