Chapter page 30 / 3822 Inference for comparing many means
English

22  Inference for comparing many means

In Chapter 20 analysis was done to compare the average population value across two different groups. An important aspect of the analysis was to look at the difference in sample means as an estimate for the difference in population means. When comparing more than two groups, the difference (i.e., subtraction) will not fully capture the nuance in variation across the three or more groups. As with two groups, the research question will focus on whether the group membership is independent of the numerical response variable. Here, independence across groups means that knowledge of the observations in one group does not change what we would expect to happen in the other group. But what happens if the groups are dependent? In this section we focus on a new statistic which incorporates differences in means across more than two groups. Although the ideas in this chapter are quite similar to the t-test, they have earned themselves their own name: ANalysis Of VAriance, or ANOVA.

Sometimes we want to compare means across many groups. We might initially think to do pairwise comparisons. For example, if there were three groups, we might be tempted to compare the first mean with the second, then with the third, and then finally compare the second and third means for a total of three comparisons. However, this strategy can be treacherous. If we have many groups and do many comparisons, it is likely that we will eventually find a difference just by chance, even if there is no difference in the populations. Instead, we should apply a holistic test to check whether there is evidence that at least one pair of groups are in fact different, which is where ANOVA saves the day.

In this section, we will learn a new method called analysis of variance (ANOVA) and a new test statistic called an \(F\)-statistic (which we will introduce in our discussion of mathematical models). ANOVA uses a single hypothesis test to check whether the means across many groups are equal:

  • \(H_0:\) The mean outcome is the same across all groups, i.e., \(\mu_1 = \mu_2 = \cdots = \mu_k\) where \(\mu_j\) represents the mean of the outcome for observations in category \(j.\)
  • \(H_A:\) At least one mean is different.

Generally we must check three conditions on the data before performing ANOVA:

  • the observations are independent within and between groups,
  • the responses within each group are nearly normal, and
  • the variability across the groups is about equal.

When the three technical conditions are met, we may perform an ANOVA to determine whether the data provide convincing evidence against the null hypothesis that all the \(\mu_j\) are equal.

College departments commonly run multiple sections of the same introductory course each semester because of high demand. Consider a statistics department that runs three sections of an introductory statistics course. We might like to determine whether there are substantial differences in first exam scores in these three classes (Section A, Section B, and Section C). Describe appropriate hypotheses to determine whether there are any differences between the three classes.


The hypotheses may be written in the following form:

  • \(H_0:\) The average score is identical in all sections, \(\mu_A = \mu_B = \mu_C\). Assuming each class is equally difficult, the observed difference in the exam scores is due to chance.
  • \(H_A:\) The average score varies by class. We would reject the null hypothesis in favor of the alternative hypothesis if there were larger differences among the class averages than what we might expect from chance alone.

Strong evidence favoring the alternative hypothesis in ANOVA is described by unusually large differences among the group means. We will soon learn that assessing the variability of the group means relative to the variability among individual observations within each group is key to ANOVA’s success.

Examine Figure 22.1. Compare groups I, II, and III. Can you visually determine if the differences in the group centers is unlikely to have occurred if there were no differences in the groups? Now compare groups IV, V, and VI. Do these differences appear to be unlikely to have occurred if there were no differences in the groups?


Any real difference in the means of groups I, II, and III is difficult to discern, because the data within each group are very volatile relative to any differences in the average outcome. On the other hand, it appears there are differences in the centers of groups IV, V, and VI. For instance, group V appears to have a higher mean than that of the other two groups. Investigating groups IV, V, and VI, we see the differences in the groups’ centers are noticeable because those differences are large relative to the variability in the individual observations within each group.

Two sets of side by side dot plots. The first set shows three groups of observations where the variability within a group is so large that it swamps out any variability across the groups. The second set shows three groups of observations where the variability within a group is much smaller and the center of the groups appears different.
Figure 22.1: Side-by-side dot plot for the outcomes for six groups. Two sets of groups; first set is comprised of Groups I, II, and III, the second set is comprised of Groups IV, V, and VI.

22.1 Case study: Batting

We would like to discern whether there are real differences between the batting performance of baseball players according to their position: outfielder (OF), infielder (IF), and catcher (C). We will use a dataset called mlb_players_18, which includes batting records of 429 Major League Baseball (MLB) players from the 2018 season who had at least 100 at bats. Six of the 429 cases represented in mlb_players_18 are shown in Table 22.1, and descriptions for each variable are provided in Table 22.2. The measure we will use for the player batting performance (the outcome variable) is on-base percentage (OBP). The on-base percentage roughly represents the fraction of the time a player successfully gets on base or hits a home run.

The mlb_players_18 data can be found in the openintro R package.

Table 22.1: Six cases and some of the variables from the mlb_players_18 data frame.
name team position AB H HR RBI AVG OBP
Abreu, J CWS IF 499 132 22 78 0.265 0.325
Acuna Jr., R ATL OF 433 127 26 64 0.293 0.366
Adames, W TB IF 288 80 10 34 0.278 0.348
Adams, M STL IF 306 73 21 57 0.239 0.309
Adduci, J DET IF 176 47 3 21 0.267 0.290
Adrianza, E MIN IF 335 84 6 39 0.251 0.301
Table 22.2: Variables and their descriptions for the mlb_players_18 dataset.
Variable Description
name Player name
team The abbreviated name of the player's team
position The player's primary field position (OF, IF, C)
AB Number of opportunities at bat
H Number of hits
HR Number of home runs
RBI Number of runs batted in
AVG Batting average, which is equal to H/AB
OBP On-base percentage, which is roughly equal to the fraction of times a player gets on base or hits a home run

The null hypothesis under consideration is the following: \(\mu_{OF} = \mu_{IF} = \mu_{C} % = \mu_{DH}.\) Write the null and corresponding alternative hypotheses in plain language.1

The player positions have been divided into three groups: outfield (OF), infield (IF), and catcher (C). What would be an appropriate point estimate of the on-base percentage by outfielders, \(\mu_{OF}\)?


A good estimate of the on-base percentage by outfielders would be the sample average of OBP for just those players whose position is outfield: \(\bar{x}_{OF} = 0.320.\)

22.2 Randomization test for comparing many means

Table 22.3 provides summary statistics for each group. A side-by-side box plots for the on-base percentage is shown in Figure 22.2. Notice that the variability appears to be approximately constant across groups; nearly constant variance across groups is an important assumption that must be satisfied before we consider the ANOVA approach.

Table 22.3: Summary statistics of on-base percentage, split by player position.
Position n Mean SD
OF 160 0.320 0.043
IF 205 0.318 0.038
C 64 0.302 0.038
Side-by-side box plots describing on-base percentage broken down by outfield, infield, or catcher. The catchers seems to have a slightly lower on-base percentage, but the vast majority of players have an on-base percentage 0.27 and 0.35.
Figure 22.2: Side-by-side box plots of the on-base percentage for 429 players across three groups. There are a few potential outliers, but with large numbers of observations in each group, the outliers are not extreme enough to have an impact on the calculations, so it is not a concern for moving forward with the analysis.

The largest difference between the sample means is between the catcher and the outfielder positions. Consider again the original hypotheses:

  • \(H_0:\) \(\mu_{OF} = \mu_{IF} = \mu_{C}\)
  • \(H_A:\) The average on-base percentage \((\mu_j)\) varies across some (or all) groups.

Why might it be inappropriate to run the test by simply estimating whether the difference of \(\mu_{C}\) and \(\mu_{OF}\) is “statistically discernible” at a 0.05 discernibility level?


The primary issue here is that we are inspecting the data before picking the groups that will be compared. It is inappropriate to examine all data by eye (informal testing) and only afterwards decide which parts to formally test. This is called data snooping or data fishing. Naturally, we would pick the groups with the large differences for the formal test, and this would lead to an inflation in the Type I error rate. To understand inflated Type I error rates better, let’s consider a slightly different problem.

Suppose we are to measure the aptitude for students in 20 classes in a large elementary school at the beginning of the year. In this school, all students are randomly assigned to classrooms, so any differences we observe between the classes at the start of the year are completely due to chance. However, with so many groups, we will probably observe a few groups that look rather different from each other. If we select only the classes that look different and then perform a formal test, we will probably make the wrong conclusion that the assignment wasn’t random. While we might only formally test differences for a few pairs of classes, we informally evaluated the other classes by eye before choosing the most extreme cases for a comparison.

22.2.1 Observed data

In the next section we will learn how to use the \(F\) statistic to test whether observed differences in sample means could have happened just by chance even if there was no difference in the respective population means.

The method of analysis of variance in this context focuses on answering one question: is the variability in the sample means so large that it seems unlikely to be from chance alone? This question is different from earlier testing procedures since we will simultaneously consider many groups, and evaluate whether their sample means differ more than we would expect from natural variation. We call this variability the mean square between groups (MSG) \((MSG)\), and it has an associated degrees of freedom, \(df_{G} = k - 1\) when there are \(k\) groups. The \(MSG\) can be thought of as a scaled variance formula for means. If the null hypothesis is true, any variation in the sample means is due to chance and shouldn’t be too large. Details of \(MSG\) calculations are provided in the footnote.2 However, we typically use software for these computations.

The mean square between the groups is, on its own, quite useless in a hypothesis test. We need a benchmark value for how much variability should be expected among the sample means if the null hypothesis is true. To this end, we compute a pooled variance estimate, often abbreviated as the mean square error \((MSE)\), which has an associated degrees of freedom value \(df_E = n - k.\) It is helpful to think of \(MSE\) as a measure of the variability within the groups. Details of the computations of the \(MSE\) and a link to an extra online section for ANOVA calculations are provided in the footnote.3

When the null hypothesis is true, any differences among the sample means are only due to chance, and the \(MSG\) and \(MSE\) should be about equal. As a test statistic for ANOVA, we examine the fraction of \(MSG\) and \(MSE:\)

\[F = \frac{MSG}{MSE}\]

The \(MSG\) represents a measure of the between-group variability, and \(MSE\) measures the variability within each of the groups.

The test statistic for three or more means is an F.

The F statistic is a ratio of how the groups differ (MSG) as compared to how the observations within a group vary (MSE).

\[F = \frac{MSG}{MSE}\]

When the null hypothesis is true and the conditions are met, F has an F-distribution with \(df_1 = k-1\) and \(df_2 = n-k.\)

Conditions:

  • independent observations, both within and across groups
  • large samples and no extreme outliers

22.2.2 Variability of the statistic

We recall the exams from Section 20.1 which demonstrated a two-sample randomization test for a comparison of means. Suppose now that the teacher had had such an extremely large class that three different exams were given: A, B, and C. Table 22.4 and Figure 22.3 provide a summary of the data including exam C. Again, we would like to investigate whether the difficulty of the exams is the same across the three exams, so the test is

  • \(H_0: \mu_A = \mu_B = \mu_C.\) The inherent average difficulty is the same across the three exams.
  • \(H_A:\) not \(H_0.\) At least one of the exams is inherently more (or less) difficult than the others.

The classdata data can be found in the openintro R package.

Table 22.4: Summary statistics of scores for each exam version.
Exam n Mean SD Min Max
A 58 75.1 13.9 44 100
B 55 72.0 13.8 38 100
C 51 78.9 13.1 45 100
Side-by-side box plots of exam score broken down by exam A, exam B, or exam C. Exam C's median is above 80 which is higher than exam A with a median around 74 and exam B with a median around 72.
Figure 22.3: Exam scores for students given one of three different exams.

Figure 22.4 shows the process of randomizing the three different exams to the observed exam scores. If the null hypothesis is true, then the score on each exam should represent the true student ability on that material. It shouldn’t matter whether they were given exam A or exam B or exam C. By reallocating which student got which exam, we are able to understand how the difference in average exam scores changes due only to natural variability. There is only one iteration of the randomization process in Figure 22.4, leading to three different randomized sample means (computed assuming the null hypothesis is true).

Four panels representing four different orientations of a toy dataset of 13 exam scores. The first panel provides the observed data; 4 of the exams were version A and the average score was 77.25; 5 of the exams were version B and the average score was 75.8; 4 of the exams were version C and the average score was 78.5. The observed F statistic is 0.0747. The second panel shows the shuffled reassignment of the exam versions (4 of the scores are randomly reassigned to A, 5 of the scores are randomly reassigned to B, 4 of the scores are randomly reassigned to C). The third panel shows which score is connected with which new reassigned version of the exam. And the fourth panel sorts the exams so that version A exams are together, version B exams are together, and version C exams are together. In the randomly reassigned versions, the average score for version A is 72.25, the average score for version B is 78, and the average score for version C is 75.25. The randomized F statistic is 0.1637.
Figure 22.4: The version of the test (A or B or C) is randomly allocated to the test scores, under the null assumption that the tests are equally difficult.

In the two-sample case, the null hypothesis was investigated using the difference in the sample means. However, as noted above, with three groups (three different exams), the comparison of the three sample means gets slightly more complicated. We have already derived the F-statistic which is exactly the way to compare the averages across three or more groups! Recall, the F statistic is a ratio of how the groups differ (MSG) as compared to how the observations within a group vary (MSE).

Building on Figure 22.4, Figure 22.5 shows the values of the simulated \(F\) statistics over 1,000 random simulations. We see that, just by chance, the F statistic can be as large as 7.

Histogram of the F statistics that were calculated from 1000 different randomizations of the exam type. The distribution is right-skewed with most of the values less than 2. The tail extends to about 6.
Figure 22.5: Histogram of F statistics calculated from 1,000 different randomizations of the exam type.

22.2.3 Observed statistic vs. null statistics

Histogram of the F statistics that were calculated from 1000 different randomizations of the exam type. The distribution is right-skewed with most of the values less than 2. The tail extends to about 6. The observed F statistic is given as a red vertical line at 3.48. The area to the right of 3.48 is more extreme than the observed value and represents the p-value.
Figure 22.6: Histogram of F statistics calculated from 1000 different randomizations of the exam type. The observed F statistic is given as a red vertical line at 3.48. The area to the right is more extreme than the observed value and represents the p-value.

Using statistical software, we can calculate that 3.6% of the randomized F test statistics were at or above the observed test statistic of \(F= 3.48.\) That is, the p-value of the test is 0.036. Assuming that we had set the level of discernibility to be \(\alpha = 0.05,\) the p-value is smaller than the level of discernibility which would lead us to reject the null hypothesis. We claim that the difficulty level (i.e., the true average score, \(\mu)\) is different for at least one of the exams.

While it is temping to say that exam C is harder than the other two (given the inability to differentiate between exam A and exam B in Section 20.1), we must be very careful about conclusions made using different techniques on the same data.

When the null hypothesis is true, random variability that exists in nature sometimes produces data with p-values less than 0.05. How often does that happen? 5% of the time. That is to say, if you use 20 different models applied to the same data where there is no signal (i.e., the null hypothesis is true), you are reasonably likely to get a p-value less than 0.05 in one of the tests you run. The details surrounding the ideas of this problem, called a multiple comparisons test or multiple comparisons problem, are outside the scope of this textbook, but should be something that you keep in the back of your head. To best mitigate any extra Type I errors, we suggest that you set up your hypotheses and testing protocol before running any analyses. Once the conclusions have been reached, you should report your findings instead of running a different type of test on the same data.

22.3 Mathematical model for test for comparing many means

As seen with many of the tests and statistics from previous sections, the randomization test on the F statistic has corresponding mathematical theory to describe the distribution that can be used without using a computational approach.

We return to the baseball example from Table 22.3 to demonstrate the mathematical model applied to the ANOVA setting.

22.3.1 Variability of the statistic

The larger the observed variability in the sample means \((MSG)\) relative to the within-group observations \((MSE)\), the larger \(F\)-statistic will be and the stronger the evidence against the null hypothesis. Because larger \(F\)-statistics represent stronger evidence against the null hypothesis, we use the upper tail of the distribution to compute a p-value.

The F statistic and the F-test.

Analysis of variance (ANOVA) is used to test whether the mean outcome differs across two or more groups. ANOVA uses a test statistic, the \(F\)-statistic, which represents a standardized ratio of variability in the sample means relative to the variability within the groups. If \(H_0\) is true and the model conditions are satisfied, an \(F\)-statistic follows an \(F\) distribution with parameters \(df_{1} = k - 1\) and \(df_{2} = n - k.\) The upper tail of the \(F\) distribution is used to represent the p-value.

For the baseball data, \(MSG = 0.00803\) and \(MSE=0.00158\). Identify the degrees of freedom associated with MSG and MSE and verify the \(F\)-statistic is approximately 5.077.4

22.3.2 Observed statistic vs. null statistics

We can use the \(F\)-statistic to evaluate the hypotheses in what is called an F-test. A p-value can be computed from the \(F\) statistic using an \(F\) distribution, which has two associated parameters: \(df_{1}\) and \(df_{2}.\) For the \(F\)-statistic in ANOVA, \(df_{1} = df_{G}\) and \(df_{2} = df_{E}.\) An \(F\) distribution with 2 and 426 degrees of freedom, corresponding to the \(F\) statistic for the baseball hypothesis test, is shown in Figure 22.7.

A line plot showing the theoretical F distribution with the first degree of freedom equal to 2 and the second degree of freedom equal to 426. The curve is right-skewed with most values less than 4. The tail extends to approximately 8. Area to the right of 5.077 has been shaded in red.
Figure 22.7: An \(F\) distribution with \(df_1=2\) and \(df_2=426.\)

The p-value corresponding to the shaded area in Figure 22.7 is equal to about 0.0066. Does this provide strong evidence against the null hypothesis?


The p-value is smaller than 0.05, indicating the evidence is strong enough to reject the null hypothesis at a discernibility level of 0.05. That is, the data provide strong evidence that the average on-base percentage varies by player’s primary field position.

Note that the small p-value indicates that there is a notable difference between the mean batting averages of the different positions. However, the ANOVA test does not provide a mechanism for knowing which group is driving the differences. If we move forward with all possible two mean comparisons, we run the risk of a high Type I error rate. As we saw at the end of Section 22.2, the follow-up questions surrounding individual group comparisons is called a problem of multiple comparisons and is outside the scope of this text. We encourage you to learn more about multiple comparisons, however, so that additional comparisons, after you have rejected the null hypothesis in an ANOVA test, do not lead to undue false positive conclusions.

22.3.3 Reading an ANOVA table from software

The calculations required to perform an ANOVA by hand are tedious and prone to human error. For these reasons, it is common to use statistical software to calculate the \(F\)-statistic and p-value.

An ANOVA analysis can be summarized in a table very similar to that of a regression summary, which we saw in Chapter 7 and Chapter 8. Table 22.5 shows an ANOVA summary to test whether the mean of on-base percentage varies by player positions in the MLB. Many of these values should look familiar; in particular, the \(F\)-statistic and p-value can be retrieved from the last two columns.

Table 22.5: ANOVA summary for testing whether the average on-base percentage differs across player positions.
term df sumsq meansq statistic p.value
position 2 0.0161 0.0080 5.08 0.0066
Residuals 426 0.6740 0.0016

22.3.4 Conditions for an ANOVA analysis

There are three conditions we must check for an ANOVA analysis: all observations must be independent, the data in each group must be nearly normal (or each group must have reasonably large sample sizes), and the variance within each group must be approximately equal.

  • Independence. If the data are a simple random sample, this condition can be assumed to be satisfied. For processes and experiments, carefully consider whether the data may be independent (e.g., no pairing). For example, in the MLB data, the data were not sampled. However, there are not obvious reasons why independence would not hold for most or all observations.

  • Approximately normal. As with one- and two-sample testing for means, the normality assumption is especially important when the sample size is quite small when it is ironically difficult to check for non-normality. A histogram of the observations from each group is shown in Figure 22.8. Since each of the groups we are considering have relatively large sample sizes, what we are looking for are major outliers. None are apparent, so this conditions is reasonably met.

  • Constant variance. The last assumption is that the variance in the groups is about equal from one group to the next. This assumption can be checked by examining side-by-side box plots of the outcomes across the groups, as in Figure 22.2. In this case, the variability is similar in the four groups but not identical. We see in Table 22.3 that the standard deviation does not vary much from one group to the next.

Diagnostics for an ANOVA analysis.

Independence is always important to an ANOVA analysis. The normality condition is important when the sample sizes for each group are relatively small (if the group sizes are large, the observations do not need to be normal but should also not contain any large outliers). The constant variance condition is especially important when the sample sizes differ between groups.

Three separate histograms of on-base percentage, one for each field position of outfielder, infielder, and catcher. All three histograms are reasonably bell-shaped and symmetric.
Figure 22.8: Histograms of OBP for each field position.

22.4 Chapter review

22.4.1 Summary

In this chapter we have provided both the randomization test and the mathematical model appropriate for addressing questions of equality of means across two or more groups. Note that there were important technical conditions required for confirming that the F distribution appropriately modeled the ANOVA test statistic. Also, you may have noticed that there was no discussion of creating confidence intervals. That is because the ANOVA statistic does not have a direct analogue parameter to estimate. If there is interest in comparisons of mean differences (across each set of two groups), then the methods from Chapter 20 comparing two independent means should be applied.

22.4.2 Terms

The terms introduced in this chapter are presented in Table 22.6. If you’re not sure what some of these terms mean, we recommend you go back in the text and review their definitions. You should be able to easily spot them as bolded text.

Table 22.6: Terms introduced in this chapter.
analysis of variance degrees of freedom multiple comparisons
ANOVA F-test sum of squared error (SSE)
data fishing mean square between groups (MSG) sum of squares between groups (SSG)
data snooping mean square error (MSE) sum of squares total (SST)

22.5 Exercises

Answers to odd-numbered exercises can be found in Appendix A.22.

  1. Fill in the blank. When doing an ANOVA, you observe large differences in means between groups. Within the ANOVA framework, this would most likely be interpreted as evidence strongly favoring the _____________ hypothesis.
  1. Which test? We would like to test if students who are in the social sciences, natural sciences, arts and humanities, and other fields spend the same amount of time, on average, studying for a course. What type of test should we use? Explain your reasoning.
  1. Cuckoo bird egg lengths, randomize once. Cuckoo birds lay their eggs in other birds’ nests, making them known as brood parasites. One question relates to whether the size of the cuckoo egg differs depending on the species of the host bird.5 (Latter 1902) Consider the following plots, one represents the original data, the second represents data where the host species has been randomly assigned to the egg length.

    1. Consider the average length of the eggs for each species. Is the average length for the original data: more variable, less variable, or about the same as the randomized species? Describe what you see in the plots.

    2. Consider the standard deviation of the lengths of the eggs within each species. Is the within species standard deviation of the length for the original data: bigger, smaller, or about the same as the randomized species?

    3. Recall that the F statistic’s numerator measures how much the groups vary (MSG) with the denominator measuring how much the within species values vary (MSE), which of the plots above would have a larger F statistic, the original data or the randomized data? Explain.

  1. Cuckoo bird egg lengths, randomization test. Cuckoo birds lay their eggs in other birds’ nests, making them known as brood parasites. One question relates to whether the size of the cuckoo egg differs depending on the species of the host bird.6 (Latter 1902) Using the randomization distribution of the F statistic (host species randomized to egg length), conduct a hypothesis test to evaluate if there is a difference, in the population, between the average egg lengths for different host bird species. Make sure to state your hypotheses clearly and interpret your results in context of the data.

  1. Chicken diet and weight, many groups. An experiment was conducted to measure and compare the effectiveness of various feed supplements on the growth rate of chickens. Newly hatched chicks were randomly allocated into six groups, and each group was given a different feed supplement. Sample statistics and a visualization of the observed data are shown below. (McNeil 1977)

    Feed type Mean SD n
    casein 323.58 64.43 12
    horsebean 160.20 38.63 10
    linseed 218.75 52.24 12
    meatmeal 276.91 64.90 11
    soybean 246.43 54.13 14
    sunflower 328.92 48.84 12

    Using the ANOVA output below, conduct a hypothesis test to determine if these data provide convincing evidence that the average weight of chicks varies across some (or all) groups. Make sure to check relevant conditions.

    term df sumsq meansq statistic p.value
    feed 5 231,129 46,226 15.4 <0.0001
    Residuals 65 195,556 3,009
  1. Teaching descriptive statistics. A study compared five different methods for teaching descriptive statistics. The five methods were traditional lecture and discussion, programmed textbook instruction, programmed text with lectures, computer instruction, and computer instruction with lectures. 45 students were randomly assigned, 9 to each method. After completing the course, students took a 1-hour exam.

    1. What are the hypotheses for evaluating if the average test scores are different for the different teaching methods?

    2. What are the degrees of freedom associated with the \(F\)-test for evaluating these hypotheses?

    3. Suppose the p-value for this test is 0.0168. What is the conclusion?

  1. Coffee, depression, and physical activity. Caffeine is the world’s most widely used stimulant, with approximately 80% consumed in the form of coffee. Participants in a study investigating the relationship between coffee consumption and exercise were asked to report the number of hours they spent per week on moderate (e.g., brisk walking) and vigorous (e.g., strenuous sports and jogging) exercise. Based on these data the researchers estimated the total hours of metabolic equivalent tasks (MET) per week, a value always greater than 0. The table below gives summary statistics of MET for women in this study based on the amount of coffee consumed. (Lucas et al. 2011)

    Caffeinated coffee consumption
    1 cup / week or fewer 2-6 cups / week 1 cups / day 2-3 cups / day 4 cups / day or more
    Mean 18.7 19.6 19.3 18.9 17.5
    SD 21.1 25.5 22.5 22.0 22.0
    n 12,215.0 6,617.0 17,234.0 12,290.0 2,383.0
    1. Write the hypotheses for evaluating if the average physical activity level varies among the different levels of coffee consumption.

    2. Check conditions and describe any assumptions you must make to proceed with the test.

    3. Below is the output associated with this test. What is the conclusion of the test?

    df sumsq meansq statistic p.value
    cofee 4 10,508 2,627 5.2 0
    Residuals 50,734 25,564,819 504
    Total 50,738 25,575,327
  1. Student performance across discussion sections. A professor who teaches a large introductory statistics class (197 students) with eight discussion sections would like to test if student performance differs by discussion section, where each discussion section has a different teaching assistant. The summary table below shows the average final exam score for each discussion section as well as the standard deviation of scores and the number of students in each section.

    Sec 1 Sec 2 Sec 3 Sec 4 Sec 5 Sec 6 Sec 7 Sec 8
    Mean 92.94 91.11 91.80 92.45 89.30 88.30 90.12 93.35
    SD 4.21 5.58 3.43 5.92 9.32 7.27 6.93 4.57
    n 33.00 19.00 10.00 29.00 33.00 10.00 32.00 31.00

    The ANOVA output below can be used to test for differences between the average scores from the different discussion sections.

    df sumsq meansq statistic p.value
    section 7 525 75.0 1.87 0.077
    Residuals 189 7,584 40.1
    Total 196 8,109

    Conduct a hypothesis test to determine if these data provide convincing evidence that the average score varies across some (or all) groups. Check conditions and describe any assumptions you must make to proceed with the test.

  1. GPA and major. Undergraduate students in an introductory statistics course at Duke University conducted a survey about GPA and major. The density plots show the distributions of GPA among three groups of majors. ANOVA output is also provided.

    term df sumsq meansq statistic p.value
    major 2 0.03 0.02 0.21 0.81
    Residuals 195 15.77 0.08
    1. Write the hypotheses for testing for a difference between average GPA across majors.

    2. What is the conclusion of the hypothesis test?

    3. How many students answered the questions on the survey, i.e., what is the sample size?

  1. Work hours and education. The General Social Survey collects data on demographics, education, and work, among many other characteristics of US residents. (NORC 2010) Using ANOVA, we can consider educational attainment levels for all 1,172 respondents at once. Below are the distributions of hours worked by educational attainment and relevant summary statistics that will be helpful in carrying out this analysis.

    Educational attainment Mean SD n
    Lt High School 38.7 15.8 121
    High School 39.6 15.0 546
    Junior College 41.4 18.1 97
    Bachelor 42.5 13.6 253
    Graduate 40.8 15.5 155
    1. Write hypotheses for evaluating whether the average number of hours worked varies across the five groups.

    2. Check conditions and describe any assumptions you must make to proceed with the test.

    3. Below is the output associated with this test. What is the conclusion of the test?

    term df sumsq meansq statistic p.value
    degree 4 2,006 502 2.19 0.07
    Residuals 1,167 267,382 229
  1. True / False: ANOVA, I. Determine if the following statements are true or false in ANOVA, and explain your reasoning for statements you identify as false.

    1. As the number of groups increases, the modified discernibility level for pairwise tests increases as well.

    2. As the total sample size increases, the degrees of freedom for the residuals increases as well.

    3. The constant variance condition can be somewhat relaxed when the sample sizes are relatively consistent across groups.

    4. The independence assumption can be relaxed when the total sample size is large.

  1. True / False: ANOVA, II. Determine if the following statements are true or false, and explain your reasoning for statements you identify as false.

    If the null hypothesis that the means of four groups are all the same is rejected using ANOVA at a 5% discernibility level, then…

    1. we can then conclude that all the means are different from one another.

    2. the standardized variability between groups is higher than the standardized variability within groups.

    3. the pairwise analysis will identify at least one pair of means that are discernibly different.

    4. the appropriate \(\alpha\) to be used in pairwise comparisons is 0.05 / 4 = 0.0125 since there are four groups.

  1. Matching observed data with randomized F statistics. Consider the following two datasets. The response variable is the score and the explanatory variable is whether the individual is in one of four groups.

    The randomizations (randomly assigning group to the score, calculating a randomization F statistic) were done 1000 times for each of Dataset A and B. The red line on each plot indicates the observed F statistic for the original (unrandomized) data.

    1. Does the randomization distribution on the left correspond to Dataset A or B? Explain.

    2. Does the randomization distribution on the right correspond to Dataset A or B? Explain.

  1. Child care hours. The China Health and Nutrition Survey aims to examine the effects of the health, nutrition, and family planning policies and programs implemented by national and local governments. (UNC Carolina Population Center 2006) It, for example, collects information on number of hours Chinese parents spend taking care of their children under age 6. The side-by-side box plots below show the distribution of this variable by educational attainment of the parent. Also provided below is the ANOVA output for comparing average hours across educational attainment categories.

    term df sumsq meansq statistic p.value
    edu 4 4,142 1,036 1.26 0.28
    Residuals 794 653,048 822
    1. Write the hypotheses for testing for a difference between the average number of hours spent on child care across educational attainment levels.

    2. What is the conclusion of the hypothesis test?


  1. \(H_0:\) The average on-base percentage is equal across the four positions. \(H_A:\) The average on-base percentage varies across some (or all) groups.↩︎

  2. Let \(\bar{x}\) represent the mean of outcomes across all groups. Then the mean square between groups is computed as \(MSG = \frac{1}{df_{G}}SSG = \frac{1}{k-1}\sum_{j=1}^{k} n_{j} \left(\bar{x}_{j} - \bar{x}\right)^2\) where \(SSG\) is called the sum of squares between groups \((SSG)\) and \(n_{j}\) is the sample size of group \(j.\)↩︎

  3. See additional details on ANOVA calculations for interested readers. Let \(\bar{x}\) represent the mean of outcomes across all groups. Then the sum of squares total \((SST)\) is computed as \[SST = \sum_{i=1}^{n} \left(x_{i} - \bar{x}\right)^2\] where the sum is over all observations in the dataset. Then we compute the sum of squared errors \((SSE)\) in one of two equivalent ways: \(SSE = SST - SSG = (n_1-1)s_1^2 + (n_2-1)s_2^2 + \cdots + (n_k-1)s_k^2\) where \(s_j^2\) is the sample variance (square of the standard deviation) of the residuals in group \(j.\) Then the \(MSE\) is the standardized form of \(SSE: MSE = \frac{1}{df_{E}}SSE.\)↩︎

  4. There are \(k = 3\) groups, so \(df_{G} = k - 1 = 2.\) There are \(n = n_1 + n_2 + n_3 = 429\) total observations, so \(df_{E} = n - k = 426.\) Then the \(F\)-statistic is computed as the ratio of \(MSG\) and \(MSE:\) \(F = \frac{MSG}{MSE} = \frac{0.00803}{0.00158} = 5.082 \approx 5.077.\) \((F = 5.077\) was computed by using values for \(MSG\) and \(MSE\) that were not rounded.)↩︎

  5. The Cuckoo data used in this exercise can be found in the Stat2Data R package.↩︎

  6. The data Cuckoo used in this exercise can be found in the Stat2Data R package.↩︎

中文

22  比较多个均值的推断

第 20 分析的目的是比较两个不同组的总体均值。该分析的一个重要方面是考察样本均值之差,作为总体均值之差的估计。当比较两组以上的组时,差异(即相减)无法完全捕捉三个或更多组之间变异的细微差别。与两组的情形一样,研究问题将聚焦于组别归属是否与数值型响应变量相互独立。这里,组间的独立性意味着,了解某一组中的观测值不会改变我们对另一组结果的预期。但如果各组是 因变量呢?在本节中,我们关注一个新的统计量,它综合了两组以上均值之间的差异。虽然本章的思想与t检验非常相似,但它们有了自己的名称: ANalysis Of VAriance,即方差分析(ANOVA)。

有时我们想比较多组之间的均值。我们最初可能想到做两两比较。例如,如果有三组,我们可能会想把第一组的均值与第二组比较,再与第三组比较,最后再比较第二组和第三组的均值,总共进行三次比较。然而,这种策略可能暗藏风险。如果我们有很多组并进行很多次比较,即使总体之间没有差异,也很可能仅凭偶然就会最终发现某个差异。相反,我们应该应用一种整体性检验来检查是否有证据表明至少有一对组确实存在差异,这正是 ANOVA 大显身手的地方。

在本节中,我们将学习一种称为 方差分析(ANOVA) 的新方法,以及一种称为 \(F\)-统计量(我们将在讨论数学模型时介绍)的新检验统计量。ANOVA 使用单一假设检验来检查许多组的均值是否相等:

  • \(H_0:\) 所有组的结果均值相同,即, \(\mu_1 = \mu_2 = \cdots = \mu_k\) ,其中 \(\mu_j\) 表示类别 \(j.\)
  • \(H_A:\) 中观测值结果的均值。

至少有一个均值不同。

  • 通常,在执行 ANOVA 之前,我们必须对数据检查三个条件:
  • 各组内部以及各组之间的观测值相互独立,
  • 每组内的响应近似正态,并且

各组之间的变异性大致相等。 \(\mu_j\) 当满足这三个技术条件时,我们可以执行 ANOVA 来确定数据是否提供了令人信服的证据,以反对所有

相等的原假设。


由于需求量大,大学的院系通常每学期为同一门入门课程开设多个班次。假设某统计系为一门入门统计学课程开设了三个班次。我们可能想确定这三个班级(A 班、B 班和 C 班)的第一次考试成绩是否存在实质性差异。请描述适当的假设,以确定这三个班级之间是否存在任何差异。

  • \(H_0:\) 假设可以写成如下形式: 所有班级的平均分相同, \(\mu_A = \mu_B = \mu_C\). 假设每个班级难度相同,那么观察到的考试成绩差异是由偶然因素造成的。
  • \(H_A:\) 平均分因班级而异。如果班级平均分之间的差异大于仅凭偶然因素所能预期的差异,我们将拒绝原假设而支持备择假设。

在方差分析(ANOVA)中,支持备择假设的有力证据表现为组均值之间存在异常大的差异。我们很快会了解到,评估组均值的变异性相对于每组内个体观测值之间的变异性,是方差分析成功的关键。

考察 图 22.1. 比较第 I、II、III 组。你能通过视觉判断,如果各组之间没有差异,这些组中心的差异是否不太可能出现?现在比较第 IV、V、VI 组。如果各组之间没有差异,这些差异是否看起来不太可能出现?


第 I、II、III 组均值之间的任何真实差异都很难辨别,因为相对于平均结果的任何差异,每组内的数据波动非常大。另一方面,第 IV、V、VI 组的中心似乎存在差异。例如,第 V 组的均值似乎高于其他两组。考察第 IV、V、VI 组,我们可以看到这些组中心之间的差异是明显的,因为这些差异相对于 每组内个体观测值的变异性来说很大.

Two sets of side by side dot plots. The first set shows three groups of observations where the variability within a group is so large that it swamps out any variability across the groups. The second set shows three groups of observations where the variability within a group is much smaller and the center of the groups appears different.
图 22.1:六个组结果的并列点图。包含两组;第一组由第 I、II、III 组组成,第二组由第 IV、V、VI 组组成。

22.1 案例研究:击球

我们想弄清楚棒球运动员的击球表现是否因其在场上的位置不同而存在真实差异:外场手(OF)、内场手(IF)和捕手(C)。我们将使用一个名为 mlb_players_18的数据集,其中包含 2018 赛季至少有 100 次打击的 429 名美国职业棒球大联盟(MLB)球员的击球记录。 mlb_players_18 中所代表的 429 个案例中的六个案例显示在 表 22.1中,每个变量的描述见 表 22.2。我们将用于衡量球员击球表现(结果变量)的指标是上垒率(OBP)。上垒率大致表示球员成功上垒或击出本垒打的时间比例。

mlb_players_18 数据可以在 openintro R 包中找到。

表 22.1: mlb_players_18 数据框中的六个案例及部分变量。
name team position AB H HR RBI AVG OBP
Abreu, J CWS 内场手 499 132 22 78 0.265 0.325
Acuna Jr., R ATL 外场手 433 127 26 64 0.293 0.366
Adames, W TB 内场手 288 80 10 34 0.278 0.348
Adams, M STL 内场手 306 73 21 57 0.239 0.309
Adduci, J DET 内场手 176 47 3 21 0.267 0.290
Adrianza, E MIN 内场手 335 84 6 39 0.251 0.301
表 22.2: 变量及其描述,用于 mlb_players_18 数据集的最后五行。
变量 描述
name 球员姓名
team 球员所属球队的缩写名称
position 球员的主要场上位置(OF、IF、C)
AB 打击机会数(打数)
H 安打数
HR 本垒打数
RBI 打点数
AVG 打击率,等于 H/AB
OBP 上垒率,大致等于球员上垒或击出本垒打的次数比例

所考虑的原假设如下: \(\mu_{OF} = \mu_{IF} = \mu_{C} % = \mu_{DH}.\) 用通俗语言写出原假设和对应的备择假设。1

球员位置被分为三组:外场手(OF)、内场手(IF)和捕手(C)。外场手上垒率的合适点估计是什么, \(\mu_{OF}\)?


外场手上垒率的一个好的估计值是 OBP 仅针对位置为外场手的球员: \(\bar{x}_{OF} = 0.320.\)

22.2 用于比较多组均值的随机化检验

表 22.3 提供了每组的汇总统计量。上垒率的并排箱线图显示在 图 22.2。注意各组之间的变异性看起来大致恒定;各组之间方差近乎恒定是一个重要假设,在考虑使用方差分析(ANOVA)方法之前必须满足该假设。

表 22.3:按球员位置划分的上垒率汇总统计量。
位置 n 平均数 标准差
外场手 160 0.320 0.043
内场手 205 0.318 0.038
C 64 0.302 0.038
Side-by-side box plots describing on-base percentage broken down by outfield, infield, or catcher. The catchers seems to have a slightly lower on-base percentage, but the vast majority of players have an on-base percentage 0.27 and 0.35.
图 22.2:429 名球员在三个组别中上垒率的并排箱线图。有几个潜在异常值,但由于每组观测数量较多,这些异常值并不极端到会影响计算,因此不必担心,可以继续进行分析。

样本均值之间最大的差异出现在捕手和外场手位置之间。再次考虑原始假设:

  • \(H_0:\) \(\mu_{OF} = \mu_{IF} = \mu_{C}\)
  • \(H_A:\) 平均上垒率 \((\mu_j)\) 在部分(或全部)组别之间存在差异。

为什么仅仅通过估计 \(\mu_{C}\)\(\mu_{OF}\) 的差异在 0.05 可辨识水平上是否“统计上可辨识”来进行检验可能是不恰当的?


这里的主要问题在于,我们在选择要比较的组别之前就已经检查了数据。先用肉眼检查所有数据(非正式检验),然后再决定对哪些部分进行正式检验,这种做法是不恰当的。这被称为 数据窥探数据捕捞。很自然地,我们会挑选差异较大的组进行正式检验,这会导致第一类错误率膨胀。为了更好地理解膨胀的第一类错误率,让我们考虑一个略有不同的问题。

假设我们要在一所大型小学的20个班级中,在学年初测量学生的能力倾向。在这所学校里,所有学生都被随机分配到各个班级,因此我们在学年初观察到的班级之间的任何差异完全是由偶然因素造成的。然而,由于组数众多,我们很可能会观察到少数几个看起来彼此差异相当大的组。如果我们只挑选那些看起来不同的班级并进行正式检验,我们很可能会得出分配并非随机的错误结论。虽然我们可能只对少数几对班级进行正式检验,但我们在选择最极端的案例进行比较之前,已经用肉眼对其他班级进行了非正式的评估。

22.2.1 观测数据

在下一节中,我们将学习如何使用 \(F\) 统计量来检验样本均值之间观察到的差异是否可能仅仅由偶然因素造成,即使各总体均值之间实际上没有差异。

在这种情形下,方差分析方法着重于回答一个问题:样本均值之间的变异性是否大到看起来不太可能仅由偶然因素造成?这个问题与之前的检验程序不同,因为我们将 同时 考虑多个组,并评估它们的样本均值之间的差异是否超出自然变异的预期。我们将这种变异性称为 组间均方(MSG) \((MSG)\),当有 \(df_{G} = k - 1\) 个组时,它具有相应的自由度 \(k\)\(MSG\) 可以被看作是均值的经过缩放的方差公式。如果原假设为真,样本均值的任何变异都是由偶然因素造成的,不应太大。 \(MSG\) 计算的详细内容见脚注。2 然而,我们通常使用软件来进行这些计算。

组间均方本身在假设检验中几乎没有用处。我们需要一个基准值,用于衡量当原假设为真时样本均值之间应有多大的变异性。为此,我们计算合并方差估计,通常缩写为 均方误差 \((MSE)\),其对应的自由度值为 \(df_E = n - k.\)\(MSE\) 理解为组内变异性的度量是很有帮助的。关于 \(MSE\) 计算的细节以及方差分析(ANOVA)计算的额外在线章节的链接见脚注。3

当原假设为真时,样本均值之间的任何差异都只是偶然造成的,此时 \(MSG\)\(MSE\) 应当大致相等。作为方差分析的检验统计量,我们考察 \(MSG\)\(MSE:\)

\[F = \frac{MSG}{MSE}\]

\(MSG\)\(MSE\) 的比值。其中

是组间变异性的度量,而

度量每组内部的变异性。

\[F = \frac{MSG}{MSE}\]

检验三个或更多均值时的检验统计量是 F。 \(df_1 = k-1\)\(df_2 = n-k.\)

F 统计量是各组之间差异程度(MSG)与组内观测值变异程度(MSE)的比值。

  • 当原假设为真且条件满足时,F 服从自由度为 ',' 的 F 分布。条件:观测值相互独立,包括组内和组间
  • 大样本且无极端离群值

22.2.2 统计量的变异性

我们回顾一下 Section 20.1 中演示的用于均值比较的双样本随机化检验。假设现在这位老师的班级极其庞大,使用了三份不同的试卷:A、B 和 C。 表 22.4图 22.3 提供了包含试卷 C 在内的数据汇总。同样,我们想探究三份试卷的难度是否相同,因此检验为

  • \(H_0: \mu_A = \mu_B = \mu_C.\) 三份试卷的内在平均难度相同。
  • \(H_A:\)\(H_0.\) 至少有一份试卷的内在难度高于(或低于)其他试卷。

classdata 数据可以在 openintro R 包中找到。

表 22.4:各试卷版本成绩的汇总统计量。
试卷 n 平均数 标准差 最小值 最大值
A 58 75.1 13.9 44 100
B 55 72.0 13.8 38 100
C 51 78.9 13.1 45 100
Side-by-side box plots of exam score broken down by exam A, exam B, or exam C. Exam C's median is above 80 which is higher than exam A with a median around 74 and exam B with a median around 72.
图 22.3:使用三份不同试卷之一的学生成绩。

图 22.4 展示了将三份不同试卷随机分配到观测到的考试成绩的过程。如果原假设为真,那么每份试卷的成绩应代表学生在该材料上的真实能力。无论他们拿到的是试卷 A、试卷 B 还是试卷 C,都不应有影响。通过重新分配哪位学生拿到哪份试卷,我们能够理解平均考试成绩的差异仅由自然变异引起的变化。 图 22.4中只有一次随机化过程的迭代,得到三个不同的随机化样本均值(在假设原假设为真的情况下计算)。

Four panels representing four different orientations of a toy dataset of 13 exam scores. The first panel provides the observed data; 4 of the exams were version A and the average score was 77.25; 5 of the exams were version B and the average score was 75.8; 4 of the exams were version C and the average score was 78.5. The observed F statistic is 0.0747. The second panel shows the shuffled reassignment of the exam versions (4 of the scores are randomly reassigned to A, 5 of the scores are randomly reassigned to B, 4 of the scores are randomly reassigned to C). The third panel shows which score is connected with which new reassigned version of the exam. And the fourth panel sorts the exams so that version A exams are together, version B exams are together, and version C exams are together. In the randomly reassigned versions, the average score for version A is 72.25, the average score for version B is 78, and the average score for version C is 75.25. The randomized F statistic is 0.1637.
图 22.4:在试卷难度相同的原假设下,将试卷版本(A 或 B 或 C)随机分配到考试成绩。

在双样本情形中,原假设是通过样本均值之差来检验的。然而,如上所述,对于三组(三份不同的试卷),三个样本均值的比较会稍微复杂一些。我们已经推导出了 F 统计量,它正是比较三组或更多组平均值的方法!回顾一下,F 统计量是组间差异程度(MSG)与组内观测变异程度(MSE)之比。

基于 图 22.4, 图 22.5 展示了 1,000 次随机模拟中 \(F\) 统计量的取值。我们看到,仅凭偶然机会,F 统计量就可以大到 7。

Histogram of the F statistics that were calculated from 1000 different randomizations of the exam type. The distribution is right-skewed with most of the values less than 2. The tail extends to about 6.
图 22.5:由考试类型的 1,000 次不同随机化计算得到的 F 统计量的直方图。

22.2.3 观测统计量与零假设统计量的对比

Histogram of the F statistics that were calculated from 1000 different randomizations of the exam type. The distribution is right-skewed with most of the values less than 2. The tail extends to about 6. The observed F statistic is given as a red vertical line at 3.48. The area to the right of 3.48 is more extreme than the observed value and represents the p-value.
图 22.6:由考试类型的 1000 次不同随机化计算得到的 F 统计量的直方图。观测到的 F 统计量以位于 3.48 处的红色竖线标出。右侧的面积比观测值更极端,表示 p 值。

使用统计软件,我们可以计算出 3.6% 的随机化 F 检验统计量达到或超过了观测到的检验统计量 \(F= 3.48.\) 也就是说,该检验的 p 值为 0.036。假设我们已将可辨别性水平设为 \(\alpha = 0.05,\) p 值小于可辨别性水平,这将使我们拒绝原假设。我们声称,至少有一场考试的难度水平(即真实平均分数, \(\mu)\) 是不同的。

虽然很想说考试 C 比另外两场更难(鉴于在 Section 20.1中无法区分考试 A 和考试 B),但我们必须非常小心,避免对同一数据使用不同技术得出结论。

当原假设为真时,自然界中存在的随机变异性有时会产生 p 值小于 0.05 的数据。这种情况发生的频率是多少?5% 的时间。也就是说,如果你对没有信号(即原假设为真)的同一数据应用 20 个不同的模型,你很有可能在运行的某个检验中得到小于 0.05 的 p 值。围绕这个问题的细节,称为 多重比较检验多重比较问题,超出了本教科书的范围,但你应该将其牢记在心。为了最好地减轻额外的第一类错误,我们建议你在运行任何分析之前先建立假设和检验方案。一旦得出结论,你应该报告你的发现,而不是对同一数据运行不同类型的检验。

22.3 比较多个均值的检验的数学模型

正如前面章节中的许多检验和统计量一样,基于 F 统计量的随机化检验有相应的数学理论来描述其分布,从而可以不使用计算方法。

我们回到 表 22.3 的棒球例子,以演示应用于 ANOVA 情形的数学模型。

22.3.1 统计量的变异性

样本均值 \((MSG)\) 的观测变异相对于组内观测值 \((MSE)\)的变异越大, \(F\)统计量就越大,反对原假设的证据也越强。由于较大的 \(F\)统计量代表反对原假设的更强证据,我们使用分布的上尾来计算 p 值。

F 统计量与 F 检验。

方差分析(ANOVA)用于检验结果均值在两个或更多组之间是否存在差异。ANOVA 使用一个检验统计量,即 \(F\)统计量,它表示样本均值的变异相对于组内变异的标准化比值。如果 \(H_0\) 为真且模型条件得到满足,则 \(F\)统计量服从参数为 \(F\)\(df_{1} = k - 1\)\(df_{2} = n - k.\) 分布。上尾的 \(F\) 分布用于表示 p 值。

对于棒球数据, \(MSG = 0.00803\)\(MSE=0.00158\)。请确定与 MSG 和 MSE 相关的自由度,并验证 \(F\)统计量约为 5.077。4

22.3.2 观测统计量与零假设统计量的对比

我们可以使用 \(F\)统计量来检验假设,这称为 F 检验。p 值可以通过使用 \(F\) 分布从 \(F\) 统计量计算得出,该分布有两个相关参数: \(df_{1}\)\(df_{2}.\) 对于 ANOVA 中的 \(F\)统计量, \(df_{1} = df_{G}\)\(df_{2} = df_{E}.\) 自由度为 2 和 426 的 \(F\) 分布,对应于棒球假设检验的 \(F\) 统计量,如 图 22.7.

A line plot showing the theoretical F distribution with the first degree of freedom equal to 2 and the second degree of freedom equal to 426. The curve is right-skewed with most values less than 4. The tail extends to approximately 8. Area to the right of 5.077 has been shaded in red.
所示。 图 22.7:自由度为 \(F\)\(df_1=2\)\(df_2=426.\)

分布, 与 图 22.7 约等于 0.0066。这是否提供了反对零假设的有力证据?


p 值小于 0.05,表明证据足够强,可以在 0.05 的可辨别性水平上拒绝零假设。也就是说,数据提供了有力证据,表明平均上垒率因球员的主要守备位置而异。

注意,较小的 p 值表明不同位置的平均击球率之间存在显著差异。然而,方差分析(ANOVA)检验并未提供一种机制来知道 哪个 位置组是造成差异的原因。如果我们继续进行所有可能的两两均值比较,就会面临较高的第一类错误率的风险。正如我们在 第 22.2 节末尾所看到的,围绕单个组比较的后续问题被称为 多重比较 问题,超出了本书的范围。然而,我们鼓励你进一步学习多重比较的相关知识,以便在方差分析检验中拒绝零假设之后进行额外的比较时,不会导致过多的假阳性结论。

22.3.3 从软件读取方差分析表

手工进行方差分析所需的计算繁琐且容易出错。因此,通常使用统计软件来计算 \(F\)统计量和 p 值。

方差分析可以总结在一个与回归摘要非常相似的表格中,我们在 第 7 章第 8 章. 表 22.5 中已经见过。该表展示了一个用于检验 MLB 中球员平均上垒率是否因位置而异的方差分析摘要。其中许多数值应该看起来很熟悉;特别是, \(F\)统计量和 p 值可以从最后两列中获取。

表 22.5:用于检验平均上垒率是否因球员位置而异的方差分析摘要。
term df sumsq meansq 统计量 std.error
position 2 0.0161 0.0080 5.08 0.0066
残差 426 0.6740 0.0016

22.3.4 ANOVA 分析的条件

进行 ANOVA 分析时必须检查三个条件:所有观测值必须相互独立,每组数据必须近似正态(或每组样本量足够大),并且每组内部的方差必须大致相等。

  • 独立性。 如果数据是简单随机样本,则可以假定该条件已满足。对于过程和实验,需仔细考虑数据是否可能独立(例如,不存在配对)。例如,在 MLB 数据中,数据并非抽样所得。然而,没有明显的理由表明独立性对大部分或全部观测值不成立。

  • 近似正态。 与单样本和双样本均值检验一样,当样本量很小时,正态性假设尤为重要,而讽刺的是,此时恰恰难以检验非正态性。每组观测值的直方图如 图 22.8所示。由于我们所考虑的每组样本量都相对较大,我们要寻找的是显著的离群值。没有发现明显的离群值,因此该条件基本满足。

  • 方差恒定。 最后一个假设是各组之间的方差大致相等。该假设可以通过考察各组结果的并排箱线图来检验,如 图 22.2所示。在本例中,四组的变异性相似但不完全相同。从 表 22.3 中可以看到,标准差在各组之间变化不大。

方差分析的诊断方法。

独立性对方差分析始终很重要。当每组的样本量相对较小时,正态性条件很重要(如果各组样本量较大,观测值不需要服从正态分布,但也不应包含较大的离群值)。当各组样本量不同时,等方差条件尤为重要。

Three separate histograms of on-base percentage, one for each field position of outfielder, infielder, and catcher. All three histograms are reasonably bell-shaped and symmetric.
图 22.8:各场上位置球员的 OBP 直方图。

22.4 本章复习

22.4.1 小结

在本章中,我们同时提供了随机化检验和适用于解决两组或多组之间均值相等性问题的数学模型。请注意,确认 F 分布能恰当地对 ANOVA 检验统计量进行建模需要满足一些重要的技术条件。此外,你可能已经注意到,我们没有讨论置信区间的构建。这是因为 ANOVA 统计量没有可直接对应的待估参数。如果对(每两组之间的)均值差异比较感兴趣,则应采用 第 20 章 中比较两个独立均值的方法。

22.4.2 术语

本章中介绍的术语列于 表 22.6。如果您不确定其中一些术语的含义,我们建议您回到正文中复习它们的定义。您应该能够很容易地发现它们是 粗体文本.

表 22.6:本章介绍的术语。
方差分析 自由度 多重比较
ANOVA F 检验 误差平方和(SSE)
数据捕捞 组间均方(MSG) 组间平方和(SSG)
数据窥探 均方误差(MSE) 总平方和(SST)

22.5 练习

奇数编号习题的答案见 附录 A.22.

  1. 填空题。 在做方差分析(ANOVA)时,你观察到各组均值之间存在较大差异。在方差分析的框架下,这最有可能被解释为强烈支持 _____________ 假设的证据。
  1. 哪种检验? 我们想检验社会科学、自然科学、艺术与人文以及其他专业的学生平均而言花在学习一门课程上的时间是否相同。我们应该使用哪种类型的检验?请解释你的理由。
  1. 杜鹃鸟蛋的长度,随机化一次。 杜鹃鸟把蛋产在其他鸟的巢中,因此被称为巢寄生鸟。一个问题涉及杜鹃蛋的大小是否因宿主鸟的物种而异。5 (1902年下半年) 考虑以下图形,第一个表示原始数据,第二个表示宿主物种被随机分配给蛋长度的数据。

    1. 考虑每个物种蛋的平均长度。原始数据的平均长度与随机分配物种的数据相比:是更分散、更不分散,还是大致相同?请描述你在图形中看到的情况。

    2. 考虑每个物种内部蛋长度的标准差。原始数据中物种内部蛋长度的标准差与随机分配物种的数据相比:是更大、更小,还是大致相同?

    3. 回想一下,F 统计量的分子衡量组间差异程度(MSG),分母衡量物种内部数值的差异程度(MSE),上面哪个图形会有更大的 F 统计量,是原始数据还是随机化后的数据?请解释。

  1. 杜鹃鸟蛋的长度,随机化检验。 杜鹃鸟把蛋产在其他鸟的巢中,因此被称为巢寄生鸟。一个问题涉及杜鹃蛋的大小是否因宿主鸟的物种而异。6 (1902年下半年) 使用F统计量的随机化分布(宿主物种随机分配到蛋长度),进行假设检验以评估不同宿主鸟类的平均蛋长度在总体上是否存在差异。请确保清楚地陈述你的假设,并结合数据背景解释你的结果。

  1. 鸡的饮食与体重,多组。 进行了一项实验,以测量和比较各种饲料补充剂对鸡生长速度的效果。新孵出的雏鸡被随机分配到六个组中,每组给予不同的饲料补充剂。样本统计量和观测数据的可视化如下所示。 (McNeil 1977)

    饲料类型 平均数 标准差 n
    酪蛋白 323.58 64.43 12
    豆科牧草 160.20 38.63 10
    亚麻籽 218.75 52.24 12
    肉粉 276.91 64.90 11
    大豆 246.43 54.13 14
    葵花籽 328.92 48.84 12

    使用下面的方差分析(ANOVA)输出,进行假设检验以确定这些数据是否提供了令人信服的证据,表明雏鸡的平均体重在某些(或所有)组之间存在差异。请务必检查相关条件。

    term df sumsq meansq 统计量 std.error
    feed 5 231,129 46,226 15.4 <0.0001
    残差 65 195,556 3,009
  1. 描述性统计教学。 一项研究比较了五种不同的描述性统计教学方法。这五种方法分别是:传统讲授与讨论、程序化教科书教学、程序化教科书结合讲授、计算机教学,以及计算机教学结合讲授。45名学生被随机分配,每种方法9人。完成课程后,学生参加了一场1小时的考试。

    1. 用于评估不同教学方法下平均考试成绩是否存在差异的假设是什么?

    2. 与评估这些假设的 \(F\)-检验相关的自由度是多少?

    3. 假设该检验的 p 值为 0.0168。结论是什么?

  1. 咖啡、抑郁与身体活动。 咖啡因是世界上使用最广泛的兴奋剂,大约80%以咖啡的形式摄入。一项研究咖啡消费与锻炼之间关系的调查中,参与者被要求报告他们每周花在中等强度(如快走)和高强度(如剧烈运动和慢跑)锻炼上的小时数。基于这些数据,研究人员估计了每周代谢当量任务(MET)的总小时数,该值始终大于0。下表给出了该研究中按咖啡消费量划分的女性 MET 汇总统计。 (Lucas et al. 2011)

    含咖啡因咖啡的消费量
    每周1杯或更少 每周2-6杯 每天1杯 每天2-3杯 每天4杯或更多
    平均数 18.7 19.6 19.3 18.9 17.5
    标准差 21.1 25.5 22.5 22.0 22.0
    n 12,215.0 6,617.0 17,234.0 12,290.0 2,383.0
    1. 写出用于检验平均身体活动水平是否因咖啡饮用量的不同水平而有所差异的假设。

    2. 检查条件并描述为继续进行该检验你必须做出的任何假设。

    3. 以下是该检验的输出结果。该检验的结论是什么?

    df sumsq meansq 统计量 std.error
    cofee 4 10,508 2,627 5.2 0
    残差 50,734 25,564,819 504
    总计 50,738 25,575,327
  1. 不同讨论课班级的学生表现。 一位教授讲授一门大型统计学导论课(197名学生),共有八个讨论课班级,他想检验学生的表现是否因讨论课班级而异,其中每个讨论课班级由不同的助教负责。下表汇总了每个讨论课班级的期末考试平均分,以及分数的标准差和每个班级的学生人数。

    第1班 第2班 第3班 第4班 第5班 第6节 第7节 第8节
    平均数 92.94 91.11 91.80 92.45 89.30 88.30 90.12 93.35
    标准差 4.21 5.58 3.43 5.92 9.32 7.27 6.93 4.57
    n 33.00 19.00 10.00 29.00 33.00 10.00 32.00 31.00

    下面的方差分析(ANOVA)输出可用于检验不同讨论课小组的平均分数之间是否存在差异。

    df sumsq meansq 统计量 std.error
    section 7 525 75.0 1.87 0.077
    残差 189 7,584 40.1
    总计 196 8,109

    进行假设检验,以判断这些数据是否提供了令人信服的证据,表明平均分数在某些(或所有)组之间存在差异。检查条件,并说明为进行该检验你必须做出的任何假设。

  1. GPA与专业。 杜克大学一门统计学导论课程的本科生进行了一项关于GPA与专业的调查。密度图显示了三组专业的GPA分布情况。同时还提供了方差分析(ANOVA)输出。

    term df sumsq meansq 统计量 std.error
    major 2 0.03 0.02 0.21 0.81
    残差 195 15.77 0.08
    1. 写出检验不同专业平均GPA是否存在差异的假设。

    2. 假设检验的结论是什么?

    3. 有多少学生回答了调查中的问题,即样本量是多少?

  1. 工作时长与教育程度。 综合社会调查(General Social Survey)收集美国居民的人口统计、教育和职业等诸多特征的数据。 (NORC 2010) 使用方差分析(ANOVA),我们可以同时考虑全部1,172名受访者的受教育水平。下面是按教育程度划分的工作时长分布以及有助于进行该分析的相关汇总统计量。

    受教育程度 平均数 标准差 n
    高中以下 38.7 15.8 121
    高中 39.6 15.0 546
    大专 41.4 18.1 97
    本科 42.5 13.6 253
    研究生 40.8 15.5 155
    1. 写出用于评估平均工作时长是否在五个组之间存在差异的假设。

    2. 检查条件并描述为继续进行该检验你必须做出的任何假设。

    3. 以下是该检验的输出结果。该检验的结论是什么?

    term df sumsq meansq 统计量 std.error
    degree 4 2,006 502 2.19 0.07
    残差 1,167 267,382 229
  1. 判断对错:方差分析,I。 判断以下关于方差分析的陈述是对还是错,并对你认为是错误的陈述解释理由。

    1. 随着组数的增加,两两检验的修正显著性水平也会增加。

    2. 随着总样本量的增加,残差的自由度也会增加。

    3. 当各组样本量相对一致时,可以适当放宽常数方差条件。

    4. 当总样本量很大时,可以放宽独立性假设。

  1. 判断对错:方差分析,II。 判断以下陈述是对还是错,并对你认为是错误的陈述解释理由。

    如果在5%显著性水平下使用方差分析拒绝了四个组的均值都相同的原假设,那么……

    1. 我们就可以得出结论:所有均值彼此都不相同。

    2. 组间的标准化变异高于组内的标准化变异。

    3. 两两比较分析将识别出至少一对可辨别不同的均值。

    4. 两两比较中使用的合适 \(\alpha\) 为 0.05 / 4 = 0.0125,因为共有四个组。

  1. 将观测数据与随机化的 F 统计量进行匹配。 考虑以下两个数据集。响应变量是 score ,解释变量是个体是否属于四个组之一。

    对数据集 A 和 B 各进行了 1000 次随机化(将组别随机分配给分数,并计算随机化 F 统计量)。每个图中的红线表示原始(未随机化)数据的观测 F 统计量。

    1. 左边的随机化分布对应数据集 A 还是 B?请解释。

    2. 右边的随机化分布对应数据集 A 还是 B?请解释。

  1. 照看儿童的时间。 中国健康与营养调查旨在检验国家和地方政府实施的健康、营养和计划生育政策与项目的效果。 (北卡罗来纳大学卡罗来纳人口中心 2006 年) 例如,该调查收集了中国父母照看 6 岁以下儿童所花费的小时数。下面的并排箱线图按父母的教育程度显示了该变量的分布。下面还提供了按教育程度类别比较平均小时数的方差分析(ANOVA)输出结果。

    term df sumsq meansq 统计量 std.error
    edu 4 4,142 1,036 1.26 0.28
    残差 794 653,048 822
    1. 写出检验不同教育水平之间照看孩子的平均小时数是否存在差异的假设。

    2. 假设检验的结论是什么?


  1. \(H_0:\) 四个位置的平均上垒率相等。 \(H_A:\) 平均上垒率在某些(或所有)组之间存在差异。↩︎

  2. \(\bar{x}\) 表示所有组结果的总均值。然后组间均方计算为 \(MSG = \frac{1}{df_{G}}SSG = \frac{1}{k-1}\sum_{j=1}^{k} n_{j} \left(\bar{x}_{j} - \bar{x}\right)^2\) ,其中 \(SSG\) 被称为 组间平方和 \((SSG)\)\(n_{j}\) 是第 \(j.\)↩︎

  3. 组的样本量 参见 ANOVA计算的更多细节 \(\bar{x}\) ,供有兴趣的读者参考。设 表示所有组结果的总均值。然后 \((SST)\) 总平方和 \[SST = \sum_{i=1}^{n} \left(x_{i} - \bar{x}\right)^2\] 计算为 误差平方和 \((SSE)\) 以两种等价方式之一: \(SSE = SST - SSG = (n_1-1)s_1^2 + (n_2-1)s_2^2 + \cdots + (n_k-1)s_k^2\) ,其中 \(s_j^2\) 是组 \(j.\) 然后, \(MSE\)\(SSE: MSE = \frac{1}{df_{E}}SSE.\)↩︎

  4. 的标准化形式。共有 \(k = 3\) 个组,所以 \(df_{G} = k - 1 = 2.\) 共有 \(n = n_1 + n_2 + n_3 = 429\) 个观测值,所以 \(df_{E} = n - k = 426.\) 然后, \(F\)-统计量按以下比值计算: \(MSG\)\(MSE:\) \(F = \frac{MSG}{MSE} = \frac{0.00803}{0.00158} = 5.082 \approx 5.077.\) \((F = 5.077\) 的计算使用了未四舍五入的 \(MSG\)\(MSE\) 值。)↩︎

  5. Cuckoo 本练习中使用的数据可在 Stat2Data R 包中找到。↩︎

  6. 本练习中使用的数据 Cuckoo 可在 Stat2Data R 包中找到。↩︎