NMath Stats User's Guide

TOC | Previous | Next | Index

9.5 Kruskal-Wallis Test (.NET, C#, CSharp, VB, Visual Basic, F#)

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 Kruskal-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:

Code Example – C# Kruskal-Wallis test

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

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

var test = new KruskalWallisTest( data_);

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

Code Example – C# Kruskal-Wallis test

bool correct_for_ties = true;
var 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:

Code Example – C# Kruskal-Wallis test

var 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:

Code Example – C# Kruskal-Wallis test

var 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;
var 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 Kruskal-Wallis Table

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

Code Example – C# Kruskal-Wallis test

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:

Code Example – C# Kruskal-Wallis test

KruskalWallisTable table = test.able;

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

DegreesOfFreedomBetween gets the between-groups degrees of freedom.

DegreesOfFreedomWithin gets the within-groups degrees of freedom.

DegreesOfFreedomTotal gets the total degrees of freedom.

SumOfSquaresBetween gets the between-groups sum of squares.

SumOfSquaresWithin gets the within-groups sum of squares.

SumOfSquaresTotal gets the total sum of squares.

MeanSquareBetween gets the between-groups mean square. The between-groups mean square is the between-groups sum of squares divided by the between-groups degrees of freedom.

MeanSquareWithin gets the within-group mean square. The within-groups mean square is the within-group sum of squares divided by the within-group degrees of freedom.

MeanSquareTotal gets the total mean square. The total mean square is the total sum of squares divided by the total degrees of freedom.

Statistic gets the test statistic.

PValue gets the p-value for the test statistic.

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:

Ranks gets an array of vectors containing the ranks of the data.

GrandMeanRank gets the grand mean rank of the data. The grand mean rank is the mean of all of the data ranks.

GroupMeanRanks gets a vector of group mean ranks.

GroupSizes gets an array of group sizes.

GroupNames gets an array of group names. If the test was constructed from a data frame using a grouping column, the group names are the sorted, unique Factor levels created from the column values. If the test object was constructed from a matrix or an array of vectors, the group names are simply Group_0, Group_1...Group_n.

GetGroupRanks() returns the ranks for a specified group, identified either by group name or group number (a zero-based index into the Ranks array).

GetGroupMeanRank() returns the mean rank for a specified group, identified either by group name or group number (a zero-based index into the GroupMeanRanks vector).

GetGroupSize() returns the mean for a specified group, identified either by group name or group number (a zero-based index into the GroupSizes array).

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:

Code Example – C# Kruskal-Wallis test

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:

Code Example – C# Kruskal-Wallis test

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:

Code Example – C# Kruskal-Wallis test

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

Updating Kruskal-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:

Code Example – C# Kruskal-Wallis test

test.SetData( df, 2, 5 );


Top

Top