How to make Quantile-Quantile plots

In this recipe, we will see how to make Quantile-Quantile (Q-Q) plots, which are useful for comparing two probability distributions.

Getting ready

For this recipe, we don't need to load any additional libraries. We just need to type the recipe at the R prompt or run it as a script.

How to do it...

Let's see how the distribution of mpg in the mtcars dataset compares with a normal distribution using the qnorm() function:

qqnorm(mtcars$mpg)
qqline(mtcars$mpg)
How to do it...

How it works...

In the example, we used the qqnorm() function to create a normal Q-Q plot of mpg values. We added a straight line with the qqline() function. The closer the dots to this line the closer the distribution to a normal one.

There's more...

Another way of making a Q-Q plot is by calling the plot() function on a model fit. For example, let's plot the following linear model fit:

lmfit<-lm(mtcars$mpg~mtcars$disp)
par(mfrow=c(2,2))
plot(lmfit)
There's more...

The second plot is a Q-Q plot comparing the model fit to a normal distribution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset