How to use qqplot()?
The base R function ‘qqplot(x, y)’ takes two vectors of numeric values as arguments and plots their sorted values against each other. This allows us to visually compare if ‘x’ and ‘y’ come from similar distributions. For example, we can compare two random samples from different distributions:
R
set.seed (123) x <- rnorm (100) y <- rt (100, df = 5) qqplot (x, y) abline (0, 1) |
Output:
We can see that the points deviate from the reference line, especially at the tails, indicating that ‘x’ and ‘y’ have different distributions. However, if we want to check if a single variable (or residuals) follows a normal distribution, we need to compare it with a vector of quantiles from a normal distribution. This is what ‘qqPlot()’ from the ‘car` package does automatically. For example:
R
library (car) x <- rnorm (100) qqPlot (x) |
Output:
We can see that the points are close to the reference line and within the shaded area, indicating that ‘x’ follows a normal distribution. To achieve the same result with ‘qqplot()’, we need to manually generate a vector of quantiles from a normal distribution using ‘qnorm()‘ and ‘ppoints()’. For example:
R
# sample from normal distribution x <- rnorm (100) # vector of quantiles from standard normal distribution z <- qnorm ( ppoints (x)) qqplot (z, x) # add reference line abline (0, 1) |
Output:
We can see that we get essentially the same plot as with `qqPlot()`, except for some minor differences in labels and aesthetics.
How to use qqplot() instead of qqPlot() in car package?
In this article, we will explain how to use the base R function ‘qqplot()’ instead of the ‘qqPlot()’ function from the `car` package to check the normality of a variable or a set of residuals. I will also show how to customize the plot and add confidence envelopes.
Contact Us