Suppose you want to examine the impact of diet and exercise on pulse rate. To investigate these issues, you collect a sample of 18 individuals and group them according to their dietary preferences: meat eaters and vegetarians. You then divide each diet category into three groups, randomly assigning each group to one of three types of exercise: exercise1, exercise2, exercise 3. In addition to these between-subjects factors, you want to include a single within-subjects factor in the analysis. Each subject’s pulse rate will be measured at three levels of exertion: intensity1, intensity2, intensity3.
So we have 3 factors to work with:
- Two between-subjects (grouping) factors: dietary preference and exercise type.
- One within-subjects factor : intensity (of exertion)
This is what our data looks like. Onwards, then!
After reading in the file, we give the columns appropriate names.
diet <- read_excel(path,col_names=F) names(diet) <- c("subject","exercise","intensity1","intensity2","intensity3", "diet")
Then we convert ‘exercise’,’subject’ and ‘diet’ into factors .
diet$exercise<- factor(diet$exercise) diet$diet<- factor(diet$diet) diet$subject <- factor(diet$subject)
For repeated measures ANOVA, the data must be in the long form . We will use the melt() form the reshape2 package to achieve this. We are now at one row per participant per condition.
diet.long <- melt(diet, id = c("subject","diet","exercise"), measure = c("intensity1","intensity2","intensity3"), variable.name="intensity")
At this point we’re ready to actually construct our ANOVA.
Our anova looks like this –
mod <- aov(value ~ diet*exercise*intensity + Error(subject/intensity) , data=diet.long)
The asterisk specifies that we want to look at the interaction between the three factors. But since this is a repeated measures design as well, we need to specify an error term that accounts for natural variation from participant to participant.
Running a summary() on our anova above yields the following results –
The main conclusions we can arrive at are as follows:
- There is a significant main effect of ‘diet’ on the pulse rate. We can conclude that a statistically significant difference exists between vegetarians and meat eaters on their overall pulse rates.
- There is a statistically significant within-subjects main effect for intensity.
- There is a marginally statistically significant interaction between diet and intensity. We’ll look at this later.
- The type of exercise has no statistically significant effect on overall pulse rates.
Let’s plot the average pulse rate as explained by diet, exercise, and the intensity.
mean_pulse1<-with(diet.long,tapply(value,list(diet,intensity,exercise),mean)) mean_pulse1 mp1 <- stack(as.data.frame(mean_pulse1)) mp1<- separate(mp1,ind,c("Intensity","Exercise")) mp1$Diet<- rep(seq_len(nrow(mean_pulse1)),ncol(mean_pulse1)) mp1$Diet <- factor(mp1$Diet,labels = c("Meat","Veg")) mp1$Intensity<-factor(mp1$Intensity) mp1$Exercise<-factor(mp1$Exercise) ggplot(mp1,aes(Intensity,values,group=Diet,color=Diet)) + geom_line(lwd=1) + xlab("Intensity of the exercise") + ylab("Mean Pulse Rate") + ggtitle("Mean Pulse rate - \n Exercise Intensity vs Diet") + theme_grey()+ facet_grid(Exercise ~.)
The plot agrees with our observations from earlier.
UPDATE: Understanding the Results
Earlier we had rejected a null hypothesis and concluded that change in mean pulse rate across intensity levels marginally depends upon dietary preference. Now ,we will turn our attention to the study of this interaction.
We begin by plotting an interaction plot as follows:
interaction.plot(mp1$Intensity, mp1$Diet, mp1$values , type="b", col=c("red","blue"), legend=F, lwd=2, pch=c(18,24), xlab="Exertion intensity", ylab="Mean pulse rate ", main="Interaction Plot") legend("bottomright",c("Meat","Veg"),bty="n",lty=c(1,2),lwd=2,pch=c(18,24),col=c("red","blue"),title="Diet")
We see that the mean pulse rate increases across exertion intensity(‘trials’) : this is the within-subject effect.
Further, it’s clear that vegetarians have a lower average pulse rate than do meat eaters at every trial: this is the diet main effect.
The difference between the mean pulse rate of meat-eaters vs vegetarians is different at each exertion level. This is the result of the diet by intensity interaction.
The main effect for diet is reflected in the fact that meat-eaters had a mean pulse rate roughly 10 to 20 points higher than that for vegetarians.
The main effect of intensity is reflected in the fact for both diet groups, the mean pulse rate after jogging increased about 50 points beyond the rate after warmup exercises, and increased another 55 points (approx.) after running.
The interaction effect of diet and intensity is reflected in the fact that the gap between the two dietary groups changes across the three intensities. But this change is not as significant as the main effects of diet and intensity.
Did you find this article helpful? Can it be improved upon ? Let me know!
Also, you can find the code here.
Until next time!