test.norm: Tests for Univariate and Multivariate Normality

View source: R/test.norm.R

test.normR Documentation

Tests for Univariate and Multivariate Normality

Description

This function performs the Kolmogorov-Smirnov-Lilliefors, the Shapiro-Wilk test, the Anderson–Darling test, and the Cramer-von Mises test for testing for testing univariate normality (null hypothesis) against univariate non-normality (alternative hypothesis), and the Henze-Zirkler test, Henze-Wagner test, and the Royston test for testing multivariate normality (null hypothesis) against multivariate non-normality (alternative hypothesis). By default, the function performs the Shapiro-Wilk test for testing univariate normality, and Henze-Zirkler test for testing multivariate normality. Note that the function also tests normality of residuals of a linear model when specifying an object of class "lm" for the argument data.

Usage

test.norm(data, ..., print = c("all", "multi", "uni"),
          method.uni = c("lillie", "sw", "ad", "cm"),
          method.multi = c("hz", "hw", "royston"), p.adj = "none", digits = 2,
          p.digits = 3, as.na = NULL, plot = FALSE, geom = c("all", "qq", "hist"),
          impute = FALSE, filename = NULL, width = NA, height = NA, dpi = 600,
          write = NULL, append = TRUE, check = TRUE, output = TRUE)

Arguments

data

a fitted model of class lm, a numeric vector, or data frame.

...

an expression indicating the variable names in data, e.g., test.norm(dat, x1, x2). Note that the operators +, -, ~, :, ::, and ! can also be used to select variables, see 'Details' in the df.subset function.

print

a character vector indicating which results to show on the console, i.e. "all" for tests for multivariate and univariate normality, "multi" (default) for a test for multivariate normality, and "uni" for tests for univariate normality. Note that this argument is only used when specifying more than one variable for the analysis.

method.uni

a character string indicating the method used for testing univariate normality, i.e., "lillie" for the Kolmogorov-Smirnov-Lilliefors test (Lilliefors, 1967), "sw" (default) for the Shapiro-Wilk test (Shapiro & Wilk, 1965), "ad" for the Anderson–Darling test (Anderson & Darling, 1954), and "cm" for the Cramer-von Mises test (Stephens, 1986).

method.multi

a character string indicating the method used for testing multivariate normality, i.e., "hz" (default) for the Henze-Zirkler test (Henze & Zirkler, 1990), "hw" for the Henze-Wagner test (Henze & Wagner, 1997), and "royston" for the Royston test (Royston, 1983).

p.adj

a character string indicating an adjustment method for multiple testing based on p.adjust, i.e., none (default), bonferroni, holm, hochberg, hommel, BH, BY, or fdr.

digits

an integer value indicating the number of decimal places to be used for displaying results.

p.digits

an integer value indicating the number of decimal places to be used for displaying the p-value.

as.na

a numeric vector indicating user-defined missing values, i.e. these values are converted to NA before conducting the analysis.

plot

logical: if TRUE, a plot showing univariate or multivariate Mahalanobis Q-Q plot is drawn. For additional plotting arguments, see Details in the help page of the function plot.misty.object.

geom

a character vector indicating which plot to draw, i.e., "all" for plotting a Q-Q plot and histogram, "qq" for plotting Q-Q plots, and "hist" for plotting histograms. Note that "all" and c("qq", "hist") are valid only when specifying a fitted model of class lm or a single variable. When specifying "all" or c("qq", "hist") given multiple variables, a multivariate Mahalanobis Q-Q plot is drawn.

impute

logical: if TRUE, missing values are imputed when plotting multiple variables plot = TRUE using the mice function in the R package mice using the predictive mean matching method. Note that listwise deletion is always used for tests for univariate and multivariate normality.

filename

a character string indicating the filename argument including the file extension in the ggsave function. Note that one of ".eps", ".ps", ".tex", ".pdf" (default), ".jpeg", ".tiff", ".png", ".bmp", ".svg" or ".wmf" needs to be specified as file extension in the file argument. Note that plots can only be saved when plot = TRUE.

width

a numeric value indicating the width argument (default is the size of the current graphics device) for the ggsave function.

height

a numeric value indicating the height argument (default is the size of the current graphics device) for the ggsave function.

dpi

a numeric value indicating the dpi argument (default is 600) for the ggsave function.

write

a character string naming a text file with file extension ".txt" (e.g., "Output.txt") for writing the output into a text file.

append

logical: if TRUE (default), output will be appended to an existing text file with extension .txt specified in write, if FALSE existing text file will be overwritten.

check

logical: if TRUE (default), argument specification is checked.

output

logical: if TRUE (default), output is shown on the console.

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

data

numeric vector or data frame

plot

ggplot2 object for plotting the results

args

specification of function arguments

result

list with result tables, i.e., uni for test(s) for univariate normality and multi for test for multivariate normality

Note

This function is based on the hz(), hw(), and royston() function in the MVN package by Selcuk Korkmaz, Dincer Goksuluk and Gokmen Zararsiz, and the ad.test(), lillie.test(), and cvm.test() function in the nortest package by Juergen Gross and Uwe Ligges.

Author(s)

Takuya Yanagida

References

Anderson, T. W., & Darling, D. A. (1954). A test of goodness of fit. Journal of the American Statistical Association, 49(268), 765-769. https://doi.org/10.1080/01621459.1954.10501232

Gross J, & Ligges U (2015). nortest: Tests for Normality. R package version 1.0-4, https://doi.org/10.32614/CRAN.package.nortest

Henze, N., & Zirkler, B. (1990). A class of invariant consistent tests for multivariate normality. Communications in Statistics—Theory and Methods, 19(10), 3595-3617. https://doi.org/10.1080/03610929008830400

Henze, N., & Wagner, T. (1997). A new approach to the BHEP tests for multivariate normality. Journal of Multivariate Analysis, 62(1), 1-23

Korkmaz S, Goksuluk D, & Zararsiz G. (2014) MVN: An R Package for Assessing Multivariate Normality. The R Journal, 6(2), 151-162.

Lilliefors, H. W. (1967). On the Kolmogorov-Smirnov test for normality with mean and variance unknown. Journal of the American Statistical Association, 62(318), 399-402. https://doi.org/10.1080/01621459.1967.10482916

Royston, J.P. (1983). Some techniques for assessing multivariate normality based on the Shapiro-Wilk W. Journal of the Royal Statistical Society. Series C (Applied Statistics), 32, 121-133. https://doi.org/10.2307/2347291

Shapiro, S. S., & Wilk, M. B. (1965). An analysis of variance test for normality (complete samples). Biometrika, 52(3–4), 591–611. https://doi.org/10.1093/biomet/52.3-4.591

Stephens, M. A. (1986). Tests based on EDF statistics. In R. B. D'Agostino & M. A. Stephens (Eds.), Goodness-of-Fit Techniques (pp. 97.193). Marcel Dekker.

See Also

skewness, kurtosis, check.resid, test.linear, test.homo, test.levene

Examples

## Not run: 
#————————————————————————————————————————————————————————————————————————————
# Fitted Model of Class "lm"

# Estimate linear model
mod <- lm(mpg ~ cyl + disp, data = mtcars)

# Example 1a: Shapiro-Wilk test for normality of residuals
test.norm(mod)

# Example 1b: Lilliefors (Kolomorov-Smirnov) test for normality of residuals
test.norm(mod, method.uni = "lillie")

#————————————————————————————————————————————————————————————————————————————
# One Variable

# Example 2: Shapiro-Wilk test for normality
test.norm(mtcars, mpg)

#————————————————————————————————————————————————————————————————————————————
# Multiple Variables

# Example 3a: Henze-Zirkler test for multivariate normality
test.norm(mtcars, hp, drat, wt, qsec)

# Example 3b: Henze-Wagner test for multivariate normality
test.norm(mtcars, hp, drat, wt, qsec, method.multi = "hw")

# Example 3c: Multiple Shapiro-Wilk tests for normality
test.norm(mtcars, hp, drat, wt, qsec, print = "uni")

# Example 3c: Henze-Wagner test for multivariate normality
#             Multiple Shapiro-Wilk tests for normality
test.norm(mtcars, hp, drat, wt, qsec, print = "all")

#————————————————————————————————————————————————————————————————————————————
# Plot

# Example 4a: Q-Q plot and histogram
test.norm(mtcars, mpg, plot = TRUE)

# Example 4b: Multivariate Mahalanobis Q-Q plot
test.norm(mtcars, hp, drat, wt, qsec, plot = TRUE)

# Example 4c: Multiple Q-Q plots
test.norm(mtcars, hp, drat, wt, qsec, print = "uni", plot = TRUE)

# Example 4d: Multiple histograms
test.norm(mtcars, hp, drat, wt, qsec, print = "uni", plot = TRUE, geom = "hist")

# Example 4e: Multivariate Mahalanobis Q-Q plot, impute data for plotting
test.norm(airquality, plot = TRUE, impute = TRUE)

# Example 4f: Plot results using the plot() function, use additional arguments
# see Details in the help page of the function plot.misty.object
object <- test.norm(mtcars, mpg)
plot(object, point.shape = 2, ribbon.fill = "red", xlim = c(0, 40))

#————————————————————————————————————————————————————————————————————————————
# Write Results and Save Plot

# Example 5a: Write results into a text file
test.norm(mtcars, hp, drat, wt, qsec, write = "Multinorm.txt")

# Example 5b: Write results into an Excel file
test.norm(mtcars, hp, drat, wt, qsec, write = "Multinorm.xlsx")

# Example 5c: Save plot as PNG file
test.norm(mtcars, mpg, plot = TRUE, filename = "Multinorm.png",
          width = 13, height = 6)

## End(Not run)

misty documentation built on Nov. 24, 2026, 5:06 p.m.

Related to test.norm in misty...