Extreme Optimization >
User's Guide >
Statistics Library >
Analysis of Variance >
One-way ANOVA with Repeated Measures
Extreme Optimization User's Guide
User's Guide
Up: Analysis of Variance Next: Two-way ANOVA Previous: One-way ANOVA Contents
One-Way ANOVA with Repeated Measures
An ANOVA design where multiple observations are made for each subject is called a repeated measures design. A
design with one factor and one subject variable is a one-way ANOVA model with repeated measures, sometimes shortened
to RANOVA. The one-way analysis of variance with repeated measures is implemented by the OneWayRAnovaModel class.
Constructing One-Way Repeated Measures ANOVA Models
The OneWayRAnovaModel class has three constructors.
The first constructor takes three parameters: a NumericalVariable that specifies the dependent variable, and two
CategoricalVariable objects, one that specifies the
independent variable or factor, and one that specifies the subjects. All three variables must have the same number of
observations.
As an example, we construct an ANOVA model to measure the effect of package color and shape on sales. Our data
comes from 12 stores. The two categorical variables are the shape color and the shape shape. The dependent variable
is the total sales of the product in the store.
| C# | Copy Code |
string[] treatmentData = {"one", "two", "three"...};
CategoricalVariable treatment = new CategoricalVariable("treatment", treatmentData);
int[] subjectData = {1, 1, 1...};
CategoricalVariable subject = new CategoricalVariable("subject", subjectData);
double[] effectData = {30, 28, 16...};
NumericalVariable effect = new NumericalVariable("effect", effectData);
OneWayRAnovaModel anova1 = new OneWayRAnovaModel(effect, subject, treatment); |
| Visual Basic | Copy Code |
Dim treatmentData As String() = {"one", "two", "three"...}
Dim treatment As CategoricalVariable = _
New CategoricalVariable("treatment", treatmentData)
Dim subjectData As Integer() = {1, 1, 1...}
Dim subject As CategoricalVariable = _
New CategoricalVariable("subject", subjectData)
Dim effectData As Double() = {30, 28, 16...}
Dim effectAs NumericalVariable = _
New NumericalVariable("effect", effectData)
Dim anova1 As OneWayRAnovaModel = New OneWayRAnovaModel(effect, subject, treatment) |
The second constructor takes four parameters. The first parameter is a VariableCollection that contains the variables you wish to use in the
analysis. The second parameter is the name of the dependent variable in the collection. The third parameter is the
name of the independent variable or factor. The fourth parameter is the name of the variable in the collection that
represents the subjects. Using the variables we created in the previous example, we get:
| C# | Copy Code |
VariableCollection variables = new VariableCollection();
variables.Add(effect);
variables.Add(treatment);
variables.Add(subject);
OneWayRAnovaModel anova2 = new OneWayRAnovaModel(variables, "effect", "treatment", "subject"); |
| Visual Basic | Copy Code |
Dim variables As VariableCollection = New VariableCollection()
variables.Add(effect)
variables.Add(subject)
variables.Add(treatment)
Dim anova2 As OneWayRAnovaModel = _
New OneWayRAnovaModel(variables, "effect", "treatment", "subject") |
The third constructor also takes four parameters. The first parameter is a DataTable that contains
the data for the analysis. The second parameter is the name of the column that contains the dependent variable data.
The third parameter is the name of the column that represents the independent variable or factor. The fourth
parameter is the name of the column that represents the subjects.
| C# | Copy Code |
DataTable table = new DataTable();
// Fill the DataTable from some data source...
OneWayRAnovaModel anova3 = new OneWayRAnovaModel(table, "effect", "treatment", "subject"); |
| Visual Basic | Copy Code |
Dim table As DataTable = New DataTable()
' Fill the DataTable from some data source...
Dim anova3 As OneWayRAnovaModel = New OneWayRAnovaModel(table, "effect", "treatment", "subject") |
All three examples produce the same ANOVA model.
Performing the analysis
The
Compute method performs the actual analysis.
The results of the analysis can be obtained through the model's AnovaTable property. The ANOVA table for a one-way design with repeated
measures has four rows, commonly labeled 'Between subjects,' 'Between groups,' 'Within groups,' and 'Total.'
The crucial data is provided by the Between Groups row, which shows the contribution to the total
variation of the factor under consideration. It is the first row in the ANOVA table, and therefore has index 0. It
can be retrieved through the GetModelRow method. Since
the entire model of a one-way analysis of variance consists of the contribution of one factor, this property is also
available as the CompleteModelRow property. The
Between subjects row shows the variation due to the subjects. It has index 1.
The AnovaModelRow objects obtained in this way shows the
results of the test for significance of the variation due to the factor compared to the variation not explained by
the factor. The FStatistic property gives the value
of the F statistic for this ratio, while the PValue gives
the actual significance of the F statistic.
The Within Groups row shows the variation of the data around the group means. It corresponds to the error
or residual of the variation in the data after the model has been taken into account. The row is available through
the ANOVA table's ErrorRow property.
The Total row contains the summary data for the entire data set. It can be retrieved through the
TotalRow property of the ANOVA table.
The AnovaModelRow objects obtained in this way show the
results of the test for significance of the variation due to the factor compared to the variation not explained by
the model. The FStatistic property gives the value of
the F statistic for this ratio, while the PValue gives
the actual significance of the F statistic.
The Within Groups row shows the variation of the data around the group means. It corresponds to the error
or residual of the variation in the data after the model has been taken into account. The row is available through
the ANOVA table's ErrorRow property.
The Total row contains the summary data for the entire data set. It can be retrieved through the
TotalRow property of the ANOVA table.
The example below illustrates these properties:
| C# | Copy Code |
anova.Compute();
Console.WriteLine("F statistic: {0}", anova.AnovaTable.ModelRow.FStatistic);
Console.WriteLine("P-value : {0}", anova.AnovaTable.ModelRow.PValue);
Console.WriteLine("Sum of sq. total: {0}",
anova.AnovaTable.TotalRow.SumOfSquares);
Console.WriteLine("Sum of sq. error: {0}",
anova.AnovaTable.ErrorRow.SumOfSquares);
Console.WriteLine("Sum of sq. treatment: {0}",
anova.AnovaTable.GetModelRow(0).SumOfSquares);
Console.WriteLine("Sum of sq. subjects: {0}",
anova.AnovaTable.GetModelRow(1).SumOfSquares);
Console.WriteLine(anova.AnovaTable.ToString()); |
| Visual Basic | Copy Code |
anova.Compute();
Console.WriteLine("F statistic: {0}", anova.AnovaTable.ModelRow.FStatistic)
Console.WriteLine("P-value : {0}", anova.AnovaTable.ModelRow.PValue)
Console.WriteLine("Sum of sq. total: {0}", _
anova.AnovaTable.TotalRow.SumOfSquares)
Console.WriteLine("Sum of sq. error: {0}", _
anova.AnovaTable.ErrorRow.SumOfSquares)
Console.WriteLine("Sum of sq. treatment: {0}", _
anova.AnovaTable.GetModelRow(0).SumOfSquares)
Console.WriteLine("Sum of sq. subjects: {0}", _
anova.AnovaTable.GetModelRow(1).SumOfSquares)
Console.WriteLine(anova.AnovaTable.ToString()) |
For our example above, we find that the between groups F statistic is 18.1064, corresponding to a tiny
p-value of less than 0.0001. For the between subjects row, we find an F statistic of 24.7589 with a p-value
of about 0.0001. We conclude that the treatments have a significantly different effect, and that there is also
significant variation between the subjects.
The group means can be accessed through the model's Cells property. In the example below, we first
obtain the CategoricalScale object corresponding to the treatment factor. We then iterate through the
levels of the scale, and print the group means for each factor:
| C# | Copy Code |
CategoricalScale treatmentFactor = anova.GetFactor(0);
foreach(object level in treatmentFactor.GetLevels())
Console.WriteLine("Mean for treatment '{0}': {1:F4}",
level, anova.Cells[level, Cell.All].Mean); |
| Visual Basic | Copy Code |
Dim treatmentFactor As CategoricalScale = anova.GetFactor(0)
For Each level As Object In treatmentFactor.GetLevels()
Console.WriteLine("Mean for treatment '{0}': {1:F4}", _
level, anova.Cells[level, Cell.All].Mean);
Next |
The grand mean is obtained by setting both indices to Cell.All:
| C# | Copy Code |
Console.WriteLine("Grand mean: {0:F4}",
anova.Cells[Cell.All, Cell.All].Mean); |
| Visual Basic | Copy Code |
Console.WriteLine("Grand mean: {0:F4}", _
anova.Cells[Cell.All, Cell.All].Mean) |
Up: Analysis of Variance Next: Two-way ANOVA Previous: One-way ANOVA Contents
Copyright 2004-2008,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual
Studio.NET, and the Visual Studio Logo are registered trademarks of Microsoft Corporation