STATISTICA







STATISTICA BASIC Program Levene.stb

{This program performs the Brown-Forsythe & Levene Tests for homogeneity of variances for the two group case.

Levene and Brown-Forsythe tests for homogeneity of variances (HOV)

A important assumption in the t-test for mean differences is that the variances in the two groups are equal (homogeneous). Two powerful and commonly used tests of this assumption are the Levene test and the Brown-Forsythe modification of this test. However, it is important to realize that (1) the homogeneity of variances assumption is usually not as crucial as other assumptions for the t-test for mean differences, in particular in the case of equal n, and (2) that the tests described below are not necessarily very robust themselves (e.g., Glass and Hopkins, 1996, p. 436, call these tests "fatally flawed;" see also the description of these tests below). If you are concerned about a violation of the HOV assumption, it is always advisable to repeat the key analyses using nonparametric methods.

Levene's test (homogeneity of variances): For each dependent variable, a t-test for mean differences is performed on the absolute deviations of values from the respective group means. If the Levene test is statistically significant, then the hypothesis of homogeneous variances should be rejected.

Brown & Forsythe's test (homogeneity of variances): Recently, some authors (e.g., Glass and Hopkins, 1996) have called into question the power of the Levene test for unequal variances. Specifically, the absolute deviation (from the group means) scores can be expected to be highly skewed; thus, the normality assumption for t-test for mean differences of those absolute deviation scores is usually violated. This poses a particular problem when there is unequal n in the two groups that are to be compared. A more robust test that is very similar to the Levene test has been proposed by Brown and Forsythe (1974). Instead of performing the t-test for mean differences on the deviations from the mean, one can perform the analysis on the deviations from the group medians. Olejnik and Algina (1987) have shown that this test will give quite accurate error rates even when the underlying distributions for the raw scores deviate significantly from the normal distribution. However, as Glass and Hopkins (1996, p. 436) have pointed out, both the Levene test as well as the Brown-Forsythe modification suffer from what those authors call a "fatal flaw," namely, that both tests themselves rely on the homogeneity of variances assumption (of the absolute deviations from the means or medians); and hence, it is not clear how robust these tests are themselves in the presence of significant variance heterogeneity and unequal n.

Program written, modified, or edited at StatSoft, Inc.}


RandomAccess;
NoDataFileVariableNames;
ReDim group(NCases);
ReDim grpsort(NCases);
ReDim y(NCases);
ReDim yvar(NVars-1);
Dim gvar(1);
ReDim code(NCases);

if (SelectVariables2 ('Select one grouping variable and the dependent variables', 1, 1, gvar, Count1, 'Grouping variable:', 1,
NVars-1, yvar, Count2, 'Dependent variables:')=0) then stop;
for i:=1 to Count2 do begin
  if gvar(1)=yvar(i) then begin
    DisplayMessageBox(MB_ICONSTOP,'Overlapping Variable Lists',
    'The grouping variable is part of the dependent variable list');
    stop;
  end;
end;

MatrixCopy(Data, 1, gvar(1), NCases, 1, group, 1, 1);
{Determine the group levels in the specified grouping variable.}
MatrixCopy(Data, 1, gvar(1), NCases, 1, grpsort, 1, 1);
VectorSort (grpsort, SORT_ASCENDING);

count:=1;
code(count):=grpsort(1);
for i:=2 to NCases do begin
   if grpsort(i)<>grpsort(i-1) then begin
       count:=count+1;
       code(count):=grpsort(i);
   end;
end;

if count>2 then begin
    DisplayMessageBox (MB_ICONSTOP, 'Invalid grouping variable.', 'The grouping variable cannot have more than 2 levels.');
    stop;
end;

ReDim code(2);
{Begin calcualtion loop for the dependent variables.}
for i:=1 to Count2 do begin
  MatrixCopy(Data, 1, yvar(i), NCases, 1, y, 1, 1);
  {Create an array of values for each group.}
  ReDim g1(NCases);
  ReDim g2(NCases);

  k1:=1;k2:=1;
  for j:=1 to NCases do begin
     if (group(j)=code(1)) and (Valid(y(j))=1) and (SelectionConditions(j)=1) then begin
        g1(k1):=y(j);
        k1:=k1+1;
     end;
     if (group(j)=code(2)) and (Valid(y(j))=1) and (SelectionConditions(j)=1) then begin
        g2(k2):=y(j);
        k2:=k2+1;
     end;
  end;
  n1:=k1-1;n2:=k2-1;

  ReDim df(Count2,1);
  ReDim f(Count2,2);
  ReDim pval(Count2,2);
  {Check if either group has no observations.}
  if (n1=0) or (n2=0) then begin
    DisplayMessageBox (MB_ICONSTOP, 'Invalid group in '+VarName(yvar(i)), 'One of your groups has no valid cases.');
    df(i,1):=Missing;
    f(i,1):=Missing;
    pval(i,1):=Missing;
    f(i,2):=Missing;
    pval(i,2):=Missing;
  end
  else begin
  {If check is ok, then perform tests.}
    ReDim g1(n1);
    ReDim g2(n2);
    ReDim z1(n1);
    ReDim z2(n2);

    ValMedian (g1, 1, n1, med1);
    ValMedian (g2, 1, n2, med2);
    ValMean (g1, 1, n1, mean1);
    ValMean (g2, 1, n2, mean2);

    {BROWN-FORSYTHE TEST}
    for j:=1 to n1 do z1(j):= abs(g1(j)-med1);
    for j:=1 to n2 do z2(j):= abs(g2(j)-med2);

    ValMean (z1, 1, n1, zmean1);
    ValMean (z2, 1, n2, zmean2);

    sumsqr1:=0;sumsqr2:=0;
    sum1:=0;sum2:=0;
    for j:=1 to n1 do begin
      sum1:=sum1+z1(j);
      sumsqr1:=sumsqr1+ (z1(j))**2;
    end;
    for j:=1 to n2 do begin
     sum2:=sum2+z2(j);
     sumsqr2:=sumsqr2+ (z2(j))**2;
    end;

    sumsqr:=(sum1+sum2)**2;
    SST:=sumsqr1+sumsqr2-sumsqr/(n1+n2);
    SSTRT:=(sum1**2)/n1+(sum2**2)/n2-sumsqr/(n1+n2);
    SSE:=SST-SSTRT;
    df(i,1):=n1+n2-2;
    f(i,1):=SSTRT/(SSE/df(i,1));
    pval(i,1):=1-IFDistr (f(i,1), 1, df(i,1));

    {LEVENE TEST}
    for j:=1 to n1 do z1(j):= abs(g1(j)-mean1);
    for j:=1 to n2 do z2(j):= abs(g2(j)-mean2);

    ValMean (z1, 1, n1, zmean1);
    ValMean (z2, 1, n2, zmean2);

    sumsqr1:=0;sumsqr2:=0;
    sum1:=0;sum2:=0;
    for j:=1 to n1 do begin
      sum1:=sum1+z1(j);
      sumsqr1:=sumsqr1+ (z1(j))**2;
    end;
    for j:=1 to n2 do begin
     sum2:=sum2+z2(j);
     sumsqr2:=sumsqr2+ (z2(j))**2;
    end;

    sumsqr:=(sum1+sum2)**2;
    SST:=sumsqr1+sumsqr2-sumsqr/(n1+n2);
    SSTRT:=(sum1**2)/n1+(sum2**2)/n2-sumsqr/(n1+n2);
    SSE:=SST-SSTRT;
    df(i,1):=n1+n2-2;
    f(i,2):=SSTRT/(SSE/df(i,1));
    pval(i,2):=1-IFDistr (f(i,2), 1, df(i,1));
  end;
end;
{End calcualtion loop for the dependent variables.}
{Create & display the results Scrollsheet.}
ReDim results(Count2,6);
if (DisplayNumericInputBox ('Specify significance level.', 'alpha:', alpha)=0) then stop;

for i:=1 to Count2 do begin
  results(i,1):=f(i,1);
  results(i,2):=df(i,1);
  results(i,3):=pval(i,1);
  results(i,4):=f(i,2);
  results(i,5):=df(i,1);
  results(i,6):=pval(i,2);
end;

title$:='Grouping: ' + VarName(gvar(1)) + ': ' + VarLongName(gvar(1));
scroll:=NewScrollsheet (Count2, 6, results, title$, ?RowNames$, ?ColumnNames$);
title2$:='Group 1: ' + GetText (gvar(1), code(1));
title3$:='Group 2: ' + GetText (gvar(1), code(2));
ScrollsheetSetTitle (scroll, 2, title2$);
ScrollsheetSetTitle (scroll, 3, title3$);
ScrollsheetSetColumnName (scroll, 1, 'B-F', 'F(1,df)');
ScrollsheetSetColumnName (scroll, 2, 'df', 'B-F');
ScrollsheetSetColumnName (scroll, 3, 'p', 'B-F');
ScrollsheetSetColumnName (scroll, 4, 'Levene', 'F(1,df)');
ScrollsheetSetColumnName (scroll, 5, 'df', 'Levene');
ScrollsheetSetColumnName (scroll, 6, 'p', 'Levene');
ScrollsheetSetRowNameWidth (scroll, 8);
for i:=1 to Count2 do
  ScrollsheetSetRowName (scroll, i, VarName(yvar(i)));
ScrollsheetSetColumnFormat (scroll, 2, SCF_INTEGER, 4);
ScrollsheetSetColumnFormat (scroll, 5, SCF_INTEGER, 4);
for i:=1 to Count2 do begin
  if results(i,3)<alpha then begin
    for j:=1 to 3 do ScrollsheetSetHilite(scroll,i,j,1);
  end;
  if results(i,6)<alpha then begin
    for j:=4 to 6 do ScrollsheetSetHilite(scroll,i,j,1);
  end;
end;
Back to List of Programs



[StatSoft]
2300 East 14th Street, Tulsa, OK 74104
Phone: (918) 749-1119; Fax: (918) 749-2217

[StatSoft]e-mail: info@statsoft.com

©Copyright StatSoft, Inc., 1984-2004.
StatSoft, StatSoft logo, STATISTICA, SEWSS, SEDAS, Data Miner, SEPATH and GTrees are trademarks of StatSoft, Inc.