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 eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781789802924
Category :
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Mar 29, 2019
Length: 448 pages
Edition : 1st
Language : English
ISBN-13 : 9781789802924
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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.