NMath Stats User's Guide

TOC |  Previous |  Next |  Index

9.3 Kruskall-Wallis Test (.NET, C#, CSharp, Visual Basic, VB.NET)

Class KruskalWallisTest performs a Kruskal-Wallis rank sum test. The Kruskal-Wallis test is a non-parametric test for equality of population medians among groups. It is a non-parametric version of the classical one-way ANOVA. The interface for KruskalWallisTest is nearly identical to OneWayAnova.

Creating Kruskall-Wallis Objects

A KruskalWallisTest instance is constructed from numeric data organized into different groups. The groups need not contain the same number of observations. For example, this code constructs a KruskalWallisTest from an array of DoubleVector objects. Each vector in the array contains data for a single group:

DoubleVector a =
  new DoubleVector(6.4, 6.8, 7.2, 8.3, 8.4, 9.1, 9.4, 9.7);
DoubleVector b =
  new DoubleVector(2.5, 3.7, 4.9, 5.4, 5.9, 8.1, 8.2);
DoubleVector c = 
  new DoubleVector(1.3, 4.1, 4.9, 5.2, 5.5, 8.2);

DoubleVector[] data_ = new DoubleVector[] { a, b, c };

KruskalWallisTest test = new KruskalWallisTest( data_);

An optional boolean parameter may also be supplied to the constructor. If true, a standard correction for ties is applied.

bool correct_for_ties = true;
KruskalWallisTest test =
  new KruskalWallisTest( data, correct_for_ties_);

This correction usually makes little difference in the value of the test statistic, unless there are a large number of ties.

This code constructs a KruskalWallisTest from a data frame df:

KruskalWallisTest test = new KruskalWallisTest( df, 1, 3 );

Two column indices are also provided: a group column and a data column. A Factor is constructed from the group column using the DataFrame method GetFactor(), which creates a sorted array of the unique values. The specified data column must be of type DFNumericColumn.

Lastly, you can also construct a KruskalWallisTest from a DoubleMatrix:

DoubleMatrix Data = new DoubleMatrix( "6 x 5 [ 24 14 11 7 19 
                                               15 7 9 7 24
                                               21 12 7 7 19
                                               27 17 13 12 15
                                               33 14 12 12 10
                                               23 16 18 18 20 ]" );

bool correct_for_ties = true;
KruskalWallisTest test =
  new KruskalWallisTest( data, correct_for_ties );

Each column in the given matrix contains the data for a group. If your groups have different numbers of observations, you must pad the columns with Double.NaN values until they are all the same length, because a DoubleMatrix must be rectangular. Alternatively, use one of the other constructors described above.

The Kruskall-Wallis Table

Once you've constructed a KruskalWallisTest, you can display the complete results table:

Console.WriteLine( test );

For example:

Source     Deg of Freedom    Sum Of Sq  Mean Sq    Chi-sq   P
Between groups    2          13.5000   6.7500      0.7714   0.6800
Within groups     11         214       19.4545     .        .
Total             13         227.5000  .           .        .

Class KruskalWallisTable is provided for summarizing the information in the results table. Class KruskalWallisTable derives from DataFrame. An instance of KruskalWallisTable can be obtained from a KruskalWallisTest object using the Table property. For example:

KruskalWallisTable table = test.able;

Class KruskalWallisTable provides the following read-only properties for accessing individual elements in the results table:

Ranks, Grand Mean Ranks, Group Means Ranks, and Group Sizes

Class KruskalWallisTest provides properties and methods for retrieving the ranks, grand mean ranks, group means ranks, and group sizes:

For example, if a KruskalWallisTest is constructed from a matrix, this code returns the mean rank for the group in the third column of the matrix:

double mean = test.GetGroupMeanRank( 2 );

If a KruskalWallisTest is constructed from a data frame using a grouping column with values male and female, this code returns the mean rank for the male group:

double maleMean = test.GetGroupMeanRank( "male" );

Critical Value of the Test Statistic

Class KruskalWallisTest provides the convenience function StatisticCriticalValue() which computes the critical value for the test statistic at a given significance level. Thus:

double alpha = 0.05;
double critVal = test.StatisticCriticalValue( alpha );

Updating Kruskall-Wallis Test Objects

Method SetData() updates an entire test object with new data. As with the class constructors (see above), you can supply data as an array of group vectors, a matrix, or as a data frame. For instance, this code updates a test with data from DataFrame df, using column 2 as the group column and column 5 as the data column:

test.SetData( df, 2, 5 );

TOC |  Previous |  Next |  Index