# Assess univariate and multivariate normality

## Install required packages

wants <- c("DescTools", "MVN", "mvtnorm")
has   <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])

## Univariate normality

### Histogram and density estimate

set.seed(123)
DV <- rnorm(30, mean=1.5, sd=3)
hist(DV, breaks="FD", freq=FALSE)
lines(density(DV), col="blue", lwd=2)

M  <- mean(DV)
SD <- sd(DV)
curve(dnorm(x, mean=M, sd=SD), from=min(DV), to=max(DV),
col="red", lwd=2, add=TRUE)

### QQ-plot

qqnorm(DV, pch=20, cex=2)
qqline(DV, col="gray60", lwd=2)

### Skewness and kurtosis

library(DescTools)
Skew(DV, method=2)
[1] 0.1503207
Kurt(DV, method=2)
[1] -0.5277173

### Shapiro-Wilk-test

Composite null hypothesis: any normal distribution

shapiro.test(DV)

Shapiro-Wilk normality test

data:  DV
W = 0.97894, p-value = 0.7966

### Anderson-Darling-test

Composite null hypothesis: any normal distribution

library(DescTools)
AndersonDarlingTest(DV)

Anderson-Darling test of goodness-of-fit
Null hypothesis: uniform distribution

data:  DV
An = Inf, p-value = 2e-05

### Cramer-von-Mises-test

Composite null hypothesis: any normal distribution

library(DescTools)
CramerVonMisesTest(DV)

Cramer-von Mises normality test

data:  DV
W = 0.028604, p-value = 0.8596

### Shapiro-Francia-test

Composite null hypothesis: any normal distribution

library(DescTools)
ShapiroFranciaTest(DV)

Shapiro-Francia normality test

data:  DV
W = 0.98598, p-value = 0.9015

### Jarque-Bera-test

Composite null hypothesis: any normal distribution

library(DescTools)
JarqueBeraTest(DV)

Robust Jarque Bera Test

data:  DV
X-squared = 0.50907, df = 2, p-value = 0.7753

### Kolmogorov-Smirnov-test

Exact null hypothesis: fully specified normal distribution

ks.test(DV, "pnorm", mean=1, sd=2, alternative="two.sided")

One-sample Kolmogorov-Smirnov test

data:  DV
D = 0.15182, p-value = 0.4498
alternative hypothesis: two-sided

### Lilliefors-test

Composite null hypothesis: any normal distribution

library(DescTools)
LillieTest(DV)

Lilliefors (Kolmogorov-Smirnov) normality test

data:  DV
D = 0.091059, p-value = 0.7587

### Pearson $$\chi^{2}$$-test

Tests weaker null hypothesis (any distribution with the same probabilities for the given class intervals).

Cave: Strictly speaking, result is incorrect: PearsonTest() does not use grouped ML-estimate or maximum $$\chi^{2}$$-estimate of the mean and standard deviation.

library(DescTools)
PearsonTest(DV, n.classes=6, adjust=TRUE)

Pearson chi-square normality test

data:  DV
P = 1.2, p-value = 0.753

## Multivariate normality

### Energy-test

mu    <- c(2, 4, 5)
Sigma <- matrix(c(4,2,-3, 2,16,-1, -3,-1,9), byrow=TRUE, ncol=3)
library(mvtnorm)
X <- rmvnorm(100, mu, Sigma)
library(MVN)
result <- mvn(X, mvnTest="energy")
result$multivariateNormality  Test Statistic p value MVN 1 E-statistic 1.026584 0.08 YES ### Mardia skewness and kurtosis test library(MVN) result <- mvn(X, mvnTest="mardia") result$multivariateNormality
             Test         Statistic           p value Result
1 Mardia Skewness  14.0883039547516 0.169001764623724    YES
2 Mardia Kurtosis 0.110806002772418 0.911770185144381    YES
3             MVN              <NA>              <NA>    YES

## Detach (automatically) loaded packages (if possible)

try(detach(package:DescTools))
try(detach(package:MVN))
try(detach(package:mvtnorm))