Book Image

R Data Analysis Cookbook

By : Viswa Viswanathan, Shanthi Viswanathan
Book Image

R Data Analysis Cookbook

By: Viswa Viswanathan, Shanthi Viswanathan

Overview of this book

<p>Data analytics with R has emerged as a very important focus for organizations of all kinds. R enables even those with only an intuitive grasp of the underlying concepts, without a deep mathematical background, to unleash powerful and detailed examinations of their data.</p> <p>This book empowers you by showing you ways to use R to generate professional analysis reports. It provides examples for various important analysis and machine-learning tasks that you can try out with associated and readily available data. The book also teaches you to quickly adapt the example code for your own needs and save yourself the time needed to construct code from scratch.</p>
Table of Contents (18 chapters)
R Data Analysis Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Since the release of version 1.0 in 2000, R's popularity as an environment for statistical computing, data analytics, and graphing has grown exponentially. People who have been using spreadsheets and need to perform things that spreadsheet packages cannot readily do, or need to handle larger data volumes than what a spreadsheet program can comfortably handle, are looking to R. Analogously, people using powerful commercial analytics packages are also intrigued by this free and powerful option. As a result, a large number of people are now looking to quickly get things done in R.

Being an extensible system, R's functionality is divided across numerous packages with each one exposing large numbers of functions. Even experienced users cannot expect to remember all the details off the top of their head. This cookbook, aimed at users who are already exposed to the fundamentals of R, provides ready recipes to perform many important data analytics tasks. Instead of having to search the Web or delve into numerous books when faced with a specific task, people can find the appropriate recipe and get going in a matter of minutes.

What this book covers

Chapter 1, Acquire and Prepare the Ingredients – Your Data, covers the activities that precede the actual data analysis task. It provides recipes to read data from different input file formats. Furthermore, prior to actually analyzing the data, we perform several preparatory and data cleansing steps and the chapter also provides recipes for these: handling missing values and duplicates, scaling or standardizing values, converting between numerical and categorical variables, and creating dummy variables.

Chapter 2, What's in There? – Exploratory Data Analysis, talks about several activities that analysts typically use to understand their data before zeroing in on specific techniques to apply. The chapter presents recipes to summarize data, split data, extract subsets, and create random data partitions, as well as several recipes to plot data to reveal underlying patters using standard plots as well as the lattice and ggplot2 packages.

Chapter 3, Where Does It Belong? – Classification, covers recipes for applying classification techniques. It includes classification trees, random forests, support vector machines, Naïve Bayes, K-nearest neighbors, neural networks, linear and quadratic discriminant analysis, and logistic regression.

Chapter 4, Give Me a Number – Regression, is about recipes for regression techniques. It includes K-nearest neighbors, linear regression, regression trees, random forests, and neural networks.

Chapter 5, Can You Simplify That? – Data Reduction Techniques, covers recipes for data reduction. It presents cluster analysis through K-means and hierarchical clustering. It also covers principal component analysis.

Chapter 6, Lessons from History – Time Series Analysis, covers recipes to work with date and date/time objects, create and plot time-series objects, decompose, filter and smooth time series, and perform ARIMA analysis.

Chapter 7, It's All About Your Connections – Social Network Analysis, is about social networks. It includes recipes to acquire social network data using public APIs, create and plot social networks, and compute important network metrics.

Chapter 8, Put Your Best Foot Forward – Document and Present Your Analysis, considers techniques to disseminate your analysis. It includes recipes to use R markdown and KnitR to generate reports, to use shiny to create interactive applications that enable your audience to directly interact with the data, and to create presentations with RPres.

Chapter 9, Work Smarter, Not Harder – Efficient and Elegant R Code, addresses the issue of writing efficient and elegant R code in the context of handling large data. It covers recipes to use the apply family of functions, to use the plyr package, and to use data tables to slice and dice data.

Chapter 10, Where in the World? – Geospatial Analysis, covers the topic of exploiting R's powerful features to handle spatial data. It covers recipes to use RGoogleMaps to get GoogleMaps and to superimpose our own data on them, to import ESRI shape files into R and plot them, to import maps from the maps package, and to use the sp package to create and plot spatial data frame objects.

Chapter 11, Playing Nice – Connecting to Other Systems, covers the topic of interconnecting R to other systems. It includes recipes for interconnecting R with Java, Excel and with relational and NoSQL databases (MySQL and MongoDB respectively).

What you need for this book

We have tested all the code in this book for R versions 3.0.2 (Frisbee Sailing) and 3.1.0 (Spring Dance). When you install or load some of the packages, you may get a warning message to the effect that the code was compiled for a different version, but this will not impact any of the code in this book.

Who this book is for

This book is ideal for those who are already exposed to R, but have not yet used it extensively for data analytics and are seeking to get up and running quickly for analytics tasks. This book will help people who aspire to enhance their skills in any of the following ways:

  • perform advanced analyses and create informative and professional charts

  • become proficient in acquiring data from many sources

  • apply supervised and unsupervised data mining techniques

  • use R's features to present analyses professionally

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The read.csv() function creates a data frame from the data in the .csv file."

A block of code is set as follows:

> names(auto)

[1] "No"           "mpg"          "cylinders"
[4] "displacement" "horsepower"   "weight"
[7] "acceleration" "model_year"   "car_name"

Any command-line input or output is written as follows:

export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server
export MAKEFLAGS="LDFLAGS=-Wl,-rpath $JAVA_HOME/lib/server"

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code and data

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

About the data files used in this book

We have generated many of the data files used in this book. We have also used some publicly available data sets. The table below lists the sources of these public data sets. We downloaded most of the public data sets from the University of California at Irvine (UCI) Machine Learning Repository at http://archive.ics.uci.edu/ml/. In the table below we have indicated this as "Downloaded from UCI-MLR."

Data file name

Source

auto-mpg.csv

Quinlan, R. Combining Instance-Based and Model-Based Learning, Machine Learning Proceedings on the Tenth International Conference 1993, 236-243, held at University of Massachusetts, Amherst published by Morgan Kaufmann.

(Downloaded from UCI-MLR).

BostonHousing.csv

D. Harrison and D.L. Rubinfeld, Hedonic prices and the demand for clean air, Journal for Environmental Economics a Management, pages 81–102, 1978. (Downloaded from UCI-MLR)

daily-bike-rentals.csv

Fanaee-T, Hadi, and Gama, Joao, Event labeling combining ensemble detectors and background knowledge, Progress in Artificial Intelligence (2013): pp. 1-15, Springer Berlin Heidelberg. (Downloaded from UCI-MLR)

banknote-authentication.csv

  • Owner of database: Volker Lohweg, University of Applied Sciences, Ostwestfalen-Lippe

  • Donor of database: Helene Darksen, University of Applied Sciences, Ostwestfalen-Lippe

(Downloaded from UCI-MLR)

education.csv

Robust Regression and Outlier Detection, P. J. Rouseeuw and A. M. Leroy, Wiley, 1987.

(Downloaded from UCI-MLR)

walmart.csv

walmart-monthly.csv

Downloaded from Yahoo! Finance

prices.csv

Downloaded from the US Bureau of Labor Statistics.

infy.csv, infy-monthly.csv

Downloaded from Yahoo! Finance.

nj-wages.csv

NJ Department of Education's website and

http://federalgovernmentzipcodes.us.

nj-county-data.csv

Adapted from Wikipedia:

http://en.wikipedia.org/wiki/List_of_counties_in_New_Jersey

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/9065OS_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.