-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex 3.tex
6427 lines (5188 loc) · 260 KB
/
index 3.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}
\ifLuaTeX
\usepackage{luacolor}
\usepackage[soul]{lua-ul}
\else
\usepackage{soul}
\fi
\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{tcolorbox}{}{\usepackage[skins,breakable]{tcolorbox}}
\@ifpackageloaded{fontawesome5}{}{\usepackage{fontawesome5}}
\definecolor{quarto-callout-color}{HTML}{909090}
\definecolor{quarto-callout-note-color}{HTML}{0758E5}
\definecolor{quarto-callout-important-color}{HTML}{CC1914}
\definecolor{quarto-callout-warning-color}{HTML}{EB9113}
\definecolor{quarto-callout-tip-color}{HTML}{00A047}
\definecolor{quarto-callout-caution-color}{HTML}{FC5300}
\definecolor{quarto-callout-color-frame}{HTML}{acacac}
\definecolor{quarto-callout-note-color-frame}{HTML}{4582ec}
\definecolor{quarto-callout-important-color-frame}{HTML}{d9534f}
\definecolor{quarto-callout-warning-color-frame}{HTML}{f0ad4e}
\definecolor{quarto-callout-tip-color-frame}{HTML}{02b875}
\definecolor{quarto-callout-caution-color-frame}{HTML}{fd7e14}
\makeatother
\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 4 Epidemiology},
colorlinks=true,
linkcolor={blue},
filecolor={Maroon},
citecolor={Blue},
urlcolor={Blue},
pdfcreator={LaTeX via pandoc}}
\title{R 4 Epidemiology}
\author{}
\date{2024-07-23}
\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}
Welcome to R for Epidemiology!
This electronic textbook was originally created to accompany the
Introduction to R Programming for Epidemiologic Research course at the
\href{https://sph.uth.edu/}{University of Texas Health Science Center
School of Public Health}. However, we hope it will be useful to anyone
who is interested in R, epidemiology, or human health and well-being.
\section*{Acknowledgements}\label{acknowledgements}
\addcontentsline{toc}{section}{Acknowledgements}
\markright{Acknowledgements}
This book is currently a work in progress (and probably always will be);
however, there are already many people who have played an important role
(some unknowingly) in helping develop it thus far. First, we'd like to
offer our gratitude to all past, current, and future members of the R
Core Team for maintaining this \emph{amazing}, \emph{free} software.
We'd also like to express our gratitude to everyone at
\href{https://posit.co/}{Posit}. You are also developing and
\emph{giving away} some amazing software. In particular, we'd like to
acknowledge
\href{https://www.linkedin.com/in/garrett-grolemund-49328411/}{Garrett
Grolemund} and \href{https://hadley.nz/}{Hadley Wickham}. Both have had
a huge impact on how we use and teach R. We'd also like to thank our
students for all the feedback they've given us while taking our courses.
In particular, we want to thank
\href{https://www.linkedin.com/in/jared-wiegand/}{Jared Wiegand} and
Yiqun Wang for their many edits and suggestions.
This electronic textbook was created and published using
\href{https://cran.r-project.org/}{R},
\href{https://www.rstudio.com/}{RStudio}, the
\href{https://quarto.org/}{Quarto}, and
\href{https://github.com/}{GitHub}.
\bookmarksetup{startatroot}
\chapter*{Introduction}\label{introduction}
\addcontentsline{toc}{chapter}{Introduction}
\markboth{Introduction}{Introduction}
\section*{Goals}\label{goals}
\addcontentsline{toc}{section}{Goals}
\markright{Goals}
We're going to start the introduction by writing down some basic goals
that underlie the construction and content of this book. We're writing
this for you, the reader, but also to hold ourselves accountable as we
write. So, feel free to read if you are interested or skip ahead if you
aren't.
The goals of this book are:
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
\textbf{To teach you how to use R and RStudio as tools for applied
epidemiology}.\footnote{In this case, ``tools for applied
epidemiology'' means (1) understanding epidemiologic concepts; and
(2) completing and interpreting epidemiologic analyses.} Our goal is
not to teach you to be a computer scientist or an advanced R
programmer. Therefore, some readers who are experienced programmers
may catch some technical inaccuracies regarding what we consider to be
the fine points of what R is doing ``under the hood.''
\item
\textbf{To make this writing as accessible and practically useful as
possible without stripping out all of the complexity that makes doing
epidemiology in real life a challenge}. In other words, We're going to
try to give you all the tools you need to \emph{do} epidemiology in
``real world'' conditions (as opposed to ideal conditions) without
providing a whole bunch of extraneous (often theoretical) stuff that
detracts from \emph{doing}. Having said that, we will strive to add
links to the other (often theoretical) stuff for readers who are
interested.
\item
\textbf{To teach you to accomplish common \emph{tasks}}, rather than
teach you to use functions or families of functions. In many R courses
and texts, there is a focus on learning all the things a function, or
set of related functions, can do. It's then up to you, the reader, to
sift through all of these capabilities and decided which, if any, of
the things that \emph{can} be done will accomplish the tasks that you
are \emph{actually trying} to accomplish. Instead, we will strive to
start with the end in mind. What is the task we are actually trying to
accomplish? What are some functions/methods we could use to accomplish
that task? What are the strengths and limitations of each?
\item
\textbf{To start each concept by showing you the end result} and then
deconstruct how we arrived at that result, where possible. We find
that it is easier for many people to understand new concepts when
learning them as a component of a final product.
\item
\textbf{To learn concepts with data} instead of (or alongside)
mathematical formulas and text descriptions, where possible. We find
that it is easier for many people to understand new concepts by seeing
them in action.
\end{enumerate}
\section*{Text conventions used in this
book}\label{text-conventions-used-in-this-book}
\addcontentsline{toc}{section}{Text conventions used in this book}
\markright{Text conventions used in this book}
\begin{itemize}
\tightlist
\item
We will hyperlink many keywords or phrases to their
\href{appendices/glossary.qmd}{glossary} entry.
\item
Additionally, we may use \textbf{bold} face for a word or phrase that
we want to call attention to, but it is not necessarily a keyword or
phrase that we want to define in the glossary.
\item
\texttt{Highlighted\ inline\ code} is used to emphasize small sections
of R code and program elements such as variable or function names.
\end{itemize}
\section*{Other reading}\label{other-reading}
\addcontentsline{toc}{section}{Other reading}
\markright{Other reading}
If you are interested in R4Epi, you may also be interested in:
\begin{itemize}
\item
\href{https://rstudio-education.github.io/hopr/}{Hands-on Programming
with R} by Garrett Grolemund. This book is designed to provide a
friendly introduction to the R language.
\item
\href{https://r4ds.hadley.nz/}{R for Data Science} by Hadley Wickham,
Mine Çetinkaya-Rundel, and Garrett Grolemund. This book is designed to
teach readers how to do data science with R.
\item
\href{https://moderndive.com/}{Statistical Inference via Data Science:
A ModernDive into R and the Tidyverse}. This book is designed to be a
gentle introduction to the practice of analyzing data and answering
questions using data the way data scientists, statisticians, data
journalists, and other researchers would.
\item
\href{http://christophergandrud.github.io/RepResR-RStudio/}{Reproducable
Research with R and RStudio} by Christopher Gandrud. This book gives
you tools for data gathering, analysis, and presentation of results so
that you can create dynamic and highly reproducible research.
\item
\href{https://adv-r.hadley.nz/}{Advanced R} by Hadley Wickham. This
book is designed primarily for R users who want to improve their
programming skills and understanding of the language.
\end{itemize}
\bookmarksetup{startatroot}
\chapter*{Contributing}\label{contributing}
\addcontentsline{toc}{chapter}{Contributing}
\markboth{Contributing}{Contributing}
Over the years, we have learned so much from our students and
colleagues, and we anticipate that there is much more we can learn from
you -- our readers. Therefore, we welcome and appreciate all
constructive contributions to R4Epi!
\section*{Typos}\label{typos}
\addcontentsline{toc}{section}{Typos}
\markright{Typos}
The easiest way for you to contribute is to help us clean up the little
typos and grammatical errors that inevitably sneak into the text.
If you spot a typo, you can offer a correction directly in GitHub. You
will first need to create a free GitHub account:
\href{https://github.com/join}{sign-up at github.com}. Later in the
book, we will cover using GitHub in greater depth (See
Chapter~\ref{sec-using-git-and-github}). Here, we're just going to walk
you through how to fix a typo without much explanation of how GitHub
works.
Let's say you spot a typo while reading along.
\includegraphics{chapters/contributing/typo_on_screen.png}
Next, click the edit button in the toolbar as shown in the screenshot
below.
\includegraphics{chapters/contributing/edit_button.png}
The first time you click the icon, you will be taken to the R4Epi
repository on GitHub and asked to fork it. For our purposes, you can
think of a GitHub repository as being similar to a shared folder on
Dropbox or Google Drive.
\includegraphics{chapters/contributing/fork_button.png}
``Forking the repository'' basically just means ``make a copy of the
repository'' on your GitHub account. In other words, copy all of the
files that make up the R4Epi textbook to your GitHub account. Then, you
can fix the typos you found in your \emph{copy} of the files that make
up the book instead of directly editing the \emph{actual} files that
make up the book. This is a safeguard to prevent people from
accidentally making changes that shouldn't be made.
\begin{tcolorbox}[enhanced jigsaw, colframe=quarto-callout-note-color-frame, titlerule=0mm, leftrule=.75mm, bottomrule=.15mm, opacityback=0, opacitybacktitle=0.6, arc=.35mm, rightrule=.15mm, colback=white, breakable, bottomtitle=1mm, left=2mm, coltitle=black, toptitle=1mm, title=\textcolor{quarto-callout-note-color}{\faInfo}\hspace{0.5em}{Note}, toprule=.15mm, colbacktitle=quarto-callout-note-color!10!white]
Forking the R4Epi repository does not cost any money or add any files to
your computer.
\end{tcolorbox}
After you fork the repository, you will see a text editor on your
screen.
\includegraphics{chapters/contributing/text_editor.png}
The text editor will display the contents of the file used to make the
chapter you were looking at when you clicked the \texttt{edit} button.
In this example, it was a file named \texttt{contributing.qmd}. The
\texttt{.qmd} file extension means that the file is a Quarto file. We
will learn more about Quarto files in \textbf{?@sec-quarto-files}, but
for now just know that Quarto files can be used to create web pages and
other documents that contain a mix of R code, text, and images.
Next, scroll down through the text until you find the typo and fix it.
In this case, line 11 contains the word ``typoo''. To fix it, you just
need to click in the editor window and begin typing. In this case, you
would click next to the word ``typoo'' and delete the second ``o''.
\includegraphics{chapters/contributing/fix_typo.png}
Now, the only thing left to do is propose your typo fix to the authors.
To do so, click the green \texttt{Commit\ changes...} button on the
right side of the screen above the text editor (surrounded with a red
box in the screenshot above). When you click it, a new
\texttt{Propose\ changes} box will appear on your screen. Type a brief
(i.e., 72 characters or less) summary of the change you made in the
\texttt{Commit\ message} box. There is also an
\texttt{Extended\ description} box where you can add a more detailed
description of what you did. In the screenshot below, shows an example
commit message and extended description that will make it easy for the
author to quickly figure out exactly what changes are being proposed.
\includegraphics{chapters/contributing/propose_changes.png}
Next, click the \texttt{Propose\ changes} button. That will take you to
another screen where you will be able to create a pull request. This
screen is kind of busy, but try not to let it overwhelm you.
\includegraphics{chapters/contributing/create_pull_request_1.png}
For now, we will focus on the three different sections of the screen
that are highlighted with a red outline. We will start at the bottom and
work our way up. The red box that is closest to the bottom of the
screenshot shows us that the change that made was on line 11. The word
``typoo'' (highlighted in red) was replaced with the word ``typo''
(highlighted in green). The red box in the middle of the screenshot
shows us the brief description that was written for our proposed change
-- ``Fix a typo in contributing.qmd''. Finally, the red box closest to
the top of the screenshot is surrounding the
\texttt{Create\ pull\ request} button. You will click it to move on with
your pull request.
\includegraphics{chapters/contributing/create_pull_request_2.png}
After doing so, you will get one final chance to amend the description
of your proposed changes. If you are happy with the commit message and
description, then click the \texttt{Create\ pull\ request} button one
more time. At this point, your job is done! It is now up to the authors
to review the changes you've proposed and ``pull'' them into the file in
their repository.
In case you are curious, here is what the process looks like on the
authors' end. First, when we open the R4Epi repository page on GitHub,
we will see that there is a new pull request.
\includegraphics{chapters/contributing/create_pull_request_3.png}
When we open the pull request, we can see the proposed changes to the
file.
\includegraphics{chapters/contributing/create_pull_request_4.png}
Then, all we have to do is click the
\texttt{Merge\ pull\ request\ button} and the fixed file is ``pulled
in'' to replace the file with the typo.
\includegraphics{chapters/contributing/create_pull_request_5.png}
\section*{Issues}\label{issues}
\addcontentsline{toc}{section}{Issues}
\markright{Issues}
There may be times when you see a problem that you don't know how to
fix, but you still want to make the authors aware of. In that case, you
can create an \hyperref[glossary-issue]{issue} in the R4Epi repository.
To do so, navigate to the issue tracker using this link:
https://github.com/brad-cannell/r4epi/issues.
\includegraphics{chapters/contributing/issue_tracker.png}
Once there, you can check to see if someone has already raised the issue
you are concerned about. If not, you can click the green ``New issue''
button to raise it yourself.
Please note that R4Epi uses a
\href{https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html}{Contributor
Code of Conduct}. By contributing to this book, you agree to abide by
its terms.
\section*{License Information}\label{license-information}
\addcontentsline{toc}{section}{License Information}
\markright{License Information}
This book was created by Brad Cannell and is licensed under a Creative
Commons Attribution-NonCommercial-NoDerivatives 4.0 International
License.
\bookmarksetup{startatroot}
\chapter*{About the Authors}\label{about-the-authors}
\addcontentsline{toc}{chapter}{About the Authors}
\markboth{About the Authors}{About the Authors}
\section*{Brad Cannell}\label{brad-cannell}
\addcontentsline{toc}{section}{Brad Cannell}
\markright{Brad Cannell}
\textbf{Michael (Brad) Cannell, PhD, MPH}\\
Associate Professor\\
Elder Mistreatment Lead, UTHealth Institute of Aging\\
Director, Research Informatics Core, Cizik Nursing Research Institute\\
UTHealth Houston\\
McGovern Medical School\\
Joan and Stanford Alexander Division of Geriatric \& Palliative
Medicine\\
\href{https://www.bradcannell.com}{www.bradcannell.com}
Dr.~Cannell received his PhD in Epidemiology, and Graduate Certificate
in Gerontology, in 2013 from the University of Florida. He received his
MPH with a concentration in Epidemiology from the University of
Louisville in 2009, and his BA in Political Science and Marketing from
the University of North Texas in 2005. During his doctoral studies, he
was a Graduate Research Assistant for the Florida Office on Disability
and Health, an affiliated scholar with the Claude D. Pepper Older
Americans Independence Center, and a student-inducted member of the
Delta Omega Honorary Society in Public Health. In 2016, Dr.~Cannell
received a Graduate Certificate in Predictive Analytics from the
University of Maryland University College, and a Certificate in Big Data
and Social Analytics from the Massachusetts Institute of Technology.
He previously held professional staff positions in the Louisville Metro
Health Department and the Northern Kentucky Independent District Health
Department. He spent three years as a project epidemiologist for the
Florida Office on Disability and Health at the University of Florida. He
also served as an Environmental Science Officer in the United States
Army Reserves from 2009 to 2013.
Dr.~Cannell's research is broadly focused on healthy aging and
health-related quality of life. Specifically, he has published research
focusing on preservation of physical and cognitive function, living and
aging with disability, and understanding and preventing elder
mistreatment. Additionally, he has a strong background and training in
epidemiologic methods and predictive analytics. He has been principal or
co-investigator on multiple trials and observational studies in
community and healthcare settings. He is currently the principal
investigator on multiple data-driven federally funded projects that
utilize technological solutions to public health issues in novel ways.
\textbf{Contact}\\
Connect with Dr.~Cannell and follow his work.\\
\includegraphics[width=2em,height=2em]{chapters/about_the_authors/about_the_authors_files/figure-pdf/fa-icon-a4a233699ebb8edeaca4e7ae0455099b.pdf}
\includegraphics[width=1.75em,height=2em]{chapters/about_the_authors/about_the_authors_files/figure-pdf/fa-icon-d19241927d9e636802f0f0caca52c0af.pdf}
\includegraphics[width=1.75em,height=2em]{chapters/about_the_authors/about_the_authors_files/figure-pdf/fa-icon-f2455331e499245af38ff1988a0023d4.pdf}
\includegraphics[width=1.75em,height=2em]{chapters/about_the_authors/about_the_authors_files/figure-pdf/fa-icon-1cbfc0b82b7e84c9b17b238bfd9a886e.pdf}
\section*{Melvin Livingston}\label{melvin-livingston}
\addcontentsline{toc}{section}{Melvin Livingston}
\markright{Melvin Livingston}
\textbf{Melvin (Doug) Livingston, PhD}\\
Research Associate Professor\\
Department of Behavioral, Social, and Health Education Sciences\\
Emory University Woodruff Health Sciences Center\\
Rollins School of Public Health\\
\href{https://sph.emory.edu/faculty/profile/index.php?FID=melvin-livingston-8970}{Dr.~Livingston's
Faculty Profile}
Dr.~Livingston is a methodologist with expertise in the the application
of quasi-experimental design principals to the evaluation for both
community interventions and state policies. He has particular expertise
in time series modeling, mixed effects modeling, econometric methods,
and power analysis. As part of his work involving community trials, he
has been the statistician on the long term follow-up study of a school
based cluster randomized trial in low-income communities with a focus on
explaining the etiology of risky alcohol, drug, and sexual behaviors.
Additionally, he was the statistician for a longitudinal study examining
the etiology of alcohol use among racially diverse and economically
disadvantaged urban youth, and co-investigator for a NIAAA- and
NIDA-funded trial to prevent alcohol use and alcohol-related problems
among youth living in high-risk, low-income communities within the
Cherokee Nation. Prevention work at the community level led him to an
interest in the impact of state and federal socioeconomic policies on
health outcomes. He is a Co-Investigator of a 50-state, 30-year study of
effects of state-level economic and education policies on a diverse set
of public health outcomes, explicitly examining differential effects
across disadvantaged subgroups of the population.
His current research interests center around the application of
quasi-experimental design and econometric methods to the evaluation of
the health effects of state and federal policy.
\textbf{Contact}\\
Connect with Dr.~Livingston and follow his work.\\
\includegraphics[width=2em,height=2em]{chapters/about_the_authors/about_the_authors_files/figure-pdf/fa-icon-a4a233699ebb8edeaca4e7ae0455099b.pdf}
\includegraphics[width=1.75em,height=2em]{chapters/about_the_authors/about_the_authors_files/figure-pdf/fa-icon-ab37b36564b32e84e458655a90d49135.pdf}
\part{Getting Started}
\chapter{Installing R and RStudio}\label{installing-r-and-rstudio}
Before we can do any programming with \hyperref[glossary-r]{R}, we first
have to download it to our computer. Fortunately, R is free, easy to
install, and runs on all major operating systems (i.e., Mac and
Windows). However, R is even easier to use as when we combine it with
another program called \hyperref[glossary-rstudio]{RStudio}.
Fortunately, RStudio is also free and will also run on all major
operating systems.
At this point, you may be wondering what R is, what RStudio is, and how
they are related. We will answer those questions in the near future.
However, in the interest of keeping things brief and simple, We're not
going to get into them right now. Instead, all you have to worry about
is getting the R programming language and the RStudio IDE (IDE is short
for integrated development environment) downloaded and installed on your
computer. The steps involved are slightly different depending on whether
you are using a Mac or a PC (i.e., Windows). Therefore, please feel free
to use the table of contents on the right-hand side of the screen to
navigate directly to the instructions that you need for your computer.
\begin{tcolorbox}[enhanced jigsaw, colframe=quarto-callout-note-color-frame, titlerule=0mm, leftrule=.75mm, bottomrule=.15mm, opacityback=0, opacitybacktitle=0.6, arc=.35mm, rightrule=.15mm, colback=white, breakable, bottomtitle=1mm, left=2mm, coltitle=black, toptitle=1mm, title=\textcolor{quarto-callout-note-color}{\faInfo}\hspace{0.5em}{Note}, toprule=.15mm, colbacktitle=quarto-callout-note-color!10!white]
In this chapter, we cover how to download and install R and RStudio on
both Mac and PC. However, the screenshots in all following chapters will
be from a Mac. The good news is that RStudio operates almost identically
on Mac and PC.
\end{tcolorbox}
\textbf{Step 1:} Regardless of which operating system you are using,
please make sure your computer is on, properly functioning, connected to
the internet, and has enough space on your hard drive to save R and
RStudio.
\section{Download and install on a
Mac}\label{download-and-install-on-a-mac}
\textbf{Step 2:} Navigate to the Comprehensive R Archive Network (CRAN),
which is located at https://cran.r-project.org/.
\includegraphics{chapters/installing_r_and_rstudio/mac_cran.png}
\textbf{Step 3:} Click on Download R for macOS.
\includegraphics{chapters/installing_r_and_rstudio/mac_download_r.png}
\textbf{Step 4:} Click on the link for the latest version of R. As you
are reading this, the newest version may be different than the version
you see in this picture, but the location of the newest version should
be roughly in the same place -- the middle of the screen under ``Latest
release:''. After clicking the link, R should start to download to your
computer automatically.
\includegraphics{chapters/installing_r_and_rstudio/mac_r_version.png}
\textbf{Step 5:} Locate the package file you just downloaded and double
click it. Unless you've changed your download settings, this file will
probably be in your ``downloads'' folder. That is the default location
for most web browsers. After you locate the file, just double click it.
\includegraphics{chapters/installing_r_and_rstudio/mac_install_r1.png}
\textbf{Step 6:} A dialogue box will open and ask you to make some
decisions about how and where you want to install R on your computer. We
typically just click ``continue'' at every step without changing any of
the default options.
\includegraphics{chapters/installing_r_and_rstudio/mac_install_r2.png}
If R installed properly, you should now see it in your applications
folder.
\includegraphics{chapters/installing_r_and_rstudio/mac_view_r.png}
\textbf{Step 7:} Now, we need to install the RStudio IDE. To do this,
navigate to the RStudio desktop download website, which is located at
https://posit.co/download/rstudio-desktop/. On that page, click the
button to download the latest version of RStudio for your computer. Note
that the website may look different that what you see in the screenshot
below because websites change over time.
\includegraphics{chapters/installing_r_and_rstudio/mac_download_rstudio1.png}
\textbf{Step 8:} Again, locate the DMG file you just downloaded and
double click it. Unless you've changed your download settings, this file
should be in the same location as the R package file you already
downloaded.
\includegraphics{chapters/installing_r_and_rstudio/mac_install_rstudio1.png}
\textbf{Step 9:} A new finder window should automatically pop up that
looks like the one you see below. Click on the RStudio icon and drag it
into the Applications folder.
\includegraphics{chapters/installing_r_and_rstudio/mac_install_rstudio2.png}
You should now see RStudio in your Applications folder. Double click the
icon to open RStudio.
\includegraphics{chapters/installing_r_and_rstudio/mac_open_rstudio.png}
If this warning pops up, just click Open.
\includegraphics{chapters/installing_r_and_rstudio/mac_open_warning.png}
The RStudio IDE should open and look something like the window you see
here. If so, you are good to go! 🎉
\includegraphics{chapters/installing_r_and_rstudio/mac_view_rstudio.png}
\section{Download and install on a
PC}\label{download-and-install-on-a-pc}
\textbf{Step 2:} Navigate to the Comprehensive R Archive Network (CRAN),
which is located at https://cran.r-project.org/.
\includegraphics{chapters/installing_r_and_rstudio/pc_cran.png}
\textbf{Step 3:} Click on Download R for Windows.
\includegraphics{chapters/installing_r_and_rstudio/pc_download_r1.png}
\textbf{Step 4:} Click on the base link.
\includegraphics{chapters/installing_r_and_rstudio/pc_download_r2.png}
\textbf{Step 5:} Click on the link for the latest version of R. As you
are reading this, the newest version may be different than the version
you see in this picture, but the location of the newest version should
be roughly the same. After clicking, R should start to download to your
computer.
\includegraphics{chapters/installing_r_and_rstudio/pc_download_r3.png}
\textbf{Step 6:} Locate the installation file you just downloaded and
double click it. Unless you've changed your download settings, this file
will probably be in your downloads folder. That is the default location
for most web browsers.
\includegraphics{chapters/installing_r_and_rstudio/pc_install_r1.png}
\textbf{Step 7:} A dialogue box will open that asks you to make some
decisions about how and where you want to install R on your computer. We
typically just click ``Next'' at every step without changing any of the
default options.
\includegraphics{chapters/installing_r_and_rstudio/pc_install_r2.png}
If R installed properly, you should now see it in the Windows start
menu.
\includegraphics{chapters/installing_r_and_rstudio/pc_view_r.png}
\textbf{Step 8:} Now, we need to install the RStudio IDE. To do this,
navigate to the RStudio desktop download website, which is located at
https://posit.co/download/rstudio-desktop/. On that page, click the
button to download the latest version of RStudio for your computer. Note
that the website may look different that what you see in the screenshot
below because websites change over time.
\includegraphics{chapters/installing_r_and_rstudio/pc_download_rstudio1.png}
\textbf{Step 9:} Again, locate the installation file you just downloaded
and double click it. Unless you've changed your download settings, this
file should be in the same location as the R installation file you
already downloaded.
\includegraphics{chapters/installing_r_and_rstudio/pc_install_rstudio1.png}
\textbf{Step 10:} Another dialogue box will open and ask you to make
some decisions about how and where you want to install RStudio on your
computer. We typically just click ``Next'' at every step without
changing any of the default options.
\includegraphics{chapters/installing_r_and_rstudio/pc_install_rstudio2.png}
When RStudio is finished installing, you should see RStudio in the
Windows start menu. Click the icon to open RStudio.
\includegraphics{chapters/installing_r_and_rstudio/pc_view_rstudio1.png}
The RStudio IDE should open and look something like the window you see
here. If so, you are good to go! 🎉
\includegraphics{chapters/installing_r_and_rstudio/pc_view_rstudio2.png}
\chapter{What is R?}\label{what-is-r}
At this point in the book, you should have installed R and RStudio on
your computer, but you may be thinking to yourself, ``I don't even know
what R is.'' Well, in this chapter you'll find out. We'll start with an
overview of the R language, and then briefly touch on its capabilities
and uses. You'll also see a complete R program and some complete
documents generated by R programs. In this book you'll learn how to
create similar programs and documents, and by the end of the book you'll
be able to write your own R programs and present your results in the
form of an issue brief written for general audiences who may or may not
have public health expertise. But, before we discuss R let's discuss
something even more basic -- data. Here's a question for you: What is
data?
\section{What is data?}\label{what-is-data}
Data is information about objects (e.g., people, places, schools) and
observable phenomenon (e.g., weather, temperatures, and disease
symptoms) that is recorded and stored somehow as a collection of
symbols, numbers, and letters. So, data is just information that has
been ``written'' down.
Here we have a table, which is a common way of organizing data. In R, we
will typically refer to these tables as \textbf{data frames}.
\includegraphics{chapters/what_is_r/table.png}
Each box in a data frame is called a \textbf{cell}.
\includegraphics{chapters/what_is_r/table_cells.png}
Moving from left to right across the data frame are \textbf{columns}.
Columns are also sometimes referred to as \textbf{variables}. In this
book, we will often use the terms columns and variables interchangeably.
Each column in a data frame has one, and only one, type. For now, know
that the type tells us what kind of data is contained in a column and
what we can \emph{do} with that data. You may have already noticed that
3 of the columns in the table we've been looking at contain numbers and
1 of the columns contains words. These columns will have different types
in R and we can do different things with them based on their type. For
example, we could ask R to tell us what the average value of the numbers
in the height column are, but it wouldn't make sense to ask R to tell us
the average value of the words in the Gender column. We will talk more
about many of the different column types exist in R later in this book.
\includegraphics{chapters/what_is_r/table_columns.png}
The information contained in the first cell of each column is called the
\textbf{column name} (or variable) name.
R gives us a lot of flexibility in terms of what we can name our
columns, but there are a few rules.
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
Column names can contain letters, numbers and the dot (.) or
underscore (\_) characters.\\
\item
Additionally, they can begin with a letter or a dot -- as long as the
dot is not followed by a number. So, a name like ``.2cats'' is not
allowed.\\
\item
Finally, R has some reserved words that you are not allowed to use for
column names. These include: ``if'', ``else'', ``repeat'', ``while'',
``function'', ``for'', ``in'', ``next'', and ``break''.
\end{enumerate}
\includegraphics{chapters/what_is_r/table_column_name.png}
Moving from top to bottom across the table are \textbf{rows}, which are
sometimes referred to as records.
\includegraphics{chapters/what_is_r/table_rows.png}
Finally, the contents of each cell are called \textbf{values}.
\includegraphics{chapters/what_is_r/table_values.png}
You should now be up to speed on some basic terminology used by R, as
well as other analytic, database, and spreadsheet programs. These terms
will be used repeatedly throughout the course.
\section{What is R?}\label{what-is-r-1}
\includegraphics{chapters/what_is_r/r_logo.png}
So, what is R? Well, R is an \textbf{open source} statistical
programming language that was created in the 1990's specifically for
data analysis. We will talk more about what open source means later, but
for now, just think of R as an easy (relatively 😂) way to ask your
computer to do math and statistics for you. More specifically, by the
end of this book you will be able to independently use R to transfer
data, manage data, analyze data, and present the results of your
analysis. Let's quickly take a closer look at each of these.
\includegraphics{chapters/what_is_r/competencies_overview.png}
\subsection{Transferring data}\label{transferring-data}
\includegraphics{chapters/what_is_r/competencies_transfer.png}
So, what do we mean by ``transfer data''? Well, individuals and
organizations store their data using different computer programs that
use different file types. Some common examples that you may come across
in epidemiology are database files, spreadsheets, raw data files, and
SAS data sets. No matter how the data is stored, you can't do anything
with it until you can get it into R, in a form that R can use, and in a
location that you can reach. In other words, transferring your data.
Therefore, among our first tasks in this course will be to transfer
data.
\subsection{Managing data}\label{managing-data}
\includegraphics{chapters/what_is_r/competencies_manage.png}
This isn't very specific, but managing data is all the things you may
have to do to your data to get it ready for analysis. You may also hear
people refer to this process as data wrangling or data munging. Some
specific examples of data management tasks include:
\begin{itemize}
\tightlist
\item
\ul{Validating and cleaning data}. In other words, dealing with
potential errors in the data.\\
\item
\ul{Subsetting data}. For example, using only some of the columns or