Perform a test of normality for each group separately. The available tests: Shapiro-Wilk (default), Lilliefors (Kolmogorov-Smirnov), Anderson-Darling and other.

test_normality(
  y,
  data = NULL,
  test = "Shapiro-Wilk",
  p_adjust_method = NULL,
  ...,
  groups = NULL,
  ss = signif_syms,
  hide_error_msg = FALSE
)

# S3 method for test_normality
print(
  x,
  ...,
  digits_p = 3,
  signif_stars = !is.null(ss),
  digits_stat = 3,
  format_stat = c("auto", "f", "g"),
  rm_zero = FALSE,
  legend = TRUE,
  show_col_method = FALSE,
  hide_error_msg = attr(x, "hide_error_msg"),
  ss = attr(x, "ss")
)

# S3 method for test_normality
pander(
  x,
  caption = NA,
  ...,
  digits_p = 3,
  signif_stars = !is.null(ss),
  digits_stat = 3,
  format_stat = c("auto", "f", "g"),
  rm_zero = FALSE,
  legend = TRUE,
  show_col_method = FALSE,
  hide_error_msg = attr(x, "hide_error_msg"),
  ss = attr(x, "ss")
)

Arguments

y

(formula|numeric|character)
Either a formula, a numeric vector or a name of a column in data.

  • If y is a formula (e.g. variable ~ factor), left-hand side provides the name of a variable to be tested. In the right-hand side there are names of factor variables to be used to create subsets. If the left-hand side is empty (e.g. ~ factor), right-hand side is treated as variable name to test.

data

(data frame|NULL)
Either a data frame that contains the variables mentioned in y or NULL (if the variables are in the function's environment).

test

(string | function)
Either a function that carries out a normality test or a string (case insensitive, maybe unambiguously abbreviated) with a name of a normality test. Possible names of tests:

  • "SW", "Shapiro-Wilk" — for Shapiro-Wilk test;

  • "Lilliefors" — for Kolmogorov-Smirnov test with Lilliefor's correction;

  • "AD", "Anderson-Darling" — for Anderson-Darling test;

  • "CVM", "CM", "Cramer-von Mises" — for Cramer-von Mises test;

  • "SF", "Shapiro-Francia" — for Shapiro-Francia test;

  • "Chi-squared","Pearsons" — for Pearson's chi-squared test of normality.

p_adjust_method

(NULL|string)
A name of p value adjustment method for multiple comparisons. For available methods check stats::p.adjust.methods(). If NULL, no adjusted p value is calculated (default). If string (e.g., "holm"), an additional column p.adjust is calculated.

...

Further parameters to the function of normatity test.

groups

(NULL|factor|character)
An alternative way to provide groups. If y is a numeric vector, groups must be a factor (or NULL). If y is a sting, groups must also be a string (or NULL).

ss

# FIXME: not documented

hide_error_msg

(logical)
If FALSE column "error_msg" is not printed. If TRUE the column is printed if error occurs in calcultations. Default is FALSE.

x

normality_test object.

digits_p

(numeric) Number of significant digits to round a p value to. No less than 2.

  • if digits_p = 2:

    1. values below 0.001 are printed as "<0.001";

    2. values between 0.001 and 0.01 as "<0.01";

    3. all other values are rounded to two decimal places.

  • if digits_p = 3, only formatting "<0.01" is skipped.

signif_stars

(logical)
If TRUE, significance stars are printed.

digits_stat

(integer)
Either a number of decimal places or number of significant digits to round test statistic to.

format_stat

(character)
Number format "f", "g" or "auto" (default) for test statistic. More about number formats "f" and "g" you can find in documentation of function base::formatC() section format.

rm_zero

(logical) Flag if leading zero before the point should be removed.

legend

(logical)
If TRUE, legend for significance stars is printed.

show_col_method

(logical)
If FALSE column "method" is not printed. Default is FALSE.

caption

(string|NULL|NA)
A caption for the table with results of a normality test. If NA — a default caption is printed (default). If NULL – no caption is printed.

Value

  • Function test_normality returns a data frame with normality test results for each group.

  • print and pander methods format and print the results. By default, methods print.test_normality and pander.test_normality do not print column called "method".

See also

Examples

library(biostat) library(pander) data(CO2) # For whole dataset test_normality(~uptake, data = CO2)
#> #> The results of Shapiro-Wilk normality test #> #> statistic p.value #> 1 0.941 <0.001 *** #> #> *** - p < 0.001, ** - p < 0.01, * - p < 0.05, . - p < 0.1
# For each subgroup test_normality(uptake ~ Treatment, data = CO2)
#> #> The results of Shapiro-Wilk normality test #> #> Treatment statistic p.value #> 1 nonchilled 0.945 0.043 * #> 2 chilled 0.898 0.001 ** #> #> *** - p < 0.001, ** - p < 0.01, * - p < 0.05, . - p < 0.1
# For each subgroup by several factor variables rez <- test_normality(uptake ~ Type + Treatment, data = CO2) rez
#> #> The results of Shapiro-Wilk normality test #> #> Type Treatment statistic p.value #> 1 Quebec nonchilled 0.825 0.002 ** #> 2 Quebec chilled 0.853 0.005 ** #> 3 Mississippi nonchilled 0.855 0.005 ** #> 4 Mississippi chilled 0.948 0.315 #> #> *** - p < 0.001, ** - p < 0.01, * - p < 0.05, . - p < 0.1
# Modify printed output print(rez, rm_zero = TRUE)
#> #> The results of Shapiro-Wilk normality test #> #> Type Treatment statistic p.value #> 1 Quebec nonchilled .825 .002 ** #> 2 Quebec chilled .853 .005 ** #> 3 Mississippi nonchilled .855 .005 ** #> 4 Mississippi chilled .948 .315 #> #> *** - p < 0.001, ** - p < 0.01, * - p < 0.05, . - p < 0.1
print(rez, digits_p = 2)
#> #> The results of Shapiro-Wilk normality test #> #> Type Treatment statistic p.value #> 1 Quebec nonchilled 0.825 <0.01 ** #> 2 Quebec chilled 0.853 <0.01 ** #> 3 Mississippi nonchilled 0.855 <0.01 ** #> 4 Mississippi chilled 0.948 0.31 #> #> *** - p < 0.001, ** - p < 0.01, * - p < 0.05, . - p < 0.1
# Choose another test of normality rez2 <- test_normality(uptake ~ Type + Treatment, data = CO2, test = "chi-sq") rez2
#> #> The results of Pearson chi-square normality test #> #> Type Treatment statistic p.value #> 1 Quebec nonchilled 13.3 0.010 ** #> 2 Quebec chilled 13.3 0.010 ** #> 3 Mississippi nonchilled 10.0 0.040 * #> 4 Mississippi chilled 5.3 0.255 #> #> *** - p < 0.001, ** - p < 0.01, * - p < 0.05, . - p < 0.1
# Print as a 'pandoc' table (for R Markdown reports) pander(rez)
#> #> ------------------------------------------------- #> Type Treatment statistic p.value #> ------------- ------------ ----------- ---------- #> Quebec nonchilled 0.825 0.002 ** #> #> Quebec chilled 0.853 0.005 ** #> #> Mississippi nonchilled 0.855 0.005 ** #> #> Mississippi chilled 0.948 0.315 #> ------------------------------------------------- #> #> Table: The results of Shapiro-Wilk normality test. #> #> *** - p < 0.001 #> ** - p < 0.01 #> * - p < 0.05 #> . - p < 0.1
pander(rez, digits_stat = 2)
#> #> ------------------------------------------------- #> Type Treatment statistic p.value #> ------------- ------------ ----------- ---------- #> Quebec nonchilled 0.83 0.002 ** #> #> Quebec chilled 0.85 0.005 ** #> #> Mississippi nonchilled 0.86 0.005 ** #> #> Mississippi chilled 0.95 0.315 #> ------------------------------------------------- #> #> Table: The results of Shapiro-Wilk normality test. #> #> *** - p < 0.001 #> ** - p < 0.01 #> * - p < 0.05 #> . - p < 0.1
pander(rez, digits_p = 2)
#> #> ------------------------------------------------- #> Type Treatment statistic p.value #> ------------- ------------ ----------- ---------- #> Quebec nonchilled 0.825 <0.01 ** #> #> Quebec chilled 0.853 <0.01 ** #> #> Mississippi nonchilled 0.855 <0.01 ** #> #> Mississippi chilled 0.948 0.31 #> ------------------------------------------------- #> #> Table: The results of Shapiro-Wilk normality test. #> #> *** - p < 0.001 #> ** - p < 0.01 #> * - p < 0.05 #> . - p < 0.1
pander(rez, digits_p = 4, signif_stars = FALSE)
#> #> ------------------------------------------------ #> Type Treatment statistic p.value #> ------------- ------------ ----------- --------- #> Quebec nonchilled 0.825 0.0016 #> #> Quebec chilled 0.853 0.0048 #> #> Mississippi nonchilled 0.855 0.0052 #> #> Mississippi chilled 0.948 0.3147 #> ------------------------------------------------ #> #> Table: The results of Shapiro-Wilk normality test. #>
if (FALSE) { # View unformatted results in a separate window View(rez) } # Show object's class class(rez)
#> [1] "test_normality" "data.frame"