Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
R Statistics Cookbook
R Statistics Cookbook

R Statistics Cookbook: Over 100 recipes for performing complex statistical operations with R 3.5

eBook
€8.99 €16.99
Paperback
€20.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

R Statistics Cookbook

Univariate and Multivariate Tests for Equality of Means

We will cover the following recipes in this chapter:

  • The univariate t-test
  • The Fisher-Behrens problem
  • Paired t-test
  • Calculating ANOVA sum of squares and F tests
  • Two-way ANOVA
  • Type I, Type II, and Type III sum of Squares
  • Random effects
  • Repeated measures
  • Multivariate t-test
  • MANOVA

Introduction

One of the most basic problems that we need to solve in statistics is comparing the means from two (or more) groups. It's tempting to just take those means and compare them while ignoring all of the statistical theory. The central problem is that, if we did that, we would not have a reference level that we can compare that difference against (we wouldn't know whether that difference is large or small).

The statistical approach provides a foundation for this comparison, providing us with critical values that we should do this comparison against. In essence, this comparison depends on the variability in the data (the noisier the data is, the greater this difference needs to be to be deemed significative) and on how certain we want to be that a non-significative difference is considered significative (this is called the value, which is also known as type...

The univariate t-test

The t-test is a basic tool in statistics used to compare the means of two samples. Its most strict version assumes that both samples are distributed according to a Gaussian distribution and have the same (unknown) variance.

In statistics, we refer to the null hypothesis as the hypothesis we want to test, and the alternative hypothesis as the one we use when the null is rejected. They are usually referred to as H0 and H1.

The t-test tests the equality of means from two populations, and it can be formulated in three possible ways:

  • H0: The means of the two populations are the same versus H1: in that the means of the populations are different.
  • H0: The mean of population 1 is greater or equal than the one from population 2 versus H1: the mean of population 1 is smaller than population 2.
  • H0: The mean of population 1 is smaller or equal than the one from population...

The Fisher-Behrens problem

The original t-test is designed for two Gaussian samples with equal unknown variance. When the variances are not the same, the degrees of freedom for the test are not the usual ones (the equality of variances is known as homocedasticity). Consequently, we can't calculate the p-values and, by extension, we can't test our hypothesis. This is known as the Fisher-Behrens problem.

It has been found that the t-test (with its usual degrees of freedom) can still be used with moderate departures from the homocedasticity (equality of variances) assumption. But this does not take us very far: translating the idea that the test is robust to departures from this assumption is difficult to operationalize (the impact depends on the sample sizes, the relative differences in the variances, and so on).

If the sample is large enough, we can ignore the problem...

Paired t-test

There is a variant of the t-test that can be used when the data is paired (this usually happens when we have two observations for each subject). For example, this may occur if we use a specific program and we want to evaluate its effectiveness by taking one measurement before and after the program is executed. The advantage of this is that the difference (after-before) truly represents the impact of the program that we are evaluating (we are making sure that any possible external variable has been filtered out).

This is much better than having just two samples, where one is taken before the policy was executed and another is one (with different individuals) taken after. If there are differences between those two samples, they might bring trouble to our test. For example, let's suppose that sample1 contains individuals that perform better at work, while sample2...

Calculating ANOVA sum of squares and F tests

Analysis of Variance (ANOVA) is a technique that's used for analyzing the differences between the means from several groups (it is essentially an extension of the t-test to multiple samples). It is deeply tied to a statistical discipline known as experimental design, a discipline that analyzes how to collect the data, how to layout an experiment, and which variables should be measured.

In statistics, correlation is not the same as causality: two phenomena might be correlated, but deducing causality out of that correlation is usually wrong. For example, most animals wake up just before dawn, but we can't deduce that waking up causes the sunlight to appear.

A very important question then, is: how can we determine causality within a statistical framework? The way we identify causality in statistics is by first laying out...

Two-way ANOVA

We could extend our initial example (a website with different color palettes) to something slightly more complex: instead of having just one factor (color), we could have another one (actually, we could have even more than two). For example, we could add the font type that was used on the website and study how those two factors (color and font type) impact the number of purchases that are made. Unfortunately, this adds an extra complication, because one effect might depend on the levels for the other one: for example, the font type might be relevant to explain the number of purchases, but only when the color is red.

The effects for the color and website are usually referred to as main effects, and the interaction between them is referred to as the interaction effect. Before analyzing the main effects, we should always study the interaction effect first: if it is...

Type I, Type II, and Type III sum of squares

In a one-way ANOVA, the sum of squares can be obtained in a straightforward manner. However, in a two-way ANOVA, things get much more complicated because we have at least three possibilities for computing them. For the following examples, let's assume that we have two factors (A and B), each one with their respective levels.

Type I

The first possibility is the Type I sum of squares, which can be computed by first calculating the sum of squares for factor A, then the sum of squares of B conditional on A, and finally the sum of squares of the interaction (AB) after including the factors, A and B. This is why this is called the sequential sum of squares. Consequently, the order...

Random effects

So far, we have explored models where we have different fixed levels for each effect. This makes a lot of sense when we have a set of possible levels for an effect that we control and are interested in measuring. It also makes sense when we have a blocking effect that has a finite (and small) set values (for example, the sex or occupation of a person). In some cases, we will have a huge amount of levels that will be generally unimportant, for example, if we want to measure whether a drug is effective, and we are dealing with multiple observations per person, we want to add a blocking effect for a person. In these cases, we are not interested in the effect per se, but we certainly want to use it as a control variable for our model. A model that uses proper blocks, will be more efficient: think of ANOVA as a method of attributing variability to factors. If we have...

Repeated measures

Repeated measures designs contain several measurements for the same experimental unit. This generally occurs when we assign different doses of a treatment to a unit and want to evaluate the response through time.

We now have a fixed effect for time, which is typically coded as 1,2,3..,k that needs to be added. We would expect that this variable/effect will have an impact on the response.

These designs raise four major points, and we need to decide how to solve them:

  • Do we think that time has the same impact for all of the experimental units? We have two options for this:
    • If yes, we will just add the time variable as a fixed effect.
    • If no, we will still add the time variable as a fixed effect, plus a random effect for time by subject. What this means is that the time effect can be obtained as the sum of the fixed effect (average for all units) plus the random...

Multivariate t-test

So far, we have worked with univariate data (one variable measured across two samples), and we wanted to test whether the means are equal or not. In certain cases, we might work with multivariate data (for example, measurements of height and weight for certain individuals), and we will be interested in testing the multivariate hypothesis, which is that the means for all of the variables are equal between two groups or not. This is usually formulated as follows:

The difference is that each element is a vector, and we are testing whether all of the elements in a vector are the same between groups. The main assumption here (similar to the univariate t-test) is that the data comes from a multivariate Gaussian distribution.

A relevant question at this stage is whether we can ignore the multi-dimensionality of the problem, and just do univariate t-tests. This would...

MANOVA

We have already explained that ANOVA is the generalization of the t-test for multiple samples. On the other hand, the t-test is designed to work with just one variable, and in case we have multiple variables, we need to use Hotelling T2. Is it possible to extend ANOVA to work with multiple variables? The answer is yes, and the technique for doing so is called MANOVA (Multiple ANOVA). The assumptions for MANOVA are similar to the ones we have when using Hotelling T2 for two samples: equality of covariance matrices between the groups, the data should come from a multivariate Gaussian distribution for each group, and there should not be outliers.

In this example, we will generate data from a multivariate Gaussian distribution (with three variables) with an arbitrary covariance matrix. We will assign the columns' names to represent the History, Math, and Biology scores...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn how to apply statistical methods to your everyday research with handy recipes
  • Foster your analytical skills and interpret research across industries and business verticals
  • Perform t-tests, chi-squared tests, and regression analysis using modern statistical techniques

Description

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 is this book for?

If you are a quantitative researcher, statistician, data analyst, or data scientist looking to tackle various challenges in statistics, this book is what you need! Proficiency in R programming and basic knowledge of linear algebra is necessary to follow along the recipes covered in this book.

What you will learn

  • Become well versed with recipes that will help you interpret plots with R
  • Formulate advanced statistical models in R to understand its concepts
  • Perform Bayesian regression to predict models and input missing data
  • Use time series analysis for modelling and forecasting temporal data
  • Implement a range of regression techniques for efficient data modelling
  • Get to grips with robust statistics and hidden Markov models
  • Explore ANOVA (Analysis of Variance) and perform hypothesis testing

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 29, 2019
Length: 448 pages
Edition : 1st
Language : English
ISBN-13 : 9781789802566
Category :
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Mar 29, 2019
Length: 448 pages
Edition : 1st
Language : English
ISBN-13 : 9781789802566
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 78.97
R Statistics Cookbook
€20.99
Machine Learning with R Quick Start Guide
€24.99
Mastering Machine Learning with R
€32.99
Total 78.97 Stars icon
Banner background image

Table of Contents

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

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.6
(5 Ratings)
5 star 40%
4 star 20%
3 star 20%
2 star 0%
1 star 20%
Leo Jul 02, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Lots of examples provided.
Amazon Verified review Amazon
mark david walker Nov 04, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
A good guide to a range of statistical techniques in R. Recommend for someone wishing to deepen an introductory knowledge of R.
Amazon Verified review Amazon
Amazon Customer May 06, 2019
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
understanding R with the help of algorithms
Amazon Verified review Amazon
Neha Oct 16, 2023
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Book is nice
Subscriber review Packt
Papu Siameja Feb 06, 2024
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I haven't been able to download and read a pdf copy of this book. I sent a message about this at the time of purchase but have not received a response. That is a disappointing. The one star is for the disappointing customer service, rather than the content of the book.
Feefo Verified review Feefo
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.