Repeated Measures Anova in IronPython QuickStart Sample

Illustrates how to use the OneWayRAnovaModel class to perform a one-way analysis of variance with repeated measures in IronPython.

This sample is also available in: C#, Visual Basic, F#.

Overview

This QuickStart sample demonstrates how to perform a one-way analysis of variance (ANOVA) with repeated measures using the OneWayRAnovaModel class from Numerics.NET.

The sample analyzes a study investigating the effects of four different drugs on test scores across five subjects. Each subject is tested with all four drugs, making this a repeated measures design. The code shows how to:

  • Create a DataFrame from sample data containing subject IDs, drug treatments, and test scores
  • Set up and fit a one-way repeated measures ANOVA model using both direct specification and formula syntax
  • Check if the experimental design is balanced
  • Display the ANOVA results table showing sources of variation, degrees of freedom, and significance
  • Access and analyze group means and variance for different treatment levels
  • Extract overall summary statistics like the grand mean and total number of observations

The example illustrates key concepts in repeated measures ANOVA including:

  • Working with within-subjects factors
  • Handling repeated observations on the same subjects
  • Analyzing treatment effects while accounting for individual differences
  • Interpreting ANOVA results for repeated measures designs

The code

import numerics

from System import Array

import clr
clr.AddReference('System.Data')
from System.Data import *

from Extreme.Statistics import *

# Illustrates the use of the OneWayRAnovaModel class for performing 
# a one-way analysis of variance with repeated measures.

# This QuickStart Sample investigates the effect of the color of packages
# on the sales of the product. The data comes from 12 stores.
# Packages can be either red, green or blue.

# Set up the data in an ADO.NET data table.
dataTable = DataTable()
dataTable.Columns.Add("Person", int)
dataTable.Columns.Add("Drug", int)
dataTable.Columns.Add("Score", int)

dataTable.Rows.Add(Array[object]([1, 1, 30]))
dataTable.Rows.Add(Array[object]([1, 2, 28]))
dataTable.Rows.Add(Array[object]([1, 3, 16]))
dataTable.Rows.Add(Array[object]([1, 4, 34]))

dataTable.Rows.Add(Array[object]([2, 1, 14]))
dataTable.Rows.Add(Array[object]([2, 2, 18]))
dataTable.Rows.Add(Array[object]([2, 3, 10]))
dataTable.Rows.Add(Array[object]([2, 4, 22]))

dataTable.Rows.Add(Array[object]([3, 1, 24]))
dataTable.Rows.Add(Array[object]([3, 2, 20]))
dataTable.Rows.Add(Array[object]([3, 3, 18]))
dataTable.Rows.Add(Array[object]([3, 4, 30]))

dataTable.Rows.Add(Array[object]([4, 1, 38]))
dataTable.Rows.Add(Array[object]([4, 2, 34]))
dataTable.Rows.Add(Array[object]([4, 3, 20]))
dataTable.Rows.Add(Array[object]([4, 4, 44]))

dataTable.Rows.Add(Array[object]([5, 1, 26]))
dataTable.Rows.Add(Array[object]([5, 2, 28]))
dataTable.Rows.Add(Array[object]([5, 3, 14]))
dataTable.Rows.Add(Array[object]([5, 4, 30]))

# Construct the OneWayAnova object.
anova = OneWayRAnovaModel(dataTable, "Score", "Drug", "Person")
# Verify that the design is balanced:
if (not anova.IsBalanced):
	print "The design is not balanced."
# Perform the calculation.
anova.Compute()
			
# The AnovaTable property gives us a classic anova table.
# We can write the table directly to the console:
print anova.AnovaTable.ToString()
print 
			
# A Cell object represents the data in a cell of the model, # i.e. the data related to one level of the factor.
# We can use it to access the group means for each drug.

# We need two indices here: the second index corresponds
# to the person factor.

# First we get the CategoricalScale object so we can easily iterate
# through the levels:
drugFactor = anova.GetFactor(0)
for level in drugFactor.GetLevels():
	print "Mean for group '{0}': {1:.4f}".format(level, anova.Cells[level, Cell.All].Mean)
			
# We could have accessed the cells directly as well:
print "Variance for second drug:", anova.Cells["2", Cell.All].Variance
print 
		
# We can get the summary data for the entire model
# by using the special index 'Cell.All':
totalSummary = anova.Cells[Cell.All, Cell.All]
print "Summary data:"
print "# observations:", totalSummary.Count
print "Grand mean:     ", totalSummary.Mean