- Overview
- Anova Basics
- Exploratory Data Analysis
- Calculating ANOVA
2024-01-15
Purpose: This presentation will serve as an introduction to the Analysis of Variables model (ANOVA) as statistical method and how calculate and interpret an ANOVA using R. We will explore the process of calculating an ANOVA through a made-up example scenario with a made-up example data-set.
Scope: This presentation will cover the basics of ANOVA and ANOVA in R. For this presentation we will be using the simpler one-way ANOVA.
Prerequisite fundamental statistical knowledge including hypothesis testing is required. Basic R programming skills is also required.
ANOVA, short for ANanlysis Of VAriables, is a general linear model method for analyzing the means of two or more categorical groups and determining whether or not the difference between the means is statistically significant.
Specifically, the ANOVA model puts the square sum between groups (the number of data points times the square of the differences between the group means and the overall mean) in proportion with the square sum within groups (the sum of the squared differences of each data point and their respective group mean) resulting in an F-Value. This F-Value, sometimes called F-Statistic or F-Ratio, has its own distribution (the F distribution), which can then be used to find a P-value that will determine if there exists enough evidence to reject the null hypothesis or, say that there is a statistically significant difference between the means.
Anova models are typically used when we want to compare the means of at least three separate and independent groups.
Any ANOVA calculation assumes several things about the data for the model to be valid.
It is also strongly recommended but technically not required that each group has the same sample size.
Anova can tell us if there is a statistically significant different between the means of groups.
Anova can not tell us the specific groups for which the differences between the means are significant. To determine this, additional testing is needed.
The Anova model, like the regression model, follows the general linear model. Which means that ultimately any outcome or observation can be thought of as the sum of the model’s prediction (explained variance) and the Error (unexplained variance).
In ANOVA this equation is as follows:
Square Sum Total = Square Sum between Groups + Square Sum within groups
\(SS(T) = SS(B) + SS(W)\)
\(\sum(x_{i,j} - G)^2 = \sum n(M_i - G) + \sum(x_{i,j}-M_j)\)
Essentially, The sum of the squares of the differences between each data point and the overall mean (\(\sum(x_{i,j} - G)^2\)) is equal to the sum of \(n\) times the squared differences between each groups mean and the overall mean (\(\sum n(M_j - G)\)) plus the sum of the squared differences between each data and its group mean \(\sum(x_{i,j}-M_j)\).
As mentioned previously, ANOVA produces an F-Value which can be used to find a P-value to determine whether or not there is enough evidence to say there is a statistically significant difference between the means of the groups.
The F-Value is defined as the Mean Squares Between Groups divided by the Mean Squares within Groups or
\(n\) times the sum of the squares of the differences between the group means and the overall mean divided by its degrees of freedom (k-1) divided the sum of the squares of the differences between each value and their respective groups mean divided by its degrees of freedom (n-k)
\(F = \dfrac{\dfrac{\sum n(M_j-G)^2}{(k-1)}}{\dfrac{\sum (x_{i,j}-M_j)^2}{(n-k)}}\)
\(F = \dfrac{\dfrac{\sum n(M_j-G)^2}{(k-1)}}{\dfrac{\sum (x_{i,j}-M_j)^2}{(n-k)}}\)
Where:
\(n\) is the number of data points in each group
\(x_{i,j}\) is the \(i\)th value in the \(j\)th group
\(M_j\) is the sample mean of the \(j\)th group
\(G\) is the grand mean (the mean of the sample means)
\(k\) is the number of groups
When we the ANOVA model is calculated, it is common for a table with these values to be constructed called an ANOVA table. An Anova table is an efficient way to view all the models statistics. Example of an ANOVA table:
| Variation | Value | Degrees of Freedom |
Mean Squares |
F-Value |
|---|---|---|---|---|
| Total Sum of Squares |
\(\sum(x_{i,j} - G)^2\) | \(n-1\) | - | \(F = \dfrac{\dfrac{\sum n(M_j-G)^2}{(k-1)}}{\dfrac{\sum (x_{i,j}-M_j)^2}{(n-k)}}\) |
| Sum of Squares between Groups |
\(\sum n(M_i - G)^2\) | \(k-1\) | \(\dfrac{\sum n(M_j - G)^2}{(k-1)}\) | - |
| Sum of Squares within Groups |
\(\sum(x_{i,j}-M_i)^2\) | \(n-k\) | \(\dfrac{\sum (x_{i,j}-M_j)^2}{(n-k)}\) | - |
We will be using a made up example data set to calculate an ANOVA in R. This data set contains information on the fuel economies of 30 randomly selected motorcycle models from the past 5 years grouped by the manufacturing company’s country of origin. A random sample of 10 models were taken from 3 countries (USA, Japan, and Italy) for a total of 30 observations. We will be investigating if there is a statistically significant difference in average fuel economy (MPG) between motorcycles made in different countries with 95% confidence (\(\alpha = .05\)).
The ANOVA will ultimately tell us if there is enough evidence to reject the null hypothesis and support the claim that there is a significant difference in average MPG.
\(H_0\) -> No difference between group means
\(H_a\) -> At least one group differs significantly from the overall mean
The step step in working with any data is to explore it and get a good sense of what you are working with. Lets import the .csv data file and view its dimensions, variables, and first few rows.
#importing dataset as tibble
motorcycle_df <- read_csv("motorcycles_mpg_csv.csv")
## [1] 30 4
Just as we expected, there are 30 observations.
Let’s take a look at the 4 variables
## [1] "model" "Make" "MPG" ## [4] "country_of_origin"
We only need the mpg and country_of_origin variables. So we will subset the data frame for only those two columns.
## # A tibble: 30 × 2 ## MPG country_of_origin ## <dbl> <chr> ## 1 47 USA ## 2 55 Japan ## 3 49 USA ## 4 58 Japan ## 5 57 Japan ## 6 39 USA ## 7 56 Italy ## 8 38 USA ## 9 68 Japan ## 10 46 USA ## # ℹ 20 more rows
The grand mean is the mean of all the data points in the data set (in this case 30). If each group has the same amount of values then it can also be thought of as the mean of means.
Adding up all the MPG’s and dividing it by 30 gives us \(G =\) 50.4
Here we can visualize the Total Sum of Squares. i.e the difference between each point and the overall mean
Lets now calculate \(M_i\), the means of each group
motorcycle_means <- motorcycle_df %>% group_by(country_of_origin)%>% summarise(mean = mean(MPG)) motorcycle_means
## # A tibble: 3 × 2 ## country_of_origin mean ## <chr> <dbl> ## 1 Italy 48 ## 2 Japan 59.1 ## 3 USA 44.1
The following graph allows us to visualize the Sum of Squares within Groups
Finally we can compare the mean of each group with the grand mean. This is the Sum of Squares between Groups
What do you think? Based on the previous graphs do you think there is a significant difference in the means of MPG?
Now, it is time to calculate ANOVA for this data set and see if these differences in means are statistically significant.
To do this we will simply use the aov() function. This function will perform all of the calculations previously stated and with summary(), print an ANOVA table complete with F-value and P-value. Nice!
anova_table <- aov(MPG ~ country_of_origin, data = motorcycle_df) summary(anova_table)
## Df Sum Sq Mean Sq F value Pr(>F) ## country_of_origin 2 1211.4 605.7 19.29 6.27e-06 *** ## Residuals 27 847.8 31.4 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Df Sum Sq Mean Sq F value Pr(>F) ## country_of_origin 2 1211.4 605.7 19.29 6.27e-06 *** ## Residuals 27 847.8 31.4 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
As you can see in the top right cell, the P-value is .00000627 which is less than 0.05. Therefore we can conclude that there is enough evidence to reject the null hypothesis and support the alternative hypothesis that at least one group differs significantly from the mean