Analysis of Variance
Analysis of Variance
Analysis of Variance
(ANOVA)
Instructor: Weikang Kao, Ph.D.
When to use ANOVA?
While simple tests (like t-tests) are useful (and the best way to go in many
situations), most systems we want to examine in the real world have more than
two levels and likely contain more than two factors.
ANOVA is one type of statistical model that can handle: Predictors with multiple
levels, multiple predictors, interactions between predictors, and as will see
towards later in this course it can deal with designs containing random and nested
factors (repeated measurement).
Will review the basics of ANOVA and how to use it for data containing fixed
categorical effects with 3 or more levels (if just two levels in most cases a t-test
will get the job done easier).
Preview of ANOVA
Hypothesis of ANOVA:
H0:
H1:
Degrees of freedom:
p = number of groups
n = observations in each groups
dfT: df degrees of freedom, total df: n*p - 1
dfB: df between groups, numerator df: p - 1
dfW: df within groups, denominator df: (n1 -1) + (n2-1) + …. (nX-1)
Critical value, degrees of freedom and the F-table
http://www.statisticslectures.com/tables/ftable/
Introduction of ANOVA
Test Statistic: F
ANOVA = Analysis of variance, which means that the __________ of the data will be investigated.
Recall: what is variance?
Sum of the squares (SS) / df
Sum of the squares in ANOVA:
Sum of the squares total: A measure of the variability between each of the observations and the
grand mean of all observations.
Sum of the squares between: A measure of the variability between each of the group mean and the
grand mean of all observations.
Sum of the squares within: A measure of the variability between each of the observation and its
respective group mean.
Introduction of ANOVA
So…..
Sum of the squares total = Sum of the squares between + Sum of the squares within.
SST = SSB + SSW
SSB: the effect which can be explained by our treatment/ observation.
SSW: the effect which can NOT be explained by our treatment/ observation. (error term)
Mean square of the F-test:
What is mean square? An estimate of the average variation in between group and within group.
It helps to formulate the F-test:
F = variability (between) / variability (within)
Introduction of ANOVA
F-Statistic
When checking the F-value, we try to examine the ratio between MSB and MSW,
which is the variability explained by the difference between the groups to the
variability unexplained by the groups.
Remember we just mentioned about treatment and error term, how do those
concepts influence the F-value and what is the sequence?
If variability between is greater than the variability within, that means the statistic
will be greater than one: what does that mean?
If variability between is smaller than the variability within, the result will be less
than one: what does that mean?
Introduction of ANOVA
After the previous four steps, now we can try to make some conclusions based on
them.
Two different conclusions:
Statistical Conclusion: We check the observed (F) to see if it falls in the rejection
area, and make the conclusion. The conclusion can be either “reject the null
hypothesis” or “fail to reject the null hypothesis”.
Research conclusion: based on our original research question, we make
conclusion which will be stated in terms relating to it. For example, when having
more sugar, individual tends to gain weights.
One-way ANOVA
There are several assumptions underlying ANOVA that should be considered (some
violations are less serious than others).
1. “Normal” data – mainly we are concerned with skew and kurtosis.
2. Independence of observations – we assume that each observation is independent
from other observations (e.g., no repeated measurement). If this assumption is
violated we have a few options but a standard ANOVA is not one of them (no
check we just have to know our data).
3. Equality of variances – our factor levels are associated with the same level of
variance in our dependent variable (most important if we have unequal
observations in each level).
Normality
Now we know there is a significant difference among the targeting sample, what’s
next?
We would like to make comparisons between each groups so to understand what makes
the difference.
The advantages of Post Hoc test:
Controlling type I error: think of if we do five t-tests, what will happened?
Single-step post hoc tests:
We apply the single-step post hoc tests for all pairwise comparisons when equal
variances are assumed.
What if the variances of groups are not equal?
Post-Hoc Test
As we discussed p-values tell us there is a difference but don’t tell us the size of the
difference (i.e., the effect).
Many researchers focus on effect sizes since large effects are more likely to be
robust (replicable).
The effect size is a standardized difference in the means across group.
What is the importance of effect size?
A measure of the PRACTICE SIGNIFICANCE of the effect of the treatment.
These are large effects: <=.1 is small, .25 is medium, .4 is large (Cohen, 1988)
Application
A working example
To make things easier to follow will use a real data set where individuals provided
valuations of an items worth either as buyers, sellers, or choosers (someone who
decides the price that would make them indifferent between that amount or and an
item).
In this example our IV is perspective and it has three levels making our analysis a
One-Way ANOVA: Buyer (1), Chooser (0), Seller (2)
We can see that we want to test whether there is variation between valuations for our
three levels of perspective, and we might have some predictions about which levels
will differ (e.g., sellers > buyers, choosers = sellers, etc.)
A working example
library("moments")
First look at the overall distribution:
plot(density(ANOVAExample$Valuation))
qqnorm(data$Valuation)
D'Agostino skewness test:
agostino.test(data$Valuation)
Normality test (Shapiro-Wilks):
shapiro.test(ANOVAExample$Valuation)
Assumption: Independence of observations
Observations are randomly sampled from the population , or subjects are randomly
assigned to treatment groups, so that all the observations in the within groups and the
between groups are independent.
Scatterplot of residuals
eruption.lm = lm(Variable ~ Group, data=data)
eruption.res = resid(eruption.lm)
plot(data$Variable, eruption.res, ylab="Residuals", xlab=“group", main=“Title")
abline(0, 0)
It’s very important to know that the F-test is NOT robust to the violation, both type I and
type II errors are affected.
Assumption: Variance Equality
Bonferroni:
pairwise.t.test(ANOVAExample $IV, ANOVAExample $Group, paired = FALSE,
p.adjust.method = “method")
**method can be "none", "bonferroni", "holm", "hochberg", "hommel", "BH", or
"BY“
Kurskal-Wallis:
kruskalmc(V2~factor(Condition), data = ANOVAExample)
Tukey’s Test:
TukeyHSD(model)
Effect Size
What do we find?
1. Research Question and hypothesis?
2. Test of the assumptions?
3. Result of the ANOVA test?
4. Result of the post hoc tests?
5. Effect size?
6. Conclusion?
Summary write up
We are now dealing with a dataset (an XXX bank costumer info). The bank wants
to know how different jobs influence individual’s annual income, purchasing
ability and how many credit cards do they have?
Download the Bankinfo data.
In the Group variable, 0 represents students, 1 represents teachers, and 2
represents taxi driver.
What are the research questions?
What are the hypothesis?
Practice
Normality: fair
Independence of observations: fair
Variance Equality: Good
Is there any significant result? Yes.
Do we need a post hoc? Yes.
The result of the post hoc? There is a difference between group 0 (students) and
group 1 (teachers)
Effect size? large.
Practice: Summary
Normality: bad
Independence of observations: fair
Variance Equality: fair
Is there any significant result? Yes.
Do we need a post hoc? Yes.
The result of the post hoc? There is a difference between group 0 (students) and
group 1 (teachers) and group 1 (teachers) and group 2 (taxi drivers)
Effect size? large.
Weekly Lab