-
Notifications
You must be signed in to change notification settings - Fork 0
/
STAT462_22_L1Basics.html
901 lines (807 loc) · 34.1 KB
/
STAT462_22_L1Basics.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Lab 1: Introduction to R</title>
<script src="site_libs/header-attrs-2.11/header-attrs.js"></script>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/flatly.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<script src="site_libs/kePrint-0.0.1/kePrint.js"></script>
<link href="site_libs/lightable-0.0.1/lightable.css" rel="stylesheet" />
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<link rel="stylesheet" href="styles.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.tab('show');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
.tocify-subheader {
display: inline;
}
.tocify-subheader .tocify-item {
font-size: 0.95em;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">STAT-462</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="T1_R_Basics.html">Tutorials</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Labs
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="STAT462_22_L1Basics.html">Lab 1:R-Basics</a>
</li>
<li>
<a href="STAT462_Lab2.html">Lab 2: Exploratory Data Analysis (EDA)</a>
</li>
<li>
<a href="STAT462_Lab3.html">Lab 3: Scatterplots</a>
</li>
<li>
<a href="STAT462_Lab4.html">Lab 4: Regression Code showcase</a>
</li>
<li>
<a href="STAT462_Lab5.html">Lab 5: LINE Assumptions</a>
</li>
<li>
<a href="STAT462_Lab6.html">Lab 6: Outliers & Transformations</a>
</li>
<li>
<a href="STAT462_Lab7.html">Lab 7: Multiple Regression</a>
</li>
<li>
<a href="STAT462_Lab8.html">Lab 8: Putting it together</a>
</li>
</ul>
</li>
<li>
<a href="STAT462_LabEXAMPLE.html">LAB EXAMPLE</a>
</li>
<li>
<a href="Worked_questions.html">Worked questions</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Lab 1: Introduction to R</h1>
<h3 class="subtitle"><h5 style="font-style:normal">
STAT-462 - Regression Analysis
</h4></h3>
<h4 class="author"><h5 style="font-style:normal">
Dr Helen Greatrex
</h4></h4>
</div>
<style>
p.comment {
background-color: #DBDBDB;
padding: 10px;
border: 1px solid black;
margin-left: 0px;
border-radius: 5px;
font-style: normal;
}
h1.title {
font-weight: bold;
font-family: Arial;
}
h2.title {
font-family: Arial;
}
</style>
<style type="text/css">
#TOC {
font-size: 12px;
font-family: Arial;
}
</style>
<p><br />
</p>
<div id="welcome-to-lab-1" class="section level1" number="1">
<h1><span class="header-section-number">1</span> Welcome to Lab 1!</h1>
<div id="overview" class="section level2" number="1.1">
<h2><span class="header-section-number">1.1</span> Overview</h2>
<p>There are 8 labs in this course. Each one will build on the last one, with support provided in a series of tutorials (see the tutorials tab). You should NOT submit the tutorial work. We will work through the tutorials together and then your job is to submit the requirements on this page.</p>
<p>By the end of this week’s lab, you will be able to:</p>
<ol style="list-style-type: decimal">
<li>Get comfortable with the lab format & how to work through them</li>
<li>Know how to get help</li>
<li>Get familiar with the RStudio interface</li>
<li>Run your first programming commands</li>
<li>Put everything together into an interactive markdown website (like this guide)</li>
<li>Understand the process for submitting assignments</li>
</ol>
<p><strong>If the labs are causing major problems with your computer or your computer hardware is struggling (or you have any other software issue), Talk to Dr Greatrex</strong>. We can fix this and there are other options for “online R” that you can use.</p>
<p>THERE IS A TEAMS DISCUSSION FOR ANY LAB HELP (see canvas)</p>
<p class="comment">
<strong>Assignment 1 is due by midnight the night before your next lab on Canvas.</strong> See <a href="https://psu.instructure.com/courses/2174925/assignments/13762663">here</a> for assignment guidelines & we will cover them in detail here.
</p>
<p><br></p>
</div>
<div id="instructions" class="section level2" number="1.2">
<h2><span class="header-section-number">1.2</span> Instructions</h2>
<p><br></p>
<div id="access-r-and-r-studio" class="section level3" number="1.2.1">
<h3><span class="header-section-number">1.2.1</span> Access R and R-Studio</h3>
<p>These were covered in homework 2 and are here for reference. Your R and R studio should be up to date.</p>
<ul>
<li>Read <a href="https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#11_What_are_R_and_R-Studio">Tutorial 1.1</a>.<br><br></li>
<li>Install BOTH R and R-Studio, or create an R-Studio Cloud account. This is covered in <a href="https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#12_Accessing_R__R-Studio">Tutorial 1.2</a>.</li>
</ul>
<p><br></p>
</div>
<div id="getting-started-in-r" class="section level3" number="1.2.2">
<h3><span class="header-section-number">1.2.2</span> Getting started in R</h3>
<ul>
<li><p>Work through <a href="https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#13_Getting_started">Tutorial1.3</a> to open R-studio and change your settings.<br><br></p></li>
<li><p>Use <a href="https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#2_R-Projects_and_Packages">Tutorial 2.1</a> to make your first R-project called STAT462-Lab1-Project<br></p></li>
<li><p>[OPTIONAL BUT RECOMMENDED] Now work through <a href="https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#3_Basic_commands">Tutorial 3</a> to get used to the basics<br></p></li>
<li><p>Now go back to <a href="https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#22_R-Packages">Tutorial 2B</a> to understand what an R-Package is and to install the code packages/libraries we will need for this course.<br></p></li>
</ul>
<p><br></p>
</div>
<div id="start-markdown" class="section level3" number="1.2.3">
<h3><span class="header-section-number">1.2.3</span> Start Markdown</h3>
<ul>
<li>[OPTIONAL BUT HIGHLY ENCOURAGED] Work through <a href="https://psu-spatial.github.io/Geog364-2021/pg_Tut4_markdown.html">Tutorial 4</a> to make your first Markdown document. Save and close - this was for your reference!</li>
</ul>
<p><br></p>
</div>
<div id="lab-challenge" class="section level3" number="1.2.4">
<h3><span class="header-section-number">1.2.4</span> Lab challenge!</h3>
<p>Everything above was for your own learning - you do not need to submit any of it. Now we will start the actual lab you will submit. Consider printing this out and checking off each step one by one.</p>
<p><br></p>
<div id="reopen-r-studio" class="section level4 unnumbered">
<h4 class="unnumbered">1: Reopen R studio</h4>
<p>First, see if you remember how to close and re-open R studio. <br></p>
<p>Save everything then close down R studio.To re-open, do not reopen R from the programmes folder or icon!</p>
<p><strong>INSTEAD, GO TO THE LAB 1 FOLDER ON YOUR COMPUTER AND DOUBLE CLICK THE .RPROJ FILE CALLED STAT462-Lab1-Project!</strong>.</p>
<p>This will get you straight back into your lab folder for you to carry on.</p>
<p><br></p>
</div>
<div id="make-lab-markdown" class="section level4 unnumbered">
<h4 class="unnumbered">2: Make lab-Markdown</h4>
<p>Create a new <strong>RMarkdown document</strong> called <code>STAT462-Lab1-PSUID.Rmd</code>. e.g. for me <em>STAT462-Lab1-hlg5155.Rmd</em>. <br></p>
<p>Delete all the text/code below line 11 (e.g. everything from “R Markdown onward”)</p>
<p><br></p>
</div>
<div id="adding-paragraphs" class="section level4 unnumbered">
<h4 class="unnumbered">3: Adding paragraphs</h4>
<p>You might find it easier to use the markdown visual text editor. <br> Click the “A” symbol at the top right to switch (instructions here:<a href="https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#46_Formatting_text">Tutorial 4.6</a>)</p>
<ul>
<li><p>In the white text area, create a level 1 heading called <em>“Introduction to STAT-462”</em>. For a cheat sheet to help with this, move your mouse to the top of the page, click the help menu, then click Markdown Quick reference, or see <a href="https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#46_Formatting_text">Tutorial 4.6</a>.</p></li>
<li><p>Leave a blank line, then use the STAT-462 syllabus to describe the course late policy in your own words (e.g. you’re writing about this in the white space).</p></li>
<li><p>Press “knit” at the top of the screen. If you haven’t made a mistake a pop up should appear with a html file and your edits. If you have a made a mistake, stop and fix before continuing.</p></li>
</ul>
<p><br></p>
</div>
<div id="code-showcase" class="section level4" number="1.2.4.1">
<h4><span class="header-section-number">1.2.4.1</span> 4: Code showcase</h4>
<p><img src="Figures/Lab1_Basics_CodeChunk.png" width="954" /> <br<</p>
<p>Leave another blank line and add a new Level 1 heading called <em>“Code Showcase”</em>. Add a blank line afterwards too. Create a code chunk (). Inside use R code to calculate the following <br><br></p>
<ul>
<li>103<sup>3</sup> (e.g <code>103*103*103</code>, or <code>103^3</code>) <br></li>
</ul>
<p>_ The co-sine of your age (google is your friend, google R function for cosine) <br></p>
<ul>
<li>Use R code to work out how many characters are in <a href="https://www.bbcamerica.com/anglophenia/2015/09/how-to-pronounce-the-longest-place-name-in-the-u-k">the longest town name in Wales</a> <em>Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch</em>. <br>
<ul>
<li>Hint 1, remember you can copy paste this into your code. <br></li>
<li>Hint 2.. your tutorials and quote marks!<br></li>
<li>Hint 3.. you can google any R command, try gooling R command for number of characters in a word.<br><br></li>
</ul></li>
</ul>
<p>Press “knit” at the top of the screen. If you haven’t made a mistake a pop up should appear with a html file and your edits. If you have a made a mistake, stop and fix before continuing.</p>
<p><br></p>
</div>
<div id="packages" class="section level4" number="1.2.4.2">
<h4><span class="header-section-number">1.2.4.2</span> 5. Packages</h4>
<p>If you have run through the tutorials, you should have installed all the packages you need for this course.</p>
<p><strong>IF NOT GO AND DO THE BULK INSTALL IN <a href="https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#22_R-Packages">Tutorial 2.2</a>]</strong></p>
<p>You only need to <em>download</em> the packages once, but we still need to load them every time we need a command in that package (in the same way you only download your banking app once from the app store but need to press the icon every time you want to use it).</p>
<ul>
<li>Always INSTALL/DOWNLOAD packages using a menu or in the console</li>
<li>Always LOAD packages in a code-chunk in your script (as we need to do tihs every time)</li>
</ul>
<div id="do-this-part-even-if-you-did-tutorial-2" class="section level5" number="1.2.4.2.1">
<h5><span class="header-section-number">1.2.4.2.1</span> DO THIS PART EVEN IF YOU DID TUTORIAL 2!</h5>
<p>Today we will be using commands from the skimr package, the ggplot2 package and the plotly package.</p>
<p>Somewhere near the topc of your script, make a new code chunk and add this code. Remember to run the code chunk! (pressing the green arrow, or go to the run button on the top right and press Run All)</p>
<pre class="r"><code>library(tidyverse)
library(skimr)
library(ggplot2)
library(plotly)
library(ggpubr)</code></pre>
<p><br></p>
</div>
</div>
<div id="data-analysis" class="section level4" number="1.2.4.3">
<h4><span class="header-section-number">1.2.4.3</span> 6: Data analysis</h4>
<div id="a-load-summarise-the-data" class="section level5" number="1.2.4.3.1">
<h5><span class="header-section-number">1.2.4.3.1</span> 6a Load & summarise the data</h5>
<p>Leave a blank line, and create a new level 1 heading called <em>Car Analysis</em>. Leave a blank line afterwards too. We’re going to work with a table of data that’s already pre-loaded into R inside the ggplot2 package.</p>
<ol style="list-style-type: decimal">
<li><p>Make sure you have run the library code chunk above without error, or it won’t work. <br></p></li>
<li><p>Load the data using this command</p></li>
</ol>
<pre class="r"><code>data(mpg)</code></pre>
<p><br></p>
<ol start="3" style="list-style-type: decimal">
<li>Type the <code>?mpg</code> command in the console. This will bring up the help file. <br> Read the background of the dataset and briefly summarise it in your script (in the white space). Include the unit of analysis and a reasonable population this dataset could be used to explore. BE SPECIFIC AND READ THE HELP FILE CAREFULLY!</li>
</ol>
<p><br></p>
</div>
<div id="b-summary-analysis" class="section level5" number="1.2.4.3.2">
<h5><span class="header-section-number">1.2.4.3.2</span> 6b Summary analysis</h5>
<p>Now look at the data itself. If you look in the environment tab, you will see a new variable called mpg. Click on it’s NAME to see the spreadsheet/table itself and familiarise yourself with the data. <br> <br></p>
<p>We could have also looked at the raw data by either by typing its name into the console or a code chunk, or by using commands like <code>head(mpg)</code> to show the first few lines</p>
<p>Let’s look at the summary statistics. Leave a blank line and create a new code chunk containing the following code <br><br></p>
<pre class="r"><code># mpg comes from the ggplot2 package
# skim comes from the skimr package
skim(mpg)</code></pre>
<p>This command compiles the summary statistics for mpg. You can also use the summary() command to achieve a similar result <br><br></p>
<pre class="r"><code>summary(mpg)</code></pre>
<p>Now below your code, answer the following questions in full sentences.</p>
<ol style="list-style-type: decimal">
<li><p><strong>What is the mean manufacturing year of the car models in the dataset?</strong> <br><br></p></li>
<li><p><strong>What is the maximum value of the number of cylinders of the car models?</strong> <br><br></p></li>
<li><p><strong>What type of data is the manufacturer column of the dataset? (nominal, ordinal, interval, continous) State why you answered that.</strong></p></li>
</ol>
<p><br><br></p>
</div>
<div id="c-boxplots" class="section level5" number="1.2.4.3.3">
<h5><span class="header-section-number">1.2.4.3.3</span> 6c Boxplots</h5>
<p>Suppose I want to compare the miles per gallon of the car models being driven in cities for different classes of cars. We can make the comparison by making a side-by-side boxplot. Create a new code chunk. Use the code below to create the plot.</p>
<p>To see the full plot, press the tiny “show in new window” button at the top right of the plot itself.</p>
<pre class="r"><code># boxplot is the command. You can have a command on several lines.
boxplot(cty~class, # the miles per gallon column vs each type
data=mpg, # from the mpg table
xlab="Class",ylab="mpg in a city",
col=c("deepskyblue4","deepskyblue")) # and colours</code></pre>
<ol start="4" style="list-style-type: decimal">
<li><em>Briefly comment on what you can tell about the population/data from this plot.</em></li>
</ol>
<p><br></p>
</div>
<div id="d-selecting-columns" class="section level5" number="1.2.4.3.4">
<h5><span class="header-section-number">1.2.4.3.4</span> 6d Selecting columns</h5>
<p>As we discussed above, the mpg table is loaded into R. But what if we want to access a specific column? This is where the $ symbol is useful. It means, “select the column named..”</p>
<p>For example if you type <code>mpg$model</code> into the console, you will see that it just prints the data from the cyl column (number of cylinders) onto the screen. We could then apply a command to those numbers. For example <code>unique(mpg$model)</code> will find all the unique values in that column</p>
<ol start="5" style="list-style-type: decimal">
<li><strong>Create a new code chunk. Use the <code>median()</code> and <code>IQR()</code> commands to find the actual median and inter quartile range of the column containing highway miles per gallon. Summarise your results in full sentences</strong></li>
</ol>
</div>
<div id="e-normality-testing" class="section level5" number="1.2.4.3.5">
<h5><span class="header-section-number">1.2.4.3.5</span> 6e Normality testing</h5>
<p>I want to see if the hwy variable in our data is normally distributed. We can do this using the histogram and normal-qq plot. The code below makes a histogram and QQNorm plot for the car miles per gallon on a highway. It also runs a Shapiro-Wilks test for the horsepower.</p>
<pre class="r"><code>hist(mpg$hwy,br=10)</code></pre>
<pre class="r"><code># Instead we are going to use a nicer format from the `ggpubr` library
ggqqplot(mpg$hwy,col="red")</code></pre>
<pre class="r"><code># Shapiro Wilks test
shapiro.test(mpg$hwy)</code></pre>
<ol start="6" style="list-style-type: decimal">
<li><strong>Use the data above to assess whether the hwy variable in the mpg dataset is normally distributed. Write out a full hypothesis test in the text, using the R results to inform your conclusions</strong></li>
</ol>
<p>If you need help, see this tutorial (<a href="https://r4ds.had.co.nz/data-visualisation.html" class="uri">https://r4ds.had.co.nz/data-visualisation.html</a>).</p>
</div>
<div id="e-normal-distributions" class="section level5" number="1.2.4.3.6">
<h5><span class="header-section-number">1.2.4.3.6</span> 6e Normal distributions</h5>
<p>Now we’re going to move onto some commands that allow us to calculate normal probabilities using R, rather than slowly by hand. Using the R functions <code>pnorm()</code> and <code>qnorm()</code>, we can find the cumulative probabilities and normal quantiles for any normal distribution.</p>
<p>For example, Cat lifespans have a normal distribution with a mean of 15.4 and a standard deviation of 2.3. What’s the probability of a cat lifespan less than 14 years?</p>
<pre class="r"><code>?pnorm # use the help file of ?pnorm to see what lower.tail=TRUE/FALSE does
pnorm(14,mean=15.4,sd=2.4,lower.tail = TRUE)</code></pre>
<pre><code>## [1] 0.2798345</code></pre>
<p>So, it’s 0.2798 - or ~28%</p>
<p>What about the probability of a cat being older than 20?</p>
<pre class="r"><code>pnorm(20,mean=15.4,sd=2.4,lower.tail = FALSE)</code></pre>
<pre><code>## [1] 0.02764015</code></pre>
<p>Normal quantiles go the other way around - they tell you the z score for whatever probability you require. For example, 80% of cats live to be what age?</p>
<pre class="r"><code>qnorm(.80,mean=15.4,sd=2.4)</code></pre>
<pre><code>## [1] 17.41989</code></pre>
<p>So the the answer is 17.42 years (remember the units!)</p>
<ol start="7" style="list-style-type: decimal">
<li><p><strong>Use R to find the probability below and write your conclusions in the text</strong> <br></p>
<ul>
<li><p><strong>If X ∼ N(5,2), then find P(X ≤ 4)</strong> <br></p></li>
<li><p><strong>If X ∼ N(5,2), then find the value a such that P(X < a) = 0.025</strong> <br></p></li>
</ul></li>
</ol>
<p><br></p>
</div>
</div>
<div id="above-beyond" class="section level4" number="1.2.4.4">
<h4><span class="header-section-number">1.2.4.4</span> 7. Above & beyond</h4>
<p>Remember that an A is 94%, so you can ignore this section and still easily get an A.</p>
<p>But here is your time to shine. Also, if you are struggling in another part of the lab, you can use this to gain back points.</p>
<p><strong>To get the final 4 marks in the lab, you need to show me something new, e.g. you need to go above and beyond the lab questions in some way.</strong></p>
<ul>
<li>You get 2/4 for doing something new in any way</li>
<li>You get 4/4 for something really impressive or multiple small things.</li>
</ul>
<p>Please tell us in your R script what you did!</p>
<p>Here in lab 1, maybe you added in different text formats to make your lab script more clear (bold/italic etc). Maybe you worked out how to add axis labels to ggplot (note, it’s a pain), maybe you used nested headings or sub-headings, or worked out how to add a table of contents.</p>
<p>OR……..</p>
<p>Alternatively, you get 4/4 for successfully answering this question fully in your script, using R to help:</p>
<p><strong>Records maintained by the your admin office indicate that amount of time elapsed between the submission of travel receipts and the final reimbursement of funds has approximately a normal distribution with a mean of 39 days and a standard deviation of 6 days. If you submitted your travel claim 55 days ago, what is the probability that it should have been returned by now? What might you conclude?</strong></p>
<p><br> <br></p>
</div>
</div>
</div>
<div id="submitting-your-lab" class="section level2" number="1.3">
<h2><span class="header-section-number">1.3</span> Submitting your Lab</h2>
<p>Remember to save your work throughout and to spell check your writing (next to the save button). Now, press the knit button again. If you have not made any mistakes in the code then R should create a html file in your lab 1 folder which includes your answers. If you look at your lab 1 folder, you should see this there - complete with a very recent time-stamp.</p>
<p>In that folder, double click on the html file. This will open it in your browser. CHECK THAT THIS IS WHAT YOU WANT TO SUBMIT.</p>
<p>If you are on R studio cloud, see Tutorial 1 for how to download your files</p>
<p>Now go to Canvas and submit BOTH your html and your .Rmd file in Lab 1.</p>
<p><img src="Figures/Lab1_Basics_2022_fig1.png" width="1673" /></p>
<p><br></p>
</div>
<div id="lab-1-submission-check-list" class="section level2" number="1.4">
<h2><span class="header-section-number">1.4</span> Lab 1 submission check-list</h2>
<p>See the table below for what this means - 100% is hard to get!</p>
<p><strong>HTML FILE SUBMISSION - 8 marks</strong></p>
<p><strong>RMD CODE SUBMISSION - 8 marks</strong></p>
<p><strong>MARKDOWN/CODE STYLE - 10 MARKS</strong></p>
<p>Your code and document is neat and easy to read. LOOK AT YOUR HTML FILE IN YOUR WEB-BROWSER BEFORE YOU SUBMIT. There is also a spell check next to the save button. You have written your answers below the relevant code chunk in full sentences in a way that is easy to find and grade. For example, you have written in full sentences, it is clear what your answers are referring to.</p>
<p><strong>Introduction to STAT-462: 10 MARKS</strong></p>
<p>You have described the lab late policy clearly in a way I could share with a new student.</p>
<p><strong>Code Showcase: 20 MARKS</strong></p>
<p>You have managed to successfully complete all the code challenges</p>
<p><strong>Car/Normal analysis: 40 MARKS</strong></p>
<p>You included all the code and successfully answered the questions, providing reasoning where appropriate</p>
<p><strong>Above and beyond: 4 MARKS</strong></p>
<p>Here you need to go above and beyond the lab questions in some way. Here in lab 1, maybe you added in different text formats to make your lab script more clear (bold/italic etc). Maybe you worked out how to add axis labels to ggplot (note, it’s a pain), maybe you used nested headings or sub-headings, or worked out how to add a table of contents.</p>
<p>You get 2/4 for doing something new in any way and 4/4 for something really impressive or multiple small things.</p>
<p>[100 marks total]</p>
<p>Overall, here is what your lab should correspond to:</p>
<table class=" lightable-classic-2 table table-striped table-hover table-responsive" style="font-family: "Arial Narrow", "Source Sans Pro", sans-serif; margin-left: auto; margin-right: auto; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;">
Grade
</th>
<th style="text-align:left;">
% Mark
</th>
<th style="text-align:left;">
Rubric
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
A*
</td>
<td style="text-align:left;">
98-100
</td>
<td style="text-align:left;">
Exceptional. Not only was it near perfect, but the graders learned something. THIS IS HARD TO GET.
</td>
</tr>
<tr>
<td style="text-align:left;">
NA
</td>
<td style="text-align:left;">
96+
</td>
<td style="text-align:left;">
You went above and beyond
</td>
</tr>
<tr>
<td style="text-align:left;">
A
</td>
<td style="text-align:left;">
94+:
</td>
<td style="text-align:left;">
Everything asked for with high quality. Class example
</td>
</tr>
<tr>
<td style="text-align:left;">
A-
</td>
<td style="text-align:left;">
90+
</td>
<td style="text-align:left;">
The odd minor mistake, All code done but not written up in full sentences etc. A little less care
</td>
</tr>
<tr>
<td style="text-align:left;">
B+
</td>
<td style="text-align:left;">
87+
</td>
<td style="text-align:left;">
More minor mistakes. Things like missing units, getting the odd question wrong, no workings shown
</td>
</tr>
<tr>
<td style="text-align:left;">
B
</td>
<td style="text-align:left;">
84+
</td>
<td style="text-align:left;">
Solid work but the odd larger mistake or missing answer. Completely misinterpreted something, that type of thing
</td>
</tr>
<tr>
<td style="text-align:left;">
B-
</td>
<td style="text-align:left;">
80+
</td>
<td style="text-align:left;">
Starting to miss entire/questions sections, or multiple larger mistakes. Still a solid attempt.
</td>
</tr>
<tr>
<td style="text-align:left;">
C+
</td>
<td style="text-align:left;">
77+
</td>
<td style="text-align:left;">
You made a good effort and did some things well, but there were a lot of problems. (e.g. you wrote up the text well, but messed up the code)
</td>
</tr>
<tr>
<td style="text-align:left;">
C
</td>
<td style="text-align:left;">
70+
</td>
<td style="text-align:left;">
It’s clear you tried and learned something. Just attending labs will get you this much as we can help you get to this stage
</td>
</tr>
<tr>
<td style="text-align:left;">
D
</td>
<td style="text-align:left;">
60+
</td>
<td style="text-align:left;">
You attempt the lab and submit something. Not clear you put in much effort or you had real issues
</td>
</tr>
<tr>
<td style="text-align:left;">
F
</td>
<td style="text-align:left;">
0+
</td>
<td style="text-align:left;">
Didn’t submit, or incredibly limited attempt.
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.odd').parent('tbody').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open');
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// temporarily add toc-ignore selector to headers for the consistency with Pandoc
$('.unlisted.unnumbered').addClass('toc-ignore')
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3,h4",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = false;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>