Skip to content

Commit a90603e

Browse files
XuhuaHuangCopilot
andcommitted
Study QQ plot and normality test
Co-authored-by: Copilot <copilot@github.com>
1 parent 2ae9716 commit a90603e

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

R/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,90 @@ sample_correlation_stable <- function(x, y) {
386386
- Use `browser()` function to set breakpoints.
387387
- Use `print()` or `cat()` to display intermediate values for debugging.
388388
- Do not use `print()` in return statements of functions, just use `return(return_object)`.
389+
390+
## QQ Plot and Normality Test
391+
392+
### Quantile
393+
394+
A quantile is a value that divides a dataset into intervals containing equal proportions of the data.
395+
396+
Sort the given data from smallest to largest, a quantile tells you the value below which a certain percentage of the data falls. For example, the 25th percentile (or first quartile) is the value below which 25% of the data falls.
397+
398+
For a dataset of size `n`, the `p`-th quantile is roughly the value at the position `p * (n - 1) + 1` in the sorted data. If this position is not an integer, you can interpolate between the two nearest values.
399+
400+
If the data is normally distributed, the points in a QQ plot will approximately lie on a straight line. Deviations from this line indicate departures from normality.
401+
402+
### QQ Plot
403+
404+
A QQ plot (quantile-quantile plot) is a graphical tool to assess if a dataset follows a particular distribution, such as the normal distribution (most commonly seen). It plots the quantiles of the dataset against the quantiles of the theoretical distribution.
405+
406+
A Q–Q plot is straight because quantiles transform linearly under affine transformations, and the plot is literally visualizing that relationship.
407+
408+
```r
409+
x <- rnorm(100)
410+
y <- rnorm(100, mean = 2)
411+
412+
qqplot(x, y)
413+
abline(0, 1, col = "blue")
414+
```
415+
416+
### `R` Graphics
417+
418+
- `plot`, `hist`, `boxplot`, `qqplot`, `pairs`
419+
- `xlab`, `ylab`, `sub`, `main`, `xlim`, `ylim`
420+
421+
Symbol Options:
422+
423+
- `lty` line type: `1` solid, `2` dashed, `3` dotted, `4` dotdash, `5` longdash, `6` twodash
424+
- `lwd` line width: `1` default, `2` double, etc
425+
- `mkh` marker height (in): `mkh = 0.5`
426+
- `pch` marker type: `pch = "*"` for star, `pch = 16` for filled circle, `pch = 17` for filled triangle, etc
427+
- `col` colour type: `col = "blue"`
428+
- `cex` character expansion: `cex = 1.5` to increase size by 50%
429+
- `type` plot type: `type = "p"` for points, `type = "l"` for lines, `type = "b"` for both, etc
430+
431+
Plot Layout:
432+
433+
- Multiple figures on one plot: `par(mfrow = c(2, 2))` for 2 rows and 2 columns
434+
- Split screen: `split.screen(figs=c(2, 1))` for 2 rows and 1 column
435+
- `close.screen(all=T))` to close all split screens
436+
437+
## Graphics with Expressions
438+
439+
```r
440+
x <- seq(-1, 1, length.out = 100)
441+
y <- sqrt(1 - x^2)
442+
443+
plot(x, y,
444+
type = "l",
445+
main = expression(y == sqrt(1 - x^2)),
446+
xlab = "x",
447+
ylab = "y")
448+
```
449+
450+
```r
451+
x <- seq(-1, 1, length.out = 100)
452+
453+
y1 <- sqrt(1 - x^2)
454+
alpha <- 0.5
455+
beta <- 1
456+
y2 <- alpha + beta * x
457+
458+
plot(x, y1,
459+
type = "l",
460+
col = "blue",
461+
ylim = range(c(y1, y2)),
462+
main = "Two Functions",
463+
xlab = "x",
464+
ylab = "y")
465+
466+
lines(x, y2, col = "red")
467+
468+
legend("topright",
469+
legend = c(
470+
expression(y == sqrt(1 - x^2)),
471+
expression(y == alpha + beta * x)
472+
),
473+
col = c("blue", "red"),
474+
lty = 1)
475+
```

0 commit comments

Comments
 (0)