Package 'normality'

Title: Tests for Departure from Normality
Description: A toolkit for assessing data normality using a comprehensive collection of statistical methods. It includes descriptive measures and formal hypothesis tests, such as skewness and kurtosis tests, the Anderson–Darling test, the Shapiro–Wilk test, and the D'Agostino–Pearson K2 omnibus test.
Authors: Joon-Keat Lai [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-9840-5836>)
Maintainer: Joon-Keat Lai <[email protected]>
License: MIT + file LICENSE
Version: 0.0.5
Built: 2026-10-06 08:44:36 UTC
Source: https://github.com/p10911004-npust/normality

Help Index


Anderson-Darling Normality Test

Description

Performs the Anderson–Darling (A2) normality test, an EDF-based goodness-of-fit test that gives greater weight to deviations in the tails of the distribution.

Usage

Anderson_Darling_test(
  x,
  alpha = 0.05,
  silent = FALSE,
  summary = TRUE,
  misc = FALSE
)

Arguments

x

A numeric vector, at least length of 8.

alpha

Numeric (default: 0.05). Significance threshold, range from 0 to 1.

silent

Logical (default: FALSE). If FALSE, print out the results.

summary

Logical (default: TRUE). Produce a summary table.

misc

Logical (default: FALSE). Output other unimportant parameters.

Value

A list.

References

D’Agostino, R.B., 2017. Tests for the Normal Distribution. In: D'Agostino, R.B., Stephens, M.A. (Eds.), Goodness-of-Fit Techniques, 1st ed. Routledge, New York, pg. 372–373. https://doi.org/10.1201/9780203753064

Stephens, M.A., 2017. Tests Based on EDF Statistics. In: D'Agostino, R.B., Stephens, M.A. (Eds.), Goodness-of-Fit Techniques, 1st ed. Routledge, New York, (Table 4.8 & 4.9) pg. 126–128. https://doi.org/10.1201/9780203753064

Anderson, T.W., Darling, D.A., 1954. A Test of Goodness of Fit. J. Am. Stat. Assoc. 49, 765–769. https://doi.org/10.1080/01621459.1954.10501232

Examples

out <- Anderson_Darling_test(rnorm(10))
print(out$summary)

Normality test

Description

A wrapper function for the normality tests available in this package.

Usage

check_normality(
  data,
  formula = NULL,
  alpha = 0.05,
  method = "SWR",
  summary = TRUE
)

Arguments

data

A data frame or a numeric vector.

formula

Formula (default: NULL). If data is a data frame, define the val ~ group.

alpha

Numeric (default: 0.05). Significance level used to determine whether the null hypothesis is rejected. Must be between 0 and 1.

method

Character. Abbreviation specifying the normality test to perform. Available options are c("AD", "CVM", "DAP", "JB", "LF", "SW", "SF", "SWR").

summary

Logical (default: TRUE). If TRUE, return a summary table of the test results.

Details

The method argument specifies the statistical procedure used to assess whether a sample is consistent with a normal distribution. Different tests emphasize different characteristics of departures from normality, such as skewness, kurtosis, or discrepancies in the tails of the distribution. Because no single test performs optimally under all circumstances, the choice of method may depend on sample size and the expected type of non-normality.

Available methods are:

  • "AD": Anderson–Darling test. A modification of the empirical distribution function (EDF) approach that gives greater weight to observations in the tails of the distribution. Compared with several alternative normality tests, the Anderson–Darling procedure is often more sensitive to deviations occurring in extreme values and tail behavior. This test is applicable only for sample sizes n >= 8.

  • "CVM": Cramér–von Mises test. An empirical distribution function (EDF)-based goodness-of-fit test that measures the overall discrepancy between the empirical and theoretical cumulative distribution functions by assigning relatively uniform weight across the entire distribution. Compared with the Anderson–Darling test, the Cramér–von Mises test is generally less sensitive to deviations in the tails but performs well for detecting overall departures from normality. This test is applicable only for sample sizes n >= 8.

  • "DAP": D'Agostino–Pearson test. A combined omnibus moment test based on sample skewness and kurtosis. The procedure transforms the skewness and kurtosis statistics into approximately standard normal variables and combines them into a single test statistic. This method is designed to detect a broad range of departures from normality rather than emphasizing any particular feature. This test is applicable only for sample sizes n >= 20.

  • "JB": Jarque–Bera test. An omnibus moment test based on sample skewness and kurtosis. The test evaluates whether the observed skewness and kurtosis differ significantly from the values expected under a normal distribution. The method is commonly used in econometrics and is generally more appropriate for moderate to large sample sizes.

  • "LF": Lilliefors test. The Lilliefors test is an EDF omnibus test modified from Kolmogorov-Smirnov test for the composite hypothesis of normality. The test statistic is the maximal absolute difference between empirical and hypothetical cumulative distribution function.

  • "SW": Shapiro–Wilk test. The original normality test proposed by Shapiro and Wilk (1965), based on the correlation between ordered observations and their expected values under normality. It is widely regarded as one of the most powerful tests for detecting departures from normality in small samples. Applicable only for sample sizes ⁠3 <= n <= 50⁠.

  • "SF": Shapiro–Francia test. Proposed by Shapiro and Francia (1972) and subsequently simplified and extended by Royston (1993). This method is a computationally simpler modification of the Shapiro–Wilk procedure that performs particularly well for detecting departures associated with heavier-tailed distributions. Applicable only for sample sizes ⁠5 <= n <= 5000⁠.

  • "SWR": Shapiro–Wilk test with Royston's modifications. Uses Royston's (1992) approximations for the null distribution of the Shapiro–Wilk statistic and extends applicability to larger samples while maintaining behavior similar to the original test. Applicable only for sample sizes ⁠3 <= n <= 5000⁠.

In all methods, the null hypothesis is that the sample is drawn from a normal distribution. Small p-values indicate evidence against the assumption of normality.

Value

A list.

Examples

out_AD <- check_normality(rnorm(20), method = "AD")
out_DAP <- check_normality(rnorm(20), method = "DAP")
out_SW <- check_normality(rnorm(20), method = "SW")

Cramer-von Mises Normality Test

Description

An empirical distribution function (EDF)-based goodness-of-fit test that measures the overall discrepancy between the empirical and theoretical cumulative distribution functions by assigning relatively uniform weight across the entire distribution.

Usage

Cramer_von_Mises_test(
  x,
  alpha = 0.05,
  silent = FALSE,
  summary = TRUE,
  misc = FALSE
)

Arguments

x

A numeric vector, at least length of 8.

alpha

Numeric (default: 0.05). Significance threshold, range from 0 to 1.

silent

Logical (default: FALSE). If FALSE, print out the results.

summary

Logical (default: TRUE). Produce a summary table.

misc

Logical (default: FALSE). Output other unimportant parameters.

Value

A list.

References

Thode, H. C., Jr., 2002. Goodness of fit tests. Testing for normality. Marcel Dekker, New York. (Section 5.1.3, pg. 103)

Stephens, M.A., 2017. Tests Based on EDF Statistics. In: D'Agostino, R.B., Stephens, M.A. (Eds.), Goodness-of-Fit Techniques, 1st ed. Routledge, New York, (Table 4.9, pg. 127). https://doi.org/10.1201/9780203753064

Examples

out <- Cramer_von_Mises_test(rnorm(10))
print(out$summary)

D'Agostino–Pearson K2 Normality Test

Description

The D'Agostino–Pearson chi-squared (K2) test is a moment-based omnibus test for normality.

Usage

D.Agostino_Pearson_test(
  x,
  alpha = 0.05,
  alternative = c("two.sided", "less", "greater"),
  silent = FALSE,
  summary = TRUE,
  misc = FALSE
)

Arguments

x

Numeric vector. Must have length at least 20.

alpha

Numeric (default: 0.05). Significance level for hypothesis testing. Must be between 0 and 1.

alternative

Character (default: "two.sided"). Specifies the alternative hypothesis. Available options are c("two.sided", "less", "greater"). Note that this option is only applied to the skewness and kurtosis components of the test.

silent

Logical (default: FALSE). If FALSE, results are printed to the console.

summary

Logical (default: TRUE). Produce a summary table.

misc

Logical (default: FALSE). Output other unimportant parameters.

Details

It evaluates the null hypothesis that the data come from a normal distribution by combining standardized measures of skewness and kurtosis into a single chi-squared test statistic.

Value

A list

References

D’Agostino, R.B., Belanger, A., D’Agostino, R.B., 1990. A Suggestion for Using Powerful and Informative Tests of Normality. Am. Stat. 44, 316–321. https://doi.org/10.1080/00031305.1990.10475751

Examples

out <- D.Agostino_Pearson_test(rnorm(50))
print(out$summary)

Normality test

Description

A handy wrapper for data normality assessment using the Shapiro-Wilk-Royston, D'Agostino-Pearson, and Anderson-Darling tests.

Usage

is_normal(data, formula = NULL, alpha = 0.05, sensitivity = 2, summary = FALSE)

Arguments

data

A data frame or a numeric vector.

formula

Formula (default: NULL). If data is a data frame, define the val ~ group.

alpha

Significance threshold, range from 0 to 1 (default: 0.05).

sensitivity

Numeric, range from 1 to 3 (default: 2). The greater the value, the greater chance to consider as non-normal.

summary

Logical (default: FALSE). If TRUE, show the summary table.

Value

A boolean value (or a list if summary = TRUE).

Examples

is_normal(iris, Sepal.Length ~ Species)

Tied data

Description

Tied data

Usage

is_tied(x, ratio = 0.3, remove_NA = FALSE)

Arguments

x

A numeric vector

ratio

Numeric (default: 0.3). The ratio threshold of being considred as tied-data. The value range from 0 to 1.

remove_NA

Logical (default: TRUE). Whether or not to remove NAs.

Value

Logical

Examples

is_tied(c(1, 1, 2, 2, 2, 3, 4, 5))
#> TRUE

Jarque-Bera Normality Test

Description

Performs the Jarque-Bera chi-squared test, a moment-based omnibus test for assessing normality.

Usage

Jarque_Bera_test(
  x,
  alpha = 0.05,
  alternative = c("two.sided", "less", "greater"),
  silent = FALSE,
  summary = TRUE
)

Arguments

x

Numeric vector. Must contain at least 20 observations.

alpha

Numeric (default: 0.05). Significance level for hypothesis testing. Must be between 0 and 1.

alternative

Character (default: "two.sided"). Specifies the alternative hypothesis. Available options are c("two.sided", "less", "greater"). This argument applies only to the skewness and kurtosis components and does not affect the Jarque-Bera omnibus test statistic itself.

silent

Logical (default: FALSE). If FALSE, results are printed to the console.

summary

Logical (default: TRUE). Produce a summary table.

Details

The test evaluates the null hypothesis that the data are drawn from a normal distribution by combining standardized measures of skewness and kurtosis into a single chi-squared test statistic.

Value

A list

References

Jarque, C.M., Bera, A.K., 1987. A Test for Normality of Observations and Regression Residuals. Int. Stat. Rev. 55, 163–172. https://doi.org/10.2307/1403192

See Also

D.Agostino_Pearson_test()

Examples

out <- Jarque_Bera_test(rnorm(50))
print(out$summary)

Kurtosis test

Description

Performs a kurtosis test to assess whether a distribution deviates from normality in terms of tail heaviness.

Usage

kurtosis(
  x,
  alpha = 0.05,
  alternative = c("two.sided", "less", "greater"),
  method = c("G2", "b2", "g2"),
  silent = FALSE,
  summary = TRUE
)

Arguments

x

Numeric vector containing the input data.

alpha

Numeric (default: 0.05). Significance level for hypothesis testing. Must be between 0 and 1.

alternative

Character (default: "two.sided"). Specifies the alternative hypothesis. Available options are c("two.sided", "less", "greater").

method

Character (default: "G2"). Formula used to estimate kurtosis. Available options are c("G2", "b2", "g2"). The "g2" statistic is the classical sample kurtosis estimator, while "G2" and "b2" are bias-corrected versions of "g2".

silent

Logical (default: FALSE). If FALSE, results are printed to the console.

summary

Logical (default: TRUE). Produce a summary table.

Details

The test evaluates the null hypothesis that the population kurtosis is equal to 3, which is the kurtosis of a normal distribution. Values significantly different from 3 indicate deviations from normality, such as heavy-tailed or light-tailed behavior.

Value

A list

References

Joanes, D.N., Gill, C.A., 1998. Comparing measures of sample skewness and kurtosis. J. R. Stat. Soc. D (The Statistician) 47, 183–189. https://doi.org/10.1111/1467-9884.00122

Wright, D.B., Herrington, J.A., 2011. Problematic standard errors and confidence intervals for skewness and kurtosis. Behav. Res. Methods 43, 8–17. https://doi.org/10.3758/s13428-010-0044-x

Examples

x <- c(10:17, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 15, 15)
kurtosis(x)

Lilliefors Normality Test

Description

Performs the Lilliefors normality test, which is an empirical distribution function (EDF)-based goodness-of-fit test derived from the Kolmogorov–Smirnov test, using the approximation proposed by Molin and Abdi (1998).

Usage

Lilliefors_test(x, alpha = 0.05, silent = FALSE, summary = TRUE, misc = FALSE)

Arguments

x

A numeric vector, at least length of 8.

alpha

Numeric (default: 0.05). Significance threshold, range from 0 to 1.

silent

Logical (default: FALSE). If FALSE, print out the results.

summary

Logical (default: TRUE). Produce a summary table.

misc

Logical (default: FALSE). Output other unimportant parameters.

Value

A list.

References

Molin, P., Abdi, H., 1998. New tables and numerical approximation for the Kolmogorov-Smirnov/Lillierfors/Van Soest test of normality. Technical report, University of Bourgogne.

Examples

out <- Lilliefors_test(rnorm(10))
print(out$summary)

Standard output format

Description

The standard output format for normality package.

Usage

normality_standard_output(
  method = "what test?",
  is_normal = NA,
  alpha = NA_real_,
  alternative = c("two.sided", "less", "greater"),
  summary = NULL,
  statistic = NA_real_,
  pvalue = NA_real_,
  misc = NULL
)

Arguments

method

Character. The name of the test.

is_normal

Logical. Is the input data normally distributed?

alpha

Numeric (default: 0.05). Significance threshold.

alternative

Character. The alternative hypothesis (H1) to test. Available options are c("two.sided", "less", "greater").

summary

Statistic summary, if any.

statistic

Numeric. The value used to calculate p-value.

pvalue

Numeric. The p-value of the test.

misc

List. Miscellaneous elements.

Value

A list.


Shapiro-Wilk normality test (coefficients)

Description

Coefficients (ai) for the W test for normality.

Usage

Shapiro_Wilk_coef_table

Format

A data frame with 50 rows and 25 variables:

rownames is the sample size (n); colnames is the corresponding coefficients (ai).

References

Shapiro, S.S., Wilk, M.B., 1965. An Analysis of Variance Test for Normality (Complete Samples). Biometrika 52, 591–611. https://doi.org/10.2307/2333709


Shapiro-Wilk normality test (p-values)

Description

The percentage points (critical values of W) of the W test for n = 3(1)50.

Usage

Shapiro_Wilk_pval_table

Format

A data frame with 50 rows and 10 variables:

rownames is the sample size (n); colnames is the corresponding p-values.

References

Shapiro, S.S., Wilk, M.B., 1965. An Analysis of Variance Test for Normality (Complete Samples). Biometrika 52, 591–611. https://doi.org/10.2307/2333709


Shapiro-Wilk Normality Test

Description

Performs the Shapiro–Wilk normality test, which assesses whether a sample originates from a normally distributed population using a regression-based correlation method.

Usage

Shapiro_Wilk_test(
  x,
  alpha = 0.05,
  method = c("SWR", "SF", "SW"),
  silent = FALSE,
  summary = TRUE,
  misc = FALSE,
  resampling = TRUE
)

Arguments

x

A numeric vector.

alpha

Significance threshold (default: 0.05).

method

Character (default: "SWR"). Use which modification of the test? Available options are c("SWR", "SF", "SW").

silent

Logical (default: FALSE). If FALSE, print out the results.

summary

Logical (default: TRUE). Produce a summary table.

misc

Logical (default: FALSE). Output other unimportant parameters.

resampling

Logical (default: TRUE). If TRUE, unlock the sample size limitation of the test by using sample resampling method.

Details

method

  • "SW": Shapiro–Wilk test, the original normality test proposed by Shapiro and Wilk (1965). Applicable only for sample sizes 3 <= n <= 50.

  • "SF": Shapiro–Francia test, proposed by Shapiro and Francia (1972) and subsequently simplified and extended by Royston (1993). Applicable only for sample sizes 5 <= n <= 5000.

  • "SWR": Shapiro–Wilk test with Royston's (1992) modifications for approximating the null distribution and extending the test to larger sample sizes. Applicable only for sample sizes 3 <= n <= 5000.

Value

A list.

References

Shapiro, S.S., Wilk, M.B., 1965. An Analysis of Variance Test for Normality (Complete Samples). Biometrika 52, 591–611. https://doi.org/10.2307/2333709

Shapiro, S.S., Francia, R.S., 1972. An Approximate Analysis of Variance Test for Normality. J. Am. Stat. Assoc. 67, 215–216. https://doi.org/10.1080/01621459.1972.10481232

Royston, P., 1993. A pocket-calculator algorithm for the Shapiro–Francia test for non-normality: an application to medicine. Stat. Med. 12, 181–184. https://doi.org/10.1002/sim.4780120209

Royston, P., 1992. Approximating the Shapiro–Wilk W-test for non-normality. Stat. Comput. 2, 117–119. https://doi.org/10.1007/BF01891203

Examples

sw <- Shapiro_Wilk_test(rnorm(20), method = "SW")
print(sw$summary)
sf <- Shapiro_Wilk_test(rnorm(100) ^ 2, method = "SF")
print(sf$summary)
swr <- Shapiro_Wilk_test(rnorm(1e6), method = "SWR")
print(swr$summary)

Skewness test

Description

The test evaluates whether the population skewness is equal to zero. Under the null hypothesis, the data are assumed to originate from a symmetric distribution. Significant positive or negative skewness indicates asymmetry in the distribution and may suggest a departure from normality.

Usage

skewness(
  x,
  alpha = 0.05,
  alternative = c("two.sided", "less", "greater"),
  method = c("G1", "b1", "g1"),
  silent = FALSE,
  summary = TRUE
)

Arguments

x

Numeric vector containing the input data.

alpha

Numeric (default: 0.05). Significance level for hypothesis testing. Must be between 0 and 1.

alternative

Character (default: "two.sided"). Specifies the alternative hypothesis. Available options are c("two.sided", "less", "greater").

method

Character (default: "G1"). Formula used to estimate skewness. Available options are c("G1", "b1", "g1"). The "g1" statistic is the conventional moment-based sample skewness. The "G1" and "b1" statistics apply finite-sample corrections to reduce the bias of "g1".

silent

Logical (default: FALSE). If FALSE, the test results are printed to the console.

summary

Logical (default: TRUE). Produce a summary table.

Value

A list

References

Joanes, D.N., Gill, C.A., 1998. Comparing measures of sample skewness and kurtosis. J. R. Stat. Soc. D (The Statistician) 47, 183–189. https://doi.org/10.1111/1467-9884.00122

Wright, D.B., Herrington, J.A., 2011. Problematic standard errors and confidence intervals for skewness and kurtosis. Behav. Res. Methods 43, 8–17. https://doi.org/10.3758/s13428-010-0044-x

Examples

skewness(rnorm(30))