Q-Q plots for assessing normality
The quantile-quantile plot (Q-Q plot) is a better graphical method for revealing non-normality. In general, a Q-Q plot compares the quantiles of the data with the quantiles of a reference distribution; if the data are from a distribution of the same type (up to scaling and location), a reasonably straight line should be observed. You should know that the degrees of freedom (df) refer to the number of values or observations that can affect the system you are working with.
In the video, you saw how to generate 1000 normal data points with the rnorm()
function, as well as how to use qqnorm()
to create the Q-Q plot, and qqline()
to add a straight line for reference:
> data <- rnorm(1000, mean = 3, sd = 2)
> qqnorm(data)
> qqline(data)
In this exercise, you will create a Q-Q plot of the Dow Jones log-returns in djx
against the normal reference distribution, which you will add as a visual guide. You will then compare the plot with simulated datasets from normal, Student t and uniform distributions generated with the rnorm()
, rt()
and runif()
functions. You will be learning about the t distribution later in this chapter.
If the data are from a normal distribution the dots should be close to the red line (although there may be some deviation at the very end).
Once again, djx
has been loaded into your workspace.
This exercise is part of the course
Quantitative Risk Management in R
Exercise instructions
- Make a Q-Q plot of
djx
against normal withqqnorm()
and add a red line withqqline()
andcol = "red"
to judge whether the plot is linear. - Calculate the length of
djx
withlength()
and assign to objectn
. - Generate
n
standard normal variables withrnorm()
and assign them tox1
. Make a Q-Q plot ofx1
against normal and add a red line as before. - Generate
n
Student t variables with degree of freedom 4 and assign them tox2
(this has been done for you). Make a Q-Q plot ofx2
against normal and add a red line. - Generate
n
uniform variables and assign them tox3
(this has been done for you). Make a Q-Q plot ofx3
against normal and add a red line.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Make a Q-Q plot of djx and add a red line
___(___)
___(___, ___)
# Calculate the length of djx as n
n <- ___
# Generate n standard normal variables, make a Q-Q plot, add a red line
x1 <- ___(___)
___(___)
___(___, ___)
# Generate n Student t variables, make a Q-Q plot, add a red line
x2 <- rt(n, df = 4)
___(___)
___(___, ___)
# Generate n standard uniform variables, make a Q-Q plot, add red line
x3 <- runif(n)
___(___)
___(___, ___)