Two-way analysis of variance


p = anova2(X,reps)
p = anova2(X,reps,displayopt)
[p,table] = anova2(...)
[p,table,stats] = anova2(...)


p = anova2(X,reps) performs a balanced two-way ANOVA for comparing the means of two or more columns and two or more rows of the observations in X. The data in different columns represent changes in factor A. The data in different rows represent changes in factor B. If there is more than one observation for each combination of factors, input reps indicates the number of replicates in each position, which must be constant. (For unbalanced designs, use anovan.)

The matrix below shows the format for a set-up where column factor A has two levels, row factor B has three levels, and there are two replications (reps = 2). The subscripts indicate row, column, and replicate, respectively.


When reps is 1 (default), anova2 returns two p-values in vector p:

  1. The p value for the null hypothesis, H0A, that all samples from factor A (i.e., all column-samples in X) are drawn from the same population

  2. The p value for the null hypothesis, H0B, that all samples from factor B (i.e., all row-samples in X) are drawn from the same population

    When reps is greater than 1, anova2 returns a third p value in vector p:

  3. The p value for the null hypothesis, H0AB, that the effects due to factors A and B are additive (i.e., that there is no interaction between factors A and B)

If any p value is near zero, this casts doubt on the associated null hypothesis. A sufficiently small p value for H0A suggests that at least one column-sample mean is significantly different that the other column-sample means; i.e., there is a main effect due to factor A. A sufficiently small p value for H0B suggests that at least one row-sample mean is significantly different than the other row-sample means; i.e., there is a main effect due to factor B. A sufficiently small p value for H0AB suggests that there is an interaction between factors A and B. The choice of a limit for the p value to determine whether a result is "statistically significant" is left to the researcher. It is common to declare a result significant if the p value is less than 0.05 or 0.01.

anova2 also displays a figure showing the standard ANOVA table, which divides the variability of the data in X into three or four parts depending on the value of reps:

  • The variability due to the differences among the column means

  • The variability due to the differences among the row means

  • The variability due to the interaction between rows and columns (if reps is greater than its default value of one)

  • The remaining variability not explained by any systematic source

The ANOVA table has five columns:

  • The first shows the source of the variability.

  • The second shows the Sum of Squares (SS) due to each source.

  • The third shows the degrees of freedom (df) associated with each source.

  • The fourth shows the Mean Squares (MS), which is the ratio SS/df.

  • The fifth shows the F statistics, which is the ratio of the mean squares.

p = anova2(X,reps,displayopt) enables the ANOVA table display when displayopt is 'on' (default) and suppresses the display when displayopt is 'off'.

[p,table] = anova2(...) returns the ANOVA table (including column and row labels) in cell array table. (Copy a text version of the ANOVA table to the clipboard by using the Copy Text item on the Edit menu.)

[p,table,stats] = anova2(...) returns a stats structure that you can use to perform a follow-up multiple comparison test.

The anova2 test evaluates the hypothesis that the row, column, and interaction effects are all the same, against the alternative that they are not all the same. Sometimes it is preferable to perform a test to determine which pairs of effects are significantly different, and which are not. Use the multcompare function to perform such tests by supplying the stats structure as input.


expand all


Load the sample data.

load popcorn
popcorn =

    5.5000    4.5000    3.5000
    5.5000    4.5000    4.0000
    6.0000    4.0000    3.0000
    6.5000    5.0000    4.0000
    7.0000    5.5000    5.0000
    7.0000    5.0000    4.5000

The data come from a study of popcorn brands and popper type (Hogg 1987). The columns of the matrix popcorn are brands (Gourmet, National, and Generic). The rows are popper type (Oil and Air). The study popped a batch of each brand three times with each popper. The values are the yield in cups of popped popcorn.

Perform a two-way ANOVA.

p = anova2(popcorn,3)
p =

    0.0000    0.0001    0.7462

The vector p shows the p -values for the three brands of popcorn, 0.0000, the two popper types, 0.0001, and the interaction between brand and popper type, 0.7462. These values indicate that both popcorn brand and popper type affect the yield of popcorn, but there is no evidence of a interaction effect of the two.

The conclusion is that you can get the greatest yield using the Gourmet brand and an air popper (the three values popcorn(4:6,1)).


[1] Hogg, R. V., and J. Ledolter. Engineering Statistics. New York: MacMillan, 1987.

See Also


Was this topic helpful?