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: 15 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Group
## dbl (4): ID, 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 balance time 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 balance time 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: Researchers were interested in learning whether the mode of neuromuscular training, had an effect on dynamic stability over an eight-week 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. In one group, participants underwent a neuromuscular training regimen (NMT1) that focused on strengthening muscles around the knee, ankle, and core. Another group underwent a neuromuscular training regimen (NMT2) that focused strengthening on just the muscles around the knee and ankle. The participants (n = 15) were randomly assigned to each intervention group and all interventions lasted eight weeks. Dynamic stability was operationally defined by the single-leg balance error scoring system (BESS) time (eyes closed) and measured at three time points: 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 = "BalanceTime", 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
BalanceTime <- dat_long$BalanceTime
Next, we can calculate summary statistics for balance time by group (Control, NMT1, NMT2) and time points (Pre, Mid, Post):
sum <- dat_long %>%
group_by(Group, time) %>%
summarise(
mean = mean(BalanceTime),
sd = sd(BalanceTime)
)
## `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 Control Pre 2.6 0.548
## 2 Control Mid 3.4 1.14
## 3 Control Post 5.8 1.48
## 4 NMT1 Pre 3.4 1.14
## 5 NMT1 Mid 7 1.58
## 6 NMT1 Post 11 1.58
## 7 NMT2 Pre 2.8 0.837
## 8 NMT2 Mid 5.6 1.95
## 9 NMT2 Post 7.8 0.837
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("BESS Time (s)") +
theme(plot.title = element_text(hjust = 0.5))
Exercise: Copy and paste the above plot into your summary report and interpret the plot lines for each group across the intervention time points (Pre, Mid, Post).
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(BalanceTime ~ Group*time + Error(ID/time), dat_long)
summary(anova)
##
## Error: ID
## Df Sum Sq Mean Sq F value Pr(>F)
## Group 2 76.98 38.49 19.03 0.00019 ***
## Residuals 12 24.27 2.02
## ---
## 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 208.58 104.29 67.769 1.34e-10 ***
## Group:time 4 26.49 6.62 4.303 0.00914 **
## Residuals 24 36.93 1.54
## ---
## 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, 24) = 4.30, p = 0.009, it can be concluded
that there is a significant interaction between training and time on
dynamic stability as measured with balance time.
F(4, 24) = 4.30, p = .009 has the following meaning:
Question: What can you conclude about the differences in balance time between training groups across the intervention time period?
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 balance
time.
# 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(BalanceTime ~ time + Error(ID/time), subset(dat_long, dat_long$Group == "Control"))
NMT1_av <- aov(BalanceTime ~ time + Error(ID/time), subset(dat_long, dat_long$Group == "NMT1"))
NMT2_av <- aov(BalanceTime ~ time + Error(ID/time), subset(dat_long, dat_long$Group == "NMT2"))
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 1.733 0.8667 1.13 0.355
## Residuals 12 9.200 0.7667
Next, for mid-intervention:
summary(mid_av)
## Df Sum Sq Mean Sq F value Pr(>F)
## Group 2 32.93 16.467 6.5 0.0122 *
## Residuals 12 30.40 2.533
## ---
## 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 68.8 34.4 19.11 0.000186 ***
## Residuals 12 21.6 1.8
## ---
## 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 balance time between
interventions at both the mid- and post-intervention time points. You
could write these results as:
There was a statistically significant difference in balance time between interventions at the mid-point of the intervention time period (F(2, 12) = 6.5, p = .012) as well as at post-intevention (F(2, 12) = 19.11, 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
## NMT1-Control 3.6 0.9144095 6.285591 0.0098537
## NMT2-Control 2.2 -0.4855905 4.885591 0.1142895
## NMT2-NMT1 -1.4 -4.0855905 1.285591 0.3760742
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
## NMT1-Control 5.2 2.9362426 7.4637574 0.0001400
## NMT2-Control 2.0 -0.2637574 4.2637574 0.0856685
## NMT2-NMT1 -3.2 -5.4637574 -0.9362426 0.0069541
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 balance time 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 balance time over the intervention time period for each group.
First, for the control group (0):
summary(control_av)
##
## Error: ID
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 4 2.267 0.5667
##
## Error: ID:time
## Df Sum Sq Mean Sq F value Pr(>F)
## time 2 27.73 13.867 8.577 0.0102 *
## Residuals 8 12.93 1.617
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Next, for the NMT1 group:
summary(NMT1_av)
##
## Error: ID
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 4 17.07 4.267
##
## Error: ID:time
## Df Sum Sq Mean Sq F value Pr(>F)
## time 2 144.53 72.27 71.08 8.06e-06 ***
## Residuals 8 8.13 1.02
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
and the NMT2 group:
summary(NMT2_av)
##
## Error: ID
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 4 4.933 1.233
##
## Error: ID:time
## Df Sum Sq Mean Sq F value Pr(>F)
## time 2 62.80 31.400 15.83 0.00165 **
## Residuals 8 15.87 1.983
## ---
## 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 balance time across the intervention time period. The results of the main effect of intervetion time can then be summarized as:
There was a statistically significant changes in balance time for the control group (F(2, 8) = 8.58, p < .010), NMT1 group (F(2, 8) = 71.08, p < .001), and NMT2 group (F(2, 8) = 15.83, p = .002).
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 == "Control")
pairwise.t.test(group0$BalanceTime, group0$time, paired = TRUE, p.adjust.method = "bonferroni")
##
## Pairwise comparisons using paired t tests
##
## data: group0$BalanceTime and group0$time
##
## Pre Mid
## Mid 1.000 -
## Post 0.036 0.182
##
## P value adjustment method: bonferroni
This pairwise comparison indicates that for the control group, there was not a statistically significant change in balance time from the pre- to the mid-intervention time points (p = 1.000) but there was signficant change in balance time from pre to post-intervention (p = .036).
Now, let’s take a look at changes in balance time in the NMT1 group:
group1 <- subset(dat_long, dat_long$Group == "NMT1")
pairwise.t.test(group1$BalanceTime, group1$time, paired = TRUE, p.adjust.method = "bonferroni")
##
## Pairwise comparisons using paired t tests
##
## data: group1$BalanceTime and group1$time
##
## Pre Mid
## Mid 0.00037 -
## Post 0.00108 0.02631
##
## P value adjustment method: bonferroni
For the NMT1 group, you might report the results in APA format as:
For the leg + core training (NMT1) group, the increase in balance time from pre-intervention (3.40 +/- 1.14 s) to the midway (7.00 +/- 1.58 s) time points was statistically significant (p < .001). Likewise, balance time statistically significantly increased at post-intervention (11.00 +/- 1.58) compared to pre-intervention (p < .001) and to mid-way (p < .001).
Finally, let’s take a look at changes in balance time in the NMT2 group:
group2 <- subset(dat_long, dat_long$Group == "NMT2")
pairwise.t.test(group2$BalanceTime, group2$time, paired = TRUE, p.adjust.method = "bonferroni")
##
## Pairwise comparisons using paired t tests
##
## data: group2$BalanceTime and group2$time
##
## Pre Mid
## Mid 0.2186 -
## Post 0.0024 0.1884
##
## P value adjustment method: bonferroni
Question: What can you conclude about the changes in balance time in the NMT2 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.
Exercise: Summarize the results of this RCT in APA format, ensuring that the above questions are addressed in your summary report.
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.