(Note: This is one of three posts that I wrote some time ago that have just been languishing under the “Misc.” tab of my website for a while, because for whatever reason I didn’t feel that they were a good fit for my blog. Well, I’ve decided to go ahead and move them to the blog, so here you go!)
Link to original TalkStats thread, June 24, 2012.
Following the discussion in this recent thread and spunky’s request therein, here is a neat trick one can use to test arbitrary contrasts for ANOVA models, and even multiple degree of freedom tests, using only some basic summary statistics of the kind that would be reported in a manuscript — without needing access to the raw data.
Setup
We need the following three pieces of information to do these tricks:
- the cell means
- the sample sizes per cell
- at least one F-ratio corresponding to any of the possible contrasts from the ANOVA model
So let’s say we have a manuscript on our desk in which the authors conducted a 2*2 factorial ANOVA, with factors (having levels
and
) and
(having levels
and
). They give a table of means and sample sizes by cell, but for whatever reason only report the test of the interaction. So we have the following information:
# cell means
round(tapply(dat$y, list(A = dat$A,B = dat$B), mean), 2)
# B
# A -1 1
# -1 103.54 99.28
# 1 99.59 102.61
# cell sample sizes
table(A = dat$A,B = dat$B)
# B
# A -1 1
# -1 18 23
# 1 23 16
# t statistic for interaction contrast
summary(lm(y ~ A + B + AB, data=dat))$coef["AB",]
# Estimate Std. Error t value Pr(>|t|)
# 1.819051097 0.570385136 3.189162871 0.002073302
Case 1: single degree of freedom tests
Perhaps as curious readers we are interested in knowing whether the cell differs from the other three cells. In other words we want to test the contrast below labeled “new”:
cbind(contr, new = c(3,-1,-1,-1))
# A B AB new
# [1,] 1 1 1 3
# [2,] 1 -1 -1 -1
# [3,] -1 1 -1 -1
# [4,] -1 -1 1 -1
Following the formula here, the F-ratio can be computed as
where and
are the numbers of parameters in the full model and the nested model, respectively; and
is the total sample size.
So the only two missing quantities here are SSR and SSE. If we can get those we can compute the desired F-ratio.
Given a particular contrast ,
SSR for =
where is the contrast weight for group
,
is the mean for group
,
is the number of groups, and
is the number of observations in group
.
So in this data we have
.
Now we need to get SSE. To do this, we can use the same formula to compute SSR for a contrast for which we already know F, and then rearrange the F-ratio formula to solve for SSE.
So for the known interaction contrast we have
.
Solving the F formula for SSE gives
.
Since , we can now just plug in the numbers to get
,
so that finally we have
.
And we can check our work by running anova() on the dataset after recoding the contrasts:
anova(lm(y ~ other1 + other2 + new, data=dat))
# Analysis of Variance Table
#
# Response: y
# Df Sum Sq Mean Sq F value Pr(>F)
# other1 1 38.06 38.056 1.4988 0.224639
# other2 1 191.60 191.604 7.5462 0.007505 **
# new 1 41.50 41.496 1.6343 0.205002
# Residuals 76 1929.70 25.391
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Aside from some minimal rounding error, we have it.
Case 2: multiple degree of freedom tests
Given the same data as above, suppose now that for some reason we wish to see the 2 degree of freedom test comparing the full ANOVA model (factors A, B, and their interaction) to a model that only includes factor A.
We already saw above how to solve for SSE (and in fact we already computed it–we are using the same Full model so SSE will be the same). However, here we will get SSR in a slightly different way, using
,
where is the predicted value for observation
under the smaller or reduced model,
is the predicted value for observation
under the larger or full model, and
is the number of observations. Essentially, we are treating the predicted values from the more complex model as the data to be predicted and then computing the sum of squared errors in the normal fashion.
In the ANOVA case, this formula can be written more simply as
,
where is the number of observations in group
,
is the predicted value for group
under the smaller or reduced model,
is the predicted value for group
under the larger or full model, and
is the number of groups.
The predicted values from the Large model are straightforward: they are the group means. For the Small model, we have two sets of predicted values, those for and
, and in both cases these predicted values are weighted averages of the two cell means at each level (i.e., collapsing across the
factor), weighted by cell size.
For :
For :
So using the simplified SSR formula, we have
,
which makes our F-ratio
.
Checking our work:
anova(lm(y ~ A, data=dat),
lm(y ~ A + B + AB, data=dat))
# Analysis of Variance Table
#
# Model 1: y ~ A
# Model 2: y ~ A + B + AB
# Res.Df RSS Df Sum of Sq F Pr(>F)
# 1 78 2198.8
# 2 76 1929.7 2 269.06 5.2983 0.007013 **
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
And again we have it, save for minimal rounding error.