test.levene: Levene's Test for Homogeneity of Variance

View source: R/test.levene.R

test.leveneR Documentation

Levene's Test for Homogeneity of Variance

Description

This function performs Levene's test for homogeneity of variance across two or more independent groups including a plot showing violin plots and boxplots representing the distribution of the outcome variable for each group.

Usage

test.levene(formula, data, method = c("median", "mean"), hypo = FALSE,
            descript = FALSE, conf.level = 0.95, digits = 2, p.digits = 3,
            as.na = NULL, plot = FALSE, violin = TRUE, box = TRUE, jitter = FALSE,
            gray = FALSE, filename = NULL, width = NA, height = NA, dpi = 600,
            write = NULL, append = TRUE, check = TRUE, output = TRUE)

Arguments

formula

a formula of the form y ~ group where y is a numeric variable giving the data values and group a numeric variable, character variable or factor with two or more than two values or factor levels giving the corresponding groups.

data

a matrix or data frame containing the variables in the formula formula.

method

a character string specifying the method to compute the center of each group, i.e. method = "median" (default) to compute the Levene's test based on the median (aka Brown-Forsythe test) or method = "mean" to compute the Levene's test based on the arithmetic mean.

hypo

logical: if TRUE, null and alternative hypothesis are shown on the console.

descript

logical: if TRUE, descriptive statistics are shown on the console.

conf.level

a numeric value between 0 and 1 indicating the confidence level of the interval.

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 violins with boxplots is drawn.

violin

logical: if TRUE (default), violins are drawn.

box

logical: if TRUE (default), boxplots are drawn.

jitter

logical: if TRUE (default), jittered data points are drawn.

gray

logical: if TRUE, the plot is drawn in gray scale.

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 specifying 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: 600) for the ggsave function.

write

a character string naming a file for writing the output into either a text file with file extension ".txt" (e.g., "Output.txt") or Excel file with file extension ".xlsx" (e.g., "Output.xlsx"). If the file name does not contain any file extension, an Excel file will be written.

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.

Details

Levene's Test

The Levene's test is equivalent to a one-way analysis of variance (ANOVA) with the absolute deviations of observations from the mean of each group as dependent variable (center = "mean"). Brown and Forsythe (1974) modified the Levene's test by using the absolute deviations of observations from the median (center = "median"). By default, the Levene's test uses the absolute deviations of observations from the median.

Value

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

call

function call

type

type of analysis

data

data frame with the outcome and grouping variable

formula

formula

args

specification of function arguments

plot

ggplot2 object for plotting the results

result

result table

Author(s)

Takuya Yanagida takuya.yanagida@univie.ac.at

References

Brown, M. B., & Forsythe, A. B. (1974). Robust tests for the equality of variances. Journal of the American Statistical Association, 69, 364-367.

Rasch, D., Kubinger, K. D., & Yanagida, T. (2011). Statistics in psychology - Using R and SPSS. John Wiley & Sons.

See Also

aov.b, test.t, test.welch

Examples

#————————————————————————————————————————————————————————————————————————————
# Levene's Test

# Example 1a: Levene's test based on the median
test.levene(mpg ~ gear, data = mtcars)

# Example 1b: Levene's test based on the arithmetic mean
test.levene(mpg ~ gear, data = mtcars, method = "mean")

# Example 1c: Levene's test, print descriptive statistics
test.levene(mpg ~ gear, data = mtcars, descript = TRUE)

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

# Example 2a: Plot results, default setting
test.levene(mpg ~ gear, data = mtcars, plot = TRUE)

# Example 2b: Plot results, no violin plots, draw jittered data points
test.levene(mpg ~ gear, data = mtcars, plot = TRUE, violin = FALSE, jitter = TRUE)

# Example 2c: Plot results using the plot() function, use additional arguments
# see Details in the help page of the function plot.misty.object
object <- test.levene(mpg ~ gear, data = mtcars)
plot(object, violin.alpha = 0.1, box.width = 0.1, title = "Levene's Test")

#————————————————————————————————————————————————————————————————————————————
# Create Plot Manually

# Load ggplot2 package
library(ggplot2)

# Create misty object
object <- test.levene(mpg ~ gear, data = mtcars)

# Example 3: Plot
ggplot(object$data, aes(group, y, fill = group)) +
  geom_violin(alpha = 0.3, trim = FALSE) +
  geom_boxplot(alpha = 0.2, width = 0.2) +
  geom_jitter(alpha = 0.2, width = 0.05, height = 0, size = 1.25) +
  theme_bw() +
   ggplot2::guides(fill = "none")

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

## Not run: 

# Example 4a: Write results into a text file
test.levene(mpg ~ gear, data = mtcars, write = "Levene.txt")

# Example 4b: Write results into an Excel file
test.levene(mpg ~ gear, data = mtcars, write = "Levene.xlsx")

# Example 4c: Save plot as PNG fine
test.levene(mpg ~ gear, data = mtcars, plot = TRUE,
            filename = "Levene-Test.png", width = 6, height = 5)

## End(Not run)

misty documentation built on Aug. 2, 2026, 9:06 a.m.

Related to test.levene in misty...