({{, (RefLink[LeveneTest, paclet:ref/LeveneTest])[data] 
tests whether the variance of data is 1. }, {, (RefLink[LeveneTest, paclet:ref/LeveneTest])[{(Subscript[data, 1]),(Subscript[data, 2]),…}]
tests whether the variances of (Subscript[data, 1]), (Subscript[data, 2]), … are equal.}, {, (RefLink[LeveneTest, paclet:ref/LeveneTest])[dspec,(Subscript[σ, 0]^2)]
tests a dispersion measure against (Subscript[σ, 0]^2).}, {, (RefLink[LeveneTest, paclet:ref/LeveneTest])[dspec,(Subscript[σ, 0]^2),"property"]
returns the value of "property".}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[LeveneTest, paclet:ref/LeveneTest]) tests the null hypothesis (Subscript[H, 0]) against the alternative hypothesis (Subscript[H, a]):

where ((Subscript[σ, i])^2) is the population variance for (Subscript[data, i]).

By default, a probability value or p-value is returned.

A small p-value suggests that it is unlikely that (Subscript[H, 0]) is true.

The data in dspec must be univariate {(Subscript[x, 1]),(Subscript[x, 2]),…}.

The argument (Subscript[σ, 0]^2) can be any positive real number. The default value of (Subscript[σ, 0]^2) is 1 if not specified, and ignored if the number of groups in dspec is more than 2.

The (RefLink[LeveneTest, paclet:ref/LeveneTest]) assumes the data is normally distributed and, for the two-sample case, is much less sensitive to this assumption than the (RefLink[FisherRatioTest, paclet:ref/FisherRatioTest]).

(RefLink[LeveneTest, paclet:ref/LeveneTest])[data,(Subscript[σ, 0]^2),"HypothesisTestData"] returns a (RefLink[HypothesisTestData, paclet:ref/HypothesisTestData]) object htd that can be used to extract additional test results and properties using the form htd["property"].

(RefLink[LeveneTest, paclet:ref/LeveneTest])[data,(Subscript[σ, 0]^2),"property"] can be used to directly give the value of "property".

Properties related to the reporting of test results include:

When one sample of size n is given, the (RefLink[LeveneTest, paclet:ref/LeveneTest]) is equivalent to the (RefLink[FisherRatioTest, paclet:ref/FisherRatioTest]).

For the k-sample case {(Subscript[data, 1]),(Subscript[data, 2]),…,(Subscript[data, k])} with (Subscript[data, i])={(Subscript[x, i,1]),(Subscript[x, i,2]),…,(Subscript[x, i,(Subscript[n, i])])}, the test statistic is given by ((((∑_(i=1)^k)((Subscript[n, i])-1)) (∑_(i=1)^k)(((Subscript[n, i])((Subscript[z, i])-z))^2))/((k-1) (∑_(i=1)^k)(∑_(j=1)^(Subscript[n, i]))((((Subscript[z, i,j])-(Subscript[z, i])))^2))), where (Subscript[z, i,j])=(RefLink[Abs, paclet:ref/Abs])[(Subscript[x, i,j])-(RefLink[Mean, paclet:ref/Mean])[(Subscript[data, i])]], (Subscript[z, i])=(RefLink[Mean, paclet:ref/Mean])[{(Subscript[z, i,1]),(Subscript[z, i,2]),…,(Subscript[z, i,(Subscript[n, i])])}], and z=(RefLink[Mean, paclet:ref/Mean])[{(Subscript[z, 1]),(Subscript[z, 2]),…,(Subscript[z, k])}]. The test statistic is assumed to follow (RefLink[FRatioDistribution, paclet:ref/FRatioDistribution])[k-1,(∑_(i=1)^k)((Subscript[n, i])-1)] under (Subscript[H, 0]).

The following options can be used:

For the (RefLink[LeveneTest, paclet:ref/LeveneTest]), a cutoff α is chosen such that (Subscript[H, 0]) is rejected only if p<α. The value of α used for the "TestConclusion" and "ShortTestConclusion" properties is controlled by the (RefLink[SignificanceLevel, paclet:ref/SignificanceLevel]) option. This value α is also used in diagnostic tests of assumptions, including tests for normality and symmetry. By default, α is set to 0.05.

Named settings for (RefLink[VerifyTestAssumptions, paclet:ref/VerifyTestAssumptions]) in (RefLink[LeveneTest, paclet:ref/LeveneTest]) include:

OpenersPrimaryExamplesSection(Spacer1)Examples  (33)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Test variances from two populations for equality:

data=RandomVariate[NormalDistribution[],{2,100}];

LeveneTest[data]

Create a (RefLink[HypothesisTestData, paclet:ref/HypothesisTestData]) object for further property extraction:

ℋ=LeveneTest[data,Automatic,"HypothesisTestData"]

Properties of the test:

ℋ["Properties"]

Compare the variance of a population to a particular value:

data=RandomVariate[NormalDistribution[1,2.1],(10^3)];

LeveneTest[data,4,"TestDataTable"]

Variance[data]

Test against the alternative hypothesis (σ^2)>4:

LeveneTest[data,4,"TestDataTable",AlternativeHypothesis->"Greater"]

OpenersExampleSection1(Spacer1)Scope  (10)

OpenersExampleSubsection0(Spacer1)Testing  (8)

Test whether the variance of a population is 1:

SeedRandom[3];

data1=RandomVariate[NormalDistribution[],500];

The p-value is uniformly distributed in [0,1] under (Subscript[H, 0]):

LeveneTest[data1]

The histogram of a sample of p-values of the Levene test:

sample=Table[Quiet@LeveneTest[RandomVariate[NormalDistribution[0,1],100]],{10^3}];

Histogram[sample,Automatic, PDF]

The p-value is typically small when (Subscript[H, 0]) is false:

SeedRandom[3];

data2=RandomVariate[NormalDistribution[1,3],500];

LeveneTest[data2]

Compare the variance of a population to a particular value:

SeedRandom[3];

data1=RandomVariate[NormalDistribution[0,2],(10^4)];
data2=RandomVariate[NormalDistribution[0,2.1],(10^4)];

LeveneTest[data1,(2^2)]

LeveneTest[data2,(2^2)]

Compare the variances of two populations:

SeedRandom[3];

data1=RandomVariate[NormalDistribution[0,3],500];
data2=RandomVariate[NormalDistribution[1,3],500];

The p-value is uniformly distributed in [0,1] under (Subscript[H, 0]):

LeveneTest[{data1,data2}]

The histogram of a sample of p-values of the Levene test:

sample=Table[Quiet@LeveneTest[{RandomVariate[NormalDistribution[0,3],100],RandomVariate[NormalDistribution[1,3],100]}],{5 10^3}];

Histogram[sample,Automatic, PDF]

The p-value is typically small when the variances are not equal:

SeedRandom[3];

data3=RandomVariate[NormalDistribution[10,1],350];

LeveneTest[{data1,data3}]

Test whether the ratio of the variances of two populations is a particular value:

SeedRandom[3];

data1=RandomVariate[NormalDistribution[0,2],500];
data2=RandomVariate[NormalDistribution[0,1],500];

(Subscript[σ, 0])=4;

The following forms are equivalent:

LeveneTest[{data1,data2},(Subscript[σ, 0])]

LeveneTest[{data2,data1},1/(Subscript[σ, 0])]

The order of the datasets should be considered when determining (Subscript[σ, 0]):

LeveneTest[{data2,data1},(Subscript[σ, 0])]

Test whether the variances of three populations are identical:

SeedRandom[3];

data1=RandomVariate[NormalDistribution[10,1],500];
data2=RandomVariate[NormalDistribution[0,1],200];
data3=RandomVariate[NormalDistribution[-3,1],400];

LeveneTest[{data1,data2,data3}]

Create a (RefLink[HypothesisTestData, paclet:ref/HypothesisTestData]) object for repeated property extraction:

SeedRandom[3];

data=RandomVariate[NormalDistribution[],{2,(10^4)}];

ℋ=LeveneTest[data,1,"HypothesisTestData"];

The properties available for extraction:

ℋ["Properties"]

Extract some properties from a (RefLink[HypothesisTestData, paclet:ref/HypothesisTestData]) object:

SeedRandom[3];

data=Table[RandomVariate[NormalDistribution[0,1],i],{i,{1000,2000,3000}}];

ℋ=LeveneTest[data,1,"HypothesisTestData"];

The p-value, test statistic, and degrees of freedom:

ℋ["PValue"]

ℋ["TestStatistic"]

ℋ["DegreesOfFreedom"]

Extract any number of properties simultaneously:

SeedRandom[3];

data=RandomVariate[NormalDistribution[],{2,100}];

ℋ=LeveneTest[data,1,"HypothesisTestData"];

The p-value, test statistic, and degrees of freedom:

ℋ["PValue","TestStatistic","DegreesOfFreedom"]

OpenersExampleSubsection1(Spacer1)Reporting  (2)

Tabulate test results:

SeedRandom[3];

data=RandomVariate[NormalDistribution[],{2,(10^3)}];

ℋ=LeveneTest[data,1,"HypothesisTestData"];

ℋ["TestDataTable"]

The values from the table can be extracted using "TestData":

ℋ["TestData"]

Tabulate p-values or test statistics:

SeedRandom[3];

data=RandomVariate[NormalDistribution[],{3,(10^4)}];

ℋ=LeveneTest[data,1,"HypothesisTestData"];

ℋ["PValueTable"]

The p-value from the table:

ℋ["PValue"]

ℋ["TestStatisticTable"]

The test statistic from the table:

ℋ["TestStatistic"]

OpenersExampleSection2(Spacer1)Options  (8)

OpenersExampleSubsection2(Spacer1)AlternativeHypothesis  (3)

A two-sided test is performed by default:

data=RandomVariate[NormalDistribution[],100];

Test (Subscript[H, 0]):(σ^2)=1 versus (Subscript[H, a]):(σ^2)≠1:

LeveneTest[data,1,AlternativeHypothesis->"Unequal"]

LeveneTest[data,1,AlternativeHypothesis->Automatic]

Perform a two-sided test or a one-sided alternative:

data=RandomVariate[NormalDistribution[0,1.25],100];

Test (Subscript[H, 0]):(σ^2)=1 versus (Subscript[H, a]):(σ^2)≠1:

LeveneTest[data,Automatic,AlternativeHypothesis->"Unequal"]

Test (Subscript[H, 0]):(σ^2)≥1 versus (Subscript[H, a]):(σ^2)<1:

LeveneTest[data,Automatic,AlternativeHypothesis->"Less"]

Test (Subscript[H, 0]):(σ^2)≤1 versus (Subscript[H, a]):(σ^2)>1:

LeveneTest[data,Automatic,AlternativeHypothesis->"Greater"]

Perform tests with one-sided alternatives when a null value is given:

data1=RandomVariate[NormalDistribution[0,.9],100];
data2=RandomVariate[NormalDistribution[0,1],100];

Variance[data1]/Variance[data2]

Test  (Subscript[H, 0]):(Subscript[σ, 1]^2)/(Subscript[σ, 2]^2)≥1 versus  (Subscript[H, a]):(Subscript[σ, 1]^2)/(Subscript[σ, 2]^2)<1:

LeveneTest[{data1,data2},1,AlternativeHypothesis->"Less"]

Test  (Subscript[H, 0]):(Subscript[σ, 1]^2)/(Subscript[σ, 2]^2)≥1.5 versus  (Subscript[H, a]):(Subscript[σ, 1]^2)/(Subscript[σ, 2]^2)<1.5:

LeveneTest[{data1,data2},1.5,AlternativeHypothesis->"Less"]

OpenersExampleSubsection3(Spacer1)SignificanceLevel  (2)

Set the significance level for diagnostic tests:

data=BlockRandom[SeedRandom[5];RandomVariate[StudentTDistribution[3],50]];

LeveneTest[data,1,SignificanceLevel->.005]

By default, 0.05 is used:

LeveneTest[data,1,SignificanceLevel->Automatic]

The significance level is also used for "TestConclusion" and "ShortTestConclusion":

data=BlockRandom[SeedRandom[1];RandomVariate[NormalDistribution[0,1],100]];

ℋ1=LeveneTest[data,1.5,"HypothesisTestData",SignificanceLevel->.05];

ℋ2=LeveneTest[data,1.5,"HypothesisTestData",SignificanceLevel->.01];

ℋ1["TestConclusion"]//TraditionalForm

ℋ2["TestConclusion"]//TraditionalForm

ℋ1["ShortTestConclusion"]

ℋ2["ShortTestConclusion"]

OpenersExampleSubsection4(Spacer1)VerifyTestAssumptions  (3)

Diagnostics can be controlled as a group using (RefLink[All, paclet:ref/All]) or (RefLink[None, paclet:ref/None]):

data1=RandomVariate[StudentTDistribution[3],1000];
data2=RandomVariate[NormalDistribution[0,Sqrt[3]],1000];

Verify all assumptions:

LeveneTest[{data1,data2},1/4,VerifyTestAssumptions->All]

Check no assumptions:

LeveneTest[{data1,data2},1/4,VerifyTestAssumptions->None]

Diagnostics can be controlled independently:

data1=RandomVariate[StudentTDistribution[3],1000];
data2=RandomVariate[NormalDistribution[0,Sqrt[3]],1000];

Check for normality:

LeveneTest[{data1,data2},1/4,VerifyTestAssumptions->"Normality"]

Set the normality assumption to (RefLink[True, paclet:ref/True]):

LeveneTest[{data1,data2},1/4,VerifyTestAssumptions->"Normality"->True]

It is often useful to bypass diagnostic tests for simulation purposes:

data=RandomVariate[NormalDistribution[],{1000,100}];

AbsoluteTiming[T=Quiet@LeveneTest[#,Automatic,"TestStatistic"]&/@data;]

The assumptions of the test hold by design, so a great deal of time can be saved:

AbsoluteTiming[T2=Quiet@LeveneTest[#,Automatic,"TestStatistic",VerifyTestAssumptions->None]&/@data;]

The results are identical:

SmoothHistogram[{T,T2}]

OpenersExampleSection3(Spacer1)Applications  (1)

Set up a test for constant error variance using Levene's test, which partitions the data into two equal pieces:

HomogeneityOfVarianceTest[data_]:=With[{nh=Ceiling[((Length[data])/2)]},LeveneTest[TakeDrop[data,nh],1,"TestDataTable",VerifyTestAssumptions->None]]

Generate some data for multiple regression analysis:

data=({#1[[1]],#1[[2]],#1[[3]],1.2+(3.7+RandomReal[{-1,1}]) #1[[1]]-2 #1[[2]]+23.4 #1[[3]]}&)/@RandomReal[10,{100,3}];

lm=LinearModelFit[data,{x,y,z},{x,y,z}];

The residuals plotted against each predictor variable:

Row[Table[ListPlot[Transpose[{data[[All,i]],lm["FitResiduals"]}],Frame->True,FrameLabel->{{x,y,z}[[i]],"Residual"}],{i,3}]]

res=(SortBy[#1,First][[All,2]]&)/@Table[Transpose[{data[[All,i]],lm["FitResiduals"]}],{i,3}];

The first variable is positively correlated with its variance:

HomogeneityOfVarianceTest/@res

OpenersExampleSection4(Spacer1)Properties & Relations  (8)

The Levene test is equivalent to (RefLink[FisherRatioTest, paclet:ref/FisherRatioTest]) when a single dataset is given:

SeedRandom[3];

data=RandomVariate[NormalDistribution[],100];

LeveneTest[data]

FisherRatioTest[data]

Given a single dataset with length n, the test statistic follows a (RefLink[ChiSquareDistribution, paclet:ref/ChiSquareDistribution])[n-1] under (Subscript[H, 0]):

SeedRandom[3];

data=RandomVariate[NormalDistribution[],{1000,100}];

T=LeveneTest[#,Automatic,"TestStatistic",VerifyTestAssumptions->None]&/@data;

Show[SmoothHistogram[T,PlotStyle->Orange],Plot[PDF[ChiSquareDistribution[99],x],{x,50,160}],PlotRange->All]

The maximum-likelihood estimate of the degrees of freedom is near n-1:

EstimatedDistribution[T,ChiSquareDistribution[df]]

DistributionFitTest[T,ChiSquareDistribution[99]]

Given two datasets with lengths n and m, the test statistic follows an (RefLink[FRatioDistribution, paclet:ref/FRatioDistribution])[1,n+m-2] under (Subscript[H, 0]):

n=100;
m=75;
SeedRandom[3];
data1=RandomVariate[NormalDistribution[],{1000,n}];
data2=RandomVariate[NormalDistribution[],{1000,m}];

T=MapThread[LeveneTest[{#1,#2},Automatic,"TestStatistic",VerifyTestAssumptions->None]&,{data1,data2}];

Show[Histogram[T,{0.2},"PDF"],Plot[PDF[FRatioDistribution[1,n+m-2],x],{x,0,8},PlotRange->{0,1.5}]]

DistributionFitTest[T,FRatioDistribution[1,n+m-2],"TestConclusion"]

The Levene test is less sensitive to the assumption of normality than the (RefLink[FisherRatioTest, paclet:ref/FisherRatioTest]):

SeedRandom[3];

data1=RandomVariate[LaplaceDistribution[1,2],{1000,100}];
data2=RandomVariate[LaplaceDistribution[1,2],{1000,75}];

fisher=MapThread[FisherRatioTest[{#1,#2},VerifyTestAssumptions->None]&,{data1,data2}];

levene=MapThread[LeveneTest[{#1,#2},VerifyTestAssumptions->None]&,{data1,data2}];

The Fisher ratio test tends to underestimate the p-value and commit more Type I errors:

Histogram[{fisher,levene},Automatic,"PDF"]

The two-sample test statistic:

(Subscript[n, 1])=100;(Subscript[n, 2])=75;(Subscript[σ, 0])=2;

SeedRandom[3];

data1=RandomVariate[NormalDistribution[0,(Subscript[σ, 0])],(Subscript[n, 1])];
data2=RandomVariate[NormalDistribution[0,1],(Subscript[n, 2])];

(Subscript[n, t])=(Subscript[n, 1])+(Subscript[n, 2]);

levene[d1_,d2_]:=Block[{μZ1,μZ2,z1,z2,μZZ},
z1=Abs[Standardize[d1,Mean,1&]];
z2=(Subscript[σ, 0])Abs[Standardize[d2,Mean,1&]];
μZ1=Mean[z1];μZ2=Mean[z2];
μZZ=(((Subscript[n, 1])μZ1 +(Subscript[n, 2])μZ2)/(Subscript[n, t]));
((Subscript[n, t])-2)(((Subscript[n, 1])(((μZ1-μZZ))^2)+(Subscript[n, 2])(((μZ2-μZZ))^2))/((z1-μZ1).(z1-μZ1)+(z2-μZ2).(z2-μZ2)))
]

levene[data1,data2]

LeveneTest[{data1,data2},(Subscript[σ, 0])^2,"TestStatistic"]

The three-sample test statistic:

(Subscript[n, 1])=100;(Subscript[n, 2])=75;(Subscript[n, 3])=150;

SeedRandom[3];

data1=RandomVariate[NormalDistribution[-4,3],(Subscript[n, 1])];
data2=RandomVariate[NormalDistribution[1,3],(Subscript[n, 2])];
data3=RandomVariate[NormalDistribution[5,3],(Subscript[n, 3])];

(Subscript[n, t])=(Subscript[n, 1])+(Subscript[n, 2])+(Subscript[n, 3]);

levene3[d1_,d2_,d3_]:=Block[{μZ1,μZ2,μZ3,z1,z2,z3,μZZ},
z1=Abs[d1-Mean[d1]];
z2=Abs[d2-Mean[d2]];
z3=Abs[d3-Mean[d3]];
μZ1=Mean[z1];μZ2=Mean[z2];μZ3=Mean[z3];
μZZ=(((Subscript[n, 1])μZ1 +(Subscript[n, 2])μZ2+(Subscript[n, 3])μZ3)/(Subscript[n, t]));
((((Subscript[n, t])-3))/2)(((Subscript[n, 1])(((μZ1-μZZ))^2)+(Subscript[n, 2])(((μZ2-μZZ))^2)+(Subscript[n, 3])(((μZ3-μZZ))^2))/(Total[(z1-μZ1)^2]+Total[(z2-μZ2)^2]+Total[(z3-μZ3)^2]))
]

levene3[data1,data2,data3]

LeveneTest[{data1,data2,data3},1,"TestStatistic"]

The Levene test works with the values only when the input is a (RefLink[TimeSeries, paclet:ref/TimeSeries]):

ts=TimeSeries[];

LeveneTest[ts]

LeveneTest[ts["Values"]]

The Levene test works with all the values together when the input is a (RefLink[TemporalData, paclet:ref/TemporalData]):

td=TemporalData[];

LeveneTest[td]

Test all the values only:

data=td["ValueList"]//Flatten;
LeveneTest[data]

Test whether the variances of the two paths are equal:

{data1,data2}=td["ValueList"];

LeveneTest[{data1,data2}]

OpenersExampleSection5(Spacer1)Possible Issues  (3)

The Levene test assumes the data is drawn from a (RefLink[NormalDistribution, paclet:ref/NormalDistribution]):

data=RandomVariate[LaplaceDistribution[1,2],{2,100}];

LeveneTest[data,1,"TestDataTable"]

Use (RefLink[ConoverTest, paclet:ref/ConoverTest]) or (RefLink[SiegelTukeyTest, paclet:ref/SiegelTukeyTest]) for non-normal data:

ConoverTest[data,1,"TestDataTable"]

SiegelTukeyTest[data,1,"TestDataTable"]

The Levene test ignores the argument (Subscript[σ, 0]^2) when there are more than 2 groups:

data=RandomVariate[NormalDistribution[1,2],{4,100}];

Length[data]

LeveneTest[data,2,"TestDataTable"]

When there are more than 2 groups in the data, the Levene test only allows the two-sided test for the alternative hypothesis:

data=RandomVariate[NormalDistribution[1,2],{4,100}];

LeveneTest[data,1,AlternativeHypothesis->"Unequal"]

LeveneTest[data,1,AlternativeHypothesis->"Greater"]

LeveneTest[data,1,AlternativeHypothesis->"Less"]

OpenersExampleSection6(Spacer1)Neat Examples  (1)

Compute the statistic when the null hypothesis (Subscript[H, 0]) is true:

data=RandomVariate[NormalDistribution[],{250,100}];

T1=LeveneTest[#,1,"TestStatistic",VerifyTestAssumptions->None]&/@data;

The test statistic given a particular alternative:

T2=LeveneTest[#,3,"TestStatistic",VerifyTestAssumptions->None]&/@data;

Compare the distributions of the test statistics:

SmoothHistogram[{T1,T2},Filling->Axis,PlotLegends->{"(Subscript[H, 0]) is True","(Subscript[H, 0]) is False"}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[HypothesisTestData, paclet:ref/HypothesisTestData])  ▪  (RefLink[LocationTest, paclet:ref/LocationTest])  ▪  (RefLink[LocationEquivalenceTest, paclet:ref/LocationEquivalenceTest])  ▪  (RefLink[VarianceTest, paclet:ref/VarianceTest])  ▪  (RefLink[VarianceEquivalenceTest, paclet:ref/VarianceEquivalenceTest])  ▪  (RefLink[DistributionFitTest, paclet:ref/DistributionFitTest])  ▪  (RefLink[BrownForsytheTest, paclet:ref/BrownForsytheTest])  ▪  (RefLink[ConoverTest, paclet:ref/ConoverTest])  ▪  (RefLink[FisherRatioTest, paclet:ref/FisherRatioTest])  ▪  (RefLink[SiegelTukeyTest, paclet:ref/SiegelTukeyTest])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Hypothesis Tests, paclet:guide/HypothesisTests])}})}})