-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tex
More file actions
9115 lines (7370 loc) · 358 KB
/
index.tex
File metadata and controls
9115 lines (7370 loc) · 358 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
% Options for packages loaded elsewhere
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
%
\documentclass[
letterpaper,
DIV=11,
numbers=noendperiod]{scrreprt}
\usepackage{xcolor}
\usepackage{amsmath,amssymb}
\setcounter{secnumdepth}{5}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math} % this also loads fontspec
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
% xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
% Make \paragraph and \subparagraph free-standing
\makeatletter
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}{
\@ifstar
\xxxParagraphStar
\xxxParagraphNoStar
}
\newcommand{\xxxParagraphStar}[1]{\oldparagraph*{#1}\mbox{}}
\newcommand{\xxxParagraphNoStar}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}{
\@ifstar
\xxxSubParagraphStar
\xxxSubParagraphNoStar
}
\newcommand{\xxxSubParagraphStar}[1]{\oldsubparagraph*{#1}\mbox{}}
\newcommand{\xxxSubParagraphNoStar}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
\makeatother
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{241,243,245}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.40,0.45,0.13}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\BuiltInTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\ExtensionTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.28,0.35,0.67}{#1}}
\newcommand{\ImportTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.07,0.07,0.07}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\newsavebox\pandoc@box
\newcommand*\pandocbounded[1]{% scales image to fit in text height/width
\sbox\pandoc@box{#1}%
\Gscale@div\@tempa{\textheight}{\dimexpr\ht\pandoc@box+\dp\pandoc@box\relax}%
\Gscale@div\@tempb{\linewidth}{\wd\pandoc@box}%
\ifdim\@tempb\p@<\@tempa\p@\let\@tempa\@tempb\fi% select the smaller of both
\ifdim\@tempa\p@<\p@\scalebox{\@tempa}{\usebox\pandoc@box}%
\else\usebox{\pandoc@box}%
\fi%
}
% Set default figure placement to htbp
\def\fps@figure{htbp}
\makeatother
\ifLuaTeX
\usepackage{luacolor}
\usepackage[soul]{lua-ul}
\else
\usepackage{soul}
\fi
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\KOMAoption{captions}{tableheading}
\makeatletter
\@ifpackageloaded{bookmark}{}{\usepackage{bookmark}}
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\AtBeginDocument{%
\ifdefined\contentsname
\renewcommand*\contentsname{Table of contents}
\else
\newcommand\contentsname{Table of contents}
\fi
\ifdefined\listfigurename
\renewcommand*\listfigurename{List of Figures}
\else
\newcommand\listfigurename{List of Figures}
\fi
\ifdefined\listtablename
\renewcommand*\listtablename{List of Tables}
\else
\newcommand\listtablename{List of Tables}
\fi
\ifdefined\figurename
\renewcommand*\figurename{Figure}
\else
\newcommand\figurename{Figure}
\fi
\ifdefined\tablename
\renewcommand*\tablename{Table}
\else
\newcommand\tablename{Table}
\fi
}
\@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing}
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
\makeatother
\makeatletter
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\@ifpackageloaded{subcaption}{}{\usepackage{subcaption}}
\makeatother
\usepackage{bookmark}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same}
\hypersetup{
pdftitle={Human Geography through Merseyside - Quantitative Block: Seeing the world through numbers},
pdfauthor={Zi Ye and Ron Mahabir},
colorlinks=true,
linkcolor={blue},
filecolor={Maroon},
citecolor={Blue},
urlcolor={Blue},
pdfcreator={LaTeX via pandoc}}
\title{Human Geography through Merseyside - Quantitative Block: Seeing
the world through numbers}
\author{Zi Ye and Ron Mahabir}
\date{2026-02-28}
\begin{document}
\maketitle
\renewcommand*\contentsname{Table of contents}
{
\hypersetup{linkcolor=}
\setcounter{tocdepth}{2}
\tableofcontents
}
\bookmarksetup{startatroot}
\chapter*{Welcome}\label{welcome}
\addcontentsline{toc}{chapter}{Welcome}
\markboth{Welcome}{Welcome}
This is the website for ``Human Geography through Merseyside -
Quantitative Block: Seeing the world through numbers'' (module
\textbf{ENVS162}) at the University of Liverpool. This block of the
module is designed and delivered by Dr.~Zi Ye and Dr.~Ron Mahabir from
the Geographic Data Science Lab at the University of Liverpool. The
module seeks to provide hands-on experience and training in introductory
statistics for human geographers.
The website is \textbf{free to use} and is licensed under the
\href{https://creativecommons.org/licenses/by-nc-nd/4.0/}{Attribution-NonCommercial-NoDerivatives
4.0 International}. A compilation of this web course is hosted as a
GitHub repository that you can access:
\begin{itemize}
\tightlist
\item
As an \href{https://gdsl-ul.github.io/quant}{html website}.
\item
As a \href{https://github.com/GDSL-UL/quant}{GitHub repository}.
\end{itemize}
\section*{Contact}\label{contact}
\addcontentsline{toc}{section}{Contact}
\markright{Contact}
\begin{quote}
Zi Ye - zi.ye {[}at{]} liverpool.ac.uk Lecturer in Geographic
Information Science Office 107, Roxby Building, University of Liverpool
- 74 Bedford St S, Liverpool, L69 7ZT, United Kingdom.
\end{quote}
\begin{quote}
Ron Mahabir - Ron.Mahabir {[}at{]} liverpool.ac.uk Lecturer in
Geographic Data Science Office 4xx, Roxby Building, University of
Liverpool - 74 Bedford St S, Liverpool, L69 7ZT, United Kingdom.
\end{quote}
\bookmarksetup{startatroot}
\chapter*{Overview}\label{overview}
\addcontentsline{toc}{chapter}{Overview}
\markboth{Overview}{Overview}
\section*{Aim and Learning
Objectives}\label{aim-and-learning-objectives}
\addcontentsline{toc}{section}{Aim and Learning Objectives}
\markright{Aim and Learning Objectives}
This sub-module aims to provide training and skills on a set of basic
quantitative skills for data collection, analysis, and interpretation
and to enable you to link conceptual ideas with real world examples.
\textbf{This block serves as the foundation for ENVS225 Explore the
Social World, Year 2 BA field class and, optionally, for Year 3
dissertation.}
\textbf{Background}
Data and research are key pillars of the global economy and society
today. We need rigorous approaches to collecting and analysing both the
statistics that can tell us `how much' and if there are observable
relationships between phenomena; and the information gives us a nuanced
understanding of cultural contexts and human dynamics. Quantitative
skills enable us to explore and measure socio-economic activities and
processes at large scales, while qualitative skills enable understanding
of social, cultural, and political contexts and diverse lived
experiences. Rather than being in opposition, qualitative and
quantitative research can complement one another in the investigation of
today's pressing research questions.
To these ends, this block will help you develop your quantitative
skills, as critical tools. This course will help you understand what
quantitative statistical researchers use and develop a set of research
techniques that can be used in your field classes and dissertations.
\textbf{Learning objectives:}
\begin{itemize}
\tightlist
\item
Understand how to explore a dataset, containing a number of
observations described by a set of variables.
\item
Demonstrate an understanding in the application and interpretation of
commonly used quantitative research methods.
\item
Ability to work with quantitative data to understand real-world social
phenomenian and patterns.
\end{itemize}
\section*{Module Structure}\label{module-structure}
\addcontentsline{toc}{section}{Module Structure}
\markright{Module Structure}
\textbf{Staff:} Dr Zi Ye and Dr Ron Mahabir
\textbf{Where and When}
\textbf{Week 1 - 5 Lecture:} \textbf{Tuesday} \textbf{(12am -- 1pm)}
\textbf{@} Mathematical Sciences, Proudman Lecture Theatre
\textbf{Week 1 - 6 Practical PC session: Friday (9 -- 11 am)} \textbf{@}
Central Teaching Lab: PC Teaching Centre
Lectures will introduce and explain the fundamentals of quantitative
methods, with the opportunity to apply the method introduced in the labs
later in the week.
The computer practical sessions, will give you the chance to use and
apply quantitative methods to real-world data. These are primarily
self-directed sessions, but with support on hand if you get stuck.
Support and training in R will be provided through these sessions.
Weekly sessions will be driven by empirical research questions.
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.0887}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.5081}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.2984}}
>{\raggedright\arraybackslash}p{(\linewidth - 6\tabcolsep) * \real{0.0887}}@{}}
\toprule\noalign{}
\begin{minipage}[b]{\linewidth}\raggedright
Week
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
Topic
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
Format
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
Staff
\end{minipage} \\
\midrule\noalign{}
\endhead
\bottomrule\noalign{}
\endlastfoot
1 & Introduction
Getting Started in RStudio: Knowing Merseyside & Lecture
Computer Lab Practical & ZY/RM \\
2 & Exploratory Data Analysis: UK Election & Lecture and Computer Lab
Practical & ZY \\
3 & Sampling and data manipulation: Happiness around the world & Lecture
and Computer Lab Practical & ZY \\
4 & Correlation, data reliability and the issue of scale: Health &
Lecture and Computer Lab Practical & RM \\
5 & How robust are my findings & Lecture and Computer Lab Practical &
RM \\
6 & Online Assessment & Computer Lab & RM/ZY \\
\end{longtable}
\section*{Software and Data}\label{software-and-data}
\addcontentsline{toc}{section}{Software and Data}
\markright{Software and Data}
For quantitative training sessions, ensure you have installed and/or
have access to \textbf{RStudio}. To run the analysis and reproduce the
code in R, you need the following software installed on your machine:
\begin{itemize}
\tightlist
\item
R-4.2.2 (or later)
\item
RStudio 2022.12.0-353 (or later)
\end{itemize}
To install and update:
\begin{itemize}
\tightlist
\item
R, download the appropriate version from
\href{https://cran.r-project.org/}{The Comprehensive R Archive Network
(CRAN)}.
\item
RStudio, download the appropriate version from
\href{https://posit.co/download/rstudio-desktop/}{here}.
\end{itemize}
\textbf{This software is already installed on University Machines. But
you will need it to run the analysis on your personal devices.}
\textbf{Data}
Example datasets could be accessed through
\href{https://canvas.liverpool.ac.uk/courses/85565/modules}{ENVS 162
Canvas module} every week.
\bookmarksetup{startatroot}
\chapter*{Assessment}\label{assessment}
\addcontentsline{toc}{chapter}{Assessment}
\markboth{Assessment}{Assessment}
\textbf{Week 6 Computer-based `open book' multiple-choice exam}
\begin{itemize}
\item
The online assessment will be released at \textbf{4pm on Thursday 5th
March} and should be c\textbf{ompleted by 4pm on Friday 6th March.}
\item
\textbf{Also available 06/03/2025 9:00 -- 11:00 CTL PC Teaching Centre
(1st Floor CTL)}
\item
Should take less \textbf{90 minute}s; c.~20 questions; 24 hours to
complete
\item
Questions and answers randomised for each student (anti-cheating
measure)
\item
Some questions of factual recall, more requiring data analysis to find
answers
\end{itemize}
\textbf{\emph{Preparation for assessment}}
\begin{itemize}
\item
Weekly lecture \& weekly computer practical `clinic sessions'
\item
Weekly holding hands formative tasks at the last 20 mins of the
practical session
\item
Week 5 mock online test
\end{itemize}
\bookmarksetup{startatroot}
\chapter{Lab: Getting Started in RStudio - Knowing
Merseyside}\label{lab-getting-started-in-rstudio---knowing-merseyside}
\section{\texorpdfstring{\textbf{Overview}}{Overview}}\label{overview-1}
This practical intend to prepare students who have limited experiences
with R and RStudio. The content are adapted based on
\begin{itemize}
\item
Brunsdon, Chris, and Lex Comber. 2018. \emph{An Introduction to r for
Spatial Analysis and Mapping (2e)}. Sage.
\item
Comber, Lex, and Chris Brunsdon. 2021. \emph{Geographical Data Science
and Spatial Data Analysis: An Introduction in r}. Sage.
\end{itemize}
\section{\texorpdfstring{\textbf{Getting set up with
RStudio}}{Getting set up with RStudio}}\label{getting-set-up-with-rstudio}
\subsection{\texorpdfstring{\textbf{Install R and RStudio (if
necessary)}}{Install R and RStudio (if necessary)}}\label{install-r-and-rstudio-if-necessary}
R is a free, open-source programming language used for statistical
analysis, data visualization, and data science
RStudio is a free front-end to R, designed to make using R easier
All of the PCs in the University PC Teaching Centre used for this class
come with R and RStudio pre-installed, as do the PCs in many other
University PC Teaching Centres.
However, you may wish to install R and RStudio on your own computer, or
on a University PC that lacks them.
\textbf{University computers}: Use the \emph{Install University
Applications} app on the computer to install the latest version of
RStudio (this will also install the latest version of R)
\textbf{Your own computer}: R and RStudio can be downloaded from the
CRAN website and installed your own computer - see below for details.
\ul{\textbf{A key point is that you should install R before you install
RStudio.}}
The simplest way to get R installed on your computer is to go the
download pages on the R website - a quick search for `download R' should
take you there, but if not you could try:
\begin{itemize}
\item
Windows: \url{https://cran.r-project.org/bin/windows/base/}
\item
Mac: \url{https://cran.r-project.org/bin/macosx/}
\item
Linux: \url{http://cran.r-project.org/bin/linux/}
\end{itemize}
The Windows and Mac version come with installer packages and are easy to
install whilst the Linux binaries require use of a command terminal.
RStudio can be downloaded from
\url{https://www.rstudio.com/products/rstudio/download/} and the free
version of RStudio Desktop is more than sufficient for this module and
all the other things you will to do at degree level.
If you experience any problems installing R or RStudio on your own
computer, bring it to one of the class lab sessions where we will be
able to provide advice.
\subsection{File management}\label{file-management}
Before you start installing software or downloading data, create a
folder on your M-Drive (if working on a University networked machine) or
locally if working on your own device -- name this `ENVS162' and within
this create a sub-folder for each practical session. For this session,
create a sub-folder called~\texttt{Week1}~in your~\texttt{ENVS162}
folder on your M-Drive. Take care to ensure you do not delete any work
you do complete in the practical sessions. It is imperative that you
practice good file management!
\subsection{\texorpdfstring{\textbf{Open
RStudio}}{Open RStudio}}\label{open-rstudio}
RStudio provides an interface to the different things that R can do via
the 4 panes: the Console where code is entered (bottom left), a Source
pane with R scripts (top left), the variables in the working Environment
(top right), Files, Plots, Help etc (bottom right) - see the RStudio
environment in Figure below.
In the figure above of the RStudio interface, a new script has been
opened, a line of code had been written and then run in the console. The
code assigns a value of 100 to \texttt{x}. The file has been saved into
the current working environment. You are expected to define a similar
set up for each practical as you work through the code. Note that
\textbf{in the script}, anything that follows a \texttt{\#} is a comment
and ignored by R.
Users can set up their personal preferences for how they like their
RStudio interface. Similar to straight R, there are very few pull-down
menus in R, and therefore you will type lines of code into your script
and run these in what is termed a \emph{command line interface} (the
console). Like all command line interfaces, the learning curve is steep
but the interaction with the software is more detailed which allows
greater flexibility and precision in the specification of commands.
Beyond this there are further choices to be made. Commands can be
entered in two forms: directly into the \emph{R console} window or as a
series of commands into a script window. We strongly advise that all
code should be \textbf{written in a script} - (a \texttt{.R} file) and
then \textbf{run from the script}. - To run code in a script, place the
cursor on the line of code and then run by pressing the `Run' icon at
the top left of the script pane, or by pressing \textbf{Ctrl Enter} (PC)
(or \textbf{Cmd Enter} on a Mac).
\pandocbounded{\includegraphics[keepaspectratio]{labs/images/clipboard-1359090391.png}}
\subsection{Ways of working}\label{ways-of-working}
The first set of consideration relate to \emph{how} you should work in
R/RStudio. The key things to remember are:
\begin{itemize}
\item
R is a learning curve if you have never done anything like this
before. It can be scary. It can be intimidating. But once you have a
bit of familiarity with how things work, it is incredibly powerful.
\item
You will be working from practical worksheets which will have all the
code you need. Your job is to try to \textbf{understand} what the code
is doing and \textbf{not} to remember the code. Comments in your code
really help.
\item
To help you do this, the very strong suggestion is use the R scripts
that are provided, and that you add your own comments to help you
understand what is going on when you return to them. Comments are
prefaced by a hash (\texttt{\#}) that is ignored by R. Then you can
save your code (with comments), run it and return to it later and
modify at your leisure.
\end{itemize}
The module places a strong emphasis placed on learning by doing, which
means that you encouraged to unpick the code that you are given, adapt
it and play with it. It is not about remembering or being able to recall
each function used but about understanding what is being done. If you
can remember what you did previously (i.e.~the operations you undertook)
and understand what you did, you will be able to return to your code the
next time you want to do something similar. To help you with this you
should:
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
Always run your code from an R script\ldots{} \textbf{always}! These
are provided for each practical;
\item
Annotate you scripts with comments. These are prefixed by a hash
(\texttt{\#}) in the code;
\item
Save your R script to your folder.
\end{enumerate}
In Summary:
\begin{itemize}
\item
You should always use a script (a text file containing code) for your
code which can be saved and then re-run at a later date.
\item
You can write your own code into a script, copy and paste code into it
or use an existing script (for example as provided for each of the
R/RStudio practicals in this module).
\item
To open a new R script go to File \textgreater{} New File
\textgreater{} R Script to open a new R file, and save it with a
sensible name.
\item
To load an existing script file go to File \textgreater{} Open File
and then navigate to your file. Or, if you have recently opened the
file, go to File \textgreater{} Recent Files \textgreater.
\item
It is good practice to set the working directory at the beginning of
your R session. This can be done via the menu in RStudio Session
\textgreater{} Set Working Directory \textgreater{} \ldots. This
points the R session to the folder you choose and will ensure that any
files you wish to read, write or save are placed in this directory.
\item
To run code in a script, place the cursor on the line of code and then
run by pressing the `Run' icon at the top left of the script pane, or
by pressing Ctrl Enter (PC) or Cmd Enter (Mac).
\end{itemize}
\subsection{Your first R code}\label{your-first-r-code}
In this section you will undertake a few generic operations. You will:
\begin{itemize}
\item
undertake \textbf{assignment}: the allocation of values to an R
object.
\item
use assignment to create a \textbf{vector} of elements and a
\textbf{matrix} of elements.
\item
undertake \textbf{operations} on R objects.
\item
apply some \textbf{functions} to R objects (functions nearly always
return a value).
\item
access some of R in-built data to examine a data table (or
\texttt{data.frame} which is like an Excel spreadsheet).
\item
do some basic \textbf{plotting}, including scatter plots and
histograms.
\item
create data summaries.
\end{itemize}
On the way you will also be introduced to \textbf{indexing}.
First, you should \textbf{create a new R script} (see above) and save it
as \texttt{week1.R} in the working directory you are using for this
practical. This should be the \texttt{Week1} sub-directory you created
in the \texttt{ENVS162} folder. Note that you should create a separate
folder for each week's practical.
\subsubsection{Assignment}\label{assignment}
The command line prompt in the Console window, the
\texttt{\textgreater{}}, is an invitation to start typing in your
commands.
Write the following into your script: \texttt{3+5} and run it. Recall
that code is run done by either by pressing the Run icon at the top left
of the script pane, or by pressing \textbf{Ctrl Enter} (PC) or
\textbf{Cmd Enter} (Mac).
\begin{Shaded}
\begin{Highlighting}[]
\DecValTok{3}\SpecialCharTok{+}\DecValTok{5}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[1] 8
\end{verbatim}
Here the result is 8. The \texttt{{[}1{]}} that precedes the output it
formally indicates, \emph{first requested element will follow}. In this
case there is just one element. The \texttt{\textgreater{}} indicates
that R is ready for another command.
Now type the following in to your script and run it:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{y }\OtherTok{\textless{}{-}} \DecValTok{3}\SpecialCharTok{+}\DecValTok{5}
\NormalTok{y}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[1] 8
\end{verbatim}
Here the value of the \texttt{3+5} has been \textbf{\emph{assigned}} to
\texttt{y}. The syntax \texttt{y\ \textless{}-\ 3+5} can be read as
\texttt{y} \textbf{\emph{gets}} \texttt{3+5}. When \texttt{y} is invoked
its value is returned (8).
For the purposes of this module, in R the equals sign (\texttt{=}) is
the same as \texttt{\textless{}-}, a left diamond bracket
\texttt{\textless{}} followed by a minus sign \texttt{-}. This too is
interpreted by R as \textbf{\emph{is assigned to}} or
\textbf{\emph{gets}} when the code is read \textbf{right to left}.
Now copy and paste the following into your R script and run both lines:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x }\OtherTok{\textless{}{-}} \FunctionTok{matrix}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{,}\DecValTok{4}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{6}\NormalTok{,}\DecValTok{7}\NormalTok{,}\DecValTok{8}\NormalTok{), }\AttributeTok{nrow =} \DecValTok{4}\NormalTok{)}
\NormalTok{y }\OtherTok{=} \FunctionTok{matrix}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{8}\NormalTok{, }\AttributeTok{nrow =} \DecValTok{4}\NormalTok{, }\AttributeTok{byrow =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}
You should see the \texttt{x} appear with the \texttt{y} in the
Environment pane. \texttt{y} has now been overwritten with a new
assignment. If you click on the icon next to them, you will get a
`spreadsheet' view of the data you have created.
Of course you can also enter the following in the console and see what
is returned:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{y}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
\end{verbatim}
\textbf{Note} In the code snippets above you have used
\texttt{parentheses} - round brackets. Different kinds of brackets are
used in different ways in R. Parentheses are used with
\textbf{functions}, and contain the \textbf{arguments} that are passed
to the function, separated by commas (\texttt{,}).
In this case the functions are \texttt{c()} and \texttt{matrix()}. The
function \texttt{c()} combines or concatenates elements into a vector,
and \texttt{matrix()} creates a matrix of elements in a tabular format.
In the line of code
\texttt{x\ =\ matrix(c(1,2,3,4,5,6,7,8),\ nrow\ =\ 4)}, the arguments
passed to the \texttt{matrix()} function are the vector of values
\texttt{c(1,2,3,4,5,6,7,8)} and \texttt{nrow\ =\ 4}. Other kinds of
brackets are used in different ways as you will see later.
One final thing to note is that the matrix is \texttt{y} is has the
numbers 1 to 8, but this is specified by \texttt{1:8}. Try entering
\texttt{3:65}, \texttt{19:11}, and \texttt{1.5:5} to see how the colon
(\texttt{:}) works in this context.
\subsubsection{Operations}\label{operations}
Now you can undertake \emph{operations} on R objects and apply
\emph{functions} to them. Write the following code into your script and
then run it:
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# x is a matrix}
\NormalTok{x}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# multiplication}
\NormalTok{x}\SpecialCharTok{*}\DecValTok{2}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[,1] [,2]
[1,] 2 10
[2,] 4 12
[3,] 6 14
[4,] 8 16
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# sum of x}
\FunctionTok{sum}\NormalTok{(x)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[1] 36
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# mean of x}
\FunctionTok{mean}\NormalTok{(x)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[1] 4.5
\end{verbatim}
Operations can be undertaken directly using mathematical notation like
\texttt{*} for multiplication or using functions like \texttt{max} to
find the maximum value in an R object.
\subsubsection{Functions}\label{functions}
Functions are always followed by parenthesis (round brackets)
\texttt{(\ )}. These are different from square and curly brackets
\texttt{{[}\ {]}} and \texttt{\{\ \}}. Functions always return
something, a result if you like, and have the generic form:
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# don\textquotesingle{}t run this or write this into your script!}
\NormalTok{result }\OtherTok{=} \ControlFlowTok{function}\NormalTok{(value or R object, other parameters)}
\end{Highlighting}
\end{Shaded}
Do not run or enter this code in your script - it is an example!
\subsubsection{Data Tables}\label{data-tables}
Here we will load a data table in \texttt{data.frame} (like a
spreadsheet) in R/RStudio. R has number of in-built datasets that we can
use the code below loads one of these:
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{data}\NormalTok{(mtcars)}
\FunctionTok{class}\NormalTok{(mtcars)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[1] "data.frame"
\end{verbatim}
Have a look at what is loaded by listing the objects in the current R
session
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ls}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
[1] "mtcars" "x" "y"
\end{verbatim}
You should see the \texttt{mtcars} object. You can examine this data in
a number of ways
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# the structure of mtcars}
\FunctionTok{str}\NormalTok{(mtcars)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
'data.frame': 32 obs. of 11 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$ disp: num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec: num 16.5 17 18.6 19.4 17 ...
$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
$ am : num 1 1 1 0 0 0 0 0 0 0 ...
$ gear: num 4 4 4 3 3 3 3 4 4 4 ...
$ carb: num 4 4 1 1 2 1 4 2 2 4 ...
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# the first six rows (or head) of mtcars}
\FunctionTok{head}\NormalTok{(mtcars)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
\end{verbatim}
The \texttt{mtcars} object is a \texttt{data.frame}, a kind of data
table, and it has a number of attributes which are all numeric. The code
below prints it all out to the console:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mtcars}
\end{Highlighting}
\end{Shaded}
Data frames are `flat' in that they typically have a rectangular layout
like a spreadsheet, with rows typically relating to observations
(individuals, areas, people, houses, etc) and columns relating to their
properties or attributes (height, age, etc). The columns in data frames
can be of different types: vectors of numbers, factors (classes) or text
strings. In matrices all of the columns have to be off the same type.
Data frames are central to what we will do in R.
\subsubsection{Plotting the data: `Hello
World!'}\label{plotting-the-data-hello-world}
The code below creates a plot of 2 variables counts in the data:
\texttt{mpg} and \texttt{disp}.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(disp }\SpecialCharTok{\textasciitilde{}}\NormalTok{ mpg, }\AttributeTok{data =}\NormalTok{ mtcars, }\AttributeTok{pch=}\DecValTok{16}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\pandocbounded{\includegraphics[keepaspectratio]{labs/01.GettingStartedinRStudio_files/figure-pdf/unnamed-chunk-12-1.pdf}}
The option \texttt{pch=16} sets the plotting character to a solid black
dot. More plot characters are available - examine the help for
\texttt{points()} to see these (For any command, if you are the first
time use it, you can always ask R to explain to you by using ? as help)
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{?points}
\end{Highlighting}
\end{Shaded}