How to Run a Factorial ANOVA in Stata

✦ ✦ ✦
Descriptive alt text here

Welcome to… the Factorial ANOVA.

A Factorial ANOVA is really just a One-Way ANOVA with two independent variables instead of one.

The only extra thing you need to think about is something called an interaction effect.

The actual Stata commands are surprisingly straightforward. The hardest part is understanding what the output is actually telling you.

Here’s the workflow I use.

Factorial ANOVA Stata Commands

GoalCommand
View descriptivestab IV1 IV2, summarize(DV)
Create grouped variableegen cond = group(IV1 IV2), label
Means, SDs and sample sizestabstat DV, by(cond) stat(mean sd n)
Create interaction plotmargins IV1#IV2marginsplot
Test normalityby IV1 IV2, sort: swilk DV
Test equal variancesrobvar DV, by(cond)
Run Factorial ANOVAanova DV IV1##IV2
Calculate effect sizesestat esize
Run simple effectscontrast IV1@IV2, effects mcompare(bon)

What is a Factorial ANOVA?

A Factorial ANOVA is used when you want to investigate the effects of two independent variables on one dependent variable.

For example, suppose researchers wanted to investigate whether sex and alcohol consumption influence attractiveness ratings.

Our variables would be:

Dependent Variable (DV)

  • Attractiveness ratings

Independent Variables (IVs)

  • Sex (Male/Female)
  • Number of pints consumed (0, 2 or 4)

Unlike a One-Way ANOVA, a Factorial ANOVA answers three questions:

  • Does sex affect attractiveness ratings?
  • Does alcohol consumption affect attractiveness ratings?
  • Does the effect of alcohol depend on whether someone is male or female?

That third question is called the interaction effect, and it’s what makes a Factorial ANOVA different from a One-Way ANOVA.

Step 1: Check your descriptives

Before running any statistical test, it’s always a good idea to understand your data first.

Run these commands:

tab IV1 IV2, summarize(DV)
egen cond = group(IV1 IV2), label
tabstat DV, by(cond) stat(mean sd n)

Replace:

  • DV with your dependent variable
  • IV1 with your first independent variable
  • IV2 with your second independent variable

For our example, the commands would be:

tab sex pints, summarize(attract)
egen cond = group(sex pints), label
tabstat attract, by(cond) stat(mean sd n)

These commands tell you:

  • The mean for each condition
  • The standard deviation
  • The sample size
  • Whether any groups already appear noticeably different

In our example, this allows us to compare the average attractiveness ratings for every combination of sex and alcohol consumption before we’ve even run the ANOVA.

Step 2: Create an interaction plot

One of the biggest advantages of a Factorial ANOVA is that it can test interaction effects.

The easiest way to get an idea of whether an interaction might exist is by creating an interaction graph.

Run:

anova DV IV1##IV2
margins IV1#IV2
marginsplot

For our example:

anova attract sex##pints
margins sex#pints
marginsplot

What are you looking for?

Parallel lines

→ Probably no interaction.

Crossing or diverging lines

→ There may be an interaction between your two independent variables.

In our example, the male and female lines cross after four pints, suggesting there may be an interaction between sex and alcohol consumption.

Remember, this graph doesn’t prove an interaction exists—it simply gives you an early visual clue.

Step 3: Check the assumptions

Like a One-Way ANOVA, a Factorial ANOVA has a few assumptions that should be checked before interpreting the results.

AssumptionStata CommandGood ResultBad Result
Normalityby IV1 IV2, sort: swilk DVp > .05p < .05
Equal variancesrobvar DV, by(cond)p > .05p < .05
Normality

Run:

by IV1 IV2, sort: swilk DV

Example:

by sex pints, sort: swilk attract

This performs a Shapiro-Wilk test separately for every condition.

You’re hoping for:

p > .05

This suggests the data are approximately normally distributed.

In our example, every condition passed the Shapiro-Wilk test.

Equality of variances

Run:

robvar DV, by(cond)

Example:

robvar attract, by(cond)

Again, you’re hoping for:

p > .05

This suggests the spread of scores is similar across every condition.

In our example, all groups passed the equality of variances test.

Step 4: Run the Factorial ANOVA

Once you’ve checked your assumptions, running the analysis is easy.

anova DV IV1##IV2

Example:

anova attract sex##pints

The double hashtags (##) tell Stata to test:

  • The main effect of IV1
  • The main effect of IV2
  • The interaction between IV1 and IV2

If your assignment requires effect sizes, simply run:

estat esize

Step 5: Interpret the output

When you run a Factorial ANOVA, your output contains three separate hypothesis tests.

Let’s look at what each one means.

Main Effect of IV1

This asks:

Does the first independent variable affect the dependent variable overall?

In our example:

Do males and females differ in attractiveness ratings overall?

Notice we’re averaging across all alcohol conditions.

If the p-value is less than .05, we conclude that sex has a statistically significant effect on attractiveness ratings.

Main Effect of IV2

This asks:

Does the second independent variable affect the dependent variable overall?

In our example:

Does alcohol consumption affect attractiveness ratings?

Here we’re averaging across both males and females.

If p < .05, we conclude alcohol consumption has a statistically significant effect.

Interaction Effect

This is usually the most interesting result.

It asks:

Does the effect of one independent variable depend on the level of the other independent variable?

In our example:

Does alcohol affect attractiveness ratings differently for males and females?

If the interaction is not significant, life is fairly simple.

You interpret any significant main effects and, if needed, run pairwise comparisons.

If the interaction is significant, things change.

A significant interaction tells us that the effect of one independent variable changes depending on the other independent variable.

This means the main effects no longer tell the whole story.

Instead, we need to investigate where the interaction occurs by running simple effects analyses.

In our example, the overall model was statistically significant (p < .001). Alcohol consumption had a significant main effect, and there was also a significant interaction between sex and alcohol consumption (p = .001). This tells us that the effect of alcohol on attractiveness ratings was different for males and females.

Step 6: Follow up a significant interaction

If your interaction is significant, run:

contrast IV1@IV2, effects mcompare(bon)

Example:

contrast sex@pints, effects mcompare(bon)

This compares the levels of one independent variable separately at each level of the other independent variable.

For our example, the results showed that:

  • There was no significant difference between males and females after 0 pints.
  • There was no significant difference after 2 pints.
  • There was a statistically significant difference after 4 pints.

This tells us that alcohol only affected males and females differently after four pints had been consumed.

Specifically, females who had consumed four pints gave significantly higher attractiveness ratings than males who had also consumed four pints.

Example Write-Up

A two-way factorial ANOVA was conducted to examine the effects of sex and alcohol consumption on attractiveness ratings. There was a significant main effect of alcohol consumption, F(df₁, df₂) = X.XX, p < .001, indicating that attractiveness ratings differed across the alcohol conditions. There was no significant main effect of sex. However, there was a significant interaction between sex and alcohol consumption, F(df₁, df₂) = X.XX, p = .001, suggesting that the effect of alcohol differed for males and females. Follow-up simple effects analyses showed that females gave significantly higher attractiveness ratings than males after consuming four pints of alcohol, whereas no significant sex differences were found after zero or two pints.

And that’s it!

A Factorial ANOVA might look intimidating at first, but it’s really just a One-Way ANOVA with an extra independent variable. Once you understand the difference between main effects and interaction effects, the Stata commands become surprisingly straightforward.

🌼 About Daisy

Hi! I’m Daisy, the voice behind The Psych Diaries. I share study notes, templates, and honest little rambles about navigating psychology and university life.

Read More About Daisy →

Discover more from The Psych Diaries

Subscribe now to keep reading and get access to the full archive.

Continue reading