Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
R Statistics Cookbook

You're reading from  R Statistics Cookbook

Product type Book
Published in Mar 2019
Publisher Packt
ISBN-13 9781789802566
Pages 448 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Francisco Juretig Francisco Juretig
Profile icon Francisco Juretig

Table of Contents (12) Chapters

Preface Getting Started with R and Statistics Univariate and Multivariate Tests for Equality of Means Linear Regression Bayesian Regression Nonparametric Methods Robust Methods Time Series Analysis Mixed Effects Models Predictive Models Using the Caret Package Bayesian Networks and Hidden Markov Models Other Books You May Enjoy

Preface

R is a popular programming language for developing statistical software. This book will be a useful guide to solving common and not-so-common challenges in statistics. With this book, you'll be equipped to confidently perform essential statistical procedures across your organization with the help of cutting-edge statistical tools.

You'll start by implementing data modeling, data analysis, and machine learning to solve real-world problems. You'll then understand how to work with nonparametric methods, mixed effects models, and hidden Markov models. This book contains recipes that will guide you in performing univariate and multivariate hypothesis tests, several regression techniques, and using robust techniques to minimize the impact of outliers in data.You'll also learn how to use the caret package for performing machine learning in R. Furthermore, this book will help you understand how to interpret charts and plots to get insights for better decision making.

By the end of this book, you will be able to apply your skills to statistical computations using R 3.5. You will also become well-versed with a wide array of statistical techniques in R that are extensively used in the data science industry.

Who this book is for

If you are a quantitative researcher, statistician, data analyst, or data scientist looking to tackle common and not-so-common challenges in statistics, then this book is what you need! A solid understanding of R programming and a basic understanding of statistics and linear algebra.

What this book covers

Chapter 1, Getting Started with R and Statistics, reviews a variety of techniques in R for performing data processing, data analysis, and plotting. We will also explain how to work with some basic statistical techniques, such as sampling, maximum likelihood estimation, and random number generation. In addition, we will present some useful coding techniques, such as C++ functions using Rcpp, and R6Classes. The former will allow us to add high-performance compiled code, whereas the latter will allow us to perform object-oriented programming in R.

Chapter 2, Univariate and Multivariate Tests for Equality of Means, explains how to answer the most basic statistical question: do two (or possibly more) populations have the same mean? This arises when we want to evaluate whether certain treatment/policy is effective compared to a baseline effect. This can naturally be extended to multiple groups, and the technique used for this is called Analysis of Variance (ANOVA). ANOVA can itself be extended to accommodate multiple effects; for example, testing whether the background color of a website and the font style drive sales up. This is known as two-way ANOVA, and leads to additional complications: not only do we have multiple effects to estimate, but also we could have interaction effects happening between these two effects (for example, a certain background color could be effective when used in conjunction with a specific font type). ANOVA can also be extended in other dimensions, such as adding random effects (effects that originate from a large population and where we don't want to estimate a parameter for each one of them), or repeated measures for each observation.

A different problem arises when we have multiple variables instead of a single variable that we want to measure across (across two or more groups). In this case, we are generalizing the t-test and ANOVA to a multi-dimensional case; for the former (two groups, the technique that we use is called Hotelling's t-test, and for the latter (more than two groups), the technique is MANOVA (multiple ANOVA). We will review how to use all these techniques in R.

Chapter 3, Linear Regression, deals with the most important tool in statistics. It can be used in almost any situation where we want to predict a numeric variable in terms of lots of independent ones. As its name implies, the assumption is that there is a linear relationship between the covariates and the target. In this chapter, we will review how to formulate these models with a special focus on ordinary least squares (the most widely used algorithm for linear regression).

Chapter 4, Bayesian Regression, explains how to work with regression in a Bayesian context. Hitherto, we have assumed that there are some fixed parameters behind the data generation process (for t-tests, we assume that there are fixed means for each group), and because of sample variability, we will observe minor deviations from them. The Bayesian approach is radically different, and founded on a different methodological and epistemological foundation. The idea is that coefficients are not fixed quantities that we want to draw inferences upon, but random variables themselves.

The idea is that given a prior density (prior belief that we have) for each coefficient, we want to augment these priors using the data, in order to arrive at a posterior density. For example, if we think a person always arrives on time (this would be a prior), and we observe that this person arrived late on 8 out of 10 occasions, we should update our initial expectation accordingly. Unfortunately, Bayesian models do not generate closed formed expressions (in most practical cases), so they can't be solved easily. We will need to use sophisticated techniques to estimate these posterior densities: the tool that is used most frequently for this purpose is MCMC (Markov chain Monte Carlo). We will review how to formulate models using the best packages available: JAGS and STAN.

Chapter 5, Nonparametric Methods, explains how classical methods rely on the assumption that there is an underlying distribution (usually a Gaussian one), and derive tests for each case. For instance, the underlying assumption in t-tests is that the data originates from two Gaussian populations with the same variance. In general, these assumptions do make sense, and even when they are not met, in large samples, the violations to those assumptions become less relevant: (for example, the t-test works well for large sample even when the normality assumption is violated). But what can we do when we are working with small samples, or cases where normality is absolutely needed? Non-parametric methods are designed to work with no distributional assumptions by using a series of smart tricks that depend on each particular case. When the data follows the same distribution that we need (for example normality for t-tests), they work almost as well as the parametric ones, and when the data does not follow that distribution, they still work anyway. We will use a variety of non-parametric tools for regression, ANOVA, and many more.

Chapter 6, Robust Methods, explains why classical methods don't work well in the presence of outliers. On the other hand, robust methods are designed to intelligently flag abnormal observations, and estimate the appropriate coefficients in the presence of contamination. In this chapter, we will review some of the most frequently used robust techniques for regression, classification, ANOVA, and clustering.

Chapter 7, Time Series Analysis, describes how to work with time series (sequences of observations indexed by time). Although there are several ways of modeling them, the most widely used framework is called ARIMA. The idea is to decompose the series into the sum of deterministic and stochastic components in such a way that the past is used to predict the future of the series. It has been established that these techniques work really well with actual data but, unfortunately, they do require a lot of manual work. In this chapter, we will present several ARIMA techniques, demonstrating how to extend them to multivariate data, how to impute missing values on the series, how to detect outliers, and how to use several automatic packages that build the best model for us.

Chapter 8, Mixed Effects Models, introduces mixed effects models. These models arise when we mix fixed and random effects. Fixed effects (the ones we have used so far except for Chapter 4, Bayesian Regression) are treated as fixed parameters that are estimated. For example, if we model the sales of a product in terms of a particular month, each month will have a distinct parameter (this would be a fixed effect). On the other hand, if we were measuring whether a drug is useful for certain patients, and we had multiple observations per patient, we might want to keep a patient effect but not a coefficient for each patient. If we had 2,000 patients, those coefficients would be unmanageable and at the same time, would be introducing a lot of imprecision to our model. A neater approach would be to treat the patient effect as random: we would assume that each patient receives a random shock, and all observations belonging to the same patient will be correlated.

In this chapter, we will work with these models using the lme4 and lmer packages, and we will extend these models to non-linear mixed effects models (when the response is non-linear). The main problem for these models (both linear and non-linear) is that the degrees of freedom are unknown, rendering the usual tests useless.

Chapter 9, Predictive Models Using the Caret Package, describes how to use the caret package, which is the fundamental workhorse for (some of them have already been presented in previous chapters). It provides a consistent syntax and a unified approach for building a variety of models. In addition, it has great tools for performing preprocessing and feature selection. In this chapter, we present several models in caret, such as random forests, gradient boosting, and LASSO.

Chapter 10, Bayesian Networks and Hidden Markov Models, describes how, in some cases, we might want to model a network of relationships in such a way that we can understand how the variables are connected. For example, the office location might make employees happier, and also make them arrive earlier to work: the two combined effects might make them perform better. If they perform better, they will receive better bonuses; actually, the bonuses will be dependent on those two variables directly, and also on the office location indirectly. Bayesian networks allow us to perform complex network modeling, and the main tool used for this is the bnlearn package. Another advanced statistical tool is hidden Markov models: they allow us to estimate the state of unobserved variables by using a very complex computational machinery. In this chapter we will work with two examples using Hidden Markov Models.

To get the most out of this book

Users should have some familiarity with statistics and programming. Some general knowledge of probability, regression, and data analysis is recommended.

R is required for this book, and RStudio is highly recommended. All the packages used throughout this book can be installed following the instructions for each recipe.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/R_Statistics_Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Import the ggplot2 and reshape libraries."

A block of code is set as follows:

library(bbmle)
N <- 1000
xx <- rgamma(N, shape=20,rate=2)

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

> install.packages("tscount")

Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "The cumulative density function (CDF) returns the cumulative probability mass for each value of X."

Warnings or important notes appear like this.
Tips and tricks appear like this.

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, 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 you more knowledgeable about the recipe.

See also

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

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

lock icon The rest of the chapter is locked
Next Chapter arrow right
You have been reading a chapter from
R Statistics Cookbook
Published in: Mar 2019 Publisher: Packt ISBN-13: 9781789802566
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}