-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter6.3.qmd
293 lines (241 loc) · 8.59 KB
/
Chapter6.3.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Ch. 6 – Diagnostics
This notebook demonstrates Data Diagnostics and Summaries.
## Preliminaries
Load or install the necessary R packages.
```{r}
#| label: libraries1
#| eval: true
#| echo: true
#| warning: false
library(tidyverse,quiet=TRUE)
library(ggplot2,quiet=TRUE)
#install.packages("cowplot",quiet=TRUE)
library(cowplot,quiet=TRUE)
options(repr.plot.width = 7, repr.plot.height = 5)
```
```{r}
#| eval: false
#| label: libraries2
#| warning: false
if (!require(devtools)) install.packages("devtools",quiet=TRUE)
devtools::install_github("tuomaseerola/MusicScienceData@main",quiet=TRUE)
library(MusicScienceData,quiet=TRUE)
```
```{r}
#| eval: true
#| label: libraries3
#| echo: false
library(MusicScienceData,quiet=TRUE)
```
## Code 6.1
```{r}
#| label: 6-1
#| warning: false
print(MusicScienceData::sadness[1:4,1:7])
```
## Code 6.2
```{r}
#| label: 6-2
#| warning: false
print(MusicScienceData::priming[1:3,1:6])
```
## Code 6.3
Figure 6.1. A histogram showing the distribution of responses to a particular question (no. 25) in Attitudes towards Sad Music (ASM) instrument.
```{r}
#| label: 6-3
#| warning: false
sadness <- MusicScienceData::sadness
g1 <- sadness %>%
drop_na(ASM25) %>% # drop missing values
ggplot(aes(x = ASM25))+
geom_histogram(bins=7,fill="grey50", colour='black')+
scale_x_continuous(breaks = seq(1,7,by=1))+
ylab('Count')+
xlab('1 = Strongly disagree, 7 = Strongly agree')+
theme_MusicScience()
g1
```
## Code 6.5
Figure 6.4. A box plot showing the distribution of responses to a particular question (no. 23) in Attitudes towards Sad Music (ASM) instrument split across gender.
```{r}
#| label: 6-5
#| warning: false
g5 <- sadness %>%
drop_na(ASM25) %>% # drop missing values
ggplot(aes(y = ASM25,fill=gender))+
geom_boxplot()+
scale_y_continuous(breaks = seq(1,7,by=1))+
scale_x_discrete()+
scale_fill_grey(start = .4,end = .8,name='Gender')+
ylab('1 = Strongly disagree, 7 = Strongly agree')+
theme_MusicScience()
print(g5)
```
## Code 6.6
Figure 6.5. Alternative visualisations of data. A: density plot across gender, B: multiple boxplots, C: boxplot overlaid with original data, D: violin plot with mean and median overlaid.
```{r}
#| label: 6-6
#| warning: false
options(repr.plot.width = 12, repr.plot.height = 10)
d <- MusicScienceData::priming
g1<-ggplot(d,aes(x=RT))+
geom_histogram(binwidth=100,colour='grey50',fill='white')+
ggtitle('Bin width 100')+
ylab('Count')+
xlab('Reaction time (ms)')+
scale_x_continuous(breaks=seq(0,2000,by=400))+
theme_MusicScience()
g2<-ggplot(d,aes(x=RT))+
geom_histogram(binwidth=10,colour='grey50',fill='white')+
ggtitle('Bin width 10')+
ylab('Count')+
xlab('Reaction time (ms)')+
scale_x_continuous(breaks=seq(0,2000,by=400))+
theme_MusicScience()
g3<-ggplot(dplyr::filter(d,RT>200 & RT<1500),aes(x=RT))+
geom_histogram(binwidth=10,colour='grey50',fill='white')+
ggtitle('Bin width 10 with trimming')+
ylab('Count')+
xlab('Reaction time (ms)')+
scale_x_continuous(breaks=seq(200,1500,by=200),limits = c(0,2000))+
geom_vline(xintercept = c(200,1500),linetype='dashed')+
theme_MusicScience()
g4<-ggplot(dplyr::filter(d,RT>200 & RT<1500),aes(x=RT))+
geom_histogram(binwidth=10,colour='grey50',fill='white')+
geom_density(aes(y=10 * after_stat(count)),alpha=0.5,colour='black',fill=NA)+
ggtitle('Bin width 10 density with trimming')+
ylab('Count')+
xlab('Reaction time (ms)')+
scale_x_continuous(breaks=seq(200,1500,by=200))+
theme_MusicScience()
G1 <- plot_grid(g1, g2, g3, g4, nrow = 2)
print(G1)
```
## Code 6.7
Table 6.1: The means of the ASM question 20 across the age.
```{r}
#| label: 6-7
#| warning: false
library(Hmisc,quietly = TRUE)
table1 <- MusicScienceData::sadness %>%
drop_na(ASM20) %>% # drop missing values
group_by(age) %>%
summarise(n=n(),mean_cl_normal(ASM20))
colnames(table1) <- c('Age','N','M','95% CI LL','95% CI UL')
knitr::kable(table1,digits = 2, format='simple',
caption = 'The means of the ASM question 20 across the age.')
```
## Code 6.8
```{r}
#| label: 6-8
#| warning: false
mean(MusicScienceData::sadness$ASM20, na.rm=TRUE) # Mean (ignore missing values)
sd(MusicScienceData::sadness$ASM20,na.rm=TRUE)
```
## Code 6.9
Figure 6.6. A bar graph showing the means of the responses to the question no. 20 in Attitudes towards Sad Music (ASM) instrument across gender.
```{r}
#| label: 6-9
#| warning: false
g6 <- sadness %>%
drop_na(ASM20) %>% # drop missing values
group_by(gender) %>%
summarise(mean= mean(ASM20),ci = mean_cl_normal(ASM20)) %>%
ggplot(aes(x = gender,y = mean,fill=gender))+
geom_col(colour='black',show.legend = FALSE)+
geom_errorbar(aes(ymin=ci$ymin,ymax=ci$ymax),width=0.5)+
scale_y_continuous(breaks = seq(1,7,by=1), expand = c(0,0))+
scale_fill_grey(start=.25,end=.75)+
coord_cartesian(ylim = c(1, 7)) +
ylab('Mean ± 95% CI')+
xlab('Gender')+
theme_MusicScience()
print(g6)
```
## Code 6.10
Figure 6.7. A bar graph showing the means of the responses to the question no. 6 in Attitudes towards Sad Music (ASM) instrument across musical expertise.
```{r}
#| label: 6-10
#| warning: false
g1 <- MusicScienceData::sadness %>%
drop_na(ASM1) %>% # drop missing values
ggplot(aes(x= ASM1,color=gender))+
geom_density(adjust=1.25)+
scale_color_grey(name='Gender')+
scale_x_continuous(breaks = seq(1,7,by=1))+
ggtitle(sadness_ASM_labels[1])+
ylab('Density')+
theme_bw()+
theme(legend.justification=c(1,0), legend.position=c(0.95,0.75))+
theme(plot.title = element_text(size=11))
tmp<-as_tibble(MusicScienceData::sadness)
tmp2<-tmp[,c(3,7:10)]
dfl <- pivot_longer(tmp2,cols = c(2:5))
g2 <- dfl %>%
drop_na(value) %>% # drop missing values
ggplot(aes(x=name,y = value,fill=gender))+
geom_boxplot(outlier.shape ="")+
scale_y_continuous(breaks = seq(1,7,by=1))+
scale_x_discrete()+
scale_fill_grey(start = .75, end=.25, name="Gender")+
ggtitle('ASM items 1 to 4')+
ylab('1 = Strongly disagree, 7 = Strongly agree')+
xlab('Item')+
theme_bw()+
theme(legend.justification=c(1,0), legend.position=c(0.95,0.70))
g3 <- MusicScienceData::sadness %>%
drop_na(ASM12) %>% # drop missing values
ggplot(aes(x=1,y = ASM12))+
geom_boxplot(fill='gray70')+
geom_jitter(alpha=0.13,colour='black', width = 0.33)+
scale_y_continuous(breaks = seq(1,7,by=1))+
scale_x_discrete()+
ggtitle(sadness_ASM_labels[12])+
ylab('1 = Strongly disagree, 7 = Strongly agree')+
xlab('ASM12')+
theme_bw()
g4 <- MusicScienceData::sadness %>%
drop_na(ASM13) %>% # drop missing values
ggplot(aes(x=1,y = ASM13))+
geom_violin(fill='grey70',adjust=1.2,alpha=0.50)+
scale_y_continuous(breaks = seq(1,7,by=1))+
scale_x_discrete()+
stat_summary(fun = median, fun.min = median, fun.max = median,
geom = "crossbar", width = 0.9)+
stat_summary(fun = mean, fun.min = mean, fun.max = mean,
geom = "crossbar", width = 0.9,colour='gray50')+
ggtitle(sadness_ASM_labels[13])+
annotate("text",x=1.6,y=mean(MusicScienceData::sadness$ASM13,na.rm = TRUE),label='Mean',hjust=0)+
annotate("text",x=1.6,y=median(MusicScienceData::sadness$ASM13,na.rm = TRUE),label='Median',hjust=0)+
ylab('1 = Strongly disagree, 7 = Strongly agree')+
xlab('ASM13')+
theme_bw()
G2 <- plot_grid(g1,g2,g3,g4,labels = c("A", "B", "C", "D"),ncol = 2, nrow = 2)
print(G2)
```
## Code 6.11
Figure 6.8. A scatterplot showing the means of the ratings to 110 film soundtrack excerpts using scales tension and valence in Eerola and Vuoskoski (2011).
```{r}
#| label: 6-11
#| warning: false
g9 <- ggplot(soundtrack) +
aes(x = Valence, y = Tension, colour = TARGET_EMOTION,
label=Number,
shape= TARGET_FRAMEWORK) +
geom_point(size=4,alpha=0.80,show.legend=FALSE) +
coord_fixed(ratio = 1)+
geom_smooth(aes(shape = NULL,colour=NULL),method="lm",
formula='y ~x',se=FALSE, fullrange=TRUE,
level=0.95, colour='grey50', # adds trendline
linetype='dashed',show.legend = FALSE)+
geom_text(show.legend=FALSE,color='white',size=1.7)+ # labels
scale_colour_grey(name='Emotion',start = .6,end = 0)+
scale_shape(name='Framework')+
scale_x_continuous(breaks=seq(1,9,by=2),limits=c(1,9))+
scale_y_continuous(breaks=seq(1,9,by=2),limits=c(1,9))+
theme_MusicScience()
print(g9)
```
## References
- Eerola, T., & Peltola, H.-R. (2016). Memorable experiences with sad music - reasons, reactions and mechanisms of three types of experiences. _PloS ONE, 11(6)_, e0157444. <https://doi.org/http://dx.doi.org/10.1371/journal.pone.0157444>
- Eerola, T., & Vuoskoski, J. K. (2011). A comparison of the discrete and dimensional models of emotion in music. _Psychology of Music, 39(1)_, 18–49.