EnvStats (version 2.1.0)

qqPlot: Quantile-Quantile (Q-Q) Plot

Description

Produces a quantile-quantile (Q-Q) plot, also called a probability plot. The qqPlot function is a modified version of the Rfunctions qqnorm and qqplot. The EnvStats function qqPlot allows the user to specify a number of different distributions in addition to the normal distribution, and to optionally estimate the distribution parameters of the fitted distribution.

Usage

qqPlot(x, y = NULL, distribution = "norm", param.list = list(mean = 0, sd = 1), 
    estimate.params = plot.type == "Tukey Mean-Difference Q-Q", 
    est.arg.list = NULL, plot.type = "Q-Q", plot.pos.con = NULL, plot.it = TRUE, 
    equal.axes = qq.line.type == "0-1" || estimate.params, add.line = FALSE, 
    qq.line.type = "least squares", duplicate.points.method = "standard", 
    points.col = 1, line.col = 1, line.lwd = par("cex"), line.lty = 1, 
    digits = .Options$digits, ..., main = NULL, xlab = NULL, ylab = NULL, 
    xlim = NULL, ylim = NULL)

Arguments

x
numeric vector of observations. When y is not supplied, x represents a sample from the hypothesized distribution specifed by distribution. When y is supplied, the distribution of x
y
optional numeric vector of observations (not necessarily the same lenght as x). Missing (NA), undefined (NaN), and infinite (Inf, -Inf) values are allowed but will be removed.
distribution
when y is not supplied, a character string denoting the distribution abbreviation. The default value is distribution="norm". See the help file for Distribution.df
param.list
when y is not supplied, a list with values for the parameters of the distribution. The default value is param.list=list(mean=0, sd=1). See the help file for Distribution.df<
estimate.params
when y is not supplied, a logical scalar indicating whether to compute quantiles based on estimating the distribution parameters (estimate.params=TRUE) or using the known distribution parameters specified in par
est.arg.list
when y is not supplied and estimate.params=TRUE, a list whose components are optional arguments associated with the function used to estimate the parameters of the assumed distribution (see the help file
plot.type
a character string denoting the kind of plot. Possible values are "Q-Q" (Quantile-Quantile plot, the default) and "Tukey Mean-Difference Q-Q" (Tukey mean-difference Q-Q plot). This argument may be abbreviated (e.g.,
plot.pos.con
numeric scalar between 0 and 1 containing the value of the plotting position constant. The default value of plot.pos.con depends on whether the argument y is supplied, and if not the value of the argument distribut
plot.it
a logical scalar indicating whether to create a plot on the current graphics device. The default value is plot.it=TRUE.
equal.axes
a logical scalar indicating whether to use the same range on the $x$- and $y$-axes when plot.type="Q-Q". The default value is TRUE if qq.line.type="0-1" or estimate.params=TRUE, otherwise it is
add.line
a logical scalar indicating whether to add a line to the plot. If add.line=TRUE and plot.type="Q-Q", a line determined by the value of qq.line.type is added to the plot. If add.line=TRUE and <
qq.line.type
character string determining what kind of line to add to the Q-Q plot. Possible values are "least squares" (the default), "0-1" and "robust". For the value "least squares", a least squares line
duplicate.points.method
a character string denoting how to plot points with duplicate $(x,y)$ values. Possible values are "standard" (the default), "jitter", and "number". For the value "standard", a single plotting s
points.col
a numeric scalar or character string determining the color of the points in the plot. The default value is points.col=1. See the entry for col in the help file for par for mo
line.col
a numeric scalar or character string determining the color of the line in the plot. The default value is points.col=1. See the entry for col in the help file for par for more
line.lwd
a numeric scalar determining the width of the line in the plot. The default value is line.lwd=par("cex"). See the entry for lwd in the help file for par for more information.
line.lty
a numeric scalar determining the line type of the line in the plot. The default value is line.lty=1. See the entry for lty in the help file for par for more information. Thi
digits
a scalar indicating how many significant digits to print for the distribution parameters. The default value is digits=.Options$digits.
main, xlab, ylab, xlim, ylim, ...
additional graphical parameters (see par).

Value

  • qqPlot returns a list with components x and y, giving the $(x,y)$ coordinates of the points that have been or would have been plotted. There are four cases to consider: 1. The argument y is not supplied and plot.type="Q-Q".
  • xthe quantiles from the theoretical distribution.
  • ythe observed quantiles (order statistics) based on the data in the argument x.
  • 2. The argument y is not supplied and plot.type="Tukey Mean-Difference Q-Q".
  • xthe averages of the observed and theoretical quantiles.
  • ythe differences between the observed quantiles (order statistics) and the theoretical quantiles.
  • 3. The argument y is supplied and plot.type="Q-Q".
  • xthe observed quantiles based on the data in the argument x. Note that these are adjusted quantiles if the number of observations in the argument x is greater then the number of observations in the argument y.
  • ythe observed quantiles based on the data in the argument y. Note that these are adjusted quantiles if the number of observations in the argument y is greater then the number of observations in the argument x.
  • 4. The argument y is supplied and plot.type="Tukey Mean-Difference Q-Q".
  • xthe averages of the quantiles based on the argument x and the quantiles based on the argument y.
  • ythe differences between the quantiles based on the argument x and the quantiles based on the argument y.

Details

If y is not supplied, the vector x is assumed to be a sample from the probability distribution specified by the argument distribution (and param.list if estimate.params=FALSE). When plot.type="Q-Q", the quantiles of x are plotted on the $y$-axis against the quantiles of the assumed distribution on the $x$-axis. If y is supplied and plot.type="Q-Q", the empirical quantiles of y are plotted against the empirical quantiles of x. When plot.type="Tukey Mean-Difference Q-Q", the difference of the quantiles is plotted on the $y$-axis against the mean of the quantiles on the $x$-axis. Special Distributions When y is not supplied and the argument distribution specifies one of the following distributions, the function qqPlot behaves in the manner described below. [object Object],[object Object],[object Object],[object Object],[object Object],[object Object] Explanation of Q-Q Plots A probability plot or quantile-quantile (Q-Q) plot is a graphical display invented by Wilk and Gnanadesikan (1968) to compare a data set to a particular probability distribution or to compare it to another data set. The idea is that if two population distributions are exactly the same, then they have the same quantiles (percentiles), so a plot of the quantiles for the first distribution vs. the quantiles for the second distribution will fall on the 0-1 line (i.e., the straight line $y = x$ with intercept 0 and slope 1). If the two distributions have the same shape and spread but different locations, then the plot of the quantiles will fall on the line $y = x + b$ (parallel to the 0-1 line) where $b$ denotes the difference in locations. If the distributions have different locations and differ by a multiplicative constant $m$, then the plot of the quantiles will fall on the line $y = mx + b$ (D'Agostino, 1986a, p. 25; Helsel and Hirsch, 1986, p. 42). Various kinds of differences between distributions will yield various kinds of deviations from a straight line. Comparing Observations to a Hypothesized Distribution Let $\underline{x} = x_1, x_2, \ldots, x_n$ denote the observations in a random sample of size $n$ from some unknown distribution with cumulative distribution function $F()$, and let $x_{(1)}, x_{(2)}, \ldots, x_{(n)}$ denote the ordered observations. Depending on the particular formula used for the empirical cdf (see ecdfPlot), the $i$'th order statistic is an estimate of the $i/(n+1)$'th, $(i-0.5)/n$'th, etc., quantile. For the moment, assume the $i$'th order statistic is an estimate of the $i/(n+1)$'th quantile, that is: $$\hat{F}[x_{(i)}] = \hat{p}_i = \frac{i}{n+1} \;\;\;\;\;\; (1)$$ so $$x_{(i)} \approx F^{-1}(\hat{p}_i) \;\;\;\;\;\; (2)$$ If we knew the form of the true cdf $F$, then the plot of $x_{(i)}$ vs. $F^{-1}(\hat{p}_i)$ would form approximately a straight line based on Equation (2) above. A probability plot is a plot of $x_{(i)}$ vs. $F_0^{-1}(\hat{p}_i)$, where $F_0$ denotes the cdf associated with the hypothesized distribution. The probability plot should fall roughly on the line $y=x$ if $F=F_0$. If $F$ and $F_0$ merely differ by a shift in location and scale, that is, if $F[(x - \mu) / \sigma] = F_0(x)$, then the plot should fall roughly on the line $y = \sigma x + \mu$. The quantity $\hat{p}_i = i/(n+1)$ in Equation (1) above is called the plotting position for the probability plot. This particular formula for the plotting position is appealing because it can be shown that for any continuous distribution $$E{F[x_{(i)}]} = \frac{i}{n+1} \;\;\;\;\;\; (3)$$ (Nelson, 1982, pp. 299-300; Stedinger et al., 1993). That is, the $i$'th plotting position defined as in Equation (1) is the expected value of the true cdf evaluated at the $i$'th order statistic. Many authors and practitioners, however, prefer to use a plotting position that satisfies: $$F^{-1}(\hat{p}_i) = E[x_{(i)}] \;\;\;\;\;\; (4)$$ or one that satisfies $$F^{-1}(\hat{p}_i) = M[x_{(i)}] = F^{-1}{M[u_{(i)}]} \;\;\;\;\;\; (5)$$ where $M[x_{(i)}]$ denotes the median of the distribution of the $i$'th order statistic, and $u_{(i)}$ denotes the $i$'th order statistic in a random sample of $n$ uniform (0,1) random variates. The plotting positions in Equation (4) are often approximated since the expected value of the $i$'th order statistic is often difficult and time-consuming to compute. Note that these plotting positions will differ for different distributions. The plotting positions in Equation (5) were recommended by Filliben (1975) because they require computing or approximating only the medians of uniform (0,1) order statistics, no matter what the form of the assumed cdf $F_0$. Also, the median may be preferred as a measure of central tendency because the distributions of most order statistics are skewed. Most plotting positions can be written as: $$\hat{p}_i = \frac{i - a}{n - 2a + 1} \;\;\;\;\;\; (6)$$ where $0 \le a \le 1$ (D'Agostino, 1986a, p.25; Stedinger et al., 1993). The quantity $a$ is sometimes called the plotting position constant, and is determined by the argument plot.pos.con in the function qqPlot. The table below, adapted from Stedinger et al. (1993), displays commonly used plotting positions based on equation (6) for several distributions. llll{ Distribution Often Used Name a With References Weibull 0 Weibull, Weibull (1939), Uniform Stedinger et al. (1993) Median 0.3175 Several Filliben (1975), Vogel (1986) Blom 0.375 Normal Blom (1958), and Others Looney and Gulledge (1985) Cunnane 0.4 Several Cunnane (1978), Chowdhury et al. (1991) Gringorten 0.44 Gumbel Gringorton (1963), Vogel (1986) Hazen 0.5 Several Hazen (1914), Chambers et al. (1983), Cleveland (1993) } For moderate and large sample sizes, there is very little difference in visual appearance of the Q-Q plot for different choices of plotting positions. Comparing Two Data Sets Let $\underline{x} = x_1, x_2, \ldots, x_n$ denote the observations in a random sample of size $n$ from some unknown distribution with cumulative distribution function $F()$, and let $x_{(1)}, x_{(2)}, \ldots, x_{(n)}$ denote the ordered observations. Similarly, let $\underline{y} = y_1, y_2, \ldots, y_m$ denote the observations in a random sample of size $m$ from some unknown distribution with cumulative distribution function $G()$, and let $y_{(1)}, y_{(2)}, \ldots, y_{(m)}$ denote the ordered observations. Suppose we are interested in investigating whether the shape of the distribution with cdf $F$ is the same as the shape of the distribution with cdf $G$ (e.g., $F$ and $G$ may both be normal distributions but differ in mean and standard deviation). When $n = m$, we can visually explore this question by plotting $y_{(i)}$ vs. $x_{(i)}$, for $i = 1, 2, \ldots, n$. The values in $\underline{y}$ are spread out in a certain way depending on the true distribution: they may be more or less symmetric about some value (the population mean or median) or they may be skewed to the right or left; they may be concentrated close to the mean or median (platykurtic) or there may be several observations far away from the mean or median on either side (leptokurtic). Similarly, the values in $\underline{x}$ are spread out in a certain way. If the values in $\underline{x}$ and $\underline{y}$ are spread out in the same way, then the plot of $y_{(i)}$ vs. $x_{(i)}$ will be approximately a straight line. If the cdf $F$ is exactly the same as the cdf $G$, then the plot of $y_{(i)}$ vs. $x_{(i)}$ will fall roughly on the straight line $y = x$. If $F$ and $G$ differ by a shift in location and scale, that is, if $F[(x-\mu)/\sigma] = G(x)$, then the plot will fall roughly on the line $y = \sigma x + \mu$. When $n > m$, a slight adjustment has to be made to produce the plot. Let $\hat{p}_1, \hat{p}_2, \ldots, \hat{p}_m$ denote the plotting positions corresponding to the $m$ empirical quantiles for the $y$'s and let $\hat{p}^*_1, \hat{p}^*_2, \ldots, \hat{p}^*_n$ denote the plotting positions corresponding the $n$ empirical quantiles for the $x$'s. Then we plot $y_{(j)}$ vs. $x^*_{(j)}$ for $j = 1, 2, \ldots, m$ where $$x^*_{(j)} = (1 - r) x_{(i)} + r x_{(i+1)} \;\;\;\;\;\; (7)$$ $$r = \frac{\hat{p}_j - \hat{p}^*_i}{\hat{p}^*_{i+1} - \hat{p}^*_i} \;\;\;\;\;\; (8)$$ $$\hat{p}^*_i \le \hat{p}_j \le \hat{p}^*_{i+1} \;\;\;\;\;\; (9)$$ That is, the values for the $x^*_{(j)}$'s are determined by linear interpolation based on the values of the plotting positions for $\underline{x}$ and $\underline{y}$. A similar adjustment is made when $n < m$. Note that the Rfunction qqplot uses a different method than the one in Equation (7) above; it uses linear interpolation based on 1:n and m by calling the approx function.

References

Chambers, J.M., W.S. Cleveland, B. Kleiner, and P.A. Tukey. (1983). Graphical Methods for Data Analysis. Duxbury Press, Boston, MA, pp.11-16. Cleveland, W.S. (1993). Visualizing Data. Hobart Press, Summit, New Jersey, 360pp. D'Agostino, R.B. (1986a). Graphical Analysis. In: D'Agostino, R.B., and M.A. Stephens, eds. Goodness-of Fit Techniques. Marcel Dekker, New York, Chapter 2, pp.7-62.

See Also

ppoints, ecdfPlot, Distribution.df, qqPlotGestalt, qqPlotCensored, qqnorm.

Examples

Run this code
# The guidance document USEPA (1994b, pp. 6.22--6.25) 
  # contains measures of 1,2,3,4-Tetrachlorobenzene (TcCB) 
  # concentrations (in parts per billion) from soil samples 
  # at a Reference area and a Cleanup area.  These data are strored 
  # in the data frame EPA.94b.tccb.df.  
  #
  # Create an Q-Q plot for the reference area data first assuming a 
  # normal distribution, then a lognormal distribution, then a 
  # gamma distribution.
  
  # Assume a normal distribution
  #-----------------------------

  dev.new()
  with(EPA.94b.tccb.df, qqPlot(TcCB[Area == "Reference"]))

  dev.new()
  with(EPA.94b.tccb.df, qqPlot(TcCB[Area == "Reference"], add.line = TRUE))

  dev.new()
  with(EPA.94b.tccb.df, qqPlot(TcCB[Area == "Reference"], 
    plot.type = "Tukey", add.line = TRUE))


  # The Q-Q plot based on assuming a normal distribution shows a U-shape,
  # indicating the Reference area TcCB data are skewed to the right
  # compared to a normal distribuiton.

  # Assume a lognormal distribution
  #--------------------------------

  dev.new()
  with(EPA.94b.tccb.df, 
    qqPlot(TcCB[Area == "Reference"], dist = "lnorm", 
      digits = 2, points.col = "blue", add.line = TRUE))

  dev.new()
  with(EPA.94b.tccb.df, 
    qqPlot(TcCB[Area == "Reference"], dist = "lnorm", 
      digits = 2, plot.type = "Tukey", points.col = "blue", 
      add.line = TRUE))

  # Alternative parameterization

  dev.new()
  with(EPA.94b.tccb.df, 
    qqPlot(TcCB[Area == "Reference"], dist = "lnormAlt", 
      estimate.params = TRUE, digits = 2, points.col = "blue", 
      add.line = TRUE))

  dev.new()
  with(EPA.94b.tccb.df, 
    qqPlot(TcCB[Area == "Reference"], dist = "lnormAlt", 
      digits = 2, plot.type = "Tukey", points.col = "blue", 
      add.line = TRUE))


  # The lognormal distribution appears to be an adequate fit.
  # Now look at a Q-Q plot assuming a gamma distribution.
  #----------------------------------------------------------

  dev.new()
  with(EPA.94b.tccb.df, 
    qqPlot(TcCB[Area == "Reference"], dist = "gamma", 
      estimate.params = TRUE, digits = 2, points.col = "blue", 
      add.line = TRUE))

  dev.new()
  with(EPA.94b.tccb.df, 
    qqPlot(TcCB[Area == "Reference"], dist = "gamma", 
      digits = 2, plot.type = "Tukey", points.col = "blue", 
      add.line = TRUE))

  # Alternative Parameterization

  dev.new()
  with(EPA.94b.tccb.df, 
    qqPlot(TcCB[Area == "Reference"], dist = "gammaAlt", 
      estimate.params = TRUE, digits = 2, points.col = "blue", 
      add.line = TRUE))

  dev.new()
  with(EPA.94b.tccb.df, 
    qqPlot(TcCB[Area == "Reference"], dist = "gammaAlt", 
      digits = 2, plot.type = "Tukey", points.col = "blue", 
      add.line = TRUE))

  #-------------------------------------------------------------------------------------

  # Generate 20 observations from a gamma distribution with parameters 
  # shape=2 and scale=2, then create a normal (Gaussian) Q-Q plot for these data. 
  # (Note: the call to set.seed simply allows you to reproduce this example.)

  set.seed(357) 
  dat <- rgamma(20, shape=2, scale=2) 
  dev.new()
  qqPlot(dat, add.line = TRUE)

  # Now assume a gamma distribution and estimate the parameters
  #------------------------------------------------------------

  dev.new()
  qqPlot(dat, dist = "gamma", estimate.params = TRUE, add.line = TRUE)

  # Clean up
  #---------
  rm(dat)
  graphics.off()

Run the code above in your browser using DataLab