-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
7108 lines (6175 loc) · 177 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2024-01-10 Wed 17:42 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Fundamental Techniques in Data Science with R Notes</title>
<meta name="author" content="Martijn Voordouw" />
<meta name="generator" content="Org Mode" />
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/bigblow_theme/css/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/bigblow_theme/css/bigblow.css"/>
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/bigblow_theme/css/hideshow.css"/>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery-ui-1.10.2.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery.localscroll-min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery.scrollTo-1.4.3.1-min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery.zclip.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/bigblow.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/hideshow.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/lib/js/jquery.stickytableheaders.min.js"></script>
<script>
window.MathJax = {
tex: {
ams: {
multlineWidth: '85%'
},
tags: 'ams',
tagSide: 'right',
tagIndent: '.8em'
},
chtml: {
scale: 1.0,
displayAlign: 'center',
displayIndent: '0em'
},
svg: {
scale: 1.0,
displayAlign: 'center',
displayIndent: '0em'
},
output: {
font: 'mathjax-modern',
displayOverflow: 'overflow'
}
};
</script>
<script
id="MathJax-script"
async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
</head>
<body>
<div id="content" class="content">
<h1 class="title">Fundamental Techniques in Data Science with R Notes</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org3ce932e">1. R basics</a>
<ul>
<li><a href="#org40bc692">1.1. Getting help</a></li>
<li><a href="#org19c7100">1.2. CRAN and packages</a></li>
<li><a href="#orgcdad203">1.3. R Data formats and importing/writing data</a>
<ul>
<li><a href="#org44550e7">1.3.1. Native data formats</a></li>
<li><a href="#orga326f55">1.3.2. Delimited Data types (csv etc.)</a></li>
<li><a href="#org3c13961">1.3.3. SPSS Data (SAV)</a></li>
<li><a href="#org7211a44">1.3.4. Excel (xlsx)</a></li>
</ul>
</li>
<li><a href="#org7409bb2">1.4. R Functions</a>
<ul>
<li><a href="#orgad0361e">1.4.1. User-Defined Functions</a></li>
</ul>
</li>
<li><a href="#org6c21b1a">1.5. Iteration</a>
<ul>
<li><a href="#org789010a">1.5.1. Loops</a></li>
<li><a href="#org8621c75">1.5.2. Apply</a></li>
</ul>
</li>
<li><a href="#org62b2e7d">1.6. Some tips/ style guide</a></li>
</ul>
</li>
<li><a href="#orgb7f2500">2. R Objects & Data Types</a>
<ul>
<li><a href="#orgb578b5d">2.1. Vectors</a>
<ul>
<li><a href="#org3ab6dc3">2.1.1. Atomic modes</a></li>
<li><a href="#org3ad9509">2.1.2. Generating vectors</a></li>
<li><a href="#org2b3bbf7">2.1.3. Combining data types in vectors?</a></li>
</ul>
</li>
<li><a href="#orgd659272">2.2. Matrices</a>
<ul>
<li><a href="#orgfed9954">2.2.1. Generating Matrices</a></li>
<li><a href="#orgd3ba891">2.2.2. Mixing data types</a></li>
</ul>
</li>
<li><a href="#org8edf667">2.3. Lists</a>
<ul>
<li><a href="#org4c5f5d9">2.3.1. Creating a list</a></li>
<li><a href="#org67b1d85">2.3.2. Named list elements</a></li>
<li><a href="#org3eaf24d">2.3.3. Append onto list</a></li>
</ul>
</li>
<li><a href="#org9b54400">2.4. Data Frames</a>
<ul>
<li><a href="#org529f9ab">2.4.1. Creating a dataframe</a></li>
<li><a href="#org2ec5c13">2.4.2. Filling using sample or runif</a></li>
</ul>
</li>
<li><a href="#org26c5c3f">2.5. Factors</a>
<ul>
<li><a href="#orgd6b92fd">2.5.1. Creating factors</a></li>
<li><a href="#orgfc86913">2.5.2. Factor attributes</a></li>
<li><a href="#orga731eab">2.5.3. Internal representation</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org7acb020">3. Data Manipulation and pipes</a>
<ul>
<li><a href="#org5e1dd59">3.1. Base R Subsetting</a>
<ul>
<li><a href="#org67557f5">3.1.1. []</a></li>
<li><a href="#orgacb9981">3.1.2. [[]]</a></li>
<li><a href="#orgf7e4259">3.1.3. Matrices</a></li>
<li><a href="#org6d99492">3.1.4. $ and lists</a></li>
<li><a href="#org1d3f821">3.1.5. Data Frames</a></li>
<li><a href="#org8a9dff8">3.1.6. Logical indexing</a></li>
</ul>
</li>
<li><a href="#org4858ae0">3.2. Tidyverse subsetting</a></li>
<li><a href="#orgaec9e1b">3.3. Base R Variable Transformation</a></li>
<li><a href="#orga6f25b1">3.4. Tidyverse Variable Transformations</a></li>
<li><a href="#org5b15dd5">3.5. Sorting & Ordering</a>
<ul>
<li><a href="#org69a7d9f">3.5.1. Base R sort</a></li>
<li><a href="#org4b95e30">3.5.2. Tidyverse Ordering</a></li>
</ul>
</li>
<li><a href="#orgbc8a417">3.6. Pipes</a>
<ul>
<li><a href="#orgfe1954b">3.6.1. Example use</a></li>
<li><a href="#org20e428b">3.6.2. dot . in a pipeline, target specific argument</a></li>
<li><a href="#orgd8091cc">3.6.3. Exposition Pipe: %$%</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org3635969">4. Data Visualization</a>
<ul>
<li><a href="#org919dd7a">4.1. Baser R Graphics</a>
<ul>
<li><a href="#org98af5ee">4.1.1. Scatterplots</a></li>
<li><a href="#org536a406">4.1.2. Histogram</a></li>
<li><a href="#orgbe720da">4.1.3. Boxplots</a></li>
<li><a href="#org483ff26">4.1.4. Kernel Density Plots</a></li>
</ul>
</li>
<li><a href="#org2c83e1d">4.2. GGPlot graphics</a>
<ul>
<li><a href="#org92d5b45">4.2.1. Grammar of graphics</a></li>
<li><a href="#org5a70bc0">4.2.2. Basic Setup (Data and aesthetic)</a></li>
<li><a href="#org33f25a6">4.2.3. Geometries (line, dots etc)</a></li>
<li><a href="#org979759d">4.2.4. Statistics</a></li>
<li><a href="#org9327bbd">4.2.5. Styling</a></li>
<li><a href="#orgc51671b">4.2.6. Themes</a></li>
<li><a href="#orgce77b92">4.2.7. Facets</a></li>
<li><a href="#org8109ae4">4.2.8. Joining multiple Figures</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgca44018">5. Data Cleaning</a>
<ul>
<li><a href="#orgb086ffe">5.1. Missing data</a>
<ul>
<li><a href="#org0be84f2">5.1.1. Missing Data Pattern</a></li>
<li><a href="#org27bb512">5.1.2. Nonresponse Rates</a></li>
<li><a href="#orgba3aa9f">5.1.3. Visualizing Incomplete Data</a></li>
</ul>
</li>
<li><a href="#org856a593">5.2. Outliers</a>
<ul>
<li><a href="#org5ffe649">5.2.1. Finding outliers: Boxplot Method</a></li>
<li><a href="#orgea96523">5.2.2. Boxplot in R</a></li>
<li><a href="#orge17bd5a">5.2.3. Breakdown Point</a></li>
<li><a href="#org1f99250">5.2.4. Outliers for Categorical Data</a></li>
<li><a href="#org2bd02f3">5.2.5. Treating outliers</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orge8fbc44">6. Introduction to Linear Modeling</a>
<ul>
<li><a href="#orgd480cbc">6.1. Simple Linear Regression</a>
<ul>
<li><a href="#org98836d5">6.1.1. Thinking about error</a></li>
<li><a href="#orgb2a6c2b">6.1.2. Estimating the Regression Coefficients</a></li>
<li><a href="#org180f618">6.1.3. Model Fit, measures, r-squared</a></li>
<li><a href="#org95b49fe">6.1.4. Information Criteria</a></li>
<li><a href="#org5832e81">6.1.5. In R</a></li>
</ul>
</li>
<li><a href="#org380cc8b">6.2. Multiple Linear Regression</a>
<ul>
<li><a href="#org145a0f8">6.2.1. Comparing models/ multiple R<sup>2</sup>/ f statistic</a></li>
</ul>
</li>
<li><a href="#orgf7546d0">6.3. Categorical Predictors</a>
<ul>
<li><a href="#orgad114f9">6.3.1. Contrast</a></li>
<li><a href="#org66061fb">6.3.2. Significane Testing</a></li>
</ul>
</li>
<li><a href="#org47fcbd6">6.4. Model-Based Prediction</a>
<ul>
<li><a href="#org45ed1ac">6.4.1. Prediction example</a></li>
<li><a href="#org2bc6add">6.4.2. Interval estimates for prediction</a></li>
</ul>
</li>
<li><a href="#org883f844">6.5. Moderation</a>
<ul>
<li><a href="#org8828de4">6.5.1. Additive regression + derivation</a></li>
<li><a href="#org399e4a3">6.5.2. Test for moderation</a></li>
<li><a href="#org6e1c622">6.5.3. in R</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org3dda513">7. Regression Assumptions and Diagnostics</a>
<ul>
<li><a href="#org3836c95">7.1. Assumptions of MLR</a>
<ul>
<li><a href="#org688fc08">7.1.1. 1 The model is linear in the parameters </a></li>
<li><a href="#org20b284d">7.1.2. 2 The predictor matrix is full rank </a></li>
<li><a href="#org5e709c9">7.1.3. 3 The predictors are strictly exogenous</a></li>
<li><a href="#orgce7c377">7.1.4. 4 The errors have constant, finit variance. heteroscedasticity</a></li>
<li><a href="#org302ef3c">7.1.5. 5 The errors are uncorrelated</a></li>
<li><a href="#org0204f2e">7.1.6. 6 The errors are normally distributed.</a></li>
<li><a href="#orga30341f">7.1.7. Spherical errors (4+5)</a></li>
</ul>
</li>
<li><a href="#org55fbc23">7.2. Regression Diagnostics</a>
<ul>
<li><a href="#org59a9d53">7.2.1. Residual Plots</a></li>
</ul>
</li>
<li><a href="#org8500b75">7.3. Influential Observations</a>
<ul>
<li><a href="#org5437992">7.3.1. Outliers</a></li>
<li><a href="#orgaa44479">7.3.2. High-Leverage Points</a></li>
<li><a href="#org0db0c79">7.3.3. Influential Points</a></li>
<li><a href="#org481e977">7.3.4. Removing Influential Observations</a></li>
<li><a href="#org1139279">7.3.5. Treating Influential Points</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgf22ef5d">8. Generalized Linear Model & Logistic regression</a>
<ul>
<li><a href="#orgb9b34c4">8.1. General Linear model</a></li>
<li><a href="#org7e10be0">8.2. Generalized Linear Model</a>
<ul>
<li><a href="#org24241a1">8.2.1. Generalized Linear Model in R</a></li>
</ul>
</li>
<li><a href="#orgc810338">8.3. Logistic Regression</a></li>
<li><a href="#org3fdb487">8.4. Interpreting logistic regression</a></li>
<li><a href="#org55020a0">8.5. Model Comparison for logistic regression</a></li>
<li><a href="#org25cd4f6">8.6. Classification with logistic regression</a></li>
</ul>
</li>
<li><a href="#org4d90816">9. Assumptions of Logistic Regression</a>
<ul>
<li><a href="#orgf4dadc2">9.1. 1 The model is linear in the parameters</a>
<ul>
<li><a href="#org11c31ee">9.1.1. Checking Assumption 1</a></li>
</ul>
</li>
<li><a href="#orgb2b2b00">9.2. 2 The predictor matrix if full rank</a>
<ul>
<li><a href="#org2cd54dd">9.2.1. Checking Assumption 2</a></li>
</ul>
</li>
<li><a href="#org4e398ae">9.3. 3 The outcome is independently and identically binomially distributed</a>
<ul>
<li><a href="#orgfabd7b8">9.3.1. Checking Assumption 3</a></li>
</ul>
</li>
<li><a href="#orgbd8f2d0">9.4. Residuals</a></li>
<li><a href="#org3a615b3">9.5. Computational Considerations</a>
<ul>
<li><a href="#org2c5c538">9.5.1. 1 Sufficient sample size</a></li>
<li><a href="#org8578c5c">9.5.2. 2 Outcome classes are sufficiently balanced</a></li>
<li><a href="#org4dea8c0">9.5.3. 3 There is no perfect prediction</a></li>
</ul>
</li>
<li><a href="#orge44832a">9.6. Influential Cases</a></li>
<li><a href="#orgd0bdfb9">9.7. Classification Performance Measures</a>
<ul>
<li><a href="#org3497362">9.7.1. Confusion Matrix</a></li>
<li><a href="#org01f6cf9">9.7.2. <span class="todo TODO">TODO</span> ROC Curve</a></li>
<li><a href="#org13947da">9.7.3. Threshold Selection</a></li>
<li><a href="#orge60506e">9.7.4. Cross-Entropy Error</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-org3ce932e" class="outline-2">
<h2 id="org3ce932e"><span class="section-number-2">1.</span> R basics</h2>
<div class="outline-text-2" id="text-1">
</div>
<div id="outline-container-org40bc692" class="outline-3">
<h3 id="org40bc692"><span class="section-number-3">1.1.</span> Getting help</h3>
<div class="outline-text-3" id="text-1-1">
<p>
You can get help for a function using
</p>
<div class="org-src-container">
<pre class="src src-R">anova()
?anova
help(anova)
</pre>
</div>
</div>
</div>
<div id="outline-container-org19c7100" class="outline-3">
<h3 id="org19c7100"><span class="section-number-3">1.2.</span> CRAN and packages</h3>
<div class="outline-text-3" id="text-1-2">
<p>
<b>CRAN</b> is a repo for R packages
</p>
<div class="org-src-container">
<pre class="src src-R">install.packages("mice")
# There are two ways to load a package into R:
library(stats)
require(stats)
</pre>
</div>
</div>
</div>
<div id="outline-container-orgcdad203" class="outline-3">
<h3 id="orgcdad203"><span class="section-number-3">1.3.</span> R Data formats and importing/writing data</h3>
<div class="outline-text-3" id="text-1-3">
</div>
<div id="outline-container-org44550e7" class="outline-4">
<h4 id="org44550e7"><span class="section-number-4">1.3.1.</span> Native data formats</h4>
<div class="outline-text-4" id="text-1-3-1">
<div class="org-src-container">
<pre class="src src-R">## Load the built-in 'bfi' data from the 'psychTools' package
data(bfi, package = "psychTools")
## Access the documentation for the 'bfi' data
?psychTools::bfi
## Define the directory holding our data
dataDir <- "../../../data/"
## Load the 'boys' data from the R workspace
## '../../../data/boys.RData'
load(paste0(dataDir, "boys.RData"))
save(boys, file = paste0(dataDir, "tmp.RData"))
## Load the 'titanic' data stored in R data set
## '../../../data/titanic.rds'
titanic <- readRDS(paste0(dataDir, "titanic.rds"))
saveRDS(titanic, file="titanic2.rds")
</pre>
</div>
</div>
</div>
<div id="outline-container-orga326f55" class="outline-4">
<h4 id="orga326f55"><span class="section-number-4">1.3.2.</span> Delimited Data types (csv etc.)</h4>
<div class="outline-text-4" id="text-1-3-2">
<div class="org-src-container">
<pre class="src src-R">## Load the 'diabetes' data from the tab-delimited file
## '../../../data/diabetes.txt'
diabetes <- read.table(paste0(dataDir, "diabetes.txt"),
header = TRUE,
sep = "\t")
write.table(boys,
paste0(dataDir, "boys.txt"),
row.names = FALSE,
sep = "\t",
na = "-999")
## Load the 2017 UTMB data from the comma-separated file
## '../../../data/utmb_2017.csv'
utmb1 <- read.csv(paste0(dataDir, "utmb_2017.csv"))
write.csv2(boys, paste0(dataDir, "boys.csv"), row.names = FALSE, na = "")
</pre>
</div>
<ul class="org-ul">
<li>The read.csv() function assumes the values are seperated by commas</li>
<li>For EU-formatted CSV files (semicolons instead of commas), we can use read.csv2()</li>
</ul>
</div>
</div>
<div id="outline-container-org3c13961" class="outline-4">
<h4 id="org3c13961"><span class="section-number-4">1.3.3.</span> SPSS Data (SAV)</h4>
<div class="outline-text-4" id="text-1-3-3">
<p>
Reading this format is tricky, if we want to read SAV files there are two popular options:
</p>
<ul class="org-ul">
<li>foreign::read.spss()</li>
<li>haven::read<sub>spss</sub>()</li>
</ul>
<p>
foreign doesn’t have a read equivelant
</p>
<div class="org-src-container">
<pre class="src src-R">## Load the foreign package:
library(foreign)
## Use foreign::read.spss() to read '../../../data/mtcars.sav' into a list
mtcars1 <- read.spss(paste0(dataDir, "mtcars.sav"))
## Read '../../../data/mtcars.sav' as a data frame
mtcars2 <- read.spss(paste0(dataDir, "mtcars.sav"), to.data.frame = TRUE)
## Read '../../../data/mtcars.sav' without value labels
mtcars3 <- read.spss(paste0(dataDir, "mtcars.sav"),
to.data.frame = TRUE,
use.value.labels = FALSE)
</pre>
</div>
<div class="org-src-container">
<pre class="src src-R">library(labelled)
## Use haven::read_spss() to read '../../../data/mtcars.sav' into a tibble
mtcars4 <- read_spss(paste0(dataDir, "mtcars.sav"))
</pre>
</div>
<p>
haven::read<sub>spss</sub>() converts any SPSS variables with labels into labelled vectors
</p>
<ul class="org-ul">
<li>We can use the labbeled::unlabeleld() function to remove the value labels</li>
<li>[V-Sha~ -> V-Shaped</li>
</ul>
<p>
The best option we have for writing SPSS data is haven::write<sub>sav</sub>()
</p>
<div class="org-src-container">
<pre class="src src-R">write_sav(mtcars2, paste0(dataDir, "mctars2.sav"))
</pre>
</div>
<p>
This will preserve label information provided by factor variables and the haven<sub>labelled</sub> class
</p>
</div>
</div>
<div id="outline-container-org7211a44" class="outline-4">
<h4 id="org7211a44"><span class="section-number-4">1.3.4.</span> Excel (xlsx)</h4>
<div class="outline-text-4" id="text-1-3-4">
<p>
We have two good options for loading data from Excel spreadsheets:
</p>
<ul class="org-ul">
<li>readxl::read<sub>excel</sub>()</li>
<li>openxlsx::read.xlsx()</li>
</ul>
<div class="org-src-container">
<pre class="src src-R">## Load the packages:
library(readxl)
library(openxlsx)
## Use the readxl::read_excel() function to read the data from the 'titanic'
## sheet of the Excel workbook stored at '../../../data/example_data.xlsx'
titanic2 <- read_excel(paste0(dataDir, "example_data.xlsx"),
sheet = "titanic")
## Use the openxlsx::read.xlsx() function to read the data from the 'titanic'
## sheet of the Excel workbook stored at '../../../data/example_data.xlsx'
titanic3 <- read.xlsx(paste0(dataDir, "example_data.xlsx"),
sheet = "titanic")
</pre>
</div>
<p>
The openxlsx package provides a powerful toolkit for programmatically building Excel workbooks in R and saving the results.
</p>
<div class="org-src-container">
<pre class="src src-R">## Use the openxlsx::write.xlsx() function to write the 'diabetes' data to an
## XLSX workbook
write.xlsx(diabetes, paste0(dataDir, "diabetes.xlsx"), overwrite = TRUE)
## Use the openxlsx::write.xlsx() function to write each data frame in a list
## to a separate sheet of an XLSX workbook
write.xlsx(list(titanic = titanic, diabetes = diabetes, mtcars = mtcars),
paste0(dataDir, "example_data.xlsx"),
overwrite = TRUE)
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org7409bb2" class="outline-3">
<h3 id="org7409bb2"><span class="section-number-3">1.4.</span> R Functions</h3>
<div class="outline-text-3" id="text-1-4">
<p>
Any R command written as a word followed by parentheses is a function
</p>
<ul class="org-ul">
<li>mean()</li>
<li>library()</li>
</ul>
<p>
Infix operators are aliased functions
</p>
<ul class="org-ul">
<li><-</li>
<li>+ - *</li>
<li>> < ==</li>
</ul>
</div>
<div id="outline-container-orgad0361e" class="outline-4">
<h4 id="orgad0361e"><span class="section-number-4">1.4.1.</span> User-Defined Functions</h4>
<div class="outline-text-4" id="text-1-4-1">
<p>
We can define our own functions using the “function()” function
</p>
<div class="org-src-container">
<pre class="src src-R">square <- function(x) {
out <- x^2
out
}
square(5)
</pre>
</div>
<pre class="example">
25
</pre>
<p>
One-line functions don’t need braces:
</p>
<div class="org-src-container">
<pre class="src src-R">square <- function(x) x^2
square(5)
</pre>
</div>
<pre class="example">
25
</pre>
</div>
<ol class="org-ol">
<li><a id="org17e8930"></a>Not strictly typed<br />
<div class="outline-text-5" id="text-1-4-1-1">
<p>
Function arguments are not strictly typed
</p>
<div class="org-src-container">
<pre class="src src-R">square <- function(x) x^2
sqaure(TRUE) # 1
square(1:5)
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-right" />
</colgroup>
<tbody>
<tr>
<td class="org-right">1</td>
</tr>
<tr>
<td class="org-right">4</td>
</tr>
<tr>
<td class="org-right">9</td>
</tr>
<tr>
<td class="org-right">16</td>
</tr>
<tr>
<td class="org-right">25</td>
</tr>
</tbody>
</table>
<p>
But there are limits:
</p>
<div class="org-src-container">
<pre class="src src-R">square("bob")
</pre>
</div>
<p>
Error in x<sup>2</sup>: non-numeric argument to binary operator
</p>
</div>
</li>
<li><a id="orgee66ad3"></a>Multiple/list arguments<br />
<div class="outline-text-5" id="text-1-4-1-2">
<p>
<b>Multiple arguments:</b>
</p>
<div class="org-src-container">
<pre class="src src-R">mod <- function(x, y) x %% y
mod(10, 3)
</pre>
</div>
<p>
<b>Or you can use a list to get multiple arguments:</b>
</p>
<div class="org-src-container">
<pre class="src src-R">getLsBeta <- function(datList) {
X <- datList$X
y <- datList$y
solve(crossprod(X)) %*% t(X) %*% y
}
X <- matrix(runif(500), ncol = 5)
datList <- list(y = X %*% rep(0.5, 5), X = X)
getLsBeta(datList = datList)
</pre>
</div>
</div>
</li>
<li><a id="org51ddaa7"></a>Type/class of functions<br />
<div class="outline-text-5" id="text-1-4-1-3">
<p>
Functions are first-class objects in R.
</p>
<ul class="org-ul">
<li>We can treat functions like any other R object.</li>
</ul>
<p>
R views an unevaluated function as an object with type ”closure”.
</p>
<div class="org-src-container">
<pre class="src src-R">class(getLsBeta)
</pre>
</div>
<p>
[1] “function”
</p>
<div class="org-src-container">
<pre class="src src-R">typeof(getLsBeta)
</pre>
</div>
<p>
[1] “closure”
</p>
<p>
An evaluated functions is equivalent to the objects it returns.
</p>
<div class="org-src-container">
<pre class="src src-R">class(getLsBeta(datList))
</pre>
</div>
<p>
[1] “matrix” “array”
</p>
<div class="org-src-container">
<pre class="src src-R">typeof(getLsBeta(datList))
</pre>
</div>
<p>
[1] “double”
</p>
</div>
</li>
</ol>
</div>
</div>
<div id="outline-container-org6c21b1a" class="outline-3">
<h3 id="org6c21b1a"><span class="section-number-3">1.5.</span> Iteration</h3>
<div class="outline-text-3" id="text-1-5">
</div>
<div id="outline-container-org789010a" class="outline-4">
<h4 id="org789010a"><span class="section-number-4">1.5.1.</span> Loops</h4>
<div class="outline-text-4" id="text-1-5-1">
<p>
There are three types of loops in R: for, while and until.
Deze course behandelt alleen for (lol)
</p>
<div class="org-src-container">
<pre class="src src-R">for(INDEX in RANGE) { Stuff To Do with the Current INDEX Value }
</pre>
</div>
<p>
<i>examples:</i>
</p>
<div class="org-src-container">
<pre class="src src-R">val <- 0
for(i in 1:100) {
val <- val + i
}
val
</pre>
</div>
<pre class="example">
5050
</pre>
<p>
This loop will compute the mean of every column in the mtcars data
</p>
<div class="org-src-container">
<pre class="src src-R">means <- rep(0, ncol(mtcars))
for(j in 1:ncol(mtcars)) {
means[j] <- mean(mtcars[ , j])
}
means
</pre>
</div>
<div class="org-src-container">
<pre class="src src-R">listThing <- list(6,9,4,2)
sum <- 0
for(i in listThing) {
sum <- i + sum
}
sum
</pre>
</div>
<pre class="example">
21
</pre>
<p>
Usually there is a function to achieve what you are trying to do with the loop (sum() for example)
</p>
</div>
</div>
<div id="outline-container-org8621c75" class="outline-4">
<h4 id="org8621c75"><span class="section-number-4">1.5.2.</span> Apply</h4>
<div class="outline-text-4" id="text-1-5-2">
<p>
Usually one of two forms:
</p>
<div class="org-src-container">
<pre class="src src-R">apply(DATA, MARGIN, FUNCTION, ...)
apply(DATA, FUNCTION, ...) # not sure if this works??? but was in slides
</pre>
</div>
</div>
<ol class="org-ol">
<li><a id="orge1333a2"></a>Margin<br />
<div class="outline-text-5" id="text-1-5-2-1">
<p>
Margin is how the function should be applied:
E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names.
</p>
<div class="org-src-container">
<pre class="src src-R">data(bfi, package = "psychTools")
dat1 <- bfi[1:5,1:3]
# | 2 | 4 | 3 |
# | 2 | 4 | 5 |
# | 5 | 4 | 5 |
# | 4 | 4 | 6 |
# | 2 | 3 | 3 |
# sum over rows with above table
apply(dat1, 1, sum)
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-right" />
</colgroup>
<tbody>
<tr>
<td class="org-right">9</td>
</tr>
<tr>
<td class="org-right">11</td>
</tr>
<tr>
<td class="org-right">14</td>
</tr>
<tr>
<td class="org-right">14</td>
</tr>
<tr>
<td class="org-right">8</td>
</tr>
</tbody>
</table>
<div class="org-src-container">
<pre class="src src-R">data(bfi, package = "psychTools")
dat1 <- bfi[1:5,1:3]
# subtract 1 from every cell
apply(dat1, (1:2), function(x)x-1)
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
</colgroup>
<tbody>
<tr>
<td class="org-right">1</td>
<td class="org-right">3</td>
<td class="org-right">2</td>
</tr>
<tr>
<td class="org-right">1</td>
<td class="org-right">3</td>
<td class="org-right">4</td>
</tr>
<tr>
<td class="org-right">4</td>
<td class="org-right">3</td>
<td class="org-right">4</td>
</tr>
<tr>
<td class="org-right">3</td>
<td class="org-right">3</td>
<td class="org-right">5</td>
</tr>
<tr>
<td class="org-right">1</td>
<td class="org-right">2</td>
<td class="org-right">2</td>
</tr>
</tbody>
</table>
</div>
</li>
<li><a id="org062bea0"></a>lapply and sapply<br />
<div class="outline-text-5" id="text-1-5-2-2">
<p>
similar concept but works on lists
</p>
<div class="org-src-container">
<pre class="src src-R">list1 <- list(1,3,4,2,4)
lapply(list1, function(x) x+1)
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
<col class="org-right" />
</colgroup>
<tbody>
<tr>
<td class="org-right">2</td>
<td class="org-right">4</td>
<td class="org-right">5</td>
<td class="org-right">3</td>
<td class="org-right">5</td>
</tr>
</tbody>
</table>
<p>
sapply is a user friendly wrapper for lapply which be default returns a vector or matrix
</p>
</div>
</li>
</ol>
</div>
</div>
<div id="outline-container-org62b2e7d" class="outline-3">
<h3 id="org62b2e7d"><span class="section-number-3">1.6.</span> Some tips/ style guide</h3>
<div class="outline-text-3" id="text-1-6">
<ul class="org-ul">
<li>While using RStudie use TAB to quickly acces the documentation of the function arguments</li>
<li>Use the <a href="https://style.tidyverse.org/">tidyverse style guide</a>
<ul class="org-ul">
<li>Can be down automatically in Rstudio -> addins -> style selection</li>
</ul></li>
</ul>
</div>
</div>
</div>
<div id="outline-container-orgb7f2500" class="outline-2">
<h2 id="orgb7f2500"><span class="section-number-2">2.</span> R Objects & Data Types</h2>
<div class="outline-text-2" id="text-2">
</div>
<div id="outline-container-orgb578b5d" class="outline-3">
<h3 id="orgb578b5d"><span class="section-number-3">2.1.</span> Vectors</h3>
<div class="outline-text-3" id="text-2-1">
<p>
Vectors are the simplest kind of R object, there is no concept of a “scalar” in R
</p>
</div>
<div id="outline-container-org3ab6dc3" class="outline-4">
<h4 id="org3ab6dc3"><span class="section-number-4">2.1.1.</span> Atomic modes</h4>
<div class="outline-text-4" id="text-2-1-1">
<p>
Vectors come in one of six “atomic modes”:
</p>
<ul class="org-ul">
<li>numeric/double</li>
<li>logical -> bool</li>
<li>character</li>
<li>integer</li>
<li>complex
<ul class="org-ul">
<li>A complex value in R is defined via the pure imaginary value i.</li>
</ul></li>
<li>raw
<ul class="org-ul">
<li>Intended to hold raw bytes</li>
</ul></li>
</ul>
</div>
</div>
<div id="outline-container-org3ad9509" class="outline-4">
<h4 id="org3ad9509"><span class="section-number-4">2.1.2.</span> Generating vectors</h4>
<div class="outline-text-4" id="text-2-1-2">
<div class="org-src-container">
<pre class="src src-R">c(1, 2, 3)
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-right" />
</colgroup>
<tbody>
<tr>
<td class="org-right">1</td>
</tr>
<tr>
<td class="org-right">2</td>
</tr>
<tr>