Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Chapter 6 Two-Way ANOVA

Recall this chart that showed how most of our course would be organized: Explanatory Variable(s) Categorical Categorical Quantitative Quantitative Response Variable Categorical Quantitative Quantitative Categorical Methods Contingency Tables ANOVA Regression (not discussed)

In the previous chapter, we learned that one-way ANOVA can be used to analyze data consisting of a quantitative response variable and one categorical explanatory variable (one factor). We can incorporate two factors into our analysis using a procedure called two-way ANOVA. Its important to remember that two-way ANOVA uses two dierent explanatory variables and a response variablewere not just talking about having an explanatory variable with two values.

6.1

Basics of Two-Way ANOVA

Lets start by discussing the way we organize and label data for two-way ANOVA. We also need to formulate the basic questions that we plan to ask.

Setup

When we discussed one-way ANOVA, we thought about the factor as dividing the subjects into groups. In two-way ANOVA, we now have two factors. Each

6.1 Basics of Two-Way ANOVA

80

subject has a value for each factor, and each combination of factor values now constitutes a group. Distinction from Contingency Tables A two-way ANOVA setup is not the same as a two-way contingency table. In two-way ANOVA, we classify subjects according to their values for two categorical variables. Then, for each of those subjects, we record the value of some quantitative response variable. In a two-way contingency table, we also classify subjects according to their values for two categorical variables. However, thats all we do we simply count the number of subjects in each cell (group) and put that number in the table. Notation Call our two factors Factor A and Factor B. Let a represent the number of categories of Factor A, and let b represent the number of categories of Factor B. Then the total number of groups is ab. We will again call the total number of observations N . The response variable value for each observation will typically need to be represented as something like Yijk , where i denotes the subjects category for Factor A, and j denotes the subjects category for Factor B. Then i and j together identify a group, and k denotes which individual were talking about within this particular group. Its an odd mathematical fact that for two-way ANOVA to give completely reliable results, the number of observations in each group needs to be the same. (Dont be surprised if you see people violating this rule in practice. It still works reasonably well as long as the group sizes are similar.) Call the number of observations in each group n, and notice that N = abn. Example 6.1: Suppose we want to examine how the number of hours of TV people watch per week depends on two variables: gender and age. Each person is classied according to gender (male, female) and age (1824, 2554, 55+). There are six groupsone for each combination of gender and age. We randomly sample ve people from each group, and each person reports the time, in hours, that he or she watches TV per week. The data is shown in Figure 6.1.

6.1 Basics of Two-Way ANOVA

81

To set our notation, call gender Factor A and age Factor B, and number the categories in the order theyre shown in the table.

Male

Female

Age 1824 20 27 20 22 28 25 19 27 32 31

Age 2554 23 21 23 28 28 32 26 33 33 24

Age 55+ 33 33 39 33 37 44 43 52 43 54

Figure 6.1: Hours of TV watched per week for a random sample of ve men and ve women from each of three age groups

Questions of Interest There are three main questions that we might ask in two-way ANOVA: Does the response variable depend on Factor A? Does the response variable depend on Factor B? Does the response variable depend on Factor A dierently for dierent values of Factor B, and vice versa? All of these questions can be answered using hypothesis tests.

6.1 Basics of Two-Way ANOVA

82

Example 6.2: In the context of Example 6.1, the rst two questions above simply ask whether TV viewing time depends on age and gender. The third question asks whether TV viewing time depends on gender dierently for people of dierent ages, or whether TV viewing time depends on age dierently for men than for women. (For example, perhaps its true that women 55+ watch more TV than men 55+, but women 1824 watch less TV than men 1824.)

Two-Way ANOVA Table

Like one-way ANOVA, two-way ANOVA examines dierent kinds of variability in the data. Much of this variability can be summarized in a two-way ANOVA table. Like the ANOVA table we saw before, the two-way ANOVA table also shows sums of squares, degrees of freedom, and mean squares. To write down formulas, well need to set some more notation: ij represents the sample mean of the group in category i of Factor A Y and category j of Factor B (always an average of n observations). i represents the sample mean of all the data in category i of Factor A Y combined (always an average of bn observations). j represents the sample mean of all the data in category j of Factor B Y combined (always an average of an observations). represents the overall sample mean of all the data from all groups Y combined (always an average of all abn = N observations). Sums of Squares Two-way ANOVA involves ve dierent sums of squares: The total sum of squares, SS Tot , measures the total variability in the response variable values. Its formula is
a b n

SS Tot =
i= 1 j = 1 k = 1

Yijk Y

Like before, SS Tot (N 1) is what we would get if we lumped all N observations together, ignoring groups, and calculated the sample standard deviation.

6.1 Basics of Two-Way ANOVA

83

The Factor A sum of squares, SS A , measures the variability that can be explained by dierences in Factor A. Its formula is
a

SS A =
i=1

i Y bn Y

The Factor B sum of squares, SS B , measures the variability that can be explained by dierences in Factor B. Its formula is
b

SS B =
j =1

j Y an Y

The interaction sum of squares, SS AB , measures the variability that can be explained by interaction between the eects of Factors A and B. (Well talk more about what this means later.) Its formula is
a b

SS AB =
i=1 j =1

ij Y i Y j + Y n Y

The error sum of squares, SS E , measures the variability of the observations around their group sample means. Its formula is
a b n

SS E =
i= 1 j = 1 k = 1

ij Yijk Y

If we call the sample standard deviation within each group sij , then another formula for SS E is
a b

SS E =

(n 1)s2 ij .
i=1 j =1

It turns out to be true that SS Tot = SS A + SS B + SS AB + SS E . The total variability can be split up into dierent sources, just like we saw for one-way ANOVA. Degrees of Freedom The sums of squares in two-way ANOVA tend to be inuenced by the values of a, b, and N . We can take this into account using the degrees of freedom, df , for each sum of squares. Their formulas are dfTot = N 1, dfA = a 1, dfB = b 1, dfAB = (a 1)(b 1), dfE = N ab.

Notice that dfTot = dfA + dfB + dfAB + dfE .

6.1 Basics of Two-Way ANOVA

84

Mean Squares The mean squares are just the sums of squares divided by their degrees of freedom: MS A = SS A , dfA MS B = SS B , dfB MS AB = SS AB , dfAB MS E = SS E . dfE

(We seldom bother calculating MS Tot , because its just the square of the sample standard deviation of all N observations lumped together.) The mean squares measure the variability in a way that properly accounts for a, b, and N , unlike the sums of squares. ANOVA Table We typically present all of this information in an ANOVA table. An ANOVA table for two-way ANOVA is laid out as shown in Figure 6.2. Source Factor A Factor B Interaction Error Total df dfA dfB dfAB dfE dfTot SS SS A SS B SS AB SS E SS Tot MS MS A MS B MS AB MS E

Figure 6.2: Generic two-way ANOVA table Example 6.3: Using statistical software, we obtain the ANOVA table shown in Figure 6.3 for the TV viewing data. Source Factor A (Gender) Factor B (Age) Interaction Error Total df 1 2 2 24 29 SS MS 353.63 353.63 1520.00 760.00 109.87 54.93 429.20 17.88 2412.70

Figure 6.3: ANOVA table for the TV viewing data

6.2 Two-Way ANOVA Hypothesis Tests

85

6.2

Two-Way ANOVA Hypothesis Tests

Recall that there are three main questions that we might ask in two-way ANOVA: Does the response variable depend on Factor A? Does the response variable depend on Factor B? Does the response variable depend on Factor A dierently for dierent values of Factor B, and vice versa? All of these questions can be answered using hypothesis tests. Well see that the rst two questions can be answered using the two-way ANOVA F tests for main eects, while the third question can be answered using the two-way ANOVA F test for interaction. It turns out that the test for interaction is the right one to do rst, for reasons well explain later.

Two-Way ANOVA F Test for Interaction

The word interaction has a very specic meaning in the context of twoway ANOVA. We say that there is interaction if Y depends on Factor A dierently for dierent values of Factor B, and vice versa. Similarly, we say that there is no interaction if Y depends on Factor A in the same way for all values of Factor B, and vice versa. This idea is most easily understood with an example. Example 6.4: Figure 6.4 shows two possibilities for the population group mean TV viewing times. There is no interaction on the left. For each age group, women average watching ve more hours of TV per week than men. For each gender, the middle age group averages watching six more hours of TV per week than the youngest age group, and the oldest age group averages watching nine more hours of TV per week than the middle age group. There is interaction on the right. For each age group, women average watching more TV than men, but how much more varies for the dierent age groups. Also, for each gender, older people average watching more TV, but how much more varies by gender.

6.2 Two-Way ANOVA Hypothesis Tests 1824 2554 55+ 22 28 37 27 33 42 1824 2554 55+ 22 28 37 27 32 43

86

Male Female

Male Female

Figure 6.4: Two sets of hypothetical population group means When we ask whether or not there is interaction, were asking a question about the population, not the sample. Random variation ensures that for sample data, the group averages will almost never line up perfectly like on the left in Figure 6.4, even if there really is no interaction. But we can still use the sample data to draw a conclusion about whether or not there is interaction for the overall population. We do this using a hypothesis test called the two-way ANOVA F test for interaction. Assumptions The assumptions for the two-way ANOVA F test for interaction are exactly the same as those of the one-way ANOVA F test, with one additional requirement: the number of observations should be the the same for all groups. Hypotheses In the test for interaction, the null hypothesis is that there is no interaction, while the alternative hypothesis is that there is interaction. Test Statistic The test statistic for the test for interaction is FAB = MS AB , MS E

where the AB subscript distinguishes it from other test statistics in two-way ANOVA that well see later. Lets think about how FAB behaves: If H0 is true, which means there is no interaction, then FAB tends to be small (around 1 or smaller). So small values of FAB represent little or no evidence against H0 .

6.2 Two-Way ANOVA Hypothesis Tests

87

If H0 is false and Ha is true instead, which means there is interaction, then FAB tends to be larger. So larger values of FAB represent more evidence against H0 and in support of Ha . P-Value When H0 is true, FAB has an F distribution with degrees of freedom corresponding to the mean squares in the numerator and denominator of FAB : df1 = dfAB = (a 1)(b 1), df2 = dfE = N ab.

So then the p-value of the test is the probability to the right of (larger than) our observed test statistic value for an F distribution with degrees of freedom shown above. As weve seen before, we typically need to use software to get an exact p-value, but we can use an F table to gure out whether the p-value is larger or smaller than 0.05. Decision As usual, if the p-value is less than or equal to the signicance level that we choose in advance (often 0.05), then we reject H0 , and we believe Ha instead. If the p-value is greater than , then we fail to reject H0 , which means we believe H0 is reasonable. Lets think about what this means in the context of our actual hypotheses: If we reject H0 , that means we believe there is interaction. If we fail to reject H0 , that means we think its reasonable that there is no interaction. Example 6.5: Lets go through the ve steps of the two-way ANOVA F test for interaction for the data in Figure 6.1 using = 0.05.

6.2 Two-Way ANOVA Hypothesis Tests

88

After the Test for Interaction Remember that the test for interaction answers only one of our three questions of interest. We probably still want answers to the more obvious questions of whether or not the response depends on Factor A and whether or not it depends on Factor B. The proper way to proceed depends on the result of the test for interaction.

6.2 Two-Way ANOVA Hypothesis Tests

89

If we believe there is interaction, then we dont bother to ask whether the response depends on Factor A or Factor B separatelythe fact that there is interaction means that the response depends on Factor A dierently for dierent values of Factor B, and vice versa. So we stop here and do not perform the tests for main eects (which well talk about in the next subsection). If we believe its reasonable that there is no interaction, then that means we can look at the eects of Factor A and Factor B separately, so we proceed to the tests for main eects. Example 6.6: . . Figure 6.5 may be helpful for remembering various results and interpretations of a two-way ANOVA F test for interaction. Large FAB value Small FAB value (much larger than 1) (around 1 or less than 1) Small p-value Large p-value Evidence against H0 (for Ha ) No evidence against H0 (for Ha ) Reject H0 Fail to reject H0 Believe that there is Reasonable that there is interaction no interaction Dont test for main eects Test for main eects Figure 6.5: Results and interpretations of a two-way ANOVA F test for interaction

Two-Way ANOVA F Tests for Main Eects

If we believe its reasonable that there is no interaction, we can then ask about whether the response variable depends on Factor A and Factor B separately.

6.2 Two-Way ANOVA Hypothesis Tests

90

We do this using hypothesis tests that we call the tests for the main eects of Factor A and Factor B. When we talk about a test for the main eect of a Factor, were really just talking about testing whether or not the response variable depends on that factor. The dependence of the response variable on a factor is called the main eect of the factor (as opposed to more complicated kinds of eects, like interaction eects). Lets now go through the ve steps of the test for a main eect. To limit repetition, well primarily go through the steps as if we were dealing with Factor A, but remember that we do this test once for the main eect of Factor A, and then we do it again for the main eect of Factor B. Assumptions The assumptions for the tests for main eects are exactly the same as the assumptions for the test for interaction. Hypotheses In the test for the main eect of Factor A, the null hypothesis is that the response variable does not depend on Factor A, while the alternative hypothesis is that it does. (The hypotheses for the test for Factor B are similar.) Test Statistic The test statistic for the test for the main eect of Factor A is FA = MS A , MS E

where the A subscript distinguishes it from various other test statistics (like FAB and FB ). This test statistic behaves like any other F test statistic weve seen before: If H0 is true, which means the response variable doesnt depend on Factor A, then FA tends to be small (around 1 or smaller). So small values of FA represent little or no evidence against H0 . If H0 is false and Ha is true instead, which means the response variable depends on Factor A, then FA tends to be larger. So larger values of FA represent more evidence against H0 and in support of Ha .

6.2 Two-Way ANOVA Hypothesis Tests

91

The test statistic for the test for the main eect of Factor B is FB = and it behaves similarly to FA . P-Value When H0 is true, FA has an F distribution with degrees of freedom corresponding to the mean squares in the numerator and denominator of FA : df1 = dfA = a 1, df2 = dfE = N ab. MS B , MS E

So then the p-value of the test is the probability to the right of (larger than) our observed test statistic value for an F distribution with degrees of freedom shown above. As weve seen before, we typically need to use software to get an exact p-value, but we can use an F table to gure out whether the p-value is larger or smaller than 0.05. Similarly, when H0 is true, FB has an F distribution with df1 = dfB = b 1, df2 = dfE = N ab,

and we nd its p-value in the same way. Decision As usual, if the p-value is less than or equal to the signicance level that we choose in advance (often 0.05), then we reject H0 , and we believe Ha instead. If the p-value is greater than , then we fail to reject H0 , which means we believe H0 is reasonable. Lets think about what this means in the context of our actual hypotheses: If we reject H0 , that means we believe the response variable depends on the factor in question. If we fail to reject H0 , that means we think its reasonable that the response variable does not depend on the factor in question. Example 6.7: Lets go through the ve steps of the two-way ANOVA F test for each main eect for the data in Figure 6.1 using = 0.05. Well start with the test for the main eect of Factor A (gender).

6.2 Two-Way ANOVA Hypothesis Tests

92

6.2 Two-Way ANOVA Hypothesis Tests

93

Figure 6.6 may be helpful for remembering various results and interpretations of a two-way ANOVA F test for the main eect of Factor A. (Simply substitute FB for FA and Factor B for Factor A to obtain an equivalent chart for the test for the main eect of Factor B.) Large FA value (much larger than 1) Small p-value Evidence against H0 (for Ha ) Reject H0 Believe that response variable depends on Factor A Small FA value (around 1 or less than 1) Large p-value No evidence against H0 (for Ha ) Fail to reject H0 Reasonable that response variable does not depend on Factor A

Figure 6.6: Results and interpretations of a two-way ANOVA F test for the main eect of Factor A

Advantages of Two-Way ANOVA

When we have two factors, why do we perform a two-way ANOVA analysis, rather than simply performing a separate one-way ANOVA analysis for each factor? It turns out that a two-way ANOVA analysis is better, for a couple of reasons. Interaction Two-way ANOVA allows us to consider interaction eects, which allows us to get a more accurate representation of how the response variable depends on the two factors.

6.3 Two-Way ANOVA Condence Intervals

94

Fewer Type II Errors Two-way ANOVA considers more sources of variability than each individual one-way ANOVA does. This usually, though not always, leads to a smaller MS E in the two-way ANOVA than in either one-way ANOVA, which in turn leads to fewer type II errors when testing whether the response variable depends on each factor.

6.3

Two-Way ANOVA Condence Intervals

We may or may not have time to cover this section. If we do, Ill type up a couple of extra pages of notes here.

You might also like