# How can you implement a two-way ANOVA with nesting in R or SPSS?

I currently am conducting a study where I have three variables: one that is binary, and two numerical ratio variables. Each of the subjects in my study has values for each of the three variables.

Variables:

• Condition (binary): Values 0 and 1
• Pre (ratio)
• Post (ratio)

I want to test if there is a significant difference between the the pre and post variables of the 0 control group and the 1 experimental group. Both groups have 103 subjects. The data meet all typical ANOVA assumptions such as normality and the like. I was thinking of nesting the variables as follows and then running a two-way ANOVA.

Variable 1 is exposed / not and variable 2 is pre / post. People are nested in Variable 1 (meaning that each person gives both pre and post information for either exposed or not exposed conditions)

Would this be the correct way to approach this problem? Also how would I implement this statistical analysis, preferably in R or SPSS?

Pre-post designs are quite common and the standard method is to use the pre-measure as a control variable and the post-measure as the response with the binary variable as the covariate.

This is quite simple:

m0 <- lm(Post ~ Pre, data=dat)
m1 <- lm(Post ~ Pre+Condition, data=dat)
anova(m1,m0) # test for difference in mean(Post) after control for Pre
m2 <- lm(Post ~ Pre*Condition, data=dat)
anova(m2, m1) # test for interaction: different slopes in the two Conditions


If this had been with repeated measures within subject (i.e, pre-post measures in each Condition, you would need to specify the error structure in the aov function: You could compare this model in R where the errors for each Condition are nested in subject-ID:

 m1 <- aov(post ~ pre + Condition+Error(subjID/Condition), data=dat)


Ratio variables are notorious for being poorly behaved statistically (i.e., often having significant skew and blowing up as the denominator goes near zero.) However, if the denominator is far from zero, you should not make the mistake of looking at the raw distributions and deciding that the data fail the assumptions needed for validity of linear methods. You need to look at the residuals before conducting any test. (And even then, there is some doubt whether minor departures from normality invalidate inferences.) The help page for aov warns you to use orthogonal contrasts. There was an article in R-News several years ago on multi-variate (as opposed to multi-variable) methods and this would provide further options for model comparisons and measures of sphericity.

Another option is offered by the Anova function in John Fox's car package.

Another approach is to analyze the pairwise differences, but that approach has flaws.

• By "ratio variable", I suspect she / he means a variable measured on a ratio scale. Jul 9, 2013 at 1:33
• Do you really need to specify an error structure here? I think there is only 1 post measurement for each subject. Given that you are using the pre measure as a covariate (& I agree w/ you about that) & not as a 2nd response variable, I'm not sure if the Error(subjID/Condition) part is necessary. Jul 9, 2013 at 1:41
• That's what I thought initially and had even composed an answer using lm(). You may still be correct.
– DWin
Jul 9, 2013 at 1:47
• @gung I apologize if I was unclear. I did mean a variable measured on a ratio scale, not a ratio variable. Also there is only one "pre" and one "post" measurement for each subject. Jul 9, 2013 at 2:01
• OK. I'll post code appropiate to that data setup.
– DWin
Jul 9, 2013 at 6:40

The standard way of handling repeated measures under the linear regression framework (which encompasses ANOVAs) is to reduce the repeated measures into a series of difference scores, which represent the contrasts of interest. So, in your example, you might do what is essentially a t-test comparing the difference from pre to post in the control and the experimental conditions. You can find the details of how to implement this in R in my answer to this question.

Edit:

As described by DWin, an alternative way to analyze these data is to use the baseline measurement as a covariate in your model (i.e., basically just enter the pre-test measurement as a predictor in your linear model). As described by Van Breukelen (2006), this method will have more power than the difference score approach if you have (or expect) no pre-test differences on your dependent variable. If you do have pre-test differences on your dependent variable, the covariate approach will introduce bias into your results, and the difference score approach will therefore be preferable.