Foreword xiii
Preface xv
Acknowledgments xix
About the Author xxiii
- Getting R 1
1.1 Downloading R 1
1.2 R Version 2
1.3 32-bit versus 64-bit 2
1.4 Installing 2
1.5 Revolution R Community Edition 10
1.6 Conclusion 11
- The R Environment 13
2.1 Command Line Interface 14
2.2 RStudio 15
2.3 Revolution Analytics RPE 26
2.4 Conclusion 27
- R Packages 29
3.1 Installing Packages 29
3.2 Loading Packages 32
3.3 Building a Package 33
3.4 Conclusion 33
- Basics of R 35
4.1 Basic Math 35
4.2 Variables 36
4.3 Data Types 38
4.4 Vectors 43
4.5 Calling Functions 49
4.6 Function Documentation 49
4.7 Missing Data 50
4.8 Conclusion 51
- Advanced Data Structures 53
5.1 data.frames 53
5.2 Lists 61
5.3 Matrices 68
5.4 Arrays 71
5.5 Conclusion 72
- Reading Data into R 73
6.1 Reading CSVs 73
6.2 Excel Data 74
6.3 Reading from Databases 75
6.4 Data from Other Statistical Tools 77
6.5 R Binary Files 77
6.6 Data Included with R 79
6.7 Extract Data from Web Sites 80
6.8 Conclusion 81
- Statistical Graphics 83
7.1 Base Graphics 83
7.2 ggplot2 86
7.3 Conclusion 98
- Writing R Functions 99
8.1 Hello, World! 99
8.2 Function Arguments 100
8.3 Return Values 103
8.4 do.call 104
8.5 Conclusion 104
- Control Statements 105
9.1 if and else 105
9.2 switch 108
9.3 ifelse 109
9.4 Compound Tests 111
9.5 Conclusion 112
- Loops, the Un-R Way to Iterate 113
10.1 for Loops 113
10.2 while Loops 115
10.3 Controlling Loops 115
10.4 Conclusion 116
- Group Manipulation 117
11.1 Apply Family 117
11.2 aggregate 120
11.3 plyr 124
11.4 data.table 129
11.5 Conclusion 139
- Data Reshaping 141
12.1 cbind and rbind 141
12.2 Joins 142
12.3 reshape2 149
12.4 Conclusion 153
- Manipulating Strings 155
13.1 paste 155
13.2 sprintf 156
13.3 Extracting Text 157
13.4 Regular Expressions 161
13.5 Conclusion 169
- Probability Distributions 171
14.1 Normal Distribution 171
14.2 Binomial Distribution 176
14.3 Poisson Distribution 182
14.4 Other Distributions 185
14.5 Conclusion 186
- Basic Statistics 187
15.1 Summary Statistics 187
15.2 Correlation and Covariance 191
15.3 T-Tests 200
15.4 ANOVA 207
15.5 Conclusion 210
- Linear Models 211
16.1 Simple Linear Regression 211
16.2 Multiple Regression 216
16.3 Conclusion 232
- Generalized Linear Models 233
17.1 Logistic Regression 233
17.2 Poisson Regression 237
17.3 Other Generalized Linear Models 240
17.4 Survival Analysis 240
17.5 Conclusion 245
- Model Diagnostics 247
18.1 Residuals 247
18.2 Comparing Models 253
18.3 Cross-Validation 257
18.4 Bootstrap 262
18.5 Stepwise Variable Selection 265
18.6 Conclusion 269
- Regularization and Shrinkage 271
19.1 Elastic Net 271
19.2 Bayesian Shrinkage 290
19.3 Conclusion 295
- Nonlinear Models 297
20.1 Nonlinear Least Squares 297
20.2 Splines 300
20.3 Generalized Additive Models 304
20.4 Decision Trees 310
20.5 Random Forests 312
20.6 Conclusion 313
- Time Series and Autocorrelation 315
21.1 Autoregressive Moving Average 315
21.2 VAR 32221.3 GARCH 327
21.4 Conclusion 336
- Clustering 337
22.1 K-means 337
22.2 PAM 345
22.3 Hierarchical Clustering 352
22.4 Conclusion 357
- Reproducibility, Reports and Slide Shows with knitr 359
23.1 Installing a LATEX Program 359
23.2 LATEX Primer 360
23.3 Using knitr with LATEX 362
23.4 Markdown Tips 367
23.5 Using knitr and Markdown 368
23.6 pandoc 369
23.7 Conclusion 371
- Building R Packages 373
24.1 Folder Structure 373
24.2 Package Files 373
24.3 Package Documentation 380
24.4 Checking, Building and Installing 383
24.5 Submitting to CRAN 384
24.6 C++ Code 384
24.7 Conclusion 390
- A Real-Life Resources 391
- A.1 Meetups 391
- A.2 Stackoverflow 392
- A.3 Twitter 393
- A.4 Conferences 393
- A.5 Web Sites 393
- A.6 Documents 394
- A.7 Books 394
- A.8 Conclusion 394
- B Glossary 395
List of Figures 409
List of Tables 417
General Index 419
Index of Functions 427
Index of Packages 431
Index of People 433
Data Index 435
Jared Lander is the Chief Data Scientist of Lander Analytics a New York data science firm, Adjunct Professor at Columbia University, Organizer of the New York Open Statistical Programming meetup and the New York and Washington DC R Conferences and author of R for Everyone.