48

R for Everyone

Embed Size (px)

DESCRIPTION

R For Every One

Citation preview

  • R forEveryone

  • T he Addison-Wesley Data and Analytics Series provides readers with practical knowledge for solving problems and answering questions with data. Titles in this series primarily focus on three areas:

    1. Infrastructure: how to store, move, and manage data

    2. Algorithms: how to mine intelligence or make predictions based on data

    3. Visualizations: how to represent data and insights in a meaningful and compelling way

    The series aims to tie all three of these areas together to help the reader build end-to-end systems for fighting spam; making recommendations; building personalization; detecting trends, patterns, or problems; and gaining insight from the data exhaust of systems and user interactions.

    Visit informit.com/awdataseries for a complete list of available publications.

    Make sure to connect with us!informit.com/socialconnect

    The Addison-Wesley Data and Analytics Series

  • R forEveryone

    Advanced Analyticsand Graphics

    Jared P. Lander

    Upper Saddle River, NJ Boston Indianapolis San FranciscoNew York Toronto Montreal London Munich Paris Madrid

    Capetown Sydney Tokyo Singapore Mexico City

  • Many of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and the publisher was aware of a trademarkclaim, the designations have been printed with initial capital letters or in all capitals.

    The author and publisher have taken care in the preparation of this book, but make no expressed orimplied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumedfor incidental or consequential damages in connection with or arising out of the use of the information orprograms contained herein.

    For information about buying this title in bulk quantities, or for special sales opportunities (which mayinclude electronic versions; custom cover designs; and content particular to your business, training goals,marketing focus, or branding interests), please contact our corporate sales department [email protected] or (800) 382-3419.

    For government sales inquiries, please contact [email protected].

    For questions about sales outside the U.S., please contact [email protected].

    Visit us on the Web: informit.com/aw

    Library of Congress Cataloging-in-Publication Data

    Lander, Jared P.R for everyone / Jared P. Lander.

    pages cmIncludes bibliographical references.ISBN-13: 978-0-321-88803-7 (alk. paper)ISBN-10: 0-321-88803-0 (alk. paper)1. R (Computer program language) 2. Scripting languages (Computer science) 3. Statistics Dataprocessing. 4. Statistics Graphic methods Data processing. 5. Computer simulation. I. Title.QA76.73.R3L36 2014005.13dc23 2013027407

    Copyright 2014 Pearson Education, Inc.

    All rights reserved. Printed in the United States of America. This publication is protected by copyright, andpermission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrievalsystem, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, orlikewise. To obtain permission to use material from this work, please submit a written request to PearsonEducation, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or youmay fax your request to (201) 236-3290.

    ISBN-13: 978-0-321-88803-7ISBN-10: 0-321-88803-0Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.First printing, December 2013

  • vTo my mother and fatherv

  • This page intentionally left blank

  • Contents

    Foreword xiii

    Preface xv

    Acknowledgments xix

    About the Author xxiii

    1 Getting R 1

    1.1 Downloading R 1

    1.2 R Version 21.3 32-bit versus 64-bit 21.4 Installing 2

    1.5 Revolution R Community Edition 10

    1.6 Conclusion 11

    2 The R Environment 132.1 Command Line Interface 142.2 RStudio 152.3 Revolution Analytics RPE 26

    2.4 Conclusion 27

    3 R Packages 29

    3.1 Installing Packages 29

    3.2 Loading Packages 32

    3.3 Building a Package 33

    3.4 Conclusion 33

    4 Basics of R 354.1 Basic Math 354.2 Variables 364.3 Data Types 38

    4.4 Vectors 434.5 Calling Functions 49

    4.6 Function Documentation 494.7 Missing Data 50

    4.8 Conclusion 51

  • viii Contents

    5 Advanced Data Structures 535.1 data.frames 535.2 Lists 615.3 Matrices 685.4 Arrays 71

    5.5 Conclusion 72

    6 Reading Data into R 73

    6.1 Reading CSVs 73

    6.2 Excel Data 746.3 Reading from Databases 75

    6.4 Data from Other Statistical Tools 776.5 R Binary Files 77

    6.6 Data Included with R 796.7 Extract Data from Web Sites 806.8 Conclusion 81

    7 Statistical Graphics 837.1 Base Graphics 83

    7.2 ggplot2 867.3 Conclusion 98

    8 Writing R Functions 99

    8.1 Hello, World! 99

    8.2 Function Arguments 100

    8.3 Return Values 1038.4 do.call 1048.5 Conclusion 104

    9 Control Statements 1059.1 if and else 1059.2 switch 1089.3 ifelse 1099.4 Compound Tests 111

    9.5 Conclusion 112

    10 Loops, the Un-R Way to Iterate 113

    10.1 for Loops 11310.2 while Loops 115

  • Contents ix

    10.3 Controlling Loops 115

    10.4 Conclusion 116

    11 Group Manipulation 117

    11.1 Apply Family 117

    11.2 aggregate 120

    11.3 plyr 124

    11.4 data.table 12911.5 Conclusion 139

    12 Data Reshaping 141

    12.1 cbind and rbind 14112.2 Joins 142

    12.3 reshape2 149

    12.4 Conclusion 153

    13 Manipulating Strings 155

    13.1 paste 155

    13.2 sprintf 15613.3 Extracting Text 157

    13.4 Regular Expressions 161

    13.5 Conclusion 169

    14 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

    15 Basic Statistics 18715.1 Summary Statistics 187

    15.2 Correlation and Covariance 191

    15.3 T-Tests 200

    15.4 ANOVA 207

    15.5 Conclusion 210

  • x Contents

    16 Linear Models 21116.1 Simple Linear Regression 211

    16.2 Multiple Regression 216

    16.3 Conclusion 232

    17 Generalized Linear Models 23317.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

    18 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

    19 Regularization and Shrinkage 271

    19.1 Elastic Net 271

    19.2 Bayesian Shrinkage 290

    19.3 Conclusion 295

    20 Nonlinear Models 29720.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

    21 Time Series and Autocorrelation 31521.1 Autoregressive Moving Average 315

    21.2 VAR 322

  • Contents xi

    21.3 GARCH 327

    21.4 Conclusion 336

    22 Clustering 337

    22.1 K-means 337

    22.2 PAM 345

    22.3 Hierarchical Clustering 352

    22.4 Conclusion 357

    23 Reproducibility, Reports and Slide Showswith knitr 35923.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 36823.6 pandoc 369

    23.7 Conclusion 371

    24 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 391A.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

  • xii Contents

    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

  • Foreword

    R has had tremendous growth in popularity over the last three years. Based on that,youd think that it was a new, up-and-coming language. But surprisingly, R has beenaround since 1993. Why the sudden uptick in popularity? The somewhat obvious answerseems to be the emergence of data science as a career and a field of study. But theunderpinnings of data science have been around for many decades. Statistics, linearalgebra, operations research, artificial intelligence, and machine learning all contributeparts to the tools that a modern data scientist uses. R, more than most languages, has beenbuilt to make most of these tools only a single function call away.

    Thats why Im very excited to have this book as one of the first in the Addison-WesleyData and Analytics Series. R is indispensable for many data science tasks. Many algorithmsuseful for prediction and analysis can be accessed through only a few lines of code, whichmakes it a great fit for solving modern data challenges. Data science as a field isnt justabout math and statistics, and it isnt just about programming and infrastructure. This bookprovides a well-balanced introduction to the power and expressiveness of R and is aimed ata general audience.

    I cant think of a better author to provide an introduction to R than Jared Lander. Jaredand I first met through the New York City machine learning community in late 2009.Back then, the New York City data community was small enough to fit in a singleconference room, and many of the other data meetups had yet to be formed. Over the lastfour years, Jared has been at the forefront of the emerging data science profession.

    Through running the Open Statistical Programming Meetup, speaking at events, andteaching a course at Columbia on R, Jared has helped grow the community by educatingprogrammers, data scientists, journalists, and statisticians alike. But Jareds expertise isntlimited to teaching. As an everyday practitioner, he puts these tools to use whileconsulting for clients big and small.

    This book provides an introduction both to programming in R and to the variousstatistical methods and tools an everyday R programmer uses. Examples use publiclyavailable datasets that Jared has helpfully cleaned and made accessible through his Web site.By using real data and setting up interesting problems, this book stays engaging to the end.

    Paul Dix, Series Editor

  • This page intentionally left blank

  • Preface

    With the increasing prevalence of data in our daily lives, new and better tools areneeded to analyze the deluge. Traditionally there have been two ends of the spectrum:lightweight, individual analysis using tools like Excel or SPSS and heavy duty,high-performance analysis built with C++ and the like. With the increasing strength ofpersonal computers grew a middle ground that was both interactive and robust. Analysisdone by an individual on his or her own computer in an exploratory fashion could quicklybe transformed into something destined for a server, underpinning advanced businessprocesses. This area is the domain of R, Python, and other scripted languages.

    R, invented by Robert Gentleman and Ross Ihaka of the University of Auckland in1993, grew out of S, which was invented by John Chambers at Bell Labs. It is a high-levellanguage that was originally intended to be run interactively where the user runs acommand, gets a result, and then runs another command. It has since evolved into alanguage that can also be embedded in systems and tackle complex problems.

    In addition to transforming and analyzing data, R can produce amazing graphics andreports with ease. It is now being used as a full stack for data analysis, extracting andtransforming data, fitting models, drawing inferences and making predictions, plotting andreporting results.

    Rs popularity has skyrocketed since the late 2000s, as it has stepped out of academiaand into banking, marketing, pharmaceuticals, politics, genomics and many other fields.Its new users are often shifting from low-level, compiled languages like C++, otherstatistical packages such as SAS or SPSS, and from the 800-pound gorilla, Excel. This timeperiod also saw a rapid surge in the number of add-on packageslibraries of prewrittencode that extend Rs functionality.

    While R can sometimes be intimidating to beginners, especially for those withoutprogramming experience, I find that programming analysis, instead of pointing andclicking, soon becomes much easier, more convenient and more reliable. It is my goal tomake that learning process easier and quicker.

    This book lays out information in a way I wish I were taught when learning R ingraduate school. Coming full circle, the content of this book was developed in conjuctionwith the data science course I teach at Columbia University. It is not meant to cover everyminute detail of R, but rather the 20% of functionality needed to accomplish 80% of thework. The content is organized into self-contained chapters as follows.

    Chapter 1, Getting R: Where to download R and how to install it. This deals with thevarying operating systems and 32-bit versus 64-bit versions. It also gives advice on whereto install R.

  • xvi Preface

    Chapter 2, The R Environment: An overview of using R, particularly from withinRStudio. RStudio projects and Git integration are covered as is customizing andnavigating RStudio.

    Chapter 3, Packages: How to locate, install and load R packages.Chapter 4, Basics of R: Using R for math. Variable types such as numeric, character

    and Date are detailed as are vectors. There is a brief introduction to calling functionsand finding documentation on functions.

    Chapter 5, Advanced Data Structures: The most powerful and commonly used datastructure, data.frames, along with matrices and lists, are introduced.

    Chapter 6, Reading Data into R: Before data can be analyzed it must be read into R.There are numerous ways to ingest data, including reading from CSVs and databases.

    Chapter 7, Statistical Graphics: Graphics are a crucial part of preliminary data analysisand communicating results. R can make beautiful plots using its powerful plotting utilities.Base graphics and ggplot2 are introduced and detailed here.

    Chapter 8, Writing R Functions: Repeatable analysis is often made easier withuser-defined functions. The structure, arguments and return rules are discussed.

    Chapter 9, Control Statements: Controlling the flow of programs using if, ifelseand complex checks.

    Chapter 10, Loops, the Un-R Way to Iterate: Iterating using for and while loops.While these are generally discouraged they are important to know.

    Chapter 11, Group Manipulation: A better alternative to loops, vectorization doesnot quite iterate through data so much as operate on all elements at once. This ismore efficient and is primarily performed with the apply functions and plyrpackage.

    Chapter 12, Data Reshaping: Combining multiple datasets, whether by stacking orjoining, is commonly necessary as is changing the shape of data. The plyr and reshape2packages offer good functions for accomplishing this in addition to base tools such asrbind, cbind and merge.

    Chapter 13, Manipulating Strings: Most people do not associate character data withstatistics but it is an important form of data. R provides numerous facilities for workingwith strings, including combining them and extracting information from within. Regularexpressions are also detailed.

    Chapter 14, Probability Distributions: A thorough look at the normal, binomial andPoisson distributions. The formulas and functions for many distributions are noted.

    Chapter 15, Basic Statistics: These are the first statistics most people are taught, such asmean, standard deviation and t-tests.

    Chapter 16, Linear Models: The most powerful and common tool in statistics, linearmodels are extensively detailed.

    Chapter 17, Generalized Linear Models: Linear models are extended to include logisticand Poisson regression. Survival analysis is also covered.

    Chapter 18, Model Diagnostics: Determining the quality of models and variable selectionusing residuals, AIC, cross-validation, the bootstrap and stepwise variable selection.

  • Preface xvii

    Chapter 19, Regularization and Shrinkage: Preventing overfitting using the Elastic Netand Bayesian methods.

    Chapter 20, Nonlinear Models: When linear models are inappropriate, nonlinearmodels are a good solution. Nonlinear least squares, splines, generalized additive models,decision trees and random forests are discussed.

    Chapter 21, Time Series and Autocorrelation: Methods for the analysis of univariateand multivariate time series data.

    Chapter 22, Clustering: Clustering, the grouping of data, is accomplished by variousmethods such as K-means and hierarchical clustering.

    Chapter 23, Reproducibility, Reports and Slide Shows with knitr: Generatingreports, slide shows and Web pages from within R is made easy with knitr, LATEX andMarkdown.

    Chapter 24, Building R Packages: R packages are great for portable, reusable code.Building these packages has been made incredibly easy with the advent of devtools andRcpp.

    Appendix A, Real-Life Resources: A listing of our favorite resources for learning moreabout R and interacting with the community.

    Appendix B, Glossary: A glossary of terms used throughout this book.A good deal of the text in this book is either R code or the results of running code.

    Code and results are most often in a separate block of text and set in a distinctive font, asshown in the following example. The different parts of code also have different colors.Lines of code start with >, and if code is continued from one line to another thecontinued line begins with +.> # this is a comment>

    > # now basic math> 10 * 10

    [1] 100

    >

    > # calling a function> sqrt(4)

    [1] 2

    Certain Kindle devices do not display color so the digital edition of this book will beviewed in greyscale on those devices.

    There are occasions where code is shown inline and looks like sqrt(4).In the few places where math is necessary, the equations are indented from the margin

    and are numbered.

    eipi + 1 = 0 (1)

  • xviii Preface

    Within equations, normal variables appear as italic text (x), vectors are bold lowercaseletters (x) and matrices are bold uppercase letters (X). Greek letters, such as and ,follow the same convention.

    Function names will be written as join and package names as plyr. Objectsgenerated in code that are referenced in text are written as object1.

    Learning R is a gratifying experience that makes life so much easier for so many tasks.I hope you enjoy learning with me.

  • Acknowledgments

    To start, I must thank my mother, Gail Lander, for encouraging me to become a mathmajor. Without that I would never have followed the path that led me to statistics and datascience. In a similar vein, I have to thank my father, Howard Lander, for paying all thosetuition bills. He has been a valuable source of advice and guidance throughout my life andsomeone I have aspired to emulate in many ways. While they both insist they do notunderstand what I do, they love that I do it and have helped me all along the way. Stayingwith family, I should thank my sister and brother-in-law, Aimee and Eric Schechterman,for letting me teach math to Noah, their five-year-old son.

    There are many teachers who have helped shape me over the years. The first isRochelle Lecke, who tutored me in middle school math even when my teacher told me Idid not have worthwhile math skills.

    Then there is Beth Edmondson, my precalc teacher at Princeton Day School. After Iwasted the first half of high school as a mediocre student, she told me I had some nervesigning up for next years AP Calc given my grades. She agreed to let me take AP Calc ifI went from a C to an A+ in her class, never thinking I stood a chance. Three monthslater, she was in shock as I not only earned the A+, but turned around my entire academiccareer. She changed my life and without her, I do not know where I would be today. I amforever grateful that she was my teacher.

    For the first two years at Muhlenberg College, I was determined to be a business andcommunications major, but took math classes because they came naturally to me. Myprofessors, Dr. Penny Dunham, Dr. Bill Dunham, and Dr. Linda McGuire, all convincedme to become a math major, a decision that has greatly shaped my life. Dr. GregCicconetti gave me my first glimpse of rigorous statistics, my first research opportunity andplanted the idea in my head that I should go to grad school for statistics.

    While earning my M.A. at Columbia University, I was surrounded by brilliantminds in statistics and programming. Dr. David Madigan opened my eyes to modernmachine learning, and Dr. Bodhi Sen got me thinking about statistical programming.I had the privilege to do research with Dr. Andrew Gelman, whose insights have beenimmeasurably important to me. Dr. Richard Garfield showed me how to use statistics tohelp people in disaster and war zones when he sent me on my first assignment toMyanmar. His advice and friendship over the years have been dear to me. Dr. Jingchen Liu

  • xx Acknowledgments

    allowed and encouraged me to write my thesis on New York City pizza, which hasbrought me an inordinate amount of attention.1

    While at Columbia, I also met my good friendand one time TA Dr. Ivor Cribbenwho filled in so many gaps in my knowledge. Through him, I met Dr. Rachel Schutt, asource of great advice, and who I am now honored to teach alongside at Columbia.

    Grad school might never have happened without the encouragement and support ofShanna Lee. She helped maintain my sanity while I was incredibly overcommited to twojobs, classes and Columbias hockey team. I am not sure I would have made it throughwithout her.

    Steve Czetty gave me my first job in analytics at Sky IT Group and taught me aboutdatabases, while letting me experiment with off-the-wall programming. This sparked myinterest in statistics and data. Joe DeSiena, Philip du Plessis, and Ed Bobrin at the BardessGroup are some of the finest people I have ever had the pleasure to work with, and I amproud to be working with them to this day. Mike Minelli, Rich Kittler, Mark Barry, DavidSmith, Joseph Rickert, Dr. Norman Nie, James Peruvankal, Neera Talbert and Dave Richat Revolution Analytics let me do one of the best jobs I could possibly imagine: explainingto people in business why they should be using R. Kirk Mettler, Richard Schultz, Dr.Bryan Lewis and Jim Winfield at Big Computing encouraged me to have fun, tacklinginteresting problems in R. Vincent Saulys, John Weir, and Dr. Saar Golde at GoldmanSachs made my time there both enjoyable and educational.

    Throughout the course of writing this book, many people helped me with the process.First and foremost is Yin Cheung, who saw all the stress I constantly felt and supported methrough many ruined nights and days.

    My editor, Debra Williams, knew just how to encourage me and her guiding hand hasbeen invaluable. Paul Dix, the series editor and a good friend, was the person whosuggested I write this book, so none of this would have happened without him. Thanks toCaroline Senay and Andrea Fox for being great copy editors. Without them, this bookwould not be nearly as well put together. Robert Mauriellos technical review wasincredibly useful in honing the books presentation.

    The folks at RStudio, particularly JJ Allaire and Josh Paulson, make an amazingproduct, which made the writing process far easier than it would have been otherwise.Yihui Xie, the author of the knitr package, provided numerous feature changes that Ineeded to write this book. His software, and his speed at implementing my requests, isgreatly appreciated.

    Numerous people have provided valuable feedback as I produced this book, includingChris Bethel, Dr. Dirk Eddelbuettel, Dr. Ramnath Vaidyanathan, Dr. Eran Bellin,

    1. http://slice.seriouseats.com/archives/2010/03/the-moneyball-of-pizza-statistician-uses-statistics-to-find-nyc-best-pizza.html

  • Acknowledgments xxi

    Avi Fisher, Brian Ezra, Paul Puglia, Nicholas Galasinao, Aaron Schumaker, Adam Hogan,Jeffrey Arnold, and John Houston.

    Last fall was my first time teaching, and I am thankful to the students from the Fall2012 Introduction to Data Science class at Columbia University for being the guinea pigsfor the material that ultimately ended up in this book.

    Thank you to everyone who helped along the way.

  • This page intentionally left blank

  • About the Author

    Jared P. Lander is the founder and CEO of Lander Analytics, a statistical consulting firmbased in New York City, the organizer of the New York Open Statistical ProgrammingMeetup, and an adjunct professor of statistics at Columbia University. He is also a tourguide for Scotts Pizza Tours and an advisor to Brewla Bars, a gourmet ice pop start-up.With an M.A. from Columbia University in statistics and a B.A. from MuhlenbergCollege in mathematics, he has experience in both academic research and industry. Hiswork for both large and small organizations spans politics, tech start-ups, fund-raising,music, finance, healthcare and humanitarian relief efforts.

    He specializes in data management, multilevel models, machine learning, generalizedlinear models, visualization, data management and statistical computing.

  • This page intentionally left blank

  • This page intentionally left blank

  • Chapter 12Data Reshaping

    As noted in Chapter 11, manipulating the data takes a great deal of effort before seriousanalysis can begin. In this chapter we will consider when the data needs to be rearrangedfrom column oriented to row oriented (or the opposite) and when the data are inmultiple, separate sets and need to be combined into one.

    There are base functions to accomplish these tasks but we will focus on those in plyr,reshape2 and data.table.

    12.1 cbind and rbindThe simplest case is when we have two datasets with either identical columns (both thenumber of and names) or the same number of rows. In this case, either rbind or cbindwork great.

    As a first trivial example, we create two simple data.frames by combining a fewvectors with cbind, and then stack them using rbind.

    > # make two vectors and combine them as columns in a data.frame> sport league trophy trophies1 # make another data.frame using data.frame()> trophies2 # combine them into one data.frame with rbind> trophies

  • 142 Chapter 12 Data Reshaping

    Both cbind and rbind can take multiple arguments to combine an arbitrary numberof objects. Note that it is possible to assign new column names to vectors in cbind.

    > cbind(Sport = sport, Association = league, Prize = trophy)

    Sport Association Prize[1,] "Hockey" "NHL" "Stanley Cup"[2,] "Baseball" "MLB" "Commissioner's Trophy"[3,] "Football" "NFL" "Vince Lombardi Trophy"

    12.2 JoinsData do not always come so nicely aligned for combining using cbind, so they need to bejoined together using a common key. This concept should be familiar to SQL users. Joinsin R are not as flexible as SQL joins, but are still an essential operation in the data analysisprocess.

    The three most commonly used functions for joins are merge in base R, join in plyrand the merging functionality in data.table. Each has pros and cons with some prosoutweighing their respective cons.

    To illustrate these functions I have prepared data originally made available as partof the USAID Open Government initiative.1 The data have been chopped into eightseparate files so that they can be joined together. They are all available in a zip file athttp://jaredlander.com/data/US_Foreign_Aid.zip. These should bedownloaded and unzipped to a folder on our computer. This can be done a number ofways (including using a mouse!) but we show how to download and unzip using R.

    > download.file(url="http://jaredlander.com/data/US_Foreign_Aid.zip",+ destfile="data/ForeignAid.zip")> unzip("data/ForeignAid.zip", exdir="data")

    To load all of these files programmatically, we use a for loop as seen in Section 10.1.We get a list of the files using dir, and then loop through that list assigning each dataset toa name specified using assign.

    > require(stringr)> # first get a list of the files> theFiles ## loop through those files> for(a in theFiles)+ {+ # build a good name to assign to the data+ nameToUse

  • 12.2 Joins 143

    + # read in the csv using read.table+ # file.path is a convenient way to specify a folder and file name+ temp Aid90s00s head(Aid90s00s)

    Country.Name Program.Name1 Afghanistan Child Survival and Health2 Afghanistan Department of Defense Security Assistance3 Afghanistan Development Assistance4 Afghanistan Economic Support Fund/Security Support Assistance5 Afghanistan Food For Education6 Afghanistan Global Health and Child Survival

    FY1990 FY1991 FY1992 FY1993 FY1994 FY1995 FY1996 FY1997 FY19981 NA NA NA NA NA NA NA NA NA2 NA NA NA NA NA NA NA NA NA3 NA NA NA NA NA NA NA NA NA4 NA NA NA 14178135 2769948 NA NA NA NA5 NA NA NA NA NA NA NA NA NA6 NA NA NA NA NA NA NA NA NA

    FY1999 FY2000 FY2001 FY2002 FY2003 FY2004 FY20051 NA NA NA 2586555 56501189 40215304 398179702 NA NA NA 2964313 NA 45635526 1513349083 NA NA 4110478 8762080 54538965 180539337 1935982274 NA NA 61144 31827014 341306822 1025522037 11575301685 NA NA NA NA 3957312 2610006 32544086 NA NA NA NA NA NA NA

    FY2006 FY2007 FY2008 FY20091 40856382 72527069 28397435 NA2 230501318 214505892 495539084 5525249903 212648440 173134034 150529862 36752024 1357750249 1266653993 1400237791 14186885205 386891 NA NA NA6 NA NA 63064912 1764252

  • 144 Chapter 12 Data Reshaping

    The by.x specifies the key column(s) in the left data.frame and by.y does the samefor the right data.frame. The ability to specify different column names for eachdata.frame is the most useful feature of merge. The biggest drawback, however, is thatmerge can be much slower than the alternatives.

    12.2.2 plyr joinReturning to Hadley Wickhams plyr package, we see it includes a join function,which works similarly to merge but is much faster. The biggest drawback, though, is thatthe key column(s) in each table must have the same name. We use the same data usedpreviously to illustrate.

    > require(plyr)> Aid90s00sJoin head(Aid90s00sJoin)

    Country.Name Program.Name1 Afghanistan Child Survival and Health2 Afghanistan Department of Defense Security Assistance3 Afghanistan Development Assistance4 Afghanistan Economic Support Fund/Security Support Assistance5 Afghanistan Food For Education6 Afghanistan Global Health and Child SurvivalFY1990 FY1991 FY1992 FY1993 FY1994 FY1995 FY1996 FY1997 FY1998

    1 NA NA NA NA NA NA NA NA NA2 NA NA NA NA NA NA NA NA NA3 NA NA NA NA NA NA NA NA NA4 NA NA NA 14178135 2769948 NA NA NA NA5 NA NA NA NA NA NA NA NA NA6 NA NA NA NA NA NA NA NA NAFY1999 FY2000 FY2001 FY2002 FY2003 FY2004 FY2005

    1 NA NA NA 2586555 56501189 40215304 398179702 NA NA NA 2964313 NA 45635526 1513349083 NA NA 4110478 8762080 54538965 180539337 1935982274 NA NA 61144 31827014 341306822 1025522037 11575301685 NA NA NA NA 3957312 2610006 32544086 NA NA NA NA NA NA NA

    FY2006 FY2007 FY2008 FY20091 40856382 72527069 28397435 NA2 230501318 214505892 495539084 5525249903 212648440 173134034 150529862 36752024 1357750249 1266653993 1400237791 14186885205 386891 NA NA NA6 NA NA 63064912 1764252

    join has an argument for specifying a left, right, inner or full (outer) join.

  • 12.2 Joins 145

    We have eight data.frames containing foreign assistance data that we would like tocombine into one data.frame without hand coding each join. The best way to do this isto put all the data.frames into a list, and then successively join them together usingReduce.

    > # first figure out the names of the data.frames> frameNames # build an empty list> frameList names(frameList) # add each data.frame into the list> for (a in frameNames)+ {+ frameList[[a]]

  • 146 Chapter 12 Data Reshaping

    FY2007 FY2008 FY20091 72527069 28397435 NA2 214505892 495539084 5525249903 173134034 150529862 36752024 1266653993 1400237791 14186885205 NA NA NA6 NA 63064912 1764252

    > head(frameList[["Aid_00s"]])

    Country.Name Program.Name1 Afghanistan Child Survival and Health2 Afghanistan Department of Defense Security Assistance3 Afghanistan Development Assistance4 Afghanistan Economic Support Fund/Security Support Assistance5 Afghanistan Food For Education6 Afghanistan Global Health and Child Survival

    FY2000 FY2001 FY2002 FY2003 FY2004 FY2005 FY20061 NA NA 2586555 56501189 40215304 39817970 408563822 NA NA 2964313 NA 45635526 151334908 2305013183 NA 4110478 8762080 54538965 180539337 193598227 2126484404 NA 61144 31827014 341306822 1025522037 1157530168 13577502495 NA NA NA 3957312 2610006 3254408 3868916 NA NA NA NA NA NA NA

    FY2007 FY2008 FY20091 72527069 28397435 NA2 214505892 495539084 5525249903 173134034 150529862 36752024 1266653993 1400237791 14186885205 NA NA NA6 NA 63064912 1764252

    > head(frameList[[5]])

    Country.Name Program.Name1 Afghanistan Child Survival and Health2 Afghanistan Department of Defense Security Assistance3 Afghanistan Development Assistance4 Afghanistan Economic Support Fund/Security Support Assistance5 Afghanistan Food For Education6 Afghanistan Global Health and Child Survival

    FY1960 FY1961 FY1962 FY1963 FY1964 FY1965 FY1966 FY1967 FY19681 NA NA NA NA NA NA NA NA NA2 NA NA NA NA NA NA NA NA NA3 NA NA NA NA NA NA NA NA NA

  • 12.2 Joins 147

    4 NA NA 181177853 NA NA NA NA NA NA5 NA NA NA NA NA NA NA NA NA6 NA NA NA NA NA NA NA NA NAFY1969

    1 NA2 NA3 NA4 NA5 NA6 NA

    > head(frameList[["Aid_60s"]])

    Country.Name Program.Name1 Afghanistan Child Survival and Health2 Afghanistan Department of Defense Security Assistance3 Afghanistan Development Assistance4 Afghanistan Economic Support Fund/Security Support Assistance5 Afghanistan Food For Education6 Afghanistan Global Health and Child Survival

    FY1960 FY1961 FY1962 FY1963 FY1964 FY1965 FY1966 FY1967 FY19681 NA NA NA NA NA NA NA NA NA2 NA NA NA NA NA NA NA NA NA3 NA NA NA NA NA NA NA NA NA4 NA NA 181177853 NA NA NA NA NA NA5 NA NA NA NA NA NA NA NA NA6 NA NA NA NA NA NA NA NA NA

    FY19691 NA2 NA3 NA4 NA5 NA6 NA

    Having all the data.frames in a list allows us to iterate through the list, joiningall the elements together (or applying any function to the elements iteratively). Ratherthan using a loop, we use the Reduce function to speed up the operation.

    > allAid dim(allAid)

    [1] 2453 67

  • 148 Chapter 12 Data Reshaping

    > require(useful)> corner(allAid, c = 15)

    Country.Name Program.Name1 Afghanistan Child Survival and Health2 Afghanistan Department of Defense Security Assistance3 Afghanistan Development Assistance4 Afghanistan Economic Support Fund/Security Support Assistance5 Afghanistan Food For EducationFY2000 FY2001 FY2002 FY2003 FY2004 FY2005 FY2006

    1 NA NA 2586555 56501189 40215304 39817970 408563822 NA NA 2964313 NA 45635526 151334908 2305013183 NA 4110478 8762080 54538965 180539337 193598227 2126484404 NA 61144 31827014 341306822 1025522037 1157530168 13577502495 NA NA NA 3957312 2610006 3254408 386891

    FY2007 FY2008 FY2009 FY2010 FY1946 FY19471 72527069 28397435 NA NA NA NA2 214505892 495539084 552524990 316514796 NA NA3 173134034 150529862 3675202 NA NA NA4 1266653993 1400237791 1418688520 2797488331 NA NA5 NA NA NA NA NA NA

    > bottomleft(allAid, c = 15)

    Country.Name Program.Name FY2000 FY2001 FY20022449 Zimbabwe Other State Assistance 1341952 322842 NA2450 Zimbabwe Other USAID Assistance 3033599 8464897 66244082451 Zimbabwe Peace Corps 2140530 1150732 4078342452 Zimbabwe Title I NA NA NA2453 Zimbabwe Title II NA NA 31019776

    FY2003 FY2004 FY2005 FY2006 FY2007 FY2008 FY20092449 NA 318655 44553 883546 1164632 2455592 21930572450 11580999 12805688 10091759 4567577 10627613 11466426 419405002451 NA NA NA NA NA NA NA2452 NA NA NA NA NA NA NA2453 NA NA NA 277468 100053600 180000717 174572685

    FY2010 FY1946 FY19472449 1605765 NA NA2450 30011970 NA NA2451 NA NA NA2452 NA NA NA2453 79545100 NA NA

    Reduce can be a difficult function to grasp, so we illustrate it with a simple example.Lets say we have a vector of the first ten integers, 1:10, and want to sum them (forgetfor a moment that sum(1:10) will work perfectly). We can call Reduce(sum, 1:10),

  • 12.3 reshape2 149

    which will first add 1 and 2. It will then add 3 to that result, then 4 to that result, and soon, resulting in 55.

    Likewise, we passed a list to a function that joins its inputs, which in this case wassimply . . . , meaning that anything could be passed. Using . . . is an advanced trick of Rprogramming that can be difficult to get right. Reduce passed the first two data.framesin the list, which were then joined. That result was then joined to the nextdata.frame and so on until they were all joined together.

    12.2.3 data.table mergeLike many other operations in data.table, joining data requires a different syntax, andpossibly a different way of thinking. To start, we convert two of our foreign aid datasetsdata.frames into data.tables.

    > require(data.table)> dt90 dt00 dt0090 head(Aid_00s)

    Country.Name Program.Name1 Afghanistan Child Survival and Health2 Afghanistan Department of Defense Security Assistance

  • 150 Chapter 12 Data Reshaping

    3 Afghanistan Development Assistance4 Afghanistan Economic Support Fund/Security Support Assistance5 Afghanistan Food For Education6 Afghanistan Global Health and Child SurvivalFY2000 FY2001 FY2002 FY2003 FY2004 FY2005 FY2006

    1 NA NA 2586555 56501189 40215304 39817970 408563822 NA NA 2964313 NA 45635526 151334908 2305013183 NA 4110478 8762080 54538965 180539337 193598227 2126484404 NA 61144 31827014 341306822 1025522037 1157530168 13577502495 NA NA NA 3957312 2610006 3254408 3868916 NA NA NA NA NA NA NA

    FY2007 FY2008 FY20091 72527069 28397435 NA2 214505892 495539084 5525249903 173134034 150529862 36752024 1266653993 1400237791 14186885205 NA NA NA6 NA 63064912 1764252

    We want it set up so that each row represents a single country-program-year entry withthe dollar amount stored in one column. To achieve this we melt the data using meltfrom reshape2.

    > require(reshape2)> melt00 tail(melt00, 10)

    Country.Name24521 Zimbabwe24522 Zimbabwe24523 Zimbabwe24524 Zimbabwe24525 Zimbabwe24526 Zimbabwe24527 Zimbabwe24528 Zimbabwe24529 Zimbabwe24530 Zimbabwe

    Program.Name Year24521 Migration and Refugee Assistance FY200924522 Narcotics Control FY2009

  • 12.3 reshape2 151

    24523 Nonproliferation, Anti-Terrorism, Demining and Related FY200924524 Other Active Grant Programs FY200924525 Other Food Aid Programs FY200924526 Other State Assistance FY200924527 Other USAID Assistance FY200924528 Peace Corps FY200924529 Title I FY200924530 Title II FY2009

    Dollars24521 362738424522 NA24523 NA24524 795103224525 NA24526 219305724527 4194050024528 NA24529 NA24530 174572685

    The id.vars argument specifies which columns uniquely identify a row.After some manipulation of the Year column and aggregating, this is now prime for

    plotting, as shown in Figure 12.1. The plot uses faceting allowing us to quickly see andunderstand the funding for each program over time.

    > require(scales)> # strip the "FY" out of the year column and convert it to numeric> melt00$Year # aggregate the data so we have yearly numbers by program> meltAgg # just keep the first 10 characters of program name> # then it will fit in the plot> meltAgg$Program.Name

    > ggplot(meltAgg, aes(x=Year, y=Dollars)) ++ geom_line(aes(group=Program.Name)) ++ facet_wrap( Program.Name) ++ scale_x_continuous(breaks=seq(from=2000, to=2009, by=2)) ++ theme(axis.text.x=element_text(angle=90, vjust=1, hjust=0)) ++ scale_y_continuous(labels=multiple_format(extra=dollar,+ multiple="B"))

  • 152 Chapter 12 Data Reshaping

    Figure 12.1 Plot of foreign assistance by year for each of the programs.

    12.3.2 dcastNow that we have the foreign aid data melted, we cast it back into the wide format forillustration purposes. The function for this is dcast, and it has trickier arguments thanmelt. The first is the data to be used, in our case melt00. The second argument is aformula where the left side specifies the columns that should remain columns and theright side specifies the columns that should become row names. The third argument is thecolumn (as a character) that holds the values to be populated into the new columnsrepresenting the unique values of the right side of the formula argument.

    > cast00 head(cast00)

    Country.Name Program.Name 20001 Afghanistan Child Survival and Health NA2 Afghanistan Department of Defense Security Assistance NA3 Afghanistan Development Assistance NA

  • 12.4 Conclusion 153

    4 Afghanistan Economic Support Fund/Security Support Assistance NA5 Afghanistan Food For Education NA6 Afghanistan Global Health and Child Survival NA

    2001 2002 2003 2004 2005 20061 NA 2586555 56501189 40215304 39817970 408563822 NA 2964313 NA 45635526 151334908 2305013183 4110478 8762080 54538965 180539337 193598227 2126484404 61144 31827014 341306822 1025522037 1157530168 13577502495 NA NA 3957312 2610006 3254408 3868916 NA NA NA NA NA NA

    2007 2008 20091 72527069 28397435 NA2 214505892 495539084 5525249903 173134034 150529862 36752024 1266653993 1400237791 14186885205 NA NA NA6 NA 63064912 1764252

    12.4 ConclusionGetting the data just right to analyze can be a time-consuming part of our work flow,although it is often inescapable. In this chapter we examined combining multiple datasetsinto one and changing the orientation from column based (wide) to row based (long). Weused plyr, reshape2 and data.table along with base functions to accomplish this.This chapter combined with Chapter 11 covers most of the basics of data munging withan eye to both convenience and speed.

  • This page intentionally left blank

  • General Index

    AAddition

    matrices, 68order of operation, 36vectors, 4445

    Aggregationin data.table package, 135138groups, 120123

    AICC, 320Akaike Information Criterion (AIC),

    255257, 259260@aliases tag, 382all.obs option, 196Ampersands (&) in compound

    tests, 111Analysis of variance (ANOVA)

    alternative to, 214216cross-validation, 259260model comparisons, 254overview, 207210

    And operator in compound tests,111112

    Andersen-Gill analysis, 244245Angle brackets ()

    packages, 375regular expressions, 169

    ANOVA. See Analysis of variance(ANOVA)

    Ansari-Bradley test, 204Appearance options, 2122Appending elements to lists, 68apt-get mechanism, 2Arguments

    C++ code, 385CSV files, 74functions, 49, 100102ifelse, 110package documentation, 380

    Arithmetic mean, 187ARMA model, 315Arrays, 7172Assigning variables, 3637Asterisks (*)

    Markdown, 368

    multiple regression, 228NAMESPACE file, 377vectors, 44

    Attributes for data.frame, 54Author information

    LATEX documents, 360packages, 375

    @author tag, 382Autocompleting code, 1516Autocorrelation, 318Autoregressive (AR) moving averages,

    315322Average linkage methods, 352, 355Axes in nonlinear least squares

    model, 298

    BBack ticks ( ` ) with functions, 49Backslashes (\) in regular expressions,

    166Base graphics, 8384

    boxplots, 8586histograms, 84scatterplots, 8485

    Bayesian Information Criterion (BIC),255257, 259

    Bayesian shrinkage, 290294Beamer mode in LATEX, 369Beginning of lines in regular

    expressions, 167Bell curve, 171Bernoulli distribution, 176Beta distribution, 185186BIC (Bayesian Information Criterion),

    255257, 259Binary files, 7779Binomial distribution, 176181,

    185186Bioconductor, 373BitBucket repositories, 25, 31Books, 394bootstrap, 262265

    Boxplotsggplot2, 9194overview, 8586

    break statement, 115116Breakpoints for splines, 302Building packages, 383384Byte-compilation for packages,

    376ByteCompile field, 376

    CC++ code, 384383

    package compilation, 387390sourceCpp function, 385387

    cache option for knitr chunks, 365Calling functions, 49

    arguments, 100C++, 384conflicts, 33

    Carets () in regular expressions,167

    Case sensitivitycharacters, 40package names, 384regular expressions, 162variable names, 38

    Cauchy distribution, 185186Cauchy priors in Bayesian shrinkage,

    293294Causation vs. correlation, 199Censored data in survival analysis,

    240241Centroid linkage methods, 352, 355Change Install Location option, 9character data, 40Charts, 329chartsnthings site, 393Chi-Squared distribution, 185186Chunks

    LATEX program, 362365Markdown, 368

    Citations in LATEX documents, 366Classification trees, 311

  • 420 General Index

    Clusters, 337hierarchical, 352357K-means algorithm, 337345PAM, 345352registering, 283

    codeautocompleting, 1516C++, 384390indenting, 99running in parallel, 282

    Code Editing options, 21Coefficient plots

    Bayesian shrinkage, 292294Elastic Net, 289290logistic regression, 236model comparisons, 253254multiple regression, 226228,

    230231Poisson regression, 237240residuals, 247, 249VAR, 324325

    Collate field for packages,375376

    Colons (:)vectors, 4445

    Colorboxplots, 92K-means algorithm, 339, 341LATEX documents, 362line graphs, 96PAM, 350351scatterplots, 8890

    Column index for arrays, 71Columns

    cbind and rbind, 141142data.frame, 53, 58data.table, 131133matrices, 6870

    Comma separated files (CSVs),7374

    Command line interface, 1415comment option, 365Comments, 46

    knitr chunks, 365package documentation, 381

    Community edition, 1011Comparing

    models, 253257multiple groups, 207210multiple variables, 192vectors, 46

    Compilation in C++code, 384packages, 387390

    Complete linkage methods,352, 355

    complete.obs option, 196

    Components, installing, 5Compound tests, 111112Comprehensive R Archive Network

    (CRAN), 1, 29, 384Concatenating strings, 155156Conferences, 393Confidence intervals

    ANOVA, 207209, 215216bootstrap, 262, 264265Elastic Net, 277, 279GAM, 310multiple regression, 226one-sample t-tests, 200203paired two-sample t-tests, 207two-sample t-tests, 205206

    Control statements, 105compound tests, 111112if and else, 105108ifelse, 109111switch, 108109

    Converting shapefile objects intodata.frame, 349

    Correlation and covariance, 191200Covariates in simple linear regression,

    211Cox proportional hazards model,

    242244.cpp files, 386CRAN (Comprehensive R Archive

    Network), 1, 29, 384Create Project options, 1617Cross tables, 149Cross-validation

    Elastic Net, 276277overview, 257262

    CSVs (comma separated files), 7374Cubic splines, 302Curly braces ({})

    functions, 99if and else, 106107regular expressions, 166

    DData

    censored, 240241missing. See Missing data

    Data Analysis Using Regression andMultilevel/Hierarchical Models, 50,291, 394

    data folder, 373374data.frames, 5361

    converting shapefile objects into,349

    ddply function, 124, 126Elastic Net, 272

    joins, 145merging, 143144

    Data Gotham conference, 393Data meetups, 391Data munging, 117Data reshaping, 141

    cbind and rbind, 141142joins, 142149reshape2 package, 149153

    Data structures, 53arrays, 7172data.frame, 5361lists, 6168matrices, 6871

    Data types, 38C++ code, 387character, 40dates, 4041logical, 4143matrices, 68numeric, 3839vectors, 4348

    Databases, reading from, 7576Dates, 4041

    LATEX documents, 360packages, 375

    Decision trees, 310312\DeclareGraphics Extensions, 360Default arguments, 101102Degrees of freedom

    ANOVA, 215multiple regression, 225splines, 300t-tests, 201202

    Delimiters in CSV files, 74Delta in model comparisons, 258Dendrograms

    ggplot2, 8788hierarchical clustering, 352normal distribution, 172173

    Density plots, 8788, 184, 207Dependencies in packages, 30Dependent variables in simple linear

    regression, 211Depends field

    C++ code, 386packages, 375

    Description field, 374375DESCRIPTION file, 374377@description tag, 382Destination in installation, 45@details tag, 382dev option for knitr chunks, 365Deviance in model comparisons, 256Diffing process, 318319Dimensions in K-means algorithm,

    339

  • General Index 421

    direction argument, 265Directories

    creating, 18installation, 4names, 18

    Distance between clusters, 352Distance metric for K-means

    algorithm, 337Distributions. See Probability

    distributionsDivision

    matrices, 68order of operation, 36vectors, 4445

    Documentationfunctions, 49packages, 380383

    \documentclass, 360Documents as R resources, 394Dollar signs ($)

    data.frame, 56multiple regression, 225regular expressions, 167

    %dopar% operator, 284dot-dot-dot argument (...), 102Downloading R, 12DSN connections, 75Dynamic Documents with R andknitr, 394

    dzslides format, 369

    Eecho option for knitr chunks, 365EDA (Exploratory data analysis), 83,

    199, 219Elastic Net, 271290Elements of Statistical Learning: DataMining, Inference, and Prediction, 394

    End of lines in regular expressions, 167engine option for knitr chunks, 365Ensemble methods, 312Environment, 1314

    command line interface, 1415RStudio. See RStudio overview

    Equal to symbol (=)if and else, 105variable assignment, 36

    Equality of matrices, 68Esc key in command line

    commands, 15eval option for knitr chunks, 365everything option, 196@examples tag, 382Excel data, 7475

    Exclamation marks (!) in Markdown,368

    Expected value, 188Experimental variables in simple linear

    regression, 211Exploratory data analysis (EDA), 83,

    199, 219Exponential distribution, 185186Exponents, order of operation, 36@export tag, 382Expressions, regular, 161169Extra arguments, 102Extracting

    data from Websites, 8081text, 157161

    FF distribution, 185186F-tests

    ANOVA, 215multiple regression, 225simple linear regression, 214215two-sample, 204

    faceted plots, 8992factor data type, 40factors

    as.numeric with, 160Elastic Net, 273storing, 60vectors, 48

    FALSE valuewith if and else, 105108with logical operators, 4143

    fig.cap option, 365366fig.scap option, 365fig.show option, 365fill argument for histograms, 87Fitted values against residuals plots,

    249251folder structure, 373for loops, 113115Forests, random, 312313formula interface

    aggregation, 120123ANOVA, 208Elastic Net, 272logistic regression, 235236multiple regression, 224, 226, 230scatterplots, 8485simple linear regression, 213

    Formulas for distributions, 185186Frontend field for packages, 374Functions

    arguments, 100102assigned to objects, 99

    C++, 384calling, 49, 100conflicts, 33do.call, 104documentation, 49package documentation, 380return values, 103

    Gg++ compiler, 385Gamma distribution, 185186Gamma linear model, 240GAMs (generalized additive models),

    304310Gap statistic in K-means algorithm,

    343344Garbage collection, 38GARCH (generalized autoregressive

    conditional heteroskedasticity)models, 327336

    Gaussian distribution, 171176gcc compiler, 385General options for RStudio tools,

    2021Generalized additive models (GAMs),

    304310Generalized autoregressive conditional

    heteroskedasticity (GARCH)models, 327336

    Generalized linear models, 233logistic regression, 233237miscellaneous, 240Poisson regression, 237240

    Geometric distribution, 185186Git

    integration with RStudio, 2526selecting, 19

    Git/SVN option, 25GitHub repositories, 25

    for bugs, 392package installation from, 31, 383README files, 380

    Graphics, 83base, 8386ggplot2, 8697

    Greater than symbols (>)if and else, 105variable assignment, 37

    Groups, 117aggregation, 120123apply family, 117120comparing, 207210data.table package, 129138plyr package, 124129

  • 422 General Index

    HHadoop framework, 117Hartigans Rule, 340342Hash symbols (#)

    comments, 46Markdown, 368package documentation, 381pandoc, 369

    header command in pandoc, 369Heatmaps, 193Hello, World! program, 99100Help pages in package documentation,

    381Hierarchical clustering, 352357Histograms, 84

    bootstrap, 264ggplot2, 8788multiple regression, 219Poisson regression, 238residuals, 253

    Hotspot locations, 297298HTML tables, extracting data from,

    8081Hypergeometric distribution, 185186Hypothesis tests in t-tests, 201203

    IIDEs (Integrated Development

    Environments), 1314if else statements, 105108Images in LATEX documents, 360@import tag, 382Imports field for packages, 375include option for knitr chunks, 365Indenting code, 99Independent variables in simple linear

    regression, 211Indexes

    arrays, 71data.table, 129LATEX documents, 360lists, 66

    Indicator variablesdata.frame, 60Elastic Net, 273, 289290multiple regression, 225PAM, 345

    Inferencesensemble methods, 312multiple regression, 216

    @inheritParams tag, 382Innovation distribution, 330Input variables in simple linear

    regression, 211

    inst folder, 373374Install dependencies option, 30install.packages command, 31Install Packages option, 30installing packages, 2932,

    383384installing R, 2

    on Linux, 10on Mac OS X, 810on Windows, 27

    integer type, 3839Integers in regular expressions, 166Integrated Development

    Environments (IDEs), 1314Intel Matrix Kernel Library, 10Interactivity, 13Intercepts

    multiple regression, 216simple linear regression, 212213

    Interquartile Range (IQR), 8586Introduction to R, 394Inverse gaussian linear model, 240IQR (Interquartile Range), 8586Italics in Markdown, 367Iteration with loops, 113

    controlling, 115116for, 113115while, 115

    JJoining strings, 155156Joins, 142143

    data.table, 149merge, 143144plyr package, 144149

    Joint Statistical Meetings, 393

    Kk-fold cross-validation, 257258K-means algorithm, 337345K-medoids, 345352key columns with join, 144keys for data.table package, 133135knots for splines, 302

    LL1 penalty, 271L2 penalty, 271Lags in autoregressive moving average,

    318319lambda functions, 279282, 285289Language selection, 3

    lasso in Elastic Net, 271, 276, 279,282

    LATEX programinstalling, 359knitr, 362367overview, 360362

    Leave-one-out cross-validation, 258Legends in scatterplots, 89Length

    characters, 40lists, 6667vectors, 4546

    Less than symbols (

  • General Index 423

    Logistic distribution, 185186Logistic regression, 233237Loops, 113

    controlling, 115116for, 113115while, 115

    MMac

    C++ compilers, 385downloading R, 1installation on, 810

    Machine learning, 304Machine Learning for Hackers, 394Machine Learning meetups, 391Maintainer field for packages, 375makeCluster function, 283\makeindex, 360Makevars file, 386389Makevars.win file, 386389man folder, 373374MapReduce paradigm, 117Maps

    heatmaps, 193PAM, 350351

    Markdown tool, 367369Math, 3536Matrices

    with apply, 117118with cor, 192Elastic Net, 272overview, 6871VAR, 324

    Matrix Kernel Library (MKL), 10.md files, 369371Mean

    ANOVA, 209bootstrap, 262calculating, 187188normal distribution, 171Poisson regression, 237238t-tests, 203, 205various statistical distributions,

    185186Mean squared error in

    cross-validation, 258Measured variables in simple linear

    regression, 211Meetups, 391392Memory in 64-bit versions, 2Merging

    data.frame, 143144data.table, 149

    Minitab format, 77

    Minus signs (-) in variable assignment,3637

    Missing data, 50apply, 118cor, 195196cov, 199mean, 188NA, 50NULL, 51PAM, 346

    MKL (Matrix Kernel Library), 10Model diagnostics, 247

    bootstrap, 262265comparing models, 253257cross-validation, 257262residuals, 247253stepwise variable selection,

    265269Moving average (MA) model, 315Moving averages, autoregressive,

    315322Multicollinearity in Elastic Net, 273Multidimensional scaling in K-means

    algorithm, 339Multinomial distribution, 185186Multinomial regression, 240Multiple group comparisons, 207210Multiple imputation, 50Multiple regression, 216232Multiple time series in VAR, 322327Multiplication

    matrices, 6971order of operation, 36vectors, 4445

    Multivariate time series in VAR, 322

    Nna.or.complete option, 196na.rm argument

    cor, 195196mean, 188standard deviation, 189

    NA valuewith mean, 188overview, 50

    Name-value pairs for lists, 64Names

    arguments, 49, 100data.frame columns, 58directories, 18lists, 6364packages, 384variables, 3738vectors, 47

    names function for data.frame, 5455

    NAMESPACE file, 377379Natural cubic splines, 302Negative binomial distribution,

    185186Nested indexing of list elements, 66NEWS file, 379Nodes in decision trees, 311312Noise

    autoregressive moving average,315

    VAR, 324Nonlinear models, 297

    decision trees, 310312generalized additive model,

    304310nonlinear least squares model,

    297299random forests, 312313splines, 300304

    Nonparametric Ansari-Bradley test,204

    Normal distribution, 171176Not equal symbols (!=) with if and

    else, 105nstart argument, 339Null hypotheses

    one-sample t-tests, 201202paired two-sample t-tests, 207

    NULL value, 5051Numbers in regular expressions,

    165169numeric data, 3839

    OObjects, functions assigned to, 99Octave format, 771/mu2 function, 240One-sample t-tests, 200203Operations

    order, 36vectors, 4448

    Or operators in compound tests,111112

    Order of operations, 36Ordered factors, 48out.width option, 365Outcome variables in simple linear

    regression, 211Outliers in boxplots, 86Overdispersion in Poisson regression,

    238Overfitting, 312

  • 424 General Index

    Pp-values

    ANOVA, 208multiple regression, 225t-tests, 200203

    Package field in DESCRIPTION file,374377

    Packages, 29, 373building, 33C++ code, 384390checking and building, 383384compiling, 387390DESCRIPTION file, 374377documentation, 380383files overview, 373374folder structure, 373installing, 2932, 383384loading, 3233miscellaneous files, 379380NAMESPACE file, 377379options, 23submitting to CRAN, 384uninstalling, 32unloading, 33

    Packages pane, 2930Paired two-sample t-tests,

    206207pairwise.complete option, 197PAM (Partitioning Around Medoids),

    345352pandoc utility, 369371Pane Layout options, 2122Parallel computing, 282284@param tag, 381382Parentheses ()

    arguments, 100compound tests, 111expressions, 63functions, 99if and else, 105order of operation, 36regular expressions, 163

    Partial autocorrelation, 318319Partitioning Around Medoids (PAM),

    345352Passwords in installation, 9Patterns, searching for, 161169PDF files, 362, 369Percent symbol (%) in pandoc,

    369Periods (.)

    uses, 99variable names, 37

    Plotscoefficient. See Coefficient plotsfaceted, 8992

    Q-Q, 249, 252residuals, 250251scatterplots. See Scatterplotssilhouette, 346348

    Plus signs (+) in regular expressions,169

    Poisson distribution, 182184Poisson regression, 237240POSIXct data type, 40Pound symbols (#)

    comments, 46Markdown, 368package documentation, 381pandoc, 369

    Prediction in GARCH models,335

    Predictive Analytics meetups, 391Predictors

    decision trees, 310311Elastic Net, 272generalized additive models, 304logistic regression, 233multiple regression, 216217simple linear regression, 211, 213splines, 302303

    Priors, 290, 293294Probability distributions, 171

    binomial, 176181miscellaneous, 185186normal, 171176Poisson, 182184

    Program Files\R directory, 4Projects in RStudio, 1619prompt option for knitr chunks,

    365

    QQ-Q plots, 249, 252Quantiles

    binomial distribution, 181multiple regression, 225normal distribution, 175176summary function, 190

    Quasibinomial linear model, 240Quasipoisson family, 239Question marks (?)

    with functions, 49regular expressions, 169

    Quotes () in CSV files, 74

    RR-Bloggers site, 393R CMD commands, 383R Enthusiasts site, 393

    R folder, 373374R in Finance conference, 393R Inferno, 394R Productivity Environment (RPE),

    2627Raise to power function, 45Random numbers

    binomial distribution, 176normal distribution, 171172

    Random starts in K-means algorithm,339

    Rcmdr interface, 14.Rd files, 380, 383RData files

    creating, 77loading, 162

    Readability of functions, 99Reading data, 73

    binary files, 7779CSVs, 7374from databases, 7576Excel, 7475included with R, 7980from statistical tools, 77

    README files, 380Real-life resources, 391

    books, 394conferences, 393documents, 394meetups, 391392Stack Overflow, 392Twitter, 393Web sites, 393

    Reference Classes system, 377Registering clusters, 283Regression

    generalized additive models, 304logistic, 233237multiple, 216232Poisson, 237240simple linear, 211216survival analysis, 240245

    Regression to the mean, 211Regression trees, 310Regular expressions, 161169Regularization and shrinkage, 271

    Bayesian shrinkage, 290294Elastic Net, 271290

    Relationshipscorrelation and covariance,

    191200multiple regression, 216232simple linear regression,

    211216Removing variables, 3738Repeating command line

    commands, 15

  • General Index 425

    Reshaping data, 141cbind and rbind, 141142joins, 142149reshape2 package, 149153

    Residual standard error in least squaresmodel, 298

    Residual sum of squares (RSS),254255

    Residuals, 247253Resources. See Real-life resourcesResponses

    decision trees, 310logistic regression, 233multiple regression, 216217,

    219, 225Poisson regression, 237residuals, 247simple linear regression,

    211213@return tag, 381382Return values in functions, 103Revolution Analytics site, 393Ridge in Elastic Net, 271, 279.Rmd files, 369.Rnw files, 362Rows

    in arrays, 71bootstrap, 262cbind and rbind, 141142data.frame, 53data.table, 131with mapply, 120matrices, 6870

    RPE (R Productivity Environment),2627

    RSS (residual sum of squares),254255

    RStudio overview, 1516Git integration, 2526projects, 1619tools, 2025

    RTools, 385Run as Administrator option, 3Running code in parallel, 283

    SS3 system, 377@S3method tag, 382S4 system, 377s5 slide show format, 369SAS format, 77Scatterplots, 8485

    correlation, 192generalized additive models, 307ggplot2, 8891

    multiple regression, 220224splines, 303

    scope argument, 265Scraping web data, 81Seamless R and C++ Integration withRcpp, 394

    Searches, regular expressions for,161169

    Secret weapon, 293Sections in LATEX documents, 361@seealso tag, 382Seeds for K-means algorithm, 338Semicolons (;) for functions, 100sep argument, 155Shapefile objects, converting into

    data.frame, 349Shapiro-Wilk normality test, 204Shortcuts, keyboard, 15Shrinkage

    Bayesian, 290294Elastic Net, 271

    Silhouette plots, 346348Simple linear regression

    ANOVA alternative, 214216overview, 211214

    Single linkage methods, 352, 35564-bit vs. 32-bit R, 2Size

    binomial distributions,176179

    lists, 65sample, 187

    Slashes (/) in C++ code, 385386Slide show formats, 369slideous slide show format, 369slidy format, 369, 371Slope in simple linear regression,

    212213Small multiples, 89Smoothing functions in GAM, 304Smoothing splines, 300301Software license, 3Spelling options, 2324Splines, 300304Split-apply-combine method, 117,

    124SPSS format, 77Square brackets ([])

    arrays, 71data.frame, 56, 58lists, 65Markdown, 368vectors, 47

    Squared error loss in nonlinear leastsquares model, 297

    src folder, 373374, 387Stack Overflow source, 392

    Standard deviationmissing data, 189normal distribution, 171simple linear regression, 213t-tests, 201202, 205

    Standard errorElastic Net, 279, 289least squares model, 298multiple regression, 225226simple linear regression, 213216t-tests, 202

    start menu shortcuts, 6startup options, 5Stata format, 77Stationarity, 318Statistical graphics, 83

    base, 8386ggplot2, 8697

    Statistical tools, reading data from, 77Stepwise variable selection, 265269Strings, 155

    joining, 155156regular expressions, 161169sprintf, 156157text extraction, 157161

    stringsAsFactors argument, 75Submitting packages to CRAN,

    384Subtraction

    matrices, 68order of operation, 36vectors, 4445

    Suggests field in packages, 375376Summary statistics, 187191Survival analysis, 240245SVN repository, 17, 19, 25switch statements, 108109Systat format, 77

    Tt distribution

    functions and formulas, 185186GARCH models, 330

    t-statistic, 201202, 225t-tests, 200

    multiple regression, 225one-sample, 200203paired two-sample, 206207two-sample, 203206

    Tab key for autocompletingcode, 15

    Tables of contents in pandoc, 371Tags for roxygen2, 381382Tensor products, 308test folder, 374

  • 426 General Index

    Textextracting, 157161LATEX documents, 362regular expressions, 167169

    Themes in ggplot2, 969732-bit vs. 64-bit R, 2Tildes () in aggregation, 120Time series and autocorrelation, 315

    autoregressive moving average,315322

    GARCH models, 327336VAR, 322327

    Title field, 374375@title tag, 382Titles

    help files, 381LATEX documents, 360packages, 374375slides, 369

    Transposing matrices, 70Trees

    decision, 310312hierarchical clustering, 354

    TRUE valuewith if and else, 105108with logical operators, 4143

    Twitter resource, 393Two-sample t-tests, 203206Type field for packages, 374375Types. See Data types

    UUnderscores ( )

    Markdown, 367variable names, 37

    Unequal length vectors, 46Uniform (Continuous) distribution,

    185186Uninstalling packages, 32Unloading packages, 33@useDynLib tag, 382useful package, 273, 341

    UseMethod command, 377useR! conference, 393User installation options, 9

    VVAR (vector autoregressive) model,

    322327Variables, 36

    assigning, 3637names, 37relationships between, 211216removing, 3738stepwise selection, 265269

    Variance, 189ANOVA, 207210GARCH models, 327Poisson regression, 238t-tests, 203various statistical distributions,

    185186Vector autoregressive (VAR) model,

    322327Vectorized arguments with ifelse, 110Vectors, 4344

    data.frame, 56factors, 48in for loops, 113114multiple regression, 217multiplication, 4445operations, 4448paste, 155156sprintf, 157

    Version control, 19Version field for packages, 375version number, saving, 67Versions, 2Vertical lines (|) in compound tests,

    111vim mode, 21Violins plots, 9194Volatility in GARCH models, 330

    WWeakly informative priors, 290Websites

    extracting data from, 8081R resources, 393

    Weibull distribution, 185186Welch two-sample t-tests, 203while loops, 115White noise

    autoregressive moving average,315

    VAR, 324WiFi hotspot locations, 297298Windows

    C++ compilers, 385downloading R, 1installation on, 27

    Windows Live Writer, 15within-cluster dissimilarity, 343Wrapper functions, 386Writing R Extensions, 394

    XX-axes in nonlinear least squares

    model, 298Xcode, 385

    YY-axes in nonlinear least squares

    model, 298y-intercepts

    multiple regression, 216simple linear regression, 212213

    ZZero Intelligence Agents site, 393zypper mechanism, 2

    ContentsForewordPrefaceAcknowledgmentsAbout the Author12 Data Reshaping12.1 cbind and rbind12.2 Joins12.3 reshape212.4 Conclusion

    General IndexABCDEFGHIJKLMNOPQRSTUVWXYZ