**44.5****
**** ****Two-Way Repeated Measures ANOVA** (.NET, C#, CSharp, VB, Visual Basic, F#)

**NMath Stats**
provides two classes for calculating a two-way analysis of variance with repeated measures
(RANOVA):

● Class **TwoWayRanova** performs a balanced
two-way analysis of variance with repeated measures on one factor.

● Class **TwoWayRanovaTwo** performs a balanced
two-way analysis of variance with repeated measures on both factors.

Both classes extend **TwoWayAnova**,
and so inherit the methods and properties described in Section 44.3.
Like **TwoWayAnova**, both **TwoWayRanova** and **TwoWayRanovaTwo**
use multiple linear regression to compute the RANOVA values.

**Creating Two-Way RANOVA Objects**

Instances of both **TwoWayRanova**
and **TwoWayRanovaTwo** are constructed
from data in a data frame. Three column indices are specified in the
data frame: the column containing the first factor, the column containing
the second factor, and the column containing the numeric data. For **TwoWayRanova**, the first factor is the
repeated factor; for **TwoWayRanovaTwo**,
both factors are repeated.

For example, this code groups the numeric data in column
3 of **DataFrame**
df by factors constructed from columns 0 and 4:

Code Example – C# RANOVA

var ranova = new TwoWayRanova( df, 0, 4, 3 );

Code Example – VB RANOVA

Dim Ranova As New TwoWayRanova(DF, 0, 4, 3)

The factor constructed from column 0 is the repeated factor. In the following example, both factors are repeated:

Code Example – C# RANOVA

var ranova2 = new TwoWayRanovaTwo( df, 0, 4, 3 );

Code Example – VB RANOVA

Dim Ranova2 As New TwoWayRanovaTwo(DF, 0, 4, 3)

**NOTE—****Both
TwoWayRanova and TwoWayRanovaTwo throw an InvalidArgumentException
if the data contains missing values (NaNs).**

Once you've constructed a **TwoWayRanova**,
you can display the complete RANOVA table:

Code Example – C# RANOVA

var ranova = new TwoWayRanova( df, 0, 4, 3 );

Console.WriteLine( ranova );

Code Example – VB RANOVA

Dim Ranova As New TwoWayRanova(DF, 0, 4, 3)

Console.WriteLine(Ranova)

For instance:

Source Deg of Freedom SumOfSqu Mean Square F P

FactorA 1 0.2032 0.2032 29.2322 0.0001

Subjects 14 1.7559 0.1254 . .

FactorB 1 0.0205 0.0205 0.1635 0.6921

Interaction 1 0.0830 0.0830 11.9442 0.0039

Error 14 0.0973 0.0070 . .

Total 31 2.1599 . . .

Class **TwoWayRanovaTable** summarizes
the information in a traditional two-way RANOVA table with repeated measures
on one factor. An instance of **TwoWayRanovaTable**
can be obtained from a **TwoWayRanova**
object using the RanovaTable property. For
example:

Code Example – C# RANOVA

TwoWayRanovaTable myTable = ranova.RanovaTable;

Code Example – VB RANOVA

Dim MyTable As TwoWayRanovaTable = Ranova.RanovaTable

Class **TwoWayRanovaTable**
derives from **TwoWayAnovaTable**,
and so inherits the properties described in Section 44.3.
In addition, **TwoWayRanovaTable**
provides the following properties for accessing the new row in the RANOVA
table for repeated measures on one factor:

● SubjectsDegreesOfFreedom gets the subjects degrees of freedom.

● SubjectsSumOfSquares gets the sum of squares for the subjects.

● SubjectsMeanSquare gets the mean square for the subjects.

Similarly, once you've constructed a **TwoWayRanovaTwo**, you can display the
RANOVA table:

Code Example – C# RANOVA

var ranova2 = new TwoWayRanovaTwo( df, 0, 4, 3 );

Console.WriteLine( ranova2 );

Code Example – VB RANOVA

Dim Ranova2 As New TwoWayRanovaTwo(DF, 0, 4, 3)

Console.WriteLine(Ranova2)

For example:

Source Deg of Freedom SumOfSq Mean Square F P

FactorA 1 1.4700 1.4700 88.2000 0.0000

FactorB 2 14.5654 7.2827 59.2348 0.0000

Interaction 2 3.3387 1.6694 18.9305 0.0001

A x Subject 14 1.7213 0.1229 . .

B x Subject 7 0.1167 0.0167 . .

Error 14 1.2346 0.0882 . .

Total 47 29.3592 . . .

An instance of **TwoWayRanovaTwoTable** can be
obtained from a **TwoWayRanovaTwo**
object using the RanovaTable property. For
example:

Code Example – C# RANOVA

TwoWayRanovaTwoTable myTable = ranova2.RanovaTable;

Code Example – VB RANOVA

Dim MyTable As TwoWayRanovaTwoTable = Ranova2.RanovaTable

Class **TwoWayRanovaTwoTable**
also derives from **TwoWayAnovaTable**,
and provides the following methods for accessing the additional rows
in the RANOVA table with repeated measures on both factors:

● SubjectInteractionDegreesOfFreedom() returns the degrees of freedom for the interaction between subjects and the specified factor.

● SubjectInteractionSumOfSquares() returns the sum of squares for the interaction between subjects and the specified factor.

● SubjectInteractionMeanSquare returns the mean square for the interaction between subjects and the specified factor.