-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tex
1469 lines (1126 loc) · 57.5 KB
/
index.tex
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
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
%
\documentclass[
letterpaper,
DIV=11,
numbers=noendperiod]{scrreprt}
\usepackage{amsmath,amssymb}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\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
\usepackage{xcolor}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\setcounter{secnumdepth}{5}
% Make \paragraph and \subparagraph free-standing
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
\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}{#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}{#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}}}
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}\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
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
\makeatletter
% allow citations to break across lines
\let\@cite@ofmt\@firstofone
% avoid brackets around text for \cite:
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing
{\begin{list}{}{%
\setlength{\itemindent}{0pt}
\setlength{\leftmargin}{0pt}
\setlength{\parsep}{0pt}
% turn on hanging indent if param 1 is 1
\ifodd #1
\setlength{\leftmargin}{\cslhangindent}
\setlength{\itemindent}{-1\cslhangindent}
\fi
% set entry spacing
\setlength{\itemsep}{#2\baselineskip}}}
{\end{list}}
\usepackage{calc}
\newcommand{\CSLBlock}[1]{\hfill\break\parbox[t]{\linewidth}{\strut\ignorespaces#1\strut}}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
\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
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\usepackage{bookmark}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same} % disable monospaced font for URLs
\hypersetup{
pdftitle={R for Psychology},
colorlinks=true,
linkcolor={blue},
filecolor={Maroon},
citecolor={Blue},
urlcolor={Blue},
pdfcreator={LaTeX via pandoc}}
\title{R for Psychology}
\author{}
\date{2024-03-14}
\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!}
\includegraphics[width=2.78125in,height=\textheight]{images/cover.png}
This book is written in mind for someone working in Psychology and is
venturing into R with little to no experience. Research data in
Psychology is unique in that it is collected in formats that are
human-readable but not exactly R-readable. Data wrangling conventions
will often vary depending on research question and therefore by what
type data is collected. In ``R for Psychology'', we have compiled a
series of real-world examples from different sub-disciplines and will
walk through the process of wrangling, summary, analyses and
visualisations
This book is licensed under a
\href{https://creativecommons.org/licenses/by-nc/4.0/}{Creative Commons
Attribution-NonCommercial 4.0 International License}.
\subsection*{How to contribute}\label{how-to-contribute}
\addcontentsline{toc}{subsection}{How to contribute}
This book is built using \href{https://quarto.org/docs/}{Quarto} and
hosted via GitHub Pages. It is a living resource, we welcome any
contributions from the Psychology community that would improve the
quality of this book. There are many ways to contribute:
\begin{itemize}
\tightlist
\item
Fixing grammar and typos
\item
Clarification or expanding on existing content
\item
Contribute real-world data in worked examples
\item
Authoring an entire chapter
\end{itemize}
Take a look at our \href{contributing.qmd}{Contributing Guide} to see
how you can help!
\subsection*{Acknowledgements}\label{acknowledgements}
\addcontentsline{toc}{subsection}{Acknowledgements}
This book was created on the unceded territory of the Bedegal people who
are the Traditional custodians of the lands where the Kensington campus
is located.
The first version of this book was funded by the
\href{https://research.unsw.edu.au/research-infrastructure}{UNSW
Research Infrastructure Scheme}.
We would like to thank the following people who have contributed to the
book:
\href{https://github.com/daxkellie}{@daxkellie}
\bookmarksetup{startatroot}
\chapter{Introduction}\label{introduction}
\section{What you will learn}\label{what-you-will-learn}
How to:
\begin{itemize}
\tightlist
\item
\textbf{Read} your hard-earned data into R
\item
\textbf{Wrangle and clean} the data in a R-friendly format
\item
\textbf{Produce} summary statistics
\item
\textbf{Analyse} your data
\item
\textbf{Visualise} your findings
\end{itemize}
Pre-analysis stages:
\begin{itemize}
\tightlist
\item
\end{itemize}
Analysis stages:
\section{How book is organised}\label{how-book-is-organised}
This book is organised by data type (e.g.~\href{survey.qmd}{Survey
data}, Questionnaire data).
Each chapter will walk through the process will work with real-world
Psychology data and will walk you through reading in data, to cleaning
and eventually analysis and visualising the results.
\subsection{Conventions}\label{conventions}
We will refer to packages as \texttt{\{dplyr\}} and functions as
\texttt{mean()}. Variables and objects (such as file names or data
objects) as \texttt{age} and \texttt{mtcars}. Where it would aid
understanding, we will sometimes refer to functions within a particular
packages as \texttt{dplyr::mutute()}
\section{Prerequisites}\label{prerequisites}
Content drawn from existing resources such as
https://r4ds.hadley.nz/intro\#prerequisites
\subsection{R}\label{r}
Download Point to intro to R content (RUWithme, Environmental Computing,
Software Carpentry)
\subsection{RStudio}\label{rstudio}
RStudio projects Point to resource about Rproj (SWC)
Running R code https://r4ds.hadley.nz/intro\#running-r-code
\subsection{Version control with git}\label{version-control-with-git}
\subsubsection{What is git?}\label{what-is-git}
\subsubsection{Why do I need git?}\label{why-do-i-need-git}
\subsection{R packages}\label{r-packages}
Every code section will always begin with calls to R packages. There
will be code that is commented out (have \texttt{\#} preceding the code)
for you to install these if you don't have them on your computer
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# install.packages(dplyr)}
\FunctionTok{library}\NormalTok{(dplyr)}
\end{Highlighting}
\end{Shaded}
There are few R packages that will be on heavy rotation when it comes to
working with Psychology data. \#\#\#\# \{tidyverse\}
\href{https://www.tidyverse.org/}{\{tidyverse\}} is a collection of R
packages that is essential to a data scientist's toolkit. By installing
\texttt{\{tidyverse\}} you are actually installing 8 other packages. The
ones we will most often use include:
\begin{itemize}
\tightlist
\item
\href{https://dplyr.tidyverse.org/}{\{dplyr\}}
\item
\href{https://ggplot2.tidyverse.org/}{\{ggplot2\}}
\item
\href{https://tidyr.tidyverse.org/}{\{tidyr\}}
\end{itemize}
The handy thing is, when you load the \texttt{\{tidyverse\}} library
into R, it will load the core suite of packages for you instead of you
loading each of them independently! Efficiency!! :rocket:
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(tidyverse)}
\end{Highlighting}
\end{Shaded}
Other packages that will be helpful for your R workflows include:
\begin{itemize}
\tightlist
\item
\href{https://here.r-lib.org/}{\{here\}}\\
\item
\href{http://sfirke.github.io/janitor/}{\{janitor\}}
\end{itemize}
At the end of each chapter, we will also include our call to
\texttt{sessionInfo()} so you can see what version of packages we are
using.
\section{Virtual environments}\label{virtual-environments}
Speaking on what package versions as we write this book, we understand
the R package space is constantly changing. This means sometimes code
will break due to package updates and this is all part of the process!
To combat this problem, we've enlisted
\href{https://rstudio.github.io/renv/articles/renv.html\#getting-started}{\texttt{renv}}
to create a reproducible environment for building this book.
\subsection{Download our virtual
environment}\label{download-our-virtual-environment}
The virtual environment used to build this book is stored in a
\texttt{lockfile}. You can find this file in the
\href{https://github.com/unsw-edu-au/r4psych}{GitHub repository} where
the source code of this book lives.
The lockfile is named \texttt{renv.lock}. You can download this file
directly but clicking on the file name and clicking on the ``Download
raw file'' button.
\includegraphics{images/renv.png}
Alternatively, you can
\href{https://happygitwithr.com/clone.html?q=clone\#clone}{clone} our
repository into your computer. Learn more about cloning repositorsies
and other GitHub workflows in \href{https://happygitwithr.com/}{Happy
Git} by Jenny Bryan.
Once you have this file downloaded, move it in a relevant
\href{}{project directory} and then we can let \texttt{\{renv\}} work
its magic.
\subsection{Install}\label{install}
First things first, lets install \texttt{renv} if we don't have it
already.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{install.packages}\NormalTok{(}\StringTok{"renv"}\NormalTok{)}
\FunctionTok{library}\NormalTok{(renv)}
\end{Highlighting}
\end{Shaded}
\subsection{Recreate virtual
enviroment}\label{recreate-virtual-enviroment}
Now let's tell \texttt{renv} where our downloaded \texttt{renv.lock}
file is. Specific the path to the file in the function
\texttt{restore()} and you are good to go!
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{restore}\NormalTok{(}\AttributeTok{lockfile =} \StringTok{"path\_to\_renv.lock"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\bookmarksetup{startatroot}
\chapter{Organise}\label{organise}
There are lots of reasons to use R and R Studio for your data analysis,
but reproducibility ranks high on the list. By writing code that reads,
wrangles, cleans, visualises, and analyses your data, you are
documenting the process that your data has been through from its raw
state through to its analysed state. Reproducibility is all about
someone else (or you in the future) being able to take your data and the
code you have written and use it to produce exactly the same analysis
values that you report in your paper or thesis.
With this goal in mind, there are a few best practices we recommend, to
ensure that your code is usable in the future, irrespective of what
machine it is being run on and by whom.
\section{R projects}\label{r-projects}
R really cares about where things live on your computer, even if you
don't. Humans have gotten out of the habit of thinking very hard about
where they put things on their machine; the search capabilities on the
modern computer are quite good and you can generally find files quite
easily by searching for them.
When you are coding in R, however, you need to explicitly tell R where
to find things. You can make this process much easier for yourself by
always working within an RStudio Project.
When you work within an RStudio project, you can reference everything in
relation to the top level of that project folder. It doesn't matter
where that project folder lives on your machine (i.e.~Desktop,
Documents, OneDrive, Dropbox) the code you write is always relative to
your project folder. This means that you can share that whole project
folder with someone else (your collaborators or supervisor), and your
code won't break.
\begin{quote}
When you start a new analysis project, create a New RStudio Project via
the File tab.
\end{quote}
\begin{figure}[H]
{\centering \includegraphics{images/project.png}
}
\caption{Working in an RStudio Project called my\_new\_analysis}
\end{figure}%
Always open RStudio by double clicking on the .RProj file in the folder
on your machine. There is an icon in the top right corner of RStudio
that shows you which project you are working in and makes it easy to
switch between projects.
\section{where is here?}\label{where-is-here}
Once you have set up a project to contain your analysis, you can avoid
further file path drama by using the \texttt{here()} package. This
package makes it super easy to refer to file paths and ensures that your
code will work, even if someone else is trying to run it on a different
computer.
Once you have installed the \texttt{here()} package, use it to tell R
where you find your data like this. This code chunk loads there
\texttt{here()} and \texttt{readr()} packages and then tells R to read
in a csv file, located in the ``data'' folder, and the file is called
file.csv
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mydata }\OtherTok{\textless{}{-}} \FunctionTok{read\_csv}\NormalTok{(}\FunctionTok{here}\NormalTok{(}\StringTok{"data"}\NormalTok{, }\StringTok{"file.csv"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
By referring to the location of your data using the \texttt{here()}
package, here is no need worry about working directories, and you can be
sure that your code will work on any machine.
\begin{quote}
To read more about why projects and the \texttt{here()} package are
useful, check out
\href{https://www.tidyverse.org/blog/2017/12/workflow-vs-script/}{this
blog by Jenny Bryan}
\end{quote}
\section{folder structure}\label{folder-structure}
Once you have your project set up, you might like to think about
imposing some structure on it. It is mostly personal preference, but
many analysis projects include the following folders.
\begin{itemize}
\tightlist
\item
data
\begin{itemize}
\tightlist
\item
raw-data
\item
clean-data
\end{itemize}
\item
output
\begin{itemize}
\tightlist
\item
figures
\item
tables
\end{itemize}
\item
manuscript
\end{itemize}
You always want to keep your raw data untouched and separate from any
data that you write back to your machine after your data cleaning
process, so a raw-data subfolder can be useful.
In addition, you might want to organise your figures and tables into an
output folder and put any writing that you are doing in the manuscripts
folder.
\begin{quote}
If you want to write a manuscript (or maybe your whole thesis!) using
RMarkdown, check out the \texttt{papaja} (Preparing APA Journal
Articles) package
\end{quote}
Usually the scripts (or R Markdown) documents that you write your code
in, live in the top level of your project file. In RStudio, your project
structure might look something like this.
\begin{figure}[H]
{\centering \includegraphics{images/structure.png}
}
\caption{A project structure template}
\end{figure}%
\section{naming things}\label{naming-things}
When naming things in your analysis folder, it is a good idea to think
about your future self. In all likelihood, when you come back to this
analysis in a few months time, you will have no recollection how it
actually worked, but you can leave yourself some breadcrumbs by being a
bit intentional about naming things.
Your file names should be meaningful and orderly. The name of each file
should tell the new user (or future you) what is in the file and where
it goes in the process.
\begin{itemize}
\tightlist
\item
cleaning-functions.R
\item
1\_wrangle.Rmd
\item
2\_visualise.Rmd
\item
3\_analyse.Rmd
\end{itemize}
In this project, I can tell by glancing at the file names that I have a
script (.R) that contains functions and three R Markdown files that
contain each stage of the analysis.
Sticking with lower case is a good idea; avoid special characters and
use - or \_ to fill any gaps.
Find more useful naming tips in the
\href{https://style.tidyverse.org/files.html}{Tidyverse Style guide}.
\section{documenting things}\label{documenting-things}
\subsection{README}\label{readme}
In addition to the breadcrumbs that our file names leave, it is also a
good idea to leave explicit notes to your future self (or someone else)
in a README.md file. This is a simple text file that contains
instructions for how the user should engage with your project.
Create a new Markdown file and save it as README.md
Use it to leave yourself instructions that look a bit like this.
\begin{figure}[H]
{\centering \includegraphics{images/readme.png}
}
\caption{An example README file}
\end{figure}%
\subsection{R Markdown}\label{r-markdown}
In addition to leaving your future self explicit notes about how to
engage with the project generally in a README document, it is also best
practice to document your code in a way that makes it really clear what
the code is doing and why. For this reason, we recommend using R
Markdown documents (rather than R scripts) for your analysis.
R Markdown is a handy file format that allows you to intersperse chunks
of code with notes. This kind of document makes it easy to write
explanations, interpretations, and thoughts for your future self as you
code. This kind of documentation makes it much more likely to be able to
make sense of what you have done and why, when you come back to your
analysis in a few months time.
R Markdown documents can also be ``knitted'' into html, pdf, or word
documents, allowing you to share your analysis (and associated thoughts)
with collaborators, even if they don't know R.
To get up to speed on R Markdown and how to use it, check out
RLadiesSydney
\href{https://rladiessydney.org/courses/ryouwithme/04-markymark-1/}{\#RYouWithMe
MarkyMark module}
\bookmarksetup{startatroot}
\chapter{Import}\label{import}
\section{Reading in Excel
spreadsheets}\label{reading-in-excel-spreadsheets}
This is
\subsection{Reading in .csv}\label{reading-in-.csv}
\section{Reading in SPSS}\label{reading-in-spss}
\section{Reading in Qualtrics data}\label{reading-in-qualtrics-data}
\subsection{Reading in .sav}\label{reading-in-.sav}
\bookmarksetup{startatroot}
\chapter{Read in the data}\label{read-in-the-data}
Remember the file setup described above? This is where that starts to be
important. Remember, our working directory (i.e., where R thinks
``here'' is) was set via the Rproj file -- so it is the ``Williams Lab
Core R'' folder. You can check this by typing \texttt{getwd()} or
\texttt{here()} in the console.
For most of this core script, we'll be using data from a file called
sampledata.sav, which should be in the data subfolder from the zipped
file. If not, sort that out now!
A .sav file is in SPSS format. When you export from Qualtrics into
.sav/SPSS format, it retains helpful information like question wording
and response labels. If you export straight to .csv, you lose that info
and will find yourself cross-checking back to Qualtrics. So, strong word
of advice to always export to .sav.
The code below uses the \texttt{here} command to direct R to the data
folder \emph{from the working directory}, and then the .sav file within
it.
The \texttt{glimpse} command gives a nice overview of the variables,
their type, and a preview of the data.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{data }\OtherTok{\textless{}{-}} \FunctionTok{read\_sav}\NormalTok{(}\FunctionTok{here}\NormalTok{(}\StringTok{"data"}\NormalTok{, }\StringTok{"sampledata.sav"}\NormalTok{)) }
\FunctionTok{glimpse}\NormalTok{(data)}
\end{Highlighting}
\end{Shaded}
These variable names won't be very nice to work with with awkward and
inconsistent capitalisation. Actual Qualtrics exports are even messier!
The \texttt{clean\_names} function from \texttt{janitor} helps clean
them up!
\texttt{data\_cleanednames\ \textless{}-} at the start of the line saves
the change to a new dataframe. Alternately, you could write it back to
the same dataframe (e.g., \texttt{data\ \textless{}-} ), but this should
be done very intentionally as it makes it harder to backtrack to the
source of an error. The general rule is to create a new dataframe each
time you implement a big change on the data.
The \texttt{glimpse} command here shows you that you effectively cleaned
the variable names!
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{data\_cleanednames }\OtherTok{\textless{}{-}} \FunctionTok{clean\_names}\NormalTok{(data)}
\FunctionTok{glimpse}\NormalTok{(data\_cleanednames)}
\end{Highlighting}
\end{Shaded}
A few things about working with files in SPSS format (.sav) before we
continue. The reason why we bother with this is that the SPSS format
maximises the information in the file. Unlike exporting to .csv or
another spreadsheet format, .sav retains information about question
wording (saved as a variable label) and response labelling (saved as a
value label).
If you look at the variable types at the right of the \texttt{glimpse}
output, you'll see the some of the variables are dbl (numeric) while
some are dbl+lbl (numeric with labelled values). If you view the
\texttt{data} object (by clicking on it in the Environment or using
\texttt{view(data)}) you will see that some of the variables have the
question wording below the variable name.
Having this information on hand is really helpful when working with your
data!
The \texttt{view\_df} function from the \texttt{sjPlot} package creates
a really nicely formatted html file that includes variable names,
question wording, response options, and response labelling. This code
saves the html file to the \texttt{output\_files} folder using the
\texttt{here} package (which starts where your Rproj file is). This html
file is nice as a reference for your own use or to share with a
supervisor or collaborator!
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{view\_df}\NormalTok{(data\_cleanednames)}
\FunctionTok{view\_df}\NormalTok{(data\_cleanednames, }\AttributeTok{file=}\FunctionTok{here}\NormalTok{(}\StringTok{"output\_files"}\NormalTok{,}\StringTok{"spsstest\_codebook.html"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
The \texttt{data\_dict} function from \texttt{surveytoolbox} makes a
dataframe with all the variable and response labels - similar to the
html created above, but this can be called upon later in R as it's now
part of the environment.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{datadictionary }\OtherTok{\textless{}{-}}\NormalTok{ data\_cleanednames }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{data\_dict}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
Let's say you just want to know the question wording or response labels
for a particular variable, you can do this via code rather than checking
the whole dataset. The \texttt{extract\_vallab} command from
\texttt{surveytoolbox} returns the value labels for a given variable.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{data\_cleanednames }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{extract\_vallab}\NormalTok{(}\StringTok{"demographicscateg"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
There are (evidently) times when packages \emph{do not like} labelled
data. So, here are a few tools for removing labels from the
\texttt{haven} package. Keep these up your sleeve for problem solving
later! \texttt{zap\_labels} and \texttt{zap\_label} not surprisingly
removes the labels - the first removes the value labels and the second
removes the variable labels! The code below makes a new data dictionary
of the zapped dataframe and glimpses the new dataframe to confirm the
labels are gone.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{data\_zapped }\OtherTok{\textless{}{-}}\NormalTok{ data\_cleanednames }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{zap\_labels}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{zap\_label}\NormalTok{()}
\NormalTok{datadictionary\_zapped }\OtherTok{\textless{}{-}}\NormalTok{ data\_zapped }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{data\_dict}\NormalTok{()}
\FunctionTok{glimpse}\NormalTok{(data\_zapped)}
\end{Highlighting}
\end{Shaded}
For the rest of this script, we will work with the \emph{zapped}
dataframe. This is the recommended approach to save headaches with
errors down the line.
\bookmarksetup{startatroot}
\chapter{Wrangle}\label{wrangle}
\section{Clean names}\label{clean-names}
\section{Dealing with labels}\label{dealing-with-labels}
\section{Exclusions}\label{exclusions}
\texttt{mutate} \texttt{case\_when}
\texttt{filter}
\section{Creating scales and indexes}\label{creating-scales-and-indexes}
\texttt{group\_by} \texttt{summarise}
\texttt{row\_wise} \texttt{mutate}
\subsection{Checking reliabilty}\label{checking-reliabilty}
\bookmarksetup{startatroot}
\chapter{Describe}\label{describe}
\section{getting a feel for your
data}\label{getting-a-feel-for-your-data}
\texttt{str}
\texttt{glimpse}
\texttt{skimr}
\section{counting things}\label{counting-things}
\texttt{group\_by} + \texttt{summarise} + \texttt{count}
\texttt{n}
\texttt{tabyl}
\section{getting descriptives}\label{getting-descriptives}
\texttt{group\_by} + \texttt{summarise} + \texttt{mean} \& \texttt{sd}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{scale1\_by\_condition12 }\OtherTok{\textless{}{-}}\NormalTok{ data\_scalescomputed }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{group\_by}\NormalTok{(condition12) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{summarise}\NormalTok{(}\AttributeTok{mean\_scale1 =} \FunctionTok{mean}\NormalTok{(scale1\_index, }\AttributeTok{na.rm =} \ConstantTok{TRUE}\NormalTok{),}
\AttributeTok{sd\_scale1 =} \FunctionTok{sd}\NormalTok{(scale1\_index, }\AttributeTok{na.rm =} \ConstantTok{TRUE}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\subsection{Three things to remember}\label{three-things-to-remember}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
When we compute means, we need to set the decimals via
\texttt{round()}.
\item
We also need to tell R to calculate the mean, even if some of the
contributing data points are missing. This is what
\texttt{na.rm\ =\ TRUE} does.
\item
As noted above, \texttt{rowwise} asks R to do something for each row
(which is what we want here -- to compute the mean of the contributing
items for each participant). Whenever we use \texttt{rowwise} (or
\texttt{group\_by}), we need to \texttt{ungroup()} at the end to avoid
issues down the line.
\end{enumerate}
\section{tables??}\label{tables}
\texttt{gt}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{gt}\NormalTok{(scale1\_by\_condition12)}
\end{Highlighting}
\end{Shaded}
\texttt{apaTable}
\bookmarksetup{startatroot}
\chapter{Plot}\label{plot}
\section{Plotting raw data}\label{plotting-raw-data}
\texttt{geom\_point}
\texttt{geom\_histogram}
\texttt{geom\_boxplot}
\texttt{geom\_violin}
\section{Plotting data summaries}\label{plotting-data-summaries}
\texttt{geom\_col}
\bookmarksetup{startatroot}
\chapter{Infer}\label{infer}
\bookmarksetup{startatroot}
\chapter{Moving into analyses}\label{moving-into-analyses}
\begin{quote}
thoughts from Jenny : I wonder whether it would be useful to structure
this in terms of lets combine skills from previous chapters to..
\end{quote}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
ask a question,
\item
get plots/descriptives
\item
then answer it with inferentials?
\item
And repeat that process for t-test, linear regression and anova?
\end{enumerate}
\section{Plots and descriptives}\label{plots-and-descriptives}
Now, let's test whether this scale varies by condition (condition 1
vs.~condition 2 in condition12).
Before jumping into t-tests, let's first visualise the data