-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPS09_source.html
More file actions
1557 lines (1525 loc) · 84.1 KB
/
PS09_source.html
File metadata and controls
1557 lines (1525 loc) · 84.1 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.3.433">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Your Name">
<title>Problem Set 09</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 { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
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 { 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="PS09_source_files/libs/clipboard/clipboard.min.js"></script>
<script src="PS09_source_files/libs/quarto-html/quarto.js"></script>
<script src="PS09_source_files/libs/quarto-html/popper.min.js"></script>
<script src="PS09_source_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="PS09_source_files/libs/quarto-html/anchor.min.js"></script>
<link href="PS09_source_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="PS09_source_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="PS09_source_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="PS09_source_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="PS09_source_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="https://polyfill.io/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>
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<div class="quarto-alternate-formats"><h2>Other Formats</h2><ul><li><a href="PS09_source.pdf"><i class="bi bi-file-pdf"></i>PDF</a></li></ul></div></div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Problem Set 09</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Your Name </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">Last modified on April 23, 2025 10:26:53 Eastern Daylight Time</p>
</div>
</div>
</div>
</header>
<section id="background" class="level1">
<h1>Background</h1>
<p>First load the necessary packages:</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<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="fu">library</span>(tidyverse)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(infer)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
<p>For this Problem Set, you will work with some grade-point-average (GPA) data for college freshman. The following will read in the data:</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span>(<span class="sc">!</span><span class="fu">dir.exists</span>(<span class="st">"./Data"</span>)){</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">dir.create</span>(<span class="st">"./Data"</span>)</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>url <span class="ot"><-</span> <span class="st">"https://rudeboybert.github.io/SDS220/static/PS/sat_gpa.csv"</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span>(<span class="sc">!</span><span class="fu">file.exists</span>(<span class="st">"./Data/sat_gpa.csv"</span>)){ </span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">download.file</span>(url, <span class="at">destfile =</span> <span class="st">"./Data/sat_gpa.csv"</span>)</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>sat_gpa <span class="ot"><-</span> <span class="fu">read_csv</span>(<span class="st">"./Data/sat_gpa.csv"</span>)</span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="fu">dim</span>(sat_gpa)</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] 1000 7</code></pre>
</div>
<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="co"># Show first 6 rows of sat_gpa</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="fu">kable</span>(<span class="fu">head</span>(sat_gpa))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">…1</th>
<th style="text-align: left;">sex</th>
<th style="text-align: right;">sat_verbal</th>
<th style="text-align: right;">sat_math</th>
<th style="text-align: right;">sat_total</th>
<th style="text-align: left;">gpa_hs</th>
<th style="text-align: right;">gpa_fy</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">65</td>
<td style="text-align: right;">62</td>
<td style="text-align: right;">127</td>
<td style="text-align: left;">high</td>
<td style="text-align: right;">3.18</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: left;">Female</td>
<td style="text-align: right;">58</td>
<td style="text-align: right;">64</td>
<td style="text-align: right;">122</td>
<td style="text-align: left;">high</td>
<td style="text-align: right;">3.33</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: left;">Female</td>
<td style="text-align: right;">56</td>
<td style="text-align: right;">60</td>
<td style="text-align: right;">116</td>
<td style="text-align: left;">high</td>
<td style="text-align: right;">3.25</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">42</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">95</td>
<td style="text-align: left;">high</td>
<td style="text-align: right;">2.42</td>
</tr>
<tr class="odd">
<td style="text-align: right;">5</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">55</td>
<td style="text-align: right;">52</td>
<td style="text-align: right;">107</td>
<td style="text-align: left;">high</td>
<td style="text-align: right;">2.63</td>
</tr>
<tr class="even">
<td style="text-align: right;">6</td>
<td style="text-align: left;">Female</td>
<td style="text-align: right;">55</td>
<td style="text-align: right;">56</td>
<td style="text-align: right;">111</td>
<td style="text-align: left;">high</td>
<td style="text-align: right;">2.91</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<p>Be sure to take a look at the data in <code>sat_gpa</code>. Each row or case in this data frame is a student. The data includes the binary gender (<code>sex</code>) of each student; the math (<code>sat_math</code>), verbal (<code>sat_verbal</code>) and total SAT scores (<code>sat_total</code>) for each student; the GPA of each student in high school (<code>gpa_hs</code>) categorized as “low” or “high”; and the GPA of each student their first year of college on a numeric scale (<code>gpa_fy</code>).</p>
<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">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>We will use hypothesis testing to answer the following questions:</p>
<ul>
<li>Is the mean freshmen GPA for females greater than the mean freshmen GPA for males?</li>
<li>Is the mean total SAT score for students with a “high” GPA greater than the mean total SAT score for students with a “low” GPA?</li>
</ul>
<p>Note, if you get stuck as you are working through this, it will be helpful to go back and read <a href="https://moderndive.com/9-hypothesis-testing.html">Chapter 9</a> in <a href="https://moderndive.com/index.html">ModernDive</a>.</p>
</div>
</div>
</section>
<section id="gender-differences-in-first-year-gpa" class="level1">
<h1>Gender Differences in First-Year GPA?</h1>
<section id="exploratory-data-analysis" class="level2">
<h2 class="anchored" data-anchor-id="exploratory-data-analysis">Exploratory Data Analysis</h2>
<div class="callout callout-style-default callout-note no-icon callout-titled" title="Problem 1">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 1
</div>
</div>
<div class="callout-body-container callout-body">
<p>Calculate the mean GPA score for each sex, using the <code>group_by</code> and <code>summarize</code> commands from the <code>dplyr</code> package. Store the result in <code>avg_gpa_sex</code>. What is the difference in sample mean GPA’s? Make a guess: is this difference statistically significant?</p>
</div>
</div>
<div class="callout callout-style-default callout-important no-icon callout-titled" title="Problem 1 Answers">
<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 no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 1 Answers
</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">
<div class="cell" data-layout-align="center">
<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="co"># Type your code and comments inside the code chunk</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ul>
<li><p>Delete this and put your text answer here.</p></li>
<li><p>Delete this and put your text answer here.</p></li>
</ul>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note no-icon callout-titled" title="Problem 2">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 2
</div>
</div>
<div class="callout-body-container callout-body">
<p>Generate a data visualization that displays the GPAs of the two groups. Be sure to include a title and label your axes.</p>
</div>
</div>
<div class="callout callout-style-default callout-important no-icon callout-titled" title="Problem 2 Answers">
<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 no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 2 Answers
</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">
<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"># Type your code and comments inside the code chunk</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
</section>
<section id="stating-a-null-hypothesis" class="level2">
<h2 class="anchored" data-anchor-id="stating-a-null-hypothesis">Stating a Null Hypothesis</h2>
<p>We will now test the null hypothesis that there’s no difference in population mean GPA between the sexes at the population level versus the alternative hypothesis that the mean GPA for females is greater than the mean GPA for males. We can write this out in mathematical notation</p>
<p><span class="math display">\[\begin{aligned} H_0:&\mu_{\text{female}} = \mu_{\text{male}} \\\ \mbox{vs }H_A:& \mu_{\text{female}} > \mu_{\text{male}} \end{aligned}\]</span></p>
<p>or expressed differently, that the difference is 0 or not:</p>
<p><span class="math display">\[\begin{aligned} H_0:&\mu_{\text{female}} - \mu_{\text{male}} = 0 \\\ \mbox{vs }H_A:& \mu_{\text{female}} - \mu_{\text{male}} \neq 0 \end{aligned}\]</span></p>
</section>
<section id="testing-the-hypothesis" class="level2">
<h2 class="anchored" data-anchor-id="testing-the-hypothesis">Testing the Hypothesis</h2>
<p>Here’s how we use infer to run this hypothesis test:</p>
<section id="step-1-calculate-the-observed-difference" class="level3 unnumbered">
<h3 class="unnumbered anchored" data-anchor-id="step-1-calculate-the-observed-difference">Step 1: Calculate the Observed Difference</h3>
<p>Note that the order we choose does not matter here (female then male)…but since we used <code>order = c("Female", "Male")</code> here, we should do the same in subsequent calculations!</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<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>obs_diff_gpa_sex <span class="ot"><-</span> sat_gpa <span class="sc">%>%</span> </span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">specify</span>(gpa_fy <span class="sc">~</span> sex) <span class="sc">%>%</span> </span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">calculate</span>(<span class="at">stat =</span> <span class="st">"diff in means"</span>, </span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="at">order =</span> <span class="fu">c</span>(<span class="st">"Female"</span>, <span class="st">"Male"</span>)) <span class="sc">%>%</span> </span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>()</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a>obs_diff_gpa_sex</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] 0.1485209</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><span class="co"># OR</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>obs_diff <span class="ot"><-</span> <span class="sc">-</span><span class="fu">diff</span>(<span class="fu">tapply</span>(sat_gpa<span class="sc">$</span>gpa_fy, sat_gpa<span class="sc">$</span>sex, mean))</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>obs_diff</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> Male
0.1485209 </code></pre>
</div>
</div>
</div>
</div>
<p>Note that this is the difference in the group means we calculated earlier!</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<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>obs_diff_gpa_sex</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] 0.1485209</code></pre>
</div>
<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><span class="fl">2.544587</span> <span class="sc">-</span> <span class="fl">2.396066</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] 0.148521</code></pre>
</div>
<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>avf_avm <span class="ot"><-</span> obs_diff_gpa_sex</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>avf_avm</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] 0.1485209</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="step-2.-generate-the-null-distribution-of-delta" class="level3">
<h3 class="anchored" data-anchor-id="step-2.-generate-the-null-distribution-of-delta">Step 2. Generate the Null Distribution of <span class="math inline">\(\delta\)</span></h3>
<p>This step involves generating simulated values <em>as if</em> we lived in a world where there’s no difference between the two groups. Going back to the idea of permutation, and tactile sampling, this is akin to shuffling the GPA scores between male and female labels (i.e. removing the structure to the data) just as we could have done with index cards.</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<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>gpas_in_null_world <span class="ot"><-</span> sat_gpa <span class="sc">%>%</span> </span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">specify</span>(gpa_fy <span class="sc">~</span> sex) <span class="sc">%>%</span> </span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">hypothesize</span>(<span class="at">null =</span> <span class="st">"independence"</span>) <span class="sc">%>%</span> </span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">generate</span>(<span class="at">reps =</span> <span class="dv">2000</span>, <span class="at">type =</span> <span class="st">"permute"</span>)</span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a><span class="fu">kable</span>(<span class="fu">head</span>(gpas_in_null_world))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">gpa_fy</th>
<th style="text-align: left;">sex</th>
<th style="text-align: right;">replicate</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1.50</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: right;">3.18</td>
<td style="text-align: left;">Female</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2.38</td>
<td style="text-align: left;">Female</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: right;">1.96</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3.07</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: right;">2.98</td>
<td style="text-align: left;">Female</td>
<td style="text-align: right;">1</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note no-icon callout-titled" title="Problem 3">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 3
</div>
</div>
<div class="callout-body-container callout-body">
<p>What was the size of the “shuffled” (permuted) sample in each replicate?</p>
</div>
</div>
<div class="callout callout-style-default callout-important no-icon callout-titled" title="Problem 3 Answers">
<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 no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 3 Answers
</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">
<ul>
<li>Delete this and put your text answer here.</li>
</ul>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note no-icon callout-titled" title="Problem 4">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 4
</div>
</div>
<div class="callout-body-container callout-body">
<p>How many times did we do a different “shuffle” (permute) here to the sample? How many rows are in the <code>gpas_in_null_world</code> data frame?</p>
</div>
</div>
<div class="callout callout-style-default callout-important no-icon callout-titled" title="Problem 4 Answers">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-14-contents" aria-controls="callout-14" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 4 Answers
</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-14" class="callout-14-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<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"># Type your code and comments inside the code chunk</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ul>
<li><p>Delete this and put your text answer here.</p></li>
<li><p>Delete this and put your text answer here.</p></li>
</ul>
</div>
</div>
</div>
</section>
<section id="step-3.-calculate-the-differences-between-male-and-females-under-the-null-hypothesis" class="level3">
<h3 class="anchored" data-anchor-id="step-3.-calculate-the-differences-between-male-and-females-under-the-null-hypothesis">Step 3. Calculate the Differences Between Male and Females Under the Null Hypothesis</h3>
<p>The following calculates the differences in mean GPA for males and females for “shuffled” (permuted) data.</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<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>gpa_diff_under_null <span class="ot"><-</span> gpas_in_null_world <span class="sc">%>%</span> </span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">calculate</span>(<span class="at">stat =</span> <span class="st">"diff in means"</span>, <span class="at">order =</span> <span class="fu">c</span>(<span class="st">"Female"</span>, <span class="st">"Male"</span>)) </span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a>gpa_diff_under_null <span class="sc">%>%</span> </span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">slice</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>) <span class="sc">%>%</span> <span class="co"># show first five rows</span></span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">replicate</th>
<th style="text-align: right;">stat</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: right;">-0.0225343</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: right;">0.0044534</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: right;">0.0204698</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: right;">-0.0005518</td>
</tr>
<tr class="odd">
<td style="text-align: right;">5</td>
<td style="text-align: right;">-0.0045158</td>
</tr>
</tbody>
</table>
</div>
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Done with a for loop</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>B <span class="ot"><-</span> <span class="dv">2000</span></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a>mean_diff <span class="ot"><-</span> <span class="fu">numeric</span>(B)</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>B){</span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a> mean_diff[i] <span class="ot"><-</span> <span class="sc">-</span><span class="fu">diff</span>(<span class="fu">tapply</span>(sat_gpa<span class="sc">$</span>gpa_fy, <span class="fu">sample</span>(sat_gpa<span class="sc">$</span>sex), mean))</span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a><span class="fu">hist</span>(mean_diff)</span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a><span class="fu">abline</span>(<span class="at">v =</span> obs_diff, <span class="at">lty =</span> <span class="st">"dashed"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="PS09_source_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note no-icon callout-titled" title="Problem 5">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 5
</div>
</div>
<div class="callout-body-container callout-body">
<p>How many rows are in the <code>gpa_diff_under_null</code> data frame? Why?</p>
</div>
</div>
<div class="callout callout-style-default callout-important no-icon callout-titled" title="Problem 5 Answers">
<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 no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 5 Answers
</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">
<ul>
<li>Delete this and put your text answer here.</li>
</ul>
</div>
</div>
</div>
</section>
<section id="step-4.-visualize-how-the-observed-difference-compares-to-the-null-distribution-of-delta" class="level3">
<h3 class="anchored" data-anchor-id="step-4.-visualize-how-the-observed-difference-compares-to-the-null-distribution-of-delta">Step 4. Visualize how the Observed Difference Compares to the Null Distribution of <span class="math inline">\(\delta\)</span></h3>
<p>The following plots the <span class="math inline">\(\delta\)</span> values we calculated for each of the different “shuffled” replicates. This is the null distribution of <span class="math inline">\(\delta\)</span>. The red line shows the observed difference between male and female scores in the data (0.1485209) from Step 1.</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<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>gpa_diff_under_null <span class="sc">%>%</span> </span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">visualize</span>() <span class="sc">+</span></span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Difference in mean GPA for males and females"</span>, <span class="at">y =</span> <span class="st">"Count"</span>,</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Null distribution of differences in male and female GPAs"</span>,</span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Actual difference observed in the data is marked in red"</span>) <span class="sc">+</span> </span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">shade_p_value</span>(<span class="at">obs_stat =</span> obs_diff_gpa_sex, <span class="at">direction =</span> <span class="st">"two-sided"</span>) <span class="sc">+</span></span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="PS09_source_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</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">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Note that zero is the center of this null distribution.</strong> The null hypothesis is that there is no difference between males and females in GPA score. In the permutations, zero was the most common value, because all structure was removed from the data. GPA values were sorted into male and female <strong>at random</strong>. Values as large as 0.1 and -0.1 occurred, but much less frequently, because they are just not as likely when structure is removed from the data.</p>
</div>
</div>
</section>
<section id="step-5-calculate-a-p-value" class="level3">
<h3 class="anchored" data-anchor-id="step-5-calculate-a-p-value">Step 5: Calculate a p-value</h3>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>gpa_diff_under_null <span class="sc">%>%</span> </span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">get_pvalue</span>(<span class="at">obs_stat =</span> obs_diff_gpa_sex, <span class="at">direction =</span> <span class="st">"greater"</span>) <span class="sc">%>%</span> </span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>() <span class="ot">-></span> pvalue</span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a>pvalue</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] 5e-04</code></pre>
</div>
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="co"># OR from the for loop - slighlty more conservative approach</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>p_value <span class="ot"><-</span> (<span class="fu">sum</span>(mean_diff <span class="sc">>=</span> obs_diff) <span class="sc">+</span> <span class="dv">1</span>)<span class="sc">/</span>(B <span class="sc">+</span> <span class="dv">1</span>)</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a>p_value</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] 0.00149925</code></pre>
</div>
</div>
</div>
</div>
<p>The p-value indicates that there is a 5^{-4} or 0.0015 chance (very low even with the conservative p-value computation) that we would see a difference of 0.1485209 in GPA scores between males and females (or a bigger difference) if in fact there was truly no difference between the sexes in GPA scores at the population level.</p>
<div class="callout callout-style-default callout-note no-icon callout-titled" title="Problem 6">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 6
</div>
</div>
<div class="callout-body-container callout-body">
<p>Fill in the blanks and select the appropriate words below to write up the results & conclusions for this test:</p>
</div>
</div>
<div class="callout callout-style-default callout-important no-icon callout-titled" title="Problem 6 Answers">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-22-contents" aria-controls="callout-22" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 6 Answers
</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-22" class="callout-22-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<p>The mean GPA scores for females in our sample (<span class="math inline">\(\bar{x}_{f}\)</span> = ____) was greater than that of males (<span class="math inline">\(\bar{x}_{m}\)</span> = ____). This difference <strong>was/was not</strong> statistically significant at <span class="math inline">\(\alpha = 0.05\)</span>, (p = ____). Given this p-value, I <strong>would/would not</strong> reject the Null hypothesis and <strong>find evidence/do not find evidence</strong> that <strong>females</strong> have higher GPAs than <strong>males</strong> at the population level.</p>
</div>
</div>
</div>
</section>
<section id="step-6-calculate-a-confidence-interval-for-the-difference" class="level3">
<h3 class="anchored" data-anchor-id="step-6-calculate-a-confidence-interval-for-the-difference">Step 6: Calculate a Confidence Interval for the Difference</h3>
<p>The following will allow us to calculate a 95% bootstrap percentile confidence interval for the difference between mean GPA scores for females and males.</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<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>ci_diff_gpa_means <span class="ot"><-</span> sat_gpa <span class="sc">%>%</span> </span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">specify</span>(gpa_fy <span class="sc">~</span> sex) <span class="sc">%>%</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">generate</span>(<span class="at">reps =</span> <span class="dv">2000</span>, <span class="at">type =</span> <span class="st">"bootstrap"</span>) <span class="sc">%>%</span> </span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">calculate</span>(<span class="at">stat =</span> <span class="st">"diff in means"</span>, <span class="at">order =</span> <span class="fu">c</span>(<span class="st">"Female"</span>, <span class="st">"Male"</span>)) <span class="sc">%>%</span> </span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">get_ci</span>(<span class="at">level =</span> <span class="fl">0.95</span>, <span class="at">type =</span> <span class="st">"percentile"</span>)</span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a><span class="fu">kable</span>(ci_diff_gpa_means)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">lower_ci</th>
<th style="text-align: right;">upper_ci</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.0544304</td>
<td style="text-align: right;">0.2400219</td>
</tr>
</tbody>
</table>
</div>
<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"># Do the same thing with a for loop</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>sat_gpa <span class="sc">%>%</span> </span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(sex <span class="sc">==</span> <span class="st">"Female"</span>) <span class="sc">%>%</span> </span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(gpa_fy) <span class="sc">%>%</span> </span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>() <span class="ot">-></span> fem_gpa</span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a>sat_gpa <span class="sc">%>%</span> </span>
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(sex <span class="sc">==</span> <span class="st">"Male"</span>) <span class="sc">%>%</span> </span>
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(gpa_fy) <span class="sc">%>%</span> </span>
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>() <span class="ot">-></span> mal_gpa</span>
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true" tabindex="-1"></a>mean_ds <span class="ot"><-</span> <span class="fu">numeric</span>(B)</span>
<span id="cb27-11"><a href="#cb27-11" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>B){</span>
<span id="cb27-12"><a href="#cb27-12" aria-hidden="true" tabindex="-1"></a> bss1 <span class="ot"><-</span> <span class="fu">sample</span>(fem_gpa, <span class="at">size =</span> <span class="fu">sum</span>(<span class="sc">!</span><span class="fu">is.na</span>(fem_gpa)), <span class="at">replace =</span> <span class="cn">TRUE</span>)</span>
<span id="cb27-13"><a href="#cb27-13" aria-hidden="true" tabindex="-1"></a> bss2 <span class="ot"><-</span> <span class="fu">sample</span>(mal_gpa, <span class="at">size =</span> <span class="fu">sum</span>(<span class="sc">!</span><span class="fu">is.na</span>(mal_gpa)), <span class="at">replace =</span> <span class="cn">TRUE</span>)</span>
<span id="cb27-14"><a href="#cb27-14" aria-hidden="true" tabindex="-1"></a> mean_ds[i] <span class="ot"><-</span> <span class="fu">mean</span>(bss1) <span class="sc">-</span> <span class="fu">mean</span>(bss2)</span>
<span id="cb27-15"><a href="#cb27-15" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb27-16"><a href="#cb27-16" aria-hidden="true" tabindex="-1"></a><span class="fu">kable</span>(<span class="fu">quantile</span>(mean_ds, <span class="at">probs =</span><span class="fu">c</span>(<span class="fl">0.025</span>, <span class="fl">0.975</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">x</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">2.5%</td>
<td style="text-align: right;">0.0548812</td>
</tr>
<tr class="even">
<td style="text-align: left;">97.5%</td>
<td style="text-align: right;">0.2355701</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
</section>
<section id="complete-all-the-above-tasks-with-a-t-test" class="level2">
<h2 class="anchored" data-anchor-id="complete-all-the-above-tasks-with-a-t-test">Complete all the Above Tasks with a t-test</h2>
<p>Note that all the above steps can be done with one line of code <strong>if a slew of assumptions</strong> like normality and equal variance of the groups are met.</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">t.test</span>(gpa_fy <span class="sc">~</span> sex, <span class="at">var.equal =</span> <span class="cn">TRUE</span>, <span class="at">data =</span> sat_gpa, <span class="at">alternative =</span> <span class="st">"greater"</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>
Two Sample t-test
data: gpa_fy by sex
t = 3.1828, df = 998, p-value = 0.0007519
alternative hypothesis: true difference in means between group Female and group Male is greater than 0
95 percent confidence interval:
0.07169431 Inf
sample estimates:
mean in group Female mean in group Male
2.544587 2.396066 </code></pre>
</div>
</div>
</div>
</div>
</section>
</section>
<section id="relationship-between-high-school-first-year-gpa" class="level1">
<h1>Relationship Between High-School & First-Year GPA?</h1>
<p>For this analysis <code>sat_total</code> is the outcome variable, and <code>gpa_hs</code> is the predictor variable, with two levels “low” and “high”.</p>
<section id="exploratory-data-analysis-1" class="level2">
<h2 class="anchored" data-anchor-id="exploratory-data-analysis-1">Exploratory Data Analysis</h2>
<p>We can first calculate the mean total SAT score for each group (i.e students with a low and high GPA), using the <code>group_by</code> and <code>summarize</code> commands from the <code>dplyr</code> package.</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>avg_sat_gpa <span class="ot"><-</span> sat_gpa <span class="sc">%>%</span> </span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(gpa_hs) <span class="sc">%>%</span> </span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">sat_total =</span> <span class="fu">mean</span>(sat_total), <span class="at">n =</span> <span class="fu">n</span>())</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a><span class="fu">kable</span>(avg_sat_gpa)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">gpa_hs</th>
<th style="text-align: right;">sat_total</th>
<th style="text-align: right;">n</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">high</td>
<td style="text-align: right;">108.67828</td>
<td style="text-align: right;">488</td>
</tr>
<tr class="even">
<td style="text-align: left;">low</td>
<td style="text-align: right;">98.23047</td>
<td style="text-align: right;">512</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<p>We will next generate a data visualization that displays the total SAT scores of the two groups. Be sure to include a title and label your axes.</p>
<div class="callout callout-style-default callout-caution no-icon callout-titled" title="R Code">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
R Code
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(patchwork)</span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot"><-</span> <span class="fu">ggplot</span>(sat_gpa, <span class="fu">aes</span>(<span class="at">x =</span> gpa_hs, <span class="at">y =</span> sat_total)) <span class="sc">+</span> </span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">fill =</span> <span class="st">"darkgreen"</span>) <span class="sc">+</span> </span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"SAT scores based on high school </span><span class="sc">\n</span><span class="st"> GPA scores"</span>, </span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"GPA ranking"</span>, </span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"SAT score"</span>) <span class="sc">+</span> </span>
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>()</span>
<span id="cb31-8"><a href="#cb31-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Or</span></span>
<span id="cb31-9"><a href="#cb31-9" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot"><-</span> <span class="fu">ggplot</span>(<span class="at">data =</span> sat_gpa, </span>
<span id="cb31-10"><a href="#cb31-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> sat_total, <span class="at">color =</span> gpa_hs)) <span class="sc">+</span></span>
<span id="cb31-11"><a href="#cb31-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_density</span>() <span class="sc">+</span> </span>
<span id="cb31-12"><a href="#cb31-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb31-13"><a href="#cb31-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">color =</span> <span class="st">"High School GPA"</span>,</span>
<span id="cb31-14"><a href="#cb31-14" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Densities of SAT scores based </span><span class="sc">\n</span><span class="st"> on high school GPA scores"</span>,</span>
<span id="cb31-15"><a href="#cb31-15" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"SAT score"</span>)</span>
<span id="cb31-16"><a href="#cb31-16" aria-hidden="true" tabindex="-1"></a>p1 <span class="sc">+</span> p2</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="PS09_source_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="stating-a-null-hypothesis-1" class="level2">
<h2 class="anchored" data-anchor-id="stating-a-null-hypothesis-1">Stating a Null Hypothesis</h2>
<div class="callout callout-style-default callout-note no-icon callout-titled" title="Problem 7">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 7
</div>
</div>
<div class="callout-body-container callout-body">
<p>State the null and alternative hypotheses that you are testing (using words and symbols).</p>
</div>
</div>
<div class="callout callout-style-default callout-important no-icon callout-titled" title="Problem 7 Answers">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-28-contents" aria-controls="callout-28" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 7 Answers
</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-28" class="callout-28-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<ul>
<li><p>Delete this and put your text answer here.</p></li>
<li><p>Delete this and put your text answer here.</p></li>
</ul>
</div>
</div>
</div>
</section>
<section id="testing-the-null-hypothesis" class="level2">
<h2 class="anchored" data-anchor-id="testing-the-null-hypothesis">Testing the Null Hypothesis</h2>
<div class="callout callout-style-default callout-note no-icon callout-titled" title="Problem 8">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 8
</div>
</div>
<div class="callout-body-container callout-body">
<p>Calculate the observed difference between the mean total SAT scores of the low and high GPA high-school students. Store the result in an object named <code>obs_diff_sat_hs_gpa</code></p>
</div>
</div>
<div class="callout callout-style-default callout-important no-icon callout-titled" title="Problem 8 Answers">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-30-contents" aria-controls="callout-30" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Problem 8 Answers
</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-30" class="callout-30-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Type your code and comments inside the code chunk</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note no-icon callout-titled" title="Problem 9">
<div class="callout-header d-flex align-content-center">