This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
## Rows: 45 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (5): ID, group, pre, mid, post
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
The two-way mixed ANOVA compares the mean differences between groups that have been split on two independent variables. The primary purpose of a two-way mixed ANOVA is to understand if there is an interaction between the two independent variables on the dependent variable. The two-way mixed ANOVA is often used to determine whether there are differences between independent groups over time such as in a randomized controlled trial (RCT) where mulitple groups are tested a number of times over a period of time (i.e., training vs control at pre and post season). In ANOVA terminology, the independent variables are normally called “factors.” A two-way mixed ANOVA has one between-subjects factor (i.e., group) and one within-subjects factor.
A between-subjects factor and a within-subjects factor are independent variables, but whereas a between-subjects factor has independent groups (e.g., gender: male/female), a within-subjects factor has related groups (also known as repeated measures), such as time (e.g., pre- and post-intervention). The primary purpose of carrying out a two-way mixed ANOVA is to understand if there is a two-way interaction (i.e., an interaction between the between-subjects and within-subjects factors). This ascertains whether the effect of one of the factors is dependent on the value of the other factor. For example, you might want to understand if blood cholesterol concentration changes over time (e.g., 0, 3 and 6 months) if one embarks on an exercise-training programme of either low, moderate or high intensity. Importantly, you would usually want to know if blood cholesterol concentration changed differently over time depending on which exercise-training programme was performed (e.g., either the low, moderate or high intensity exercise-training programme). Understanding the latter requires the analysis of the two-way interaction effect. In essence, the two-way mixed ANOVA allows you to distinguish between the effects of different exercise-training interventions over time.
Scenario: A researcher was interested in discovering whether the intensity of an exercise-training programme, but with equal calorific expenditure, had an effect on cholesterol concentration over a six-month period. To answer this they implemented three different interventions. In one intervention, participants did not change their current sedentary lifestyle; this was the “control” intervention (group 0). In group 1, participants underwent a low-intensity exercise-training programme that expended 1000 kCal per week. Group 2 underwent a high-intensity exercise-training programme that also expended 1000 kCal per week (but, therefore, exercised for less total time). The participants in each intervention were different and all interventions lasted six months. Cholesterol concentration was measured three times: at the beginning, midway and at the end of the interventions.
Question: What are the independent and dependent variables? What are the research questions and the null hypotheses in this scenario?
Variable Names
You can check out the exact variable names:
## [1] "ID" "group" "pre" "mid" "post"
Let us prepare the data in the dataset for analysis. The original
file is set up in “wide” format, meaning the repeated measures of the
dependent variables are in their own respective columns. The dataset
will be converted to “long” format so that time is its own
factor. This converted dataset will be saved in the
dat_long data vector:
# define IVs as factors
dat <- within(dat,{
ID <- factor(ID)
group <- factor(group)
})
# convert from wide to long format
dat_long <- dat %>%
gather(key = "time", value = "cho", pre, mid, post)
# define IVs as factors in the converted dataset
dat_long <- within(dat_long,{
ID <- factor(ID)
group <- factor(group)
time <- factor(time)
})
# specify "pre" as the first test session
dat_long$time <- relevel(dat_long$time, "pre")
# this contains the data for the dependent variable
cho <- dat_long$cho
Next, we can calculate summary statistics for cholesterol by group and time:
sum <- dat_long %>%
group_by(group, time) %>%
summarise(
mean = mean(cho),
sd = sd(cho)
)
## `summarise()` has grouped output by 'group'. You can override using the
## `.groups` argument.
sum
## # A tibble: 9 × 4
## # Groups: group [3]
## group time mean sd
## <fct> <fct> <dbl> <dbl>
## 1 0 pre 6.04 0.574
## 2 0 mid 5.99 0.601
## 3 0 post 5.83 0.551
## 4 1 pre 5.89 0.556
## 5 1 mid 5.82 0.602
## 6 1 post 5.49 0.604
## 7 2 pre 6.01 0.470
## 8 2 mid 5.31 0.492
## 9 2 post 4.80 0.504
We can generate a plot of the dependent variable by group and time.
# plot mean cholesterol values at each time point for each exercise group
ggplot(sum, aes(x = time, y = mean, color=group, group=group)) +
geom_point() +
geom_line() +
ggtitle("Cholesterol Concentration") +
theme(plot.title = element_text(hjust = 0.5))
From the plot above you can see that the three lines are not parallel and some lines do in fact cross one another. On closer examination, it would appear that participants in the control group (the red line) maintained a similar, but declining, mean cholesterol concentration over time. On the other hand, the low-intensity exercise-training group (the green line) maintained a similar, but declining, mean cholesterol concentration (a slight reduction after three months), but then a more significant decline in mean cholesterol concentration from the mid to post time point. The most pronounced effect on mean cholesterol concentration was experienced with the high-intensity exercise-training group (the blue line) with a large decline in mean cholesterol concentration at both time points (i.e., midway and post intervention). It would appear that the different groups have different patterns of mean cholesterol concentration over time. As such, from these results, it would appear that we might expect to find an interaction effect.
Despite the usefulness of profile plots in understanding your data, you cannot determine an interaction effect from them because the profile plot is based on the sample data and we are interested in determining whether there is an interaction effect in the population. Therefore, a formal statistical test is required to test for the presence of an interaction effect (i.e., via statistical significance testing).
In this case, we will use the following code to perform a two-way mixed ANOVA, which determines whether there is a two-way interaction between the between- and within-subjects factors (i.e., a group*time interaction).
# two-way ANOVA (factors are group and time)
anova <- aov(cho ~ group*time + Error(ID/time), dat_long)
summary(anova)
##
## Error: ID
## Df Sum Sq Mean Sq F value Pr(>F)
## group 2 7.73 3.866 4.322 0.0196 *
## Residuals 42 37.58 0.895
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Error: ID:time
## Df Sum Sq Mean Sq F value Pr(>F)
## time 2 8.338 4.169 390.5 <2e-16 ***
## group:time 4 4.603 1.151 107.8 <2e-16 ***
## Residuals 84 0.897 0.011
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The row labeled by group:time shows whether or not there
is a significant interaction between the between- and within-subjects
factors. Since F(4, 84) = 107.8, p < 0.001, it can be
concluded that there is a significant interaction between training and
time on cholesterol concentration. An APA formatted statements can be
simply worded as follows:
There was a statistically significant interaction between the intervention and time on cholesterol concentration, F(4, 84) = 107.77, p < .001
The last part of the statement above, F(4, 84) = 107.77, p < .001 has the following meaning:
Question: What can you conclude about the differences in cholesterol between training groups across time?
When you have a statistically significant interaction, reporting the
main effects can be misleading and you will want to determine the
difference between groups at each category of time and vice versa,
called simple main effects. You can run simple main effects using syntax
in R. Basically, you will run separate one-way ANOVAs and one-way
repeated measures ANOVAs to determine the main effects of training
group and time, respectively, on cholesterol
concentration.
# simple main effects ANOVAs (one-way ANOVA) between groups at each time point
# use WIDE formatted dataset
pre_av <- aov(pre ~ group, dat)
mid_av <- aov(mid ~ group, dat)
post_av <- aov(post ~ group, dat)
# simple main effects ANOVAs (RM-ANOVA) between time points for each group
# use LONG formatted dataset
control_av <- aov(cho ~ time + Error(ID/time), subset(dat_long, dat_long$group == "0"))
low_av <- aov(cho ~ time + Error(ID/time), subset(dat_long, dat_long$group == "1"))
high_av <- aov(cho ~ time + Error(ID/time), subset(dat_long, dat_long$group == "2"))
Each of the above ANOVAs produces its own summary table.
Main effect of training intervention
Let’s take a look at the results between training groups at each time point.
First, for pre-intervention:
summary(pre_av)
## Df Sum Sq Mean Sq F value Pr(>F)
## group 2 0.197 0.0983 0.343 0.712
## Residuals 42 12.045 0.2868
Next, for mid-intervention:
summary(mid_av)
## Df Sum Sq Mean Sq F value Pr(>F)
## group 2 3.804 1.902 5.907 0.00549 **
## Residuals 42 13.522 0.322
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
and post-intervention:
summary(post_av)
## Df Sum Sq Mean Sq F value Pr(>F)
## group 2 8.335 4.168 13.56 2.86e-05 ***
## Residuals 42 12.906 0.307
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
You can see from the group row that there is a
statistically significant difference in cholesterol concentration
between interventions at both the mid- and post-intervention time
points. You could write these results as:
There was a statistically significant difference in cholesterol concentration between interventions at the mid-point of the intervention (F(2, 42) = 5.91, p = .005) as well as at post-intevention (F(2, 42) = 13.56, p < .001).
Since there are more than two training groups, you can perform post-hoc tests to determine pairwise differences between groups:
TukeyHSD(mid_av)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = mid ~ group, data = dat)
##
## $group
## diff lwr upr p adj
## 1-0 -0.1680000 -0.6713671 0.33536710 0.6984528
## 2-0 -0.6833333 -1.1867004 -0.17996623 0.0055284
## 2-1 -0.5153333 -1.0187004 -0.01196623 0.0437860
TukeyHSD(post_av)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = post ~ group, data = dat)
##
## $group
## diff lwr upr p adj
## 1-0 -0.3412795 -0.833039 0.1504799 0.2223834
## 2-0 -1.0344437 -1.526203 -0.5426843 0.0000218
## 2-1 -0.6931642 -1.184924 -0.2014047 0.0038862
Notice that the post-hoc tests were only performed on the mid and post-intervention data since the one-way ANOVAs above indicated significant differences between groups at these two time points only (i.e., not pre-intervention).
Question: Based on these post-hoc tests, between which two training groups exhibited differences in cholesterol concentration at the mid-point of the intervention? What about at the post-intervention point?
Main effect of time
Now let’s take a look at the changes in cholesterol over time for each group.
First, for the control group (0):
summary(control_av)
##
## Error: ID
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 14 13.61 0.9724
##
## Error: ID:time
## Df Sum Sq Mean Sq F value Pr(>F)
## time 2 0.3518 0.17591 15.4 3.08e-05 ***
## Residuals 28 0.3197 0.01142
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Next, for low-intensity group (1):
summary(low_av)
##
## Error: ID
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 14 14.25 1.018
##
## Error: ID:time
## Df Sum Sq Mean Sq F value Pr(>F)
## time 2 1.3622 0.6811 74.96 5.72e-12 ***
## Residuals 28 0.2544 0.0091
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
and high-intensity group (2):
summary(high_av)
##
## Error: ID
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 14 9.712 0.6937
##
## Error: ID:time
## Df Sum Sq Mean Sq F value Pr(>F)
## time 2 11.226 5.613 487.1 <2e-16 ***
## Residuals 28 0.323 0.012
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
We essentially performed three separate one-way repeated measures ANOVA, one per group. They show that all three groups experienced statistically significant changes in cholesterol across time. The results of the main effect of time can then be summarized as:
There was a statistically significant changes in cholesterol concentration for the control group (F(2, 28) = 15.4, p < .001), low-intensity exercise group (F(2, 28) = 74.96, p < .001), and high-intensity group (F(2, 28) = 487.1, p < .001).
As there are three time points, a follow-up test is needed to determine the time points between which the dependent variable changes for each training group. With repeated measures, a Tukey post-hoc test cannot be used since it assumes that independent groups are in each level of the independent variable. Rather, we use a paired t-test adjusted for multiple comparisons for each group:
group0 <- subset(dat_long, dat_long$group == "0")
pairwise.t.test(group0$cho, group0$time, paired = TRUE, p.adjust.method = "bonferroni")
##
## Pairwise comparisons using paired t tests
##
## data: group0$cho and group0$time
##
## pre mid
## mid 0.2698 -
## post 0.0017 0.0047
##
## P value adjustment method: bonferroni
This pairwise comparison indicates that for the control group, there was not a statistically significant change in cholesterol from the pre- to the mid-intervention time points (p = 0.270) but there were signficant changes in cholesterol from pre to post-intervention (p = .002) and from mid- to post-intervention (p = .005).
Now, let’s take a look at changes in cholesterol in the low-intensity group (1):
group1 <- subset(dat_long, dat_long$group == "1")
pairwise.t.test(group1$cho, group1$time, paired = TRUE, p.adjust.method = "bonferroni")
##
## Pairwise comparisons using paired t tests
##
## data: group1$cho and group1$time
##
## pre mid
## mid 0.28 -
## post 1.9e-07 2.8e-08
##
## P value adjustment method: bonferroni
For the low-intensity group, you might report the results in APA format as:
For the low-intensity exercise group, cholesterol concentration was not statistically significantly different between pre-intervention and midway time points (M = 0.07, SE = 0.04 mmol/L, p = .277), but cholesterol concentration was statistically significantly reduced at post-intervention compared to pre-intervention (M = 0.40, SE = 0.04 mmol/L, p < .001) and at post-intervention compared to mid-way (M = 0.33, SE = 0.03 mmol/L, p < .001).
Finally, let’s take a look at changes in cholesterol in the high-intensity group (2):
group2 <- subset(dat_long, dat_long$group == "2")
pairwise.t.test(group2$cho, group2$time, paired = TRUE, p.adjust.method = "bonferroni")
##
## Pairwise comparisons using paired t tests
##
## data: group2$cho and group2$time
##
## pre mid
## mid 5.2e-11 -
## post 5.1e-13 2.5e-09
##
## P value adjustment method: bonferroni
Question: What can you conclude about the changes in cholesterol in the high-intensity group based on this pairwise comparison?
Note: If you do not have a statistically significant interaction, you need to interpret the main effects for the between-subjects (for all time points) and within-subjects (for all groups) factors. For example, to determine the differences between training groups, a one-way ANOVA can be performed on all measurements of the dependent variable, regardless of time. Likewise, a repeated-measures ANOVA can be performed across time points, regardless of training group. You would basically run a one-way ANOVA and a repeated measures ANOVA to determine the effects of the between- and within-subjects factors on the dependent variable.
In conclusion, you can summarize these results as follows:
There was a statistically significant interaction between the intervention and time on cholesterol concentration (F(4, 84) = 107.77, p < .001). At the mid-point of the intervention, cholesterol concentration was statistically significantly greater in the control (5.99 +/- 0.60 mmol/l, p = .006) and in the low-intensity exercise group (5.82 +/- 0.60 mmol/l, p = .044) compared to the high-intensity exercise group (5.31 +/- 0.49 mmol/l). Cholesterol concentration in the low-intensity exercise group was not statistically significantly lower than the control group (p = .698). At post-intervention, cholesterol concentration was statistically significantly greater in the control (5.83 +/- 0.55 mmol/l, p < .001) and in the low-intensity exercise group (5.49 +/- 0.60 mmol/l, p = .004) compared to the high-intensity exercise group (4.80 +/- 0.50 mmol/l). Cholesterol concentration in the low-intensity exercise group was not statistically significantly lower than the control group (p = .222).
That was a short introduction to R and RStudio, but we will provide you with more functions and a more complete sense of the language as the course progresses. You might find the following tips and resources helpful.
In this course we will be using the dplyr (for data
wrangling) and ggplot2 (for data visualization)
extensively. If you are googling for R code, make sure to also include
these package names in your search query. For example, instead of
googling “scatterplot in R”, google “scatterplot in R with
ggplot2”.
The following cheathseets may come in handy throughout the course. Note that some of the code on these cheatsheets may be too advanced for this course, however majority of it will become useful as you progress through the course material.
While you will get plenty of exercise working with these packages in the labs of this course, if you would like further opportunities to practice we recommend checking out the relevant courses at DataCamp.