Statistics Toolbox | ![]() ![]() |
Example: N-Way ANOVA with Small Data Set
Consider the following two-way example using anova2
.
m = [23 15 20;27 17 63;43 3 55;41 9 90] m = 23 15 20 27 17 63 43 3 55 41 9 90 anova2(m,2) ans = 0.0197 0.2234 0.2663
The factor information is implied by the shape of the matrix m
and the number of measurements at each factor combination (2). Although anova2
does not actually require arrays of factor values, for illustrative purposes we could create them as follows.
cfactor = repmat(1:3,4,1) cfactor = 1 2 3 1 2 3 1 2 3 1 2 3 rfactor = [ones(2,3); 2*ones(2,3)] rfactor = 1 1 1 1 1 1 2 2 2 2 2 2
The cfactor
matrix shows that each column of m
represents a different level of the column factor. The rfactor
matrix shows that the top two rows of m
represent one level of the row factor, and bottom two rows of m
represent a second level of the row factor. In other words, each value m(i,j)
represents an observation at column factor level cfactor(i,j)
and row factor level rfactor(i,j)
.
To solve the above problem with anovan
, we need to reshape the matrices m
, cfactor
, and rfactor
to be vectors.
m = m(:); cfactor = cfactor(:); rfactor = rfactor(:); [m cfactor rfactor] ans = 23 1 1 27 1 1 43 1 2 41 1 2 15 2 1 17 2 1 3 2 2 9 2 2 20 3 1 63 3 1 55 3 2 90 3 2 anovan(m,{cfactor rfactor},2) ans = 0.0197 0.2234 0.2663
![]() | N-Way Analysis of Variance | Example: N-Way ANOVA with Large Data Set | ![]() |