Beginners Guide to R – R Quantile-Quantile (QQ) Plot – Base Graph

Sometimes it’s important to know whether the data is normally distributed. A quantile-quantile plot (QQ plot) is a good first check.

It shows the distribution of the data against the expected normal distribution.

If the data is normally distributed, the points fall on the 45° reference line. If the data is non-normal, the points deviate noticeably from the reference line.

Create a Normal Quantile-Quantile (QQ) Plot

To get started, you need a set of data to work with. Let’s consider the built-in ToothGrowth data set as an example data set.

Here are the first six observations of the data set.

``````# First six observations of the ‘ToothGrowth’ data set
len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5``````

ToothGrowth data set

ToothGrowth data set contains observations on effect of vitamin C on tooth growth in 60 guinea pigs, where each animal received one of three dose levels of vitamin C (0.5, 1, and 2 mg/day) by one of two delivery methods, orange juice (coded as OJ) or ascorbic acid (coded as VC).

The qqnorm() function

In R, you can create the normal quantile-quantile plot using the `qqnorm()` function. This function plots your sample against a normal distribution.

You simply give the sample you want to plot as a first argument.

The qqline() function

R also has a `qqline()` function, which adds a theoretical distribution line to your normal QQ plot. This line makes it a lot easier to evaluate whether the points deviate from the reference line.

The closer the points are to the reference line in the plot, the closer the sample data follows a normal distribution.

``````# Check 'length' sample in 'ToothGrowth' dataset for normality
with(ToothGrowth, {
qqnorm(len);
qqline(len)})
``````

Notice that majority of the points are close to the reference line except in the extreme tail; so, you can assume normality.

Compare Two Samples

Use `qqplot()` function if you want to compare two samples (compare the quantiles of two samples).

This function sorts the data of both samples and plots them against each other.

For example, to check whether the tooth growth during supplement type ascorbic acid (coded as VC) and orange juice (coded as OJ) are distributed equally, you simply do the following.

``````with(ToothGrowth,
qqplot(len[supp == "VC"],
len[supp == "OJ"]))
``````

QQ Plot with Confidence Intervals

The standard `qqplot()` functions in R do not provide confidence intervals but the `qqPlot()` function from the car package does.

``````library(car)
with(ToothGrowth, qqPlot(len))
``````

Special 95% discount

Two Machine Learning Fields

There are two sides to machine learning:

• Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
• Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

`Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  `