-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathqqplot.Rmd
84 lines (59 loc) · 3.86 KB
/
qqplot.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Chart: QQ-Plot {#qqplot}
![](images/banners/banner_qqplot.png)
*This chapter originated as a community contribution created by [hao871563506](https://github.com/hao871563506){target="_blank"}*
*This page is a work in progress. We appreciate any input you may have. If you would like to help improve this page, consider [contributing to our repo](contribute.html).*
```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,message = FALSE,
warning = FALSE)
```
## Introduction
In statistics, a Q-Q (quantile-quantile) plot is a probability plot, which is a graphical method for comparing two probability distributions by plotting their quantiles against each other. A point (x, y) on the plot corresponds to one of the quantiles of the second distribution (y-coordinate) plotted against the same quantile of the first distribution (x-coordinate). Thus the line is a parametric curve with the parameter which is the number of the interval for the quantile.
## Interpreting qqplots
## Normal or not (examples using qqnorm)
### Normal qqplot
```{r}
x <- rnorm(1000, 50, 10)
qqnorm(x)
qqline(x, col = "red")
```
The points seem to fall along a straight line. Notice the x-axis plots the theoretical quantiles. Those are the quantiles from the standard Normal distribution with mean 0 and standard deviation 1.
### Non-normal qqplot
```{r}
x <- rexp(1000, 5)
qqnorm(x)
qqline(x, col = "red")
```
Notice the points form a curve instead of a straight line. Normal Q-Q plots that look like this usually mean your sample data are skewed.
## Different kinds of qqplots
The following graph is a conclusion of all the kinds of qqplot:
![](images/qqplot_types.png)
*via [Stack Exchange](https://stats.stackexchange.com/questions/101274/how-to-interpret-a-qq-plot/){target="_blank"}*
- **Normal qqplot**: The normal distribution is symmetric, so it has no skew (the mean is equal to the median).
- **Right skewed qqplot**: Right-skew is also known as positive skew.
- **Left skewed qqplot**: Left-skew is also known as negative skew.
- **Light tailed qqplot**: meaning that compared to the normal distribution there is little more data located at the extremes of the distribution and less data in the center of the distribution.
- **Heavy tailed qqplot**: meaning that compared to the normal distribution there is much more data located at the extremes of the distribution and less data in the center of the distribution.
- **Biomodel qqplot**: illustrate a bimodal distribution.
## qqplot using ggplot
In order to use `ggplot2` to plot a qqplot, we must use a dataframe, so here we convert it to one. We can see that using ggplot to plot a qqplot has a similar outcome as using qqnorm
```{r}
library(ggplot2)
x <- rnorm(1000, 50, 10)
x <- data.frame(x)
ggplot(x, aes(sample = x)) +
stat_qq() +
stat_qq_line()
```
However, when we need to plot different groups, ggplot will be very helpful with its coloring by factor.
```{r}
library(ggplot2)
ggplot(mtcars, aes(sample = mpg, colour = factor(cyl))) +
stat_qq() +
stat_qq_line()
```
## References
- [Understanding Q-Q Plots](https://data.library.virginia.edu/understanding-q-q-plots/){target="_blank"}: A discussion from the University of Virginia Library on qqplots.
- [How to interpret a QQ plot](https://stats.stackexchange.com/questions/101274/how-to-interpret-a-qq-plot){target="_blank"}: Another resource for interpreting qqplots.
- [A QQ Plot Dissection Kit](http://seankross.com/2016/02/29/A-Q-Q-Plot-Dissection-Kit.html){target="_blank"}: An excellent walkthrough on qqplots by Sean Kross.
- [Probability plotting methods for the analysis of data](https://www.jstor.org/stable/2334448?seq=1#metadata_info_tab_contents){target="_blank"}: Paper on plotting techniques, which discusses qqplots. (Wilk, M.B.; Gnanadesikan, R. (1968))
- [QQ-Plot Wiki](https://en.wikipedia.org/wiki/Q%E2%80%93Q_plot#cite_note-1){target="_blank"}: Wikipedia entry on qqplots