This worksheet relies on the follwing pacakges: teachingtools, tidyverse, afex, emmeans, and psych. To make sure that these packages are loaded, run the following code in your console.
xfun::pkg_attach2("psych","afex","ggbeeswarm","emmeans","teachingtools","tidyverse")
If you need to install teachingtools then follow the instructions in the introduction.
This worksheet covers the material from the follwing sessions: Reading data into R, RStudio Project files, Analysis of Variance with afex, and Simple mediation.
Create a new RStudio project. Inside the project folder, create two sub-folders. One should be called data
and the other should be called scripts
.
Create a new R script, and save it into the scripts
folder of your project.
Download the following two files and save them into your data
folder of your project.
The following problems should be solved by writing code into the file in your scripts
folder
Write code to read in the data stored in the files aov_data1.csv
and medaite_data1.csv
. The data in the file aov_data1.csv
should be stored in a variable called aov_data
, and the data in mediate_data1.csv
should be stored in a variable called mediate_data
.
here()
function for finding directions
read_csv()
function for reading in data
aov_data <- readr::read_csv(file = here::here("data","aov_data1.csv"))
mediate_data <- readr::read_csv(file = here::here("data","mediate_data1.csv"))
The following questions will require the aov_data
data set you’ve just imported. This data set contains data from a working memory task. It was undertaken by 60 participants: 20 Engineering students, 20 Psychology Students, and 20 Maths students (Degree.Type
). The students performed the working memory task under two conditions (Task
). Their performance is recorded in value
.
aov_data
# A tibble: 120 x 4
SubjectCode Task Degree.Type value
<chr> <chr> <chr> <dbl>
1 s01 Angle Engineering 0.358
2 s01 Color Engineering 0.305
3 s02 Angle Engineering 0.151
4 s02 Color Engineering 0.164
5 s03 Angle Maths 0.268
6 s03 Color Maths 0.250
7 s04 Angle Engineering 0.209
8 s04 Color Engineering 0.131
9 s05 Angle Psychology 0.206
10 s05 Color Psychology 0.189
# … with 110 more rows
Conduct a 2 x 3 mixed ANOVA on the aov_data
data set. Save the model output as aov_wm
within
and between
factors.
aov_wm <- afex::aov_ez(id = "SubjectCode",
dv = "value",
between = "Degree.Type",
within = "Task",
data = aov_data)
aov_wm
Anova Table (Type 3 tests)
Response: value
Effect df MSE F ges p.value
1 Degree.Type 2, 57 0.00 0.79 .021 .458
2 Task 1, 57 0.00 57.32 *** .199 <.001
3 Degree.Type:Task 2, 57 0.00 5.03 ** .042 .010
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
Output a summary ANOVA table that includes the partial \(\eta^2\) effect size, rather than the default generalised \(\eta^2\).
anova()
function useful
pes
anova(object = aov_wm, es = "pes")
Anova Table (Type 3 tests)
Response: value
num Df den Df MSE F pes Pr(>F)
Degree.Type 2 57 0.0034893 0.7924 0.02705 0.457692
Task 1 57 0.0011455 57.3198 0.50140 3.556e-10 ***
Degree.Type:Task 2 57 0.0011455 5.0305 0.15003 0.009729 **
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
As your first question you’re interested in whether their is a difference in performance on the Color Task and the Angle Task in each of the three degree type. That is, you want to know the Task difference in each degree type.
emmeans::emmeans(aov_wm, specs = "Task", by = "Degree.Type") %>%
pairs()
Degree.Type = Engineering:
contrast estimate SE df t.ratio p.value
Angle - Color 0.0413 0.0107 57 3.858 0.0003
Degree.Type = Maths:
contrast estimate SE df t.ratio p.value
Angle - Color 0.0260 0.0107 57 2.429 0.0183
Degree.Type = Psychology:
contrast estimate SE df t.ratio p.value
Angle - Color 0.0731 0.0107 57 6.826 <.0001
Now that you know the difference in each degree type, you’d like to compare this difference across the degree types. That is, you’d like to compare the task difference between Engineering and Maths, Engineering and Psychology, and Maths and Psychology.
simple
argument
emmeans::emmeans(aov_wm, spec = "Task", by = "Degree.Type") %>%
pairs() %>%
pairs(simple = "Degree.Type")
contrast = Angle - Color:
contrast1 estimate SE df t.ratio p.value
Engineering - Maths 0.0153 0.0151 57 1.011 0.5733
Engineering - Psychology -0.0318 0.0151 57 -2.099 0.0991
Maths - Psychology -0.0471 0.0151 57 -3.109 0.0081
P value adjustment: tukey method for comparing a family of 3 estimates
Finally, you’d like to draw a plot. Draw any plot you feel is sensible.
afex_plot(aov_wm,
x = "Degree.Type",
panel = "Task",
error = "none",
dodge = 0,
data_geom = geom_boxplot)
The following questions rely on the mediate_data
data set which you imported at the start. This is data set, from Pollack et al (2012), has data from 262 entrepreneurs who were part of the Business Networking International group.
mediate_data
# A tibble: 262 x 7
tenure estress affect withdraw sex age ese
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1.67 6 2.6 3 1 51 5.33
2 0.580 5 1 1 0 45 6.05
3 0.580 5.5 2.4 3.66 1 42 5.26
4 2 3 1.16 4.66 1 50 4.35
5 5 4.5 1 4.33 1 48 4.86
6 9 6 1.5 3 1 48 5.05
7 0 5.5 1 1 0 51 3.66
8 2.5 3 1.16 1 0 47 6.13
9 0.5 5.5 1.33 2 1 40 5.26
10 0.580 6 3 4 1 43 4
# … with 252 more rows
Pollack et al (2012), proposed that economic stress estress
led to a desire to withdraw from entrepreneurial activities (withdraw
) and that this was mediated by the depressed affect affect
caused by this stress. Perform this mediation analysis. Save the model as med_with
psych::mediate()
function
med_with <- psych::mediate(y = "withdraw",
x = "estress",
m = "affect",
data = mediate_data,
plot = FALSE)
The find the bounds of the bootstrapped 95% CI of the indirect effect (ab).
boot
than contains information about the bootstrapped results
med_with$boot$ci.ab[,1]
2.5% 97.5%
0.06980001 0.20213655