How can I use the margins command with a 3-way anova interaction? (Stata 11) | Stata FAQ

The margins command, new in Stata 11, can be a very useful tool in understanding and
interpreting interactions. On this page we will use margins for a three factor
anova model with a significant 3-way interaction. We will illustrate this using
the following dataset.

use https://stats.idre.ucla.edu/stat/stata/faq/threeway, clear

The data will be analyzed as a 2x2x3 factorial anova. Variables a and b have two
levels each while variable c has three levels. We will begin by running the anova model.

Indeed, it is the case that the 3-way interaction is statistically significant. Here is the plan
for interpreting this interaction. First, we will run the margins command with the
asbalanced option on the interaction term to get adjusted cell means. In this case the
design already is balanced but the asbalanced option is included for those cases in which
the data are unbalanced so that we don’t forget it. We will also use the post option
because we are planning on doing tests among the cell means.

The significant 3-way interaction suggests that there are one or more two-way interactions that
are significant. We will select some two-way interactions to test for each of the levels of the
third variable. If any of the two-way interactions are significant we will test the main
effect of one variable at each level of the other variable. These are known as tests of simple
main effects. Finally, if necessary, we follow these tests
of simple main effects up with pair-wise comparisons among the appropriate means.

Because we have some specific prior knowledge concerning the variables in this model we will
begin by plotting the cell means for the b by c interactions for each level of
a. We do this by putting the estimates from the margins command into a
matrix mean. Then we create separate matrices for the levels of the three factor
variables, cleverly calling them a, b and c. You can look at any of these
matrices by typing matrix list followed by the matrix name. We will combine all of the
matrices together into a matrix called gph. Using the svmat command we will
put the matrix values into out data in memory. Finally, we will plot the means using the
twoway line command twice, once for each b#c interaction.

The graphs of the cell means suggest that the b#c interaction at a=1 will
be significant while the b#c interaction at a=2 will not. We will use the
test command to test the two-way interactions at for each level of a.
These tests are two degrees of freedom each and will refer to specific
cell means with correct signs to get an interaction effect.
Essentially, we are specifying a two-way interaction using just the
cell means. This is not difficult to set up especially if we use a table representing the six cells
and plus with minuses to indicate the appropriate signs. Here is the table for the first degree
of freedom.

c1 c2 c3
b1 + -
b2 - +

The second degree of freedom refers to this next table.

c1 c2 c3
b1 + -
b2 - +

These two tables translate into the test commands shown below. Each part of the
test command inside parentheses is one degree of freedom.

To simplify adjustments for multiple testing we will use a Bonferroni adjustment across all
tests of simple main effects and pair-wise comparisons. Having already run through this
analysis before
writing this web page, I know that there will be seven such tests. Therefore I have included
a display command giving the adjusted p-values for each test.

Our guess from looking at the graph of the cell means was correct. The test of b#c
at a=1 was significant while b#c at a=2 was not. We can follow this
up with tests of differences among levels of c for each level of b while
holding a at one. Each of these tests will also have two degrees of freedom

The test for differences among levels of c for b=1 while holding a at one
was significant. We will follow this significant test up by looking at all pair-wise
comparisons among the levels of c for b=1 and a=1. Each of these tests
uses one degree of freedom.