Like last week, we would like you to attempt today’s lab tasks independently. We will provide a worked example, but then the rest is up to you! You can look back at at your old scripts, exercises, and slides for help.
This will be the last session, so make sure to ask any questions you might have today!
We will be using the ice_cream_data_2.csv for today’s analysis.
Firstly, you will need to create a new project and R Script for today’s session. You will then need to load in the data file using the read.csv() function. Save this to an object called df.
Load in the required packages for a one-way ANOVA.
library(tidyverse) # For [view] function
library(psych) # For [describeBy]
library(rstatix) # For [anova_test]
library(ggpubr) # For plots such as [gghist] and [ggqqplot]
We will then need to run the following code to avoid getting scientific notation in our output.
options("scipen" = 9999, "digits" = 10)
Before starting our analyses, it is a good idea to check the data to make sure it has been read in correctly.
view(df)
One-way ANOVAs are used when we have three or more groups or levels of a factor. It is called a one-way ANOVA because these are used when we have a single independent variable. For instance, if we wanted to investigate how different drug types affected our running speed, this would have one independent variable (the variable we are manipulating - drug type) and one dependent variable (the variable we are measuring - running speed). If we only had two types of drugs (drug A and drug B), we could run a t-test on the data. But if we have more than two types of drugs (drugs A, B, and C) we would have to run multiple t-tests, which increases our risk of a Type I error (false positive). To avoid this, we can instead run a one-way ANOVA which will allow us to compare each type of drug in one test.
Here, we will be investigating the effect of Flavour on Enjoyment. Flavour has 3 levels: Chocolate, Vanilla, and Strawberry. Therefore, we need to use a one-way ANOVA. We can see from our data file that Flavour is classed as a character (“chr”). Before we can run any analyses, we need to convert this into a factor.
df$Flavour <- as.factor(df$Flavour)
For our report, it is a good idea to take a look at how many participants took part in each condition of Flavour.
table(df$Flavour)
##
## Chocolate Strawberry Vanilla
## 68 62 70
We would then want to take a look at the descriptive statistics for Enjoyment for each Flavour. Let’s take a look at the means, medians, and sd of Enjoyment at each level of Flavour.
describeBy(df$Enjoyment, df$Flavour)
We can see that Chocolate has the highest enjoyment ratings (M = 66.33) followed by Strawberry (M = 57.79) with Vanilla having the lowest ratings (M = 50.20).
We can also see if our data is normally distributed using ggqqplot.
ggqqplot(df, x = "Enjoyment", facet.by="Flavour")
the line on the plot represents the ideal values if normally distributed
data should be within the grey area around the line
ideally, all points will fall within the grey area, but the example below would be considered acceptable

As a result, we can assume that our data does not violate the assumption of normality.
From our descriptive statistics, we can see that Strawberry has the highest Enjoyment scores and Vanilla has the lowest. Now we need to run our ANOVA to see if this difference is significant.
anova <- anova_test(df, dv = Enjoyment,
between = Flavour,
within = NULL, type = 3)
# adding [detailed = TRUE] gives us extra information, such as the sum of squares
anova # This prints out our ANOVA output
## ANOVA Table (type III tests)
##
## Effect DFn DFd F p p<.05 ges
## 1 Flavour 2 197 42.5 0.000000000000000452 * 0.301
We can see from the output that our p value is below our significance threshold of p < .05, therefore our results are significant and we can reject the null hypothesis. We would report our results like this:
“We conducted a one-way ANOVA on the effect of Flavour on Enjoyment. There was a significant difference between Flavour types, F(2, 197) = 42.50, p < .001, ges = .301.”
Then, using our descriptive statistics from before, you would want to clarify which flavour had higher/lower scores.
Now we have gone through an example together, have a go at analysing the effect of Flavour on Mood and answering the following questions:
Two-way ANOVAs enable us to look at the effect of two different independent variables on one dependent variable. Lets use the last example, where we investigated the effect of ice-cream Flavour on Enjoyment. Now, imagine that we have another variable (Number of Scoops) that we wanted to investigate. As we have two independent variables, we can use a two-way ANOVA! This will allow us to look at each of the main effects (the effect of each independent variable on its own) as well as an interaction effect (the combined effect of our independent variables).
Here, we will be looking at the effect of Flavour and Number_Scoops on Enjoyment. Lets first take another look at the data.
view(df)
We can see that Flavour is still a factor as we converted it in the last exercise. Number_Scoops, however, is still classed as a character. We need to convert this into a factor.
df$Number_Scoops <- as.factor(df$Number_Scoops)
Now we can see how many participants fell within each category.
table(df$Flavour, df$Number_Scoops)
##
## 1 2
## Chocolate 34 34
## Strawberry 31 31
## Vanilla 35 35
And then view the descriptive statistics for both
describeBy(df$Enjoyment, df$Flavour)
describeBy(df$Enjoyment, df$Number_Scoops)
Side note. Across your computing sessions, you may have noticed that we can sometimes use two different approaches to get our desired output. For instance, when looking at the descriptives, we have the above approach, and you will also have seen the below approach:
df %>%
group_by(Number_Scoops) %>%
get_summary_stats(Enjoyment, type = "mean_sd")
Notice there are differences in the output, but if you were looking for means and sd, then both methods are appropriate.
Back to our example: we have already taken a look at the descriptive for Flavour on Enjoyment in Task 1, but not for Number_Scoops. We can see that two scoops (M = 60.79) seems to result in higher enjoyment scores than one scoop (M = 55.29). But, we still need to run our analysis to know for sure!
Before that, however, we need to make sure our data is appropriate for our chosen analysis. Lets also take a look at the ggqqplot to check our normality assumption.
ggqqplot(df, x = "Enjoyment", facet.by = "Flavour")
ggqqplot(df, x = "Enjoyment", facet.by = "Number_Scoops")
Look at the qqplots and see what you think of the data. Has the normality assumption been violated? Or, are we okay to continue onwards?
As the dots were all mostly the grey area, we can indeed assume normality. Now we need to run our two-way ANOVA to see if these differences are significant or if there is an interaction between Flavour and Number_Scoops.
anova2 <- anova_test(df, Enjoyment ~ Flavour*Number_Scoops, type = 3)
anova2 # View the ANOVA results
## ANOVA Table (type III tests)
##
## Effect DFn DFd F p p<.05 ges
## 1 Flavour 2 194 48.601 0.00000000000000000776 * 0.334
## 2 Number_Scoops 1 194 15.076 0.00014200000000000001 * 0.072
## 3 Flavour:Number_Scoops 2 194 7.442 0.00076900000000000004 * 0.071
There was a significant main effect for both Flavour, and Number_Scoops, as well as an interaction as all p values are below our .05 threshold.
Because we found a significant effect, we then need to run post-hocs!
df %>%
group_by(Flavour) %>%
pairwise_t_test(Enjoyment ~ Number_Scoops,
paired = FALSE, p.adjust.method = "bonferroni")
Have a look at which comparisons are significant (p < .05). Note that as we have used an adjustment (bonferroni), we pay attention to the p.adjusted column. Try to work out what these significant differences mean. What effects are our variables having?
If you find it hard to visualise these effects, you’re not alone! That’s why, regardless of whether we find significant effects or not, it is always a good idea to include a visual representation of the interaction of the variables. This helps not only the reader, but us too!
ggline(df, x = "Flavour", y = "Enjoyment",
color = "Number_Scoops", add = "mean_se", palette = c("blue", "red"))
What do we see here? Using the graph and our post-hoc comparisons, we can see that number of scoops doesn’t seem to affect how enjoyable strawberry was. We know that 2 scoops was significantly more enjoyable than 1 scoop of vanilla. Finally, we see that 2 scoops of chocolate was much more enjoyable than 1 scoop (notice the gap in between the blue and red lines is greater for chocolate than the others).
We would report the results of the two-way ANOVA as follows:
“Significant main effects were found for Flavour, F(2, 194) = 48.60, p < .001, ges = 0.334, with higher enjoyment scores for chocolate ice cream (M = 66.33, SD = 10.35) followed by strawberry (M = 57.79, SD = 10.14) and finally vanilla ice cream (M = 50.20, SD = 10.33). There was also a main effect of Number_Scoops, F(1, 194) = 15.08, p < .001, ges = .072, with higher enjoyment scores for two scoops (M = 60.79, SD = 12.59) than one scoop of ice cream (M = 55.29, SD = 11.27). There was also an interaction between these two variables (see Figure **), F(2, 194) = 7.44, p < .001, ges = .071. Post-hoc testing showed that number of scoops had no effect on enjoyment of strawberry flavour (p = .518). Meanwhile, two scoops were found to be more enjoyable for both vanilla (p = .010) and chocolate (p < .001).”
Take some time to go over the above results section and really try to understand where each result came from and how it was interpreted.
Now we have worked through the example, have a go at running the analysis for the effect of Flavour and Number_Scoops on Mood and answer the following questions:
What number of scoops results in higher Mood?
Fill in the gaps for the reporting of the two main effects from the two-way ANOVA
Flavour: F(_,__) = _, p _ . _, ges = _
Number_Scoops: F(_ , _) = _, p _ . _, ges = _
Is there an interaction between Flavour and Number_Scoops on Mood?
Should post-hoc tests be conducted? Why/why not?
Try to recreate the following plot:
As this is your final RStudio resource for the year, we wanted to provide you with a chance to test yourself on some more basic RStudio functions which are central to everything else you’ll have learnt this year. As with the rest of the exercise, try to do this independently at first.
When running an analysis in RStudio and we want to be able to save our work, should we type our code into the console or the script?
If you wanted to load in a dataset called study_1_data.csv and save it to a dataframe called study … what code would you type?
If you wanted to load in a dataset called exam_practice.csv and save it to a dataframe called exam … what code would you type?
If you were working with a dataframe called Soc and had a categorical variable called Extroversion (i.e., responses were either Extrovert or Introvert). How would you turn this variable into a factor on RStudio?
If you then wanted to see the frequency of responses in your Extroversion variable, what code would you use?
If you wanted to load the package called apaTables … what code would you use?
What does this symbol do: %>%
Imagine our data frame (called happ) has two dependent variables that both aim to measure happiness. We might want to create an total of these two scores. If our two variables were called happiness_1 and happiness_2, what code would you write to create a new variable that totaled these?
The “Chocolate” flavour has the highest mean mood score (M = 91.16, SD = 14.06)
describeBy(df$Mood, df$Flavour)
##
## Descriptive statistics by group
## group: Chocolate
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 68 91.16 14.06 91.28 90.96 15.63 60.72 123.31 62.59 0.1 -0.62
## se
## X1 1.7
## ------------------------------------------------------------
## group: Strawberry
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 62 80.5 17.06 78.86 80.45 22.48 50.37 115.56 65.19 0.01 -1.15
## se
## X1 2.17
## ------------------------------------------------------------
## group: Vanilla
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 70 74.46 14.54 74.47 74.25 15.96 38.48 112.21 73.73 0.1 -0.37
## se
## X1 1.74
The qqplots show that our normality assumption is met.
ggqqplot(df, x = "Mood", facet.by = "Flavour")
There is a significant effect of flavour on mood, F(2, 197) = 21.21, p < .001, ges = .177.
anova_test(df, dv = Mood,
between = Flavour,
within = NULL, type = 3)
## ANOVA Table (type III tests)
##
## Effect DFn DFd F p p<.05 ges
## 1 Flavour 2 197 21.214 0.00000000453 * 0.177
The two scoops group has the highest mean mood score (M = 85.43, SD = 15.81)
describeBy(df$Mood, df$Number_Scoops)
##
## Descriptive statistics by group
## group: 1
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 100 78.59 16.91 77.36 78.15 16.36 38.48 116.12 77.64 0.17 -0.6
## se
## X1 1.69
## ------------------------------------------------------------
## group: 2
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 100 85.43 15.81 87.19 85.64 18.84 52.07 123.31 71.24 -0.06 -0.62
## se
## X1 1.58
Flavour: F(2,194) = 22.30, p < .001, ges = .187
Number_Scoops: F(1, 194) = 10.141, p < .01, ges = .050
anova_test(df, Mood ~ Flavour*Number_Scoops, type = 3)
## ANOVA Table (type III tests)
##
## Effect DFn DFd F p p<.05 ges
## 1 Flavour 2 194 22.304 0.00000000191 * 0.187
## 2 Number_Scoops 1 194 10.141 0.00200000000 * 0.050
## 3 Flavour:Number_Scoops 2 194 1.239 0.29200000000 0.013
Using the anova test, we can see that there is not a significant interaction between Flavour and Number_Scoops on Mood (F(2, 194)=1.24, p = .292, ges - 0.013).
As there is no interaction, we do not need to run post-hoc tests.
For the graph, we make use of the ‘linetype =’ and ‘title =’ arguments to add the extra parameters to our graph. Then it is just a matter of setting up the correct variables and colours.
ggline(df, x = "Flavour", y = "Mood",
color = "Number_Scoops", xlab = "Flavour",
ylab = "Mood", linetype = "dashed",
add = "mean_se", title = "A very fancy looking graph!", palette = c("green", "black"))
The script! Everything in the console will be lost once we close R.
study <- read.csv("study_1_data.csv")
exam <- read.csv("exam_practice.csv")
Soc$Extroversion <- as.factor(Soc$Extroversion)
table(Soc$Extroversion)
library(apaTables)
%>% is a pipe operator. It tells RStudio to feed the previous line of code into the next one. For example:
df %>%
group_by("insert variable here")
Here, we use the pipe operator to tell RStudio to feed the first line of code (our data frame) into the second. This can be used to help us bind several functions together, or to simply save us from our typing in our data frame name lots of times!
happ$happiness_total <- (happiness_1 + hapiness_2)