NMath User's Guide

TOC | Previous | Next | Index

44.1 One-Way ANOVA (.NET, C#, CSharp, VB, Visual Basic, F#)

Class OneWayAnova computes and summarizes a traditional one-way (single factor) analysis of variance.

Creating One-Way ANOVA Objects

A OneWayAnova 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 OneWayAnova from an array of DoubleVector objects. Each vector in the array contains data for a single group:

Code Example – C# ANOVA

var data = new DoubleVector[5];



data[0] = new DoubleVector( "[24 15 21 27 33 23]" );
data[1] = new DoubleVector( "[14 7 12 17 14 16]" );
data[2] = new DoubleVector( "[11 9 7 13 12 18]" );
data[3] = new DoubleVector( "[7 7 4 7 12 18]" );
data[4] = new DoubleVector( "[19 24 19 15 10 20]" );



var anova = new OneWayAnova( data );

Code Example – VB ANOVA

Dim Data As New DoubleVector(5)



Data(0) = New DoubleVector("[24 15 21 27 33 23]")
Data(1) = New DoubleVector("[14 7 12 17 14 16]")
Data(2) = New DoubleVector("[11 9 7 13 12 18]")
Data(3) = New DoubleVector("[7 7 4 7 12 18]")
Data(4) = New DoubleVector("[19 24 19 15 10 20]")



Dim Anova As New OneWayAnova(Data)

This code constructs a OneWayAnova from a data frame df:

Code Example – C# ANOVA

var anova = new OneWayAnova( df, 1, 3 );

Code Example – VB ANOVA

Dim Anova As New OneWayAnova(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 OneWayAnova from a DoubleMatrix:

Code Example – C# ANOVA

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 ]" );
var anova = new OneWayAnova( data );

Code Example – VB ANOVA

Dim Data As 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 ]")
Dim Anova As New OneWayAnova(Data)

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 One-Way ANOVA Table

Once you've constructed a OneWayAnova, you can display the complete ANOVA table:

Code Example – C# ANOVA

Console.WriteLine( anova );

Code Example – VB ANOVA

Console.WriteLine(Anova)

For example:



Source     Deg of Freedom    Sum Of Sq  Mean Sq     F      P
Between groups    4          803.0000   200.7500  9.0076  0.0001
Within groups     25         557.1667   22.2867   .       .
Total             29         1360.1667  46.9023   .       .

Class OneWayAnovaTable is provided for summarizing the information in a traditional one-way ANOVA table. Class OneWayAnovaTable derives from DataFrame. An instance of OneWayAnovaTable can be obtained from a OneWayAnova object using the AnovaTable property. For example:

Code Example – C# ANOVA

OneWayAnovaTable myTable = anova.AnovaTable;

Code Example – VB ANOVA

Dim MyTable As OneWayAnovaTable = Anova.AnovaTable

Class OneWayAnovaTable provides the following read-only properties for accessing individual elements in the ANOVA 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.

FStatistic gets the F statistic.

FStatisticPValue gets the p-value for the F statistic.

Grand Mean, Group Means, and Group Sizes

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

GrandMean gets the grand mean of the data. The grand mean is the mean of all of the data.

GroupMeans gets a vector of group means.

GroupSizes gets an array of group sizes.

GroupNames gets an array of group names. If the anova 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 anova object was constructed from a matrix or an array of vectors, the group names are simply Group_0, Group_1...Group_n.

GetGroupMean() returns the mean for a specified group, identified either by group name or group number (a zero-based index into the GroupMeans 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 OneWayAnova is constructed from a matrix, this code returns the mean for the group in the third column of the matrix:

Code Example – C# ANOVA

double maleMean = anova.GetGroupMean( 2 );

Code Example – VB ANOVA

Dim MaleMean As Double = Anova.GetGroupMean(2)

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

Code Example – C# ANOVA

double maleMean = anova.GetGroupMean( "male" );

Code Example – VB ANOVA

Dim MaleMean As Double = Anova.GetGroupMean("male")

Critical Value of the F Statistic

Class OneWayAnova provides the convenience function FStatisticCriticalValue() which computes the critical value for the ANOVA F statistic at a given significance level. Thus:

Code Example – C# ANOVA

double alpha = 0.05;
double critVal = anova.FStatisticCriticalValue( alpha );

Code Example – VB ANOVA

Dim Alpha As Double = 0.05
Dim CritVal As Double = Anova.FStatisticCriticalValue(Alpha)

Updating One-Way ANOVA Objects

Method SetData() updates an entire analysis of variance 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 an ANOVA with data from DataFrame df, using column 2 as the group column and column 5 as the data column:

Code Example – C# ANOVA

anova.SetData( df, 2, 5 );

Code Example – VB ANOVA

Anova.SetData(DF, 2, 5)

Top

Top