Written and maintained by CASRAI Editorial Board
Published
On this page
To run Levene’s test in R, install and load the car package and call leveneTest() with a response and a grouping factor, for example leveneTest(len ~ dose, data = df). The null hypothesis is that all group variances are equal; a p-value below your chosen significance level (conventionally 0.05) is evidence that at least one group variance differs. By default the function centres each group on its median, which is the robust Brown-Forsythe variant; center = mean gives the original Levene test.
What Levene’s test is used for in research
Levene’s test checks the assumption of homogeneity of variance that sits behind the classic independent-samples t-test and the one-way and factorial analysis of variance. Before you read a group comparison, you want to know whether the spread of the response is about the same in every group. The NIST/SEMATECH e-Handbook of Statistical Methods defines the test with the null hypothesis that the variances of all k groups are equal (σ1² = σ2² = … = σk²) against the alternative that they are not equal for at least one pair of groups.
The test statistic is built from absolute deviations. Each observation is replaced by Z = |Y − group centre|, and W is the ratio of the between-group to the within-group sum of squares of those Z values, scaled by (N − k)/(k − 1). The null hypothesis is rejected at level α when W exceeds the upper critical value of the F distribution with k − 1 and N − k degrees of freedom. Because the test is an ordinary F test on deviations, R prints it in an analysis-of-variance layout.
The handbook notes that Brown and Forsythe (1974) extended the test to use the median or a trimmed mean as the centre, and it recommends the median-based definition for its robustness combined with good power. That is why the R default is the median. For the general theory, assumptions and a conceptual walk-through, see the CASRAI guide to Levene’s test of equality of variances; this page is the R implementation.
How to install or access Levene’s test in R
The standard implementation is leveneTest() in the car package (Companion to Applied Regression, by John Fox, Sanford Weisberg and Brad Price). The package is on CRAN, where the page lists version 3.1-5 published 2026-02-03, a dependency on R 3.5.0 or later, and the DOI 10.32614/CRAN.package.car. Install it once, then load it in every session:
- Open R or RStudio.
- Run
install.packages("car")and choose a CRAN mirror if prompted. - Run
library(car)at the top of every script that usesleveneTest().
If you are new to installing packages, the CASRAI guide to how to install R packages covers the mechanics. Two alternatives need no car installation: bartlett.test() and fligner.test() are in the stats package that ships with R. A third option, levene_test(), is in the rstatix package and is a wrapper around leveneTest(), so it still depends on car.
How to run Levene’s test in R step by step
The documentation for leveneTest() gives three call forms. They all compute the same test; they differ in how you describe the groups.
Step 1: put the grouping variable in a factor
For the formula and linear-model forms, the car documentation states that the right-hand-side variables must all be factors and must be completely crossed. A numeric dose column such as 0.5, 1 and 2 is the usual way to break this rule. Convert it first, using R’s built-in ToothGrowth data as the example:
library(car)df = ToothGrowthdf$dose = factor(df$dose)
Step 2: choose a call form
Use whichever form matches the objects you already have.
- Response vector and group factor (the default method):
leveneTest(df$len, df$dose). The signature isleveneTest(y, group, center = median, ...), whereyis the response andgroupis the factor that defines the groups. The documentation examplewith(Moore, leveneTest(conformity, fcategory))uses the same form. - Formula and data:
leveneTest(len ~ dose, data = df). This is the form most analysts use, because it mirrors the model formula you give toaov()orlm(). - A fitted linear model:
leveneTest(lm(len ~ dose, data = df)). The method accepts anlmobject, so you can test the model you are about to interpret without retyping its terms.
Step 3: test more than one factor
With two factors, a formula with an interaction treats every combination of the factors as a group. The car documentation shows leveneTest(conformity ~ fcategory*partner.status, data = Moore). For the ToothGrowth data the equivalent is:
df$supp = factor(df$supp)leveneTest(len ~ supp * dose, data = df)
This is the check to run before a two-way analysis of variance. It tests all six supplement-by-dose cells together rather than each factor in isolation.
Step 4: choose the centre
The center argument is a function that computes each group’s centre. According to the documentation, mean gives the original Levene’s test, while the default, median, provides a more robust test. Extra arguments are passed through to the centre function, so a trimmed mean is available with the trim argument:
leveneTest(len ~ dose, data = df) # median: Brown-Forsythe form (default)leveneTest(len ~ dose, data = df, center = mean) # original Levene testleveneTest(len ~ dose, data = df, center = mean, trim = 0.1) # 10% trimmed mean
Report which centre you used. “Levene’s test” without it is ambiguous, because the default in R is not the original mean-centred statistic. If a method section says the test was run in R with default settings, a reader can infer the median centre; stating it removes the need to infer.
Step 5: use the pipe-friendly version when you have grouped data
The rstatix package provides levene_test(data, formula, center = median), described in its documentation as a wrapper around leveneTest() that also handles grouped data. It returns a data frame rather than a printed object, with the columns df1, df2, statistic and p:
library(rstatix)levene_test(df, len ~ dose)
The rstatix examples also pipe the data in (df %>% levene_test(len ~ dose)) and apply group_by() first to run a separate Levene test within each level of another factor. Because the result is a tidy data frame, it is easy to store, join to other results or pass to a reporting function.
How to read the output
The printed result of leveneTest() is a small analysis-of-variance table with three columns: Df, F value and Pr(>F). There are two degrees-of-freedom entries. The first, on the group row, is k − 1, where k is the number of groups; the second, on the residual row, is N − k, where N is the total number of observations. These are the same two values NIST gives for the F distribution used to judge W, so you can check that your output matches your design: three dose groups with 60 observations should show 2 and 57.
Read it in three moves:
- Find the p-value in the Pr(>F) column. The F value is the statistic W described above; the p-value is the probability, under equal variances, of a statistic at least that large.
- Compare it with your significance level. A p-value at or below α means you reject the null hypothesis of equal variances. A p-value above α means the test gives no evidence of unequal variances; it does not prove they are equal.
- Decide what the result changes. If variances differ, a plain pooled-variance t-test or one-way ANOVA is no longer the straightforward choice. Welch-type tests, rank-based tests such as the one in the Kruskal-Wallis test in R guide, or a variance-robust post hoc test such as Games-Howell are the usual next steps, and the choice depends on your design.
If you have used the SPSS version of the test, the same logic applies; the CASRAI guide to Levene’s test in SPSS shows how the SPSS output maps to the same decision. For the R version of a two-group comparison, see how to run a t-test in R.
A practical note on reporting: give the centre, both degrees of freedom, the F value and the exact p-value, for example “Levene’s test (median-centred), F(2, 57) = …, p = …”, filling the figures from your own output. This page does not print example numbers because they should come from your data.
Troubleshooting Levene’s test in R
Error: could not find function “leveneTest”
The car package is not loaded. Run library(car) in the same session. If R reports that there is no package called car, run install.packages("car") first.
The groups are numeric codes, not factors
The formula and linear-model forms expect factors on the right-hand side, completely crossed. Wrap numeric codes in factor() before the call, as in Step 1. For the vector form, make sure the group argument is the factor that defines the groups, not a second numeric measurement.
Missing cells in a factorial design
“Completely crossed” means every combination of the factors has data. If one combination is empty, the interaction form does not describe a complete design. Check the cell counts with table() on the factors before testing.
The result differs from SPSS, Python or an older tutorial
Check the centre. The R default is the median; center = mean reproduces the original Levene test. Software and tutorials differ in which centre they default to, so a difference in p-value usually traces to this argument rather than to a mistake.
You wanted a test that does not depend on the car package
Use bartlett.test() or fligner.test() from base R. Both accept either a response vector with a grouping vector, or a formula with data. The NIST handbook describes Levene’s test as an alternative to Bartlett’s test, which is why the two are often named together.
Free alternatives to leveneTest()
R itself is free, as are the car and rstatix packages; the useful alternatives are other functions that test the same null hypothesis of equal variances. The data table below compares four of them by package, call form and what the documentation says each one does. In short: levene_test() adds convenience on top of leveneTest(); fligner.test() is the Fligner-Killeen test, which the stats documentation describes as a median-based test of the null that the variances are the same in each group; and bartlett.test() is Bartlett’s test, the classical alternative that NIST contrasts with Levene’s.
Choose between them by the assumption you can defend, not by the p-value you prefer. Run one pre-planned test and report it; trying several and quoting the one that supports your analysis is a form of selective reporting.
Sources
- car package: leveneTest() reference manual page (Levene’s test for homogeneity of variance), read 2026-11-17.
- CRAN: car, Companion to Applied Regression (package page, version 3.1-5), read 2026-11-17.
- NIST/SEMATECH e-Handbook of Statistical Methods, section 1.3.5.10: Levene test for equality of variances, read 2026-11-17.
- rstatix package: levene_test() reference manual page, read 2026-11-17.
- R stats package: bartlett.test() reference manual page, read 2026-11-17.
- R stats package: fligner.test() reference manual page, read 2026-11-17.
Data: R functions for testing equality of variances across groups
Four R functions that test the null hypothesis that group variances are equal, compared by package, call form and what each one's reference documentation states about it. Compiled from the car, rstatix and R stats reference manual pages and the NIST/SEMATECH e-Handbook.
| Function | Package | Installation | Call form in the documentation | What the documentation says |
|---|---|---|---|---|
| leveneTest() | car | install.packages("car") | leveneTest(y, group, center = median); formula and data; lm object | Default centre is the median (robust); center = mean gives the original Levene's test; trim passes through to the centre function |
| levene_test() | rstatix | install.packages("rstatix") | levene_test(data, formula, center = median) | Wrapper around leveneTest(); handles grouped data; returns a data frame with df1, df2, statistic and p |
| bartlett.test() | stats | None (ships with R) | x and g, or formula and data | Tests the null that the variances in each group are the same |
| fligner.test() | stats | None (ships with R) | x and g, or formula and data | Fligner-Killeen (median) test of the null that the variances in each group are the same |
Sources
- car: leveneTest reference page (accessed 2026-11-17)
- rstatix: levene_test reference page (accessed 2026-11-17)
- R stats: bartlett.test reference page (accessed 2026-11-17)
- R stats: fligner.test reference page (accessed 2026-11-17)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







