-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathInfoRate.Rmd
2732 lines (2259 loc) · 147 KB
/
InfoRate.Rmd
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Different languages, similar encoding efficiency"
subtitle: "Full statistical analysis and plots"
author: "Christophe Coupé, Yoon Mi Oh, Dan Dediu and François Pellegrino"
date: '`r date()`'
output:
html_document:
fig_caption: yes
highlight: textmate
theme: readable
toc: yes
toc_depth: 6
toc_float: yes
word_document: default
editor_options:
chunk_output_type: console
---
```{r setup, echo=F, message=F, warning=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2);
library(ggrepel);
library(reshape2);
library(plyr);
library(lmerTest);
library(gamlss);
library(gamlss.mx);
library(sjstats);
library(sjPlot);
library(moments);
library(parallel);
library(pander);
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE, # default code chunk options
fig.width=9, fig.height=6, fig.align = "center", comment=NA, # default figure dimensions
fig.path = "figures/", # save images to ./figures/
dpi=72, dev="jpeg", # please set dpi=300 and comment out dev="jpeg" for high resolution images but very big resulting HTML document
cache=TRUE, autodep=TRUE); # cache computations
dir.create("./figures", showWarnings=FALSE); # figures are saved here
do.expensive.computations = FALSE; # force the expensive computations
no.cores <- ifelse(.Platform$OS.type == "windows", 1, max(detectCores()-1, 1, na.rm=TRUE)); # use all cores in the system except 1 (including hyperthreading), except on Windows, were this defaults to 1 core
options(width = 250);
options(contrasts=c("contr.treatment","contr.poly"));
set.seed(3683); # For reproducibility of the results
```
<style>
caption, .caption {
color: #555555;
font-weight: bold;
font-size: 105%;
text-align: left}
a[hreflang]:before{}
</style>
```{r local_functions, echo=FALSE}
# Local functions ####
get_re_distrib <- function(v, myTitle = "") {
myDF <- data.frame(myCoef=v)
y <- quantile(myDF$myCoef, c(0.25, 0.75))
x <- qnorm(c(0.25, 0.75))
slope <- diff(y)/diff(x)
int <- y[1L] - slope * x[1L]
p_re <- ggplot(data = myDF, aes(sample=myCoef)) + stat_qq() + ggtitle(myTitle)
p_re <- p_re + geom_abline(slope = slope, intercept = int) + theme(text = element_text(size=11, family="serif"))
p_re
}
zeropad <- function(x, nz=1, exc.int=TRUE) {
if (is.integer(x) & exc.int) {
x
} else {
sprintf(paste0("%.", nz, "f"), x)
}
}
analyses <- function(myModel) {
# Preparing graphs
tmpDF <- data.frame(x=predict(myModel), y=residuals(myModel))
p_res <- ggplot(data=tmpDF, aes(x=x, y=y)) + geom_point(size=0.5) + xlab("Fitted values") + ylab("Quantile residuals")
p_res <- p_res + geom_smooth(method="loess")
p_res <- p_res + theme_minimal()
p_qq <- get_re_distrib(tmpDF$y, "")
momentsNames <- c("mean", "variance", "coef. of skewness", "coef. of kurtosis")
momentsValues <- zeropad(c(mean(tmpDF$y), var(tmpDF$y), skewness(tmpDF$y), kurtosis(tmpDF$y)), 3)
(tmpMoments <- data.frame(momentsNames, momentsValues))
(coef <- coef(getSmo(myModel, "mu", which = 1)))
families_re_p <- get_re_distrib(coef, 'First random effect, mu')
list(p_res, p_qq, tmpMoments, families_re_p)
}
lm.eqn <- function(m) {
l <- list(a = format(coef(m)[1], digits = 2),
b = format(abs(coef(m)[2]), digits = 2),
r2 = format(summary(m)$r.squared, digits = 3));
if (coef(m)[2] >= 0) {
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,l)
} else {
eq <- substitute(italic(y) == a - b %.% italic(x)*","~~italic(r)^2~"="~r2,l)
}
as.character(as.expression(eq));
}
display.graph.equation <- function(x, y, param1, param2, title) {
df <- data.frame(x=x, y=y)
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) + geom_point() + ggtitle(title) + xlab(param1) + ylab(param2) +
annotate("text", x=min(df$x), y=min(df$y), hjust = 0.2, vjust = 0.8, label = lm.eqn(lm(y ~ x, df)), colour="black", size = 5, parse=TRUE)
return (p)
}
display.correlation <- function(m, param1, param2, position, predictor.name, model.Name) {
x <- coef(getSmo(m, param1, which = position))
y <- coef(getSmo(m, param2, which = position))
print(summary(x));
print(summary(y));
print(cor.test(x,y,method="pearson"));
print(cor.test(x,y,method="spearman"));
myTitle <- paste0(model.Name, ": ", param1, " and " , param2, " for ", predictor.name)
print(myTitle)
p <- display.graph.equation(x, y, param1, param2, myTitle)
return(p)
}
# http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/
#
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
```
```{r loading_data, echo=FALSE, message=FALSE, warning=FALSE}
# Loading and preparing the data:
info.rate.data <- read.table("./InfoRateData.csv", header=TRUE, sep="\t", quote="", stringsAsFactors=FALSE);
# Compute Speech Rate (SR), Shannon Information Rate (ShIR), and Conditional Information Rate (IR):
info.rate.data$SR <- (info.rate.data$NS / info.rate.data$Duration);
info.rate.data$ShIR <- (info.rate.data$SR * info.rate.data$ShE);
info.rate.data$IR <- (info.rate.data$SR * info.rate.data$ID);
# Add NS(VIE) / NS(L) for each language L and text T:
info.rate.data <- merge(info.rate.data, unique(info.rate.data[info.rate.data$Language=="VIE", c("Text", "NS")]), by="Text", suffixes=c("",".y"), all.x=TRUE);
names(info.rate.data)[ncol(info.rate.data)] <- "NSVR";
info.rate.data$NSVR <- info.rate.data$NSVR / info.rate.data$NS;
# Add language family data:
info.rate.data$Family <- Vectorize(function(a) # assign language family to langauge codes
{
switch(as.character(a),
"CAT" = "Indo-European",
"CMN" = "Sino-Tibetan",
"DEU" = "Indo-European",
"ENG" = "Indo-European",
"EUS" = "Basque",
"FIN" = "Uralic",
"FRA" = "Indo-European",
"HUN" = "Uralic",
"ITA" = "Indo-European",
"JPN" = "Japanese",
"KOR" = "Korean",
"SPA" = "Indo-European",
"SRP" = "Indo-European",
"THA" = "Tai-Kadai",
"TUR" = "Turkic",
"VIE" = "Austroasiatic",
"YUE" = "Sino-Tibetan",
NA)
}, "a")(as.character(info.rate.data$Language));
# Convert to factors:
info.rate.data$Text <- as.factor(info.rate.data$Text);
info.rate.data$Speaker <- as.factor(info.rate.data$Speaker);
info.rate.data$Language <- as.factor(info.rate.data$Language);
info.rate.data$Sex <- as.factor(info.rate.data$Sex);
info.rate.data$Family <- as.factor(info.rate.data$Family);
# Make sure we use contrasts to the overall mean for factors:
contrasts(info.rate.data$Text) <- contr.sum(length(levels(info.rate.data$Text)));
contrasts(info.rate.data$Speaker) <- contr.sum(length(levels(info.rate.data$Speaker)));
contrasts(info.rate.data$Language) <- contr.sum(length(levels(info.rate.data$Language)));
contrasts(info.rate.data$Sex) <- contr.sum(length(levels(info.rate.data$Sex)));
contrasts(info.rate.data$Family) <- contr.sum(length(levels(info.rate.data$Family)));
# Sort by Language, Speaker and Text:
info.rate.data <- info.rate.data[order(info.rate.data$Language, info.rate.data$Speaker, info.rate.data$Text),];
```
## Introduction
This `Rmarkdown` script (and the corresponding TAB-separated CSV input data files `InfoRateData.csv` and `AutomaticSylDetect.csv`, and the resulting `HTML` document) contains the full analysis and plotting code accompanying the paper *Different languages, similar encoding efficiency: comparable information rates across the human communicative niche*.
## The data
```{r echo=FALSE, message=FALSE, warning=FALSE}
# Transform it into the long format:
info.rate.data.long <- melt(info.rate.data, id.vars=c("Language", "Text", "Speaker", "Sex", "Family"));
# Extract speakers, languages and texts:
speakers <- sort(unique(info.rate.data$Speaker));
languages <- sort(unique(info.rate.data$Language));
lgfams <- sort(unique(info.rate.data$Family));
texts <- sort(unique(info.rate.data$Text));
```
For more information on the data, please see [Oh (2015)](#references).
There are in total `r length(unique(info.rate.data$Language))` languages (see the table below).
### The Oral Corpus
The oral corpus is based on a subset of the *Multext* (Multilingual Text Tools and Corpora) parallel corpus [(Campione & Véronis, 1998)](#campioneveronis1998) in *British English*, *German*, and *Italian*.
The material consists of 15 short texts of 3-5 semantically connected sentences carefully translated by a native speaker in each language.
For the other 14 languages, two of the authors supervised the translation and recording of new datasets.
All participants were native speakers of the target language, with a focus on a specific variety of the language when possible – e.g. *Mandarin* spoken in Beijing, *Serbian* in Belgrade and *Korean* in Seoul.
No strict control on age or on the speakers’ social diversity was performed, but speakers were mainly students or members of academic institutions.
Speakers were asked to read three or four times (first silently and then loudly two or three times) each text.
The texts were presented one by one on the screen in random order, in a self-paced reading paradigm.
This way, speakers familiarized themselves with the text and reduced their reading errors.
The last loud recording was analyzed in this study.
With these, the oral corpus contains `r nrow(info.rate.data)` texts read by `r length(speakers)` speakers coming from `r length(languages)` languages and `r length(texts)` texts.
For each language and text we have the following number of syllables (*NS*):
```{r echo=FALSE, message=FALSE, warning=FALSE}
knitr::kable(unique(info.rate.data[,c("Language","Text","NS")]),
row.names=FALSE, caption="Number of syllables (*NS*) per language and text.");
```
### Text Corpus
Text datasets were acquired from various sources as illustrated in the table below.
After an initial data curation, each dataset was phonetically transcribed and automatically syllabified by a rule-based program written by one of the authors, except in the following cases:
i) when syllabification was already provided with the dataset (*English*, *French*, *German*, and *Vietnamese* for the multisyllabic words);
ii) when the corpus was syllabified by an automatic grapheme-to-phoneme converter (*Catalan*, *Spanish*, and *Thai*).
Additionally, no syllabification was required for Sino-Tibetan languages (*Cantonese* and *Mandarin Chinese*) since one ideogram corresponds to one syllable.
| Language | Family | ISO 639-3 | Corpus |
|------------------|----------------|-----------|-------------------------------------------------------------|
| Basque | Basque | EUS | E-Hitz [(Perea et al., 2006)](#references) |
| British English | Indo-European | ENG | WebCelex (MPI for Psycholinguistics) |
| Cantonese | Sino-Tibetan | YUE | A linguistic corpus of mid-20th century Hong Kong Cantonese |
| Catalan | Indo-European | CAT | Frequency dictionary [(Zséder et al., 2012)](#references) |
| Finnish | Uralic | FIN | Finnish Parole Corpus |
| French | Indo-European | FRA | Lexique 3.80 [(New et al., 2001)](#references) |
| German | Indo-European | DEU | WebCelex (MPI for Psycholinguistics) |
| Hungarian | Uralic | HUN | Hungarian National Corpus [(Váradi, 2002)](#references) |
| Italian | Indo-European | ITA | The PAISÀ Corpus [(Lyding et al., 2014)](#references) |
| Japanese | Japanese | JPN | Japanese Internet Corpus [(Sharoff, 2006)](#references) |
| Korean | Korean | KOR | Leipzig Corpora Collection (LCC) |
| Mandarin Chinese | Sino-Tibetan | CMN | Chinese Internet Corpus [(Sharoff, 2006)](#references) |
| Serbian | Indo-European | SRP | Frequency dictionary [(Zséder et al., 2012)](#references) |
| Spanish | Indo-European | SPA | Frequency dictionary [(Zséder et al., 2012)](#references) |
| Thai | Tai-Kadai | THA | Thai National Corpus (TNC) |
| Turkish | Turkic | TUR | Leipzig Corpora Collection (LCC) |
| Vietnamese | Austroasiatic | VIE | VNSpeechCorpus [(Le et al., 2004)](#references) |
### Dataset structure
The data is structured as follows:
- *Language* is the unique language ISO 639-3 ID with `r length(unique(info.rate.data$Language))` possible values: `r paste0('"',sort(unique(info.rate.data$Language)),'"',collapse=", ")`;
- *Language family* is the language family with `r length(unique(info.rate.data$Family))` possible values: `r paste0('"',sort(unique(info.rate.data$Family)),'"',collapse=", ")`;
- *Text* is the text identifier (similar across languages and speakers) with `r length(unique(info.rate.data$Text))` possible values: `r paste0('"',sort(unique(info.rate.data$Text)),'"',collapse=", ")`;
- *Speaker* is the unique speaker's ID (there are `r length(unique(info.rate.data$Speaker))` in the dataset);
- *Sex* is the speaker's sex;
- *Age* is the speaker's age (available only for a subset of `r tmp <- unique(info.rate.data[,c("Speaker","Age")]); sum(!is.na(tmp$Age))` speakers);
- *Duration* is the duration of the texts in seconds as spoken by a given speaker (pauses longer than 150ms being excluded);
- *NS* is the number of of syllables of the texts (the same for a given text in a given language);
- *SR* is the speech rate (syllables/second) for a given text spoken by a given speaker;
- *ShE* (resp. *ID*) is the first-order (resp. second-order) language-level entropy estimate;
- *ShIR* and *IR* are text * speaker properties obtained by multiplying *SR* by *ShE* and *ID* respectively.
### Technical notes on regressions
We use throughout *sum contrasts* for the factor IVs, which are orthogonal contrasts which compare every level of the IV to the overall mean (for example, for a two-levels factor such as *Sex* we do not compare *Males* with *Females* but each with their overall mean, which is included in the intercept).
However, in `R` the `contr.sum()` function used to define these contrasts produces level names that are very uninformative, so we explicit these below (please note that in the model outputs the *last* level is usually not shown):
```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
for( tmp in c("Sex", "Text", "Language", "Family") ) cat(paste0(" - ***",tmp,"***: ",paste0("`",tmp,1:length(levels(info.rate.data[,tmp])),"` = *",levels(info.rate.data[,tmp]),"*", collapse=", "), " (the last level, `",tmp, length(levels(info.rate.data[,tmp])),",` is usually not displayed);\n"));
```
### Exploratory plots and summaries
#### Speaker characteristics
```{r Speaker characteristics, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=10, fig.cap="NS: exploratory plots."}
tmp <- info.rate.data;
tmp$Language <- factor(tmp$Language, levels=c("VIE","EUS","CAT","DEU","ENG","FRA","ITA","SPA","SRP","JPN","KOR","CMN","YUE","THA","TUR","FIN","HUN"));
tmp$Family <- factor(tmp$Family); tmp$Family <- revalue(tmp$Family, c("Austroasiatic"="AA", "Basque"="B", "Indo-European"="IE", "Japanese"="J", "Korean"="K", "Sino-Tibetan"="ST", "Tai-Kadai"="TK", "Turkic"="T", "Uralic"="U"));
knitr::kable(unique(tmp[,c("Speaker", "Sex", "Language", "Age")]) %>%
dplyr::group_by(Language) %>%
dplyr::summarise("no.speakers"=length(Speaker),
#"females"=sum(Sex=="F",na.rm=TRUE),
"females.perc"=100*sum(Sex=="F",na.rm=TRUE)/length(Speaker),
"with.age"=sum(!is.na(Age)),
"mean.age"=mean(Age,na.rm=TRUE),
#"median.age"=median(Age,na.rm=TRUE),
"sd.age"=sd(Age,na.rm=TRUE),
"ages"=paste0("(",paste0(sort(Age),collapse=", "),")")) %>%
dplyr::arrange(as.character(Language)),
col.names=c("Lng",
"# spkrs",
#"# females",
"% fem",
"# age",
"mean(age)",
#"median(age)",
"sd(age)",
"actual ages"),
row.names=FALSE,
digits=c(NA,
0,
#0,
1,
0,
1,
#1,
1,
NA), caption="Distribution of speaker number and characteristics (sex and age) by language. *Lng*=language, *# spkrs*=number of speakers, *% fem*=percentage of female speakers, *# age*=number of speakers with age info, and for those, *mean(age)*=mean age, *sd(age)*=standard deviation of age, and *actual ages*=the sorted ages.");
```
#### NS
```{r NS by Language and Text, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=10, fig.cap="NS: exploratory plots."}
multiplot(plotlist=list(ggplot(tmp, aes(x=NS)) + geom_histogram(col="yellow") + xlab("NS"),
{
# From http://mgimond.github.io/ES218/Week06a.html:
y <- quantile(tmp$NS, c(0.25, 0.75)) # Find the 1st and 3rd quartiles
x <- qnorm( c(0.25, 0.75)) # Find the matching normal values on the x-axis
slope <- diff(y) / diff(x) # Compute the line slope
int <- y[1] - slope * x[1] # Compute the line intercept
ggplot(tmp, aes(sample=NS))+stat_qq(distribution=qnorm, color="royalblue", alpha=0.5) + geom_abline(intercept=int, slope=slope, color="firebrick2")
},
ggplot(tmp, aes(x=Language, y=NS, colour=Family)) + geom_boxplot() + ylab("NS") + theme(axis.text.x = element_text(angle = 45, hjust = 1)),
ggplot(tmp, aes(x=Text, y=NS, colour=substr(Text,1,1))) + geom_boxplot() + ylab("NS") + scale_color_discrete(name = "Text") + theme(axis.text.x = element_text(angle = 45, hjust = 1))),
cols=2);
```
```{r , echo=FALSE, message=FALSE, warning=FALSE}
cat(paste0("mean=",round(mean(tmp$NS,na.rm=TRUE),3),", ",
"median=",round(median(tmp$NS,na.rm=TRUE),3),", ",
"sd=",round(sd(tmp$NS,na.rm=TRUE),3),", ",
"CV=",round(sd(tmp$NS,na.rm=TRUE) / mean(tmp$NS,na.rm=TRUE),3),", ",
"min=",round(min(tmp$NS,na.rm=TRUE),3),", ",
"max=",round(max(tmp$NS,na.rm=TRUE),3),", ",
"kurtosis=",round(kurtosis(tmp$NS,na.rm=TRUE),3),", ",
"skewness=",round(skewness(tmp$NS,na.rm=TRUE),3),".\n"));
```
#### SR
```{r SR by Language and Text, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=10, fig.cap="SR: exploratory plots."}
multiplot(plotlist=list(ggplot(tmp, aes(x=SR)) + geom_histogram(col="yellow") + xlab("SR"),
{
# From http://mgimond.github.io/ES218/Week06a.html:
y <- quantile(tmp$SR, c(0.25, 0.75)) # Find the 1st and 3rd quartiles
x <- qnorm( c(0.25, 0.75)) # Find the matching normal values on the x-axis
slope <- diff(y) / diff(x) # Compute the line slope
int <- y[1] - slope * x[1] # Compute the line intercept
ggplot(tmp, aes(sample=SR))+stat_qq(distribution=qnorm, color="royalblue", alpha=0.5) + geom_abline(intercept=int, slope=slope, color="firebrick2")
},
ggplot(tmp, aes(x=Language, y=SR, colour=Family)) + geom_boxplot() + ylab("SR") + theme(axis.text.x = element_text(angle = 45, hjust = 1)),
ggplot(tmp, aes(x=Text, y=SR, colour=substr(Text,1,1))) + geom_boxplot() + ylab("SR") + scale_color_discrete(name = "Text") + theme(axis.text.x = element_text(angle = 45, hjust = 1))),
cols=2);
```
```{r SR by Speaker, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=5, fig.cap="SR per speaker."}
# ggplot(info.rate.data, aes(x=Speaker, y=SR, color=Language, fill=Sex)) +
# geom_boxplot() +
# scale_fill_manual(values=c("F"="white", "M"="gray50")) +
# ylab("SR") + xlab("Speakers") + theme(axis.text.x=element_blank());
ggplot(info.rate.data %>%
mutate(Speaker=reorder(Speaker, SR, median)) %>%
mutate(Speaker=reorder(Speaker, as.character(Sex), min)) %>%
mutate(Speaker=reorder(Speaker, as.character(Language), min)),
aes(x=Speaker, y=SR, color=Language, fill=Sex)) +
geom_boxplot() + #geom_vline(aes(xintercept=Language)) +
scale_fill_manual(values=c("F"="white", "M"="gray70")) +
#facet_wrap(. ~ Language, scales="free_x") +
ylab("SR") + xlab("Speakers") + theme(axis.text.x=element_blank());
```
```{r SR by Sex and Age across Languages, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=5, fig.cap="SR by Sex and Age across Languages."}
ggplot(info.rate.data,
aes(x=Age, y=SR, color=Language, shape=Sex, fill=Sex)) +
geom_point() +
geom_smooth(aes(group=Sex), method="lm", formula=y ~ poly(x,2));
```
```{r SR by Sex, Age and Language, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=8, fig.cap="SR by Sex, Age and Language."}
ggplot(info.rate.data,
aes(x=Age, y=SR, color=Sex, shape=Sex)) +
geom_point() +
geom_smooth(method="lm", formula=y ~ poly(x,2)) +
facet_wrap(. ~ Language, scales="free_x");
```
```{r SR distributions by language, echo=FALSE, message=FALSE, warning=FALSE, fig.width=7, fig.height=6, fig.cap="SR by language."}
ggplot(info.rate.data, aes(SR, fill = Language, colour = Language)) +
geom_density(alpha = 0.1) + geom_density(aes(SR), data=info.rate.data, fill=NA, color="black") + xlab("SR")
```
```{r , echo=FALSE, message=FALSE, warning=FALSE}
cat(paste0("mean=",round(mean(tmp$SR,na.rm=TRUE),3),", ",
"median=",round(median(tmp$SR,na.rm=TRUE),3),", ",
"sd=",round(sd(tmp$SR,na.rm=TRUE),3),", ",
"CV=",round(sd(tmp$SR,na.rm=TRUE) / mean(tmp$SR,na.rm=TRUE),3),", ",
"min=",round(min(tmp$SR,na.rm=TRUE),3),", ",
"max=",round(max(tmp$SR,na.rm=TRUE),3),", ",
"kurtosis=",round(kurtosis(tmp$SR,na.rm=TRUE),3),", ",
"skewness=",round(skewness(tmp$SR,na.rm=TRUE),3),".\n"));
```
#### ShE and ID
```{r ShE and ID, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=10, fig.cap="ShE and ID: exploratory plots."}
multiplot(plotlist=list(ggplot(tmp, aes(x=ShE)) + geom_histogram(col="yellow") + xlab("ShE"),
{
# From http://mgimond.github.io/ES218/Week06a.html:
y <- quantile(tmp$ShE, c(0.25, 0.75)) # Find the 1st and 3rd quartiles
x <- qnorm( c(0.25, 0.75)) # Find the matching normal values on the x-axis
slope <- diff(y) / diff(x) # Compute the line slope
int <- y[1] - slope * x[1] # Compute the line intercept
ggplot(tmp, aes(sample=ShE))+stat_qq(distribution=qnorm, color="royalblue", alpha=0.5) + geom_abline(intercept=int, slope=slope, color="firebrick2")
},
ggplot(tmp, aes(x=ID)) + geom_histogram(col="yellow") + xlab("ID"),
{
# From http://mgimond.github.io/ES218/Week06a.html:
y <- quantile(tmp$ID, c(0.25, 0.75)) # Find the 1st and 3rd quartiles
x <- qnorm( c(0.25, 0.75)) # Find the matching normal values on the x-axis
slope <- diff(y) / diff(x) # Compute the line slope
int <- y[1] - slope * x[1] # Compute the line intercept
ggplot(tmp, aes(sample=ID))+stat_qq(distribution=qnorm, color="royalblue", alpha=0.5) + geom_abline(intercept=int, slope=slope, color="firebrick2")
}),
cols=2);
```
```{r ShE vs ID, echo=FALSE, message=FALSE, warning=FALSE, fig.width=8, fig.height=6, fig.cap="ShE vs ID."}
tmp1 <- unique(info.rate.data[,c("Language","ShE","ID","Family")]);
ggplot(tmp1, aes(x=ShE, y=ID, color=Family)) +
geom_text(aes(label=Language)) + geom_smooth(method="lm", color="black") + xlab("ShE") + ylab("ID");
cor.test(tmp1$ShE, tmp1$ID, method="pearson");
cor.test(tmp1$ShE, tmp1$ID, method="spearman");
t.test(tmp1$ShE, tmp1$ID, paired=TRUE);
```
*ShE:*
```{r , echo=FALSE, message=FALSE, warning=FALSE}
cat(paste0("mean=",round(mean(tmp$ShE,na.rm=TRUE),3),", ",
"median=",round(median(tmp$ShE,na.rm=TRUE),3),", ",
"sd=",round(sd(tmp$ShE,na.rm=TRUE),3),", ",
"CV=",round(sd(tmp$ShE,na.rm=TRUE) / mean(tmp$ShE,na.rm=TRUE),3),", ",
"min=",round(min(tmp$ShE,na.rm=TRUE),3),", ",
"max=",round(max(tmp$ShE,na.rm=TRUE),3),", ",
"kurtosis=",round(kurtosis(tmp$ShE,na.rm=TRUE),3),", ",
"skewness=",round(skewness(tmp$ShE,na.rm=TRUE),3),".\n"));
```
*ID:*
```{r , echo=FALSE, message=FALSE, warning=FALSE}
cat(paste0("mean=",round(mean(tmp$ID,na.rm=TRUE),3),", ",
"median=",round(median(tmp$ID,na.rm=TRUE),3),", ",
"sd=",round(sd(tmp$ID,na.rm=TRUE),3),", ",
"CV=",round(sd(tmp$ID,na.rm=TRUE) / mean(tmp$ID,na.rm=TRUE),3),", ",
"min=",round(min(tmp$ID,na.rm=TRUE),3),", ",
"max=",round(max(tmp$ID,na.rm=TRUE),3),", ",
"kurtosis=",round(kurtosis(tmp$ID,na.rm=TRUE),3),", ",
"skewness=",round(skewness(tmp$ID,na.rm=TRUE),3),".\n"));
```
#### ShIR and IR
```{r ShIR by Language and Text, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=10, fig.cap="ShIR: exploratory plots."}
multiplot(plotlist=list(ggplot(tmp, aes(x=ShIR)) + geom_histogram(col="yellow") + xlab("ShIR"),
{
# From http://mgimond.github.io/ES218/Week06a.html:
y <- quantile(tmp$ShIR, c(0.25, 0.75)) # Find the 1st and 3rd quartiles
x <- qnorm( c(0.25, 0.75)) # Find the matching normal values on the x-axis
slope <- diff(y) / diff(x) # Compute the line slope
int <- y[1] - slope * x[1] # Compute the line intercept
ggplot(tmp, aes(sample=ShIR))+stat_qq(distribution=qnorm, color="royalblue", alpha=0.5) + geom_abline(intercept=int, slope=slope, color="firebrick2")
},
ggplot(tmp, aes(x=Language, y=ShIR, colour=Family)) + geom_boxplot() + ylab("ShIR") + theme(axis.text.x = element_text(angle = 45, hjust = 1)),
ggplot(tmp, aes(x=Text, y=ShIR, colour=substr(Text,1,1))) + geom_boxplot() + ylab("ShIRe") + scale_color_discrete(name = "Text") + theme(axis.text.x = element_text(angle = 45, hjust = 1))),
cols=2);
```
```{r ShIR by Speaker, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=5, fig.cap="ShIR per speaker."}
#ggplot(info.rate.data, aes(x=Speaker, y=ShIR, color=Language)) + geom_boxplot() + ylab("ShIR") + xlab("Speakers") + theme(axis.text.x=element_blank());
ggplot(info.rate.data %>%
mutate(Speaker=reorder(Speaker, ShIR, median)) %>%
mutate(Speaker=reorder(Speaker, as.character(Sex), min)) %>%
mutate(Speaker=reorder(Speaker, as.character(Language), min)),
aes(x=Speaker, y=ShIR, color=Language, fill=Sex)) +
geom_boxplot() + #geom_vline(aes(xintercept=Language)) +
scale_fill_manual(values=c("F"="white", "M"="gray70")) +
#facet_wrap(. ~ Language, scales="free_x") +
ylab("ShIR") + xlab("Speakers") + theme(axis.text.x=element_blank());
```
```{r ShIR by Sex and Age across Languages, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=5, fig.cap="ShIR by Sex and Age across Languages."}
ggplot(info.rate.data,
aes(x=Age, y=ShIR, color=Language, shape=Sex, fill=Sex)) +
geom_point() +
geom_smooth(aes(group=Sex), method="lm", formula=y ~ poly(x,2));
```
```{r ShIR by Sex, Age and Language, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=8, fig.cap="ShIR by Sex, Age and Language."}
ggplot(info.rate.data,
aes(x=Age, y=ShIR, color=Sex, shape=Sex)) +
geom_point() +
geom_smooth(method="lm", formula=y ~ poly(x,2)) +
facet_wrap(. ~ Language, scales="free_x");
```
```{r ShIR distributions by language, echo=FALSE, message=FALSE, warning=FALSE, fig.width=7, fig.height=6, fig.cap="ShIR by language."}
ggplot(info.rate.data, aes(ShIR, fill = Language, colour = Language)) +
geom_density(alpha = 0.1) + geom_density(aes(ShIR), data=info.rate.data, fill=NA, color="black") + xlab("ShIR")
```
```{r IR by Language and Text, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=10, fig.cap="IR: exploratory plots."}
multiplot(plotlist=list(ggplot(tmp, aes(x=IR)) + geom_histogram(col="yellow") + xlab("IR"),
{
# From http://mgimond.github.io/ES218/Week06a.html:
y <- quantile(tmp$IR, c(0.25, 0.75)) # Find the 1st and 3rd quartiles
x <- qnorm( c(0.25, 0.75)) # Find the matching normal values on the x-axis
slope <- diff(y) / diff(x) # Compute the line slope
int <- y[1] - slope * x[1] # Compute the line intercept
ggplot(tmp, aes(sample=IR))+stat_qq(distribution=qnorm, color="royalblue", alpha=0.5) + geom_abline(intercept=int, slope=slope, color="firebrick2")
},
ggplot(tmp, aes(x=Language, y=IR, colour=Family)) + geom_boxplot() + ylab("IR") + theme(axis.text.x = element_text(angle = 45, hjust = 1)),
ggplot(tmp, aes(x=Text, y=IR, colour=substr(Text,1,1))) + geom_boxplot() + ylab("IRe") + scale_color_discrete(name = "Text") + theme(axis.text.x = element_text(angle = 45, hjust = 1))),
cols=2);
```
```{r IR by Speaker, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=5, fig.cap="IR per speaker."}
#ggplot(info.rate.data, aes(x=Speaker, y=IR, color=Language)) + geom_boxplot() + ylab("IR") + xlab("Speakers") + theme(axis.text.x=element_blank());
ggplot(info.rate.data %>%
mutate(Speaker=reorder(Speaker, IR, median)) %>%
mutate(Speaker=reorder(Speaker, as.character(Sex), min)) %>%
mutate(Speaker=reorder(Speaker, as.character(Language), min)),
aes(x=Speaker, y=IR, color=Language, fill=Sex)) +
geom_boxplot() + #geom_vline(aes(xintercept=Language)) +
scale_fill_manual(values=c("F"="white", "M"="gray70")) +
#facet_wrap(. ~ Language, scales="free_x") +
ylab("IR") + xlab("Speakers") + theme(axis.text.x=element_blank());
```
```{r IR by Sex and Age across Languages, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=5, fig.cap="IR by Sex and Age across Languages."}
ggplot(info.rate.data,
aes(x=Age, y=IR, color=Language, shape=Sex, fill=Sex)) +
geom_point() +
geom_smooth(aes(group=Sex), method="lm", formula=y ~ poly(x,2));
```
```{r IR by Sex, Age and Language, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=8, fig.cap="IR by Sex, Age and Language."}
ggplot(info.rate.data,
aes(x=Age, y=IR, color=Sex, shape=Sex)) +
geom_point() +
geom_smooth(method="lm", formula=y ~ poly(x,2)) +
facet_wrap(. ~ Language, scales="free_x");
```
```{r IR distributions by language, echo=FALSE, message=FALSE, warning=FALSE, fig.width=7, fig.height=6, fig.cap="IR by language."}
ggplot(info.rate.data, aes(IR, fill = Language, colour = Language)) +
geom_density(alpha = 0.1) + geom_density(aes(IR), data=info.rate.data, fill=NA, color="black") + xlab("IR")
```
*ShIR:*
```{r , echo=FALSE, message=FALSE, warning=FALSE}
cat(paste0("mean=",round(mean(tmp$ShIR,na.rm=TRUE),3),", ",
"median=",round(median(tmp$ShIR,na.rm=TRUE),3),", ",
"sd=",round(sd(tmp$ShIR,na.rm=TRUE),3),", ",
"CV=",round(sd(tmp$ShIR,na.rm=TRUE) / mean(tmp$ShIR,na.rm=TRUE),3),", ",
"min=",round(min(tmp$ShIR,na.rm=TRUE),3),", ",
"max=",round(max(tmp$ShIR,na.rm=TRUE),3),", ",
"kurtosis=",round(kurtosis(tmp$ShIR,na.rm=TRUE),3),", ",
"skewness=",round(skewness(tmp$ShIR,na.rm=TRUE),3),".\n"));
```
*IR:*
```{r , echo=FALSE, message=FALSE, warning=FALSE}
cat(paste0("mean=",round(mean(tmp$IR,na.rm=TRUE),3),", ",
"median=",round(median(tmp$IR,na.rm=TRUE),3),", ",
"sd=",round(sd(tmp$IR,na.rm=TRUE),3),", ",
"CV=",round(sd(tmp$IR,na.rm=TRUE) / mean(tmp$IR,na.rm=TRUE),3),", ",
"min=",round(min(tmp$IR,na.rm=TRUE),3),", ",
"max=",round(max(tmp$IR,na.rm=TRUE),3),", ",
"kurtosis=",round(kurtosis(tmp$IR,na.rm=TRUE),3),", ",
"skewness=",round(skewness(tmp$IR,na.rm=TRUE),3),".\n"));
```
#### SR and ID
```{r SR vs ID, echo=FALSE, message=FALSE, warning=FALSE, fig.width=8, fig.height=6, fig.cap="SR vs ID"}
ggplot(info.rate.data, aes(y=SR, x=ID, color=Family)) + ylim(range(info.rate.data$SR)+c(0.0,0.5)) +
geom_point(alpha=0.5, size=1) +
geom_smooth(aes(y=SR, x=ID), method="lm", color="yellow") +
geom_smooth(aes(y=SR, x=ID), method="loess", color="black") +
geom_text(aes(x=ID, label=Language), data=unique(info.rate.data[,c("ID","Language","Family")]), y=max(info.rate.data$SR), angle=90, hjust=-0.30, size=3, show.legend = FALSE) +
ylab("SR") + xlab("ID");
cor.test(info.rate.data$SR, info.rate.data$ID, method="pearson");
cor.test(info.rate.data$SR, info.rate.data$ID, method="spearman");
```
## Linear Mixed Models (LMMs)
### Linear ICCs
```{r echo=FALSE, message=FALSE, warning=FALSE}
tmp1 <- icc(lmer(NS ~ 1 + (1 | Text) + (1 | Family/Language) + (1 | Speaker), data=info.rate.data))
knitr::kable(data.frame("f"=c("Text", "Family", "Language", "Speaker"), "ICC"=c(tmp1["Text"], tmp1["Family"], tmp1["Language:Family"], tmp1["Speaker"])),
digits=2, row.names=FALSE, col.names=c("Level-1 factor (f)", "ICC"), caption="ICC for model `NS ~ 1 + (1 | Text) + (1 | Family/Language)+ (1 | Speaker)`.");
tmp1 <- icc(lmer(SR ~ 1 + (1 | Text) + (1 | Family/Language) + (1 | Speaker), data=info.rate.data))
knitr::kable(data.frame("f"=c("Text", "Family", "Language", "Speaker"), "ICC"=c(tmp1["Text"], tmp1["Family"], tmp1["Language:Family"], tmp1["Speaker"])),
digits=2, row.names=FALSE, col.names=c("Level-1 factor (f)", "ICC"), caption="ICC for model `SR ~ 1 + (1 | Text) + (1 | Family/Language)+ (1 | Speaker)`.");
tmp1 <- icc(lmer(ShIR ~ 1 + (1 | Text) + (1 | Family/Language) + (1 | Speaker), data=info.rate.data))
knitr::kable(data.frame("f"=c("Text", "Family", "Language", "Speaker"), "ICC"=c(tmp1["Text"], tmp1["Family"], tmp1["Language:Family"], tmp1["Speaker"])),
digits=2, row.names=FALSE, col.names=c("Level-1 factor (f)", "ICC"), caption="ICC for model `ShIR ~ 1 + (1 | Text) + (1 | Family/Language)+ (1 | Speaker)`.");
tmp1 <- icc(lmer(IR ~ 1 + (1 | Text) + (1 | Family/Language) + (1 | Speaker), data=info.rate.data))
knitr::kable(data.frame("f"=c("Text", "Family", "Language", "Speaker"), "ICC"=c(tmp1["Text"], tmp1["Family"], tmp1["Language:Family"], tmp1["Speaker"])),
digits=2, row.names=FALSE, col.names=c("Level-1 factor (f)", "ICC"), caption="ICC for model `IR ~ 1 + (1 | Text) + (1 | Family/Language)+ (1 | Speaker)`.");
```
### SR
#### Comparing models
```{r SR_lmm, echo=F}
lmr0 <- lmer(SR ~ 1 + (1 | Text) + (1 | Family/Language) + (1 | Speaker), data=info.rate.data);
lmr1 <- lmer(SR ~ 1 + (1 | Family/Language) + (1 | Speaker), data=info.rate.data);
lmr2 <- lmer(SR ~ 1 + (1 | Text) + (1 | Speaker), data=info.rate.data);
lmr3 <- lmer(SR ~ 1 + (1 | Text) + (1 | Family/Language), data=info.rate.data);
lmr4 <- lmer(SR ~ 1 + Sex + (1 | Text) + (1 | Family/Language) + (1 | Speaker), data=info.rate.data);
lmr5 <- lmer(SR ~ 1 + Sex + (1 | Family/Language) + (1 | Speaker), data=info.rate.data);
lmr6 <- lmer(SR ~ 1 + Sex + (1 | Text) + (1 | Speaker), data=info.rate.data);
lmr7 <- lmer(SR ~ 1 + Sex + (1 | Text) + (1 | Family/Language), data=info.rate.data);
tmp <- do.call(rbind, lapply(list(lmr0, lmr1, lmr2, lmr3, lmr4, lmr5, lmr6, lmr7),
function(lmr) data.frame("model"=as.character(summary(lmr, ddf="lme4")$call$formula)[3], "AIC"=round(AIC(lmr),2), "BIC"=round(BIC(lmr),2))));
tmp$AIC[ which.min(tmp$AIC) ] <- paste0("**",tmp$AIC[ which.min(tmp$AIC) ],"**");
tmp$BIC[ which.min(tmp$BIC) ] <- paste0("**",tmp$BIC[ which.min(tmp$BIC) ],"**");
knitr::kable(tmp, row.names=FALSE, caption="AIC and BIC for various hierarchical models of `SR` (in **bold** are the minimum values).");
```
#### Residuals and random effects
We consider here the full model `SR ~ 1 + Sex + (1|Text) + (1|Family/Language) + (1|Speaker)`.
```{r SR_assumptions, echo=F}
lmr <- lmer(SR ~ 1 + Sex + (1|Text) + (1|Family/Language) + (1|Speaker), data=info.rate.data)
# summary(lmr)
plot(lmr)
get_re_distrib(residuals(lmr), "Residuals")
get_re_distrib(ranef(lmr)$Text[,1], "Levels of Text as random effect")
get_re_distrib(ranef(lmr)$Language[,1], "Levels of Language as random effect")
get_re_distrib(ranef(lmr)$Family[,1], "Levels of Family as random effect")
get_re_distrib(ranef(lmr)$Speaker[,1], "Levels of Speaker as random effect")
# sjp.lmer(lmr0,
# facet.grid = FALSE,
# sort.est = "sort.all",
# y.offset = .4)
#plot_model(lmr,
# facet.grid = FALSE,
# sort.est = "sort.all",
# y.offset = .4)
```
### IR
#### Comparing models
```{r IR_lmm, echo=F}
lmr0 <- lmer(IR ~ 1 + (1 | Text) + (1 | Family/Language) + (1 | Speaker), data=info.rate.data);
lmr1 <- lmer(IR ~ 1 + (1 | Family/Language) + (1 | Speaker), data=info.rate.data);
lmr2 <- lmer(IR ~ 1 + (1 | Text) + (1 | Speaker), data=info.rate.data);
lmr3 <- lmer(IR ~ 1 + (1 | Text) + (1 | Family/Language), data=info.rate.data);
lmr4 <- lmer(IR ~ 1 + Sex + (1 | Text) + (1 | Family/Language) + (1 | Speaker), data=info.rate.data);
lmr5 <- lmer(IR ~ 1 + Sex + (1 | Family/Language) + (1 | Speaker), data=info.rate.data);
lmr6 <- lmer(IR ~ 1 + Sex + (1 | Text) + (1 | Speaker), data=info.rate.data);
lmr7 <- lmer(IR ~ 1 + Sex + (1 | Text) + (1 | Family/Language), data=info.rate.data);
tmp <- do.call(rbind, lapply(list(lmr0, lmr1, lmr2, lmr3, lmr4, lmr5, lmr6, lmr7),
function(lmr) data.frame("model"=as.character(summary(lmr, ddf="lme4")$call$formula)[3], "AIC"=round(AIC(lmr),2), "BIC"=round(BIC(lmr),2))));
tmp$AIC[ which.min(tmp$AIC) ] <- paste0("**",tmp$AIC[ which.min(tmp$AIC) ],"**");
tmp$BIC[ which.min(tmp$BIC) ] <- paste0("**",tmp$BIC[ which.min(tmp$BIC) ],"**");
knitr::kable(tmp, row.names=FALSE, caption="AIC and BIC for various hierarchical models of `IR` (in **bold** are the minimum values).");
```
#### Residuals and random effects
We consider here the full model `IR ~ 1 + Sex + (1|Text) + (1|Family/Language) + (1|Speaker)`.
```{r IR_assumptions, echo=F}
lmr <- lmer(IR ~ 1 + Sex + (1|Text) + (1|Family/Language) + (1|Speaker), data=info.rate.data)
#summary(lmr)
plot(lmr)
get_re_distrib(residuals(lmr), "Residuals")
get_re_distrib(ranef(lmr)$Text[,1], "Levels of Text as random effect")
get_re_distrib(ranef(lmr)$Language[,1], "Levels of Language as random effect")
get_re_distrib(ranef(lmr)$Family[,1], "Levels of Family as random effect")
get_re_distrib(ranef(lmr)$Speaker[,1], "Levels of Speaker as random effect")
# sjp.lmer(lmr0,
# facet.grid = FALSE,
# sort.est = "sort.all",
# y.offset = .4)
#plot_model(lmr,
# facet.grid = FALSE,
# sort.est = "sort.all",
# y.offset = .4)
```
## Generalized Additive Models for Location, Scale and Shape (GAMLSS)
We will use a Gaussian distribution (with fixed or modelled variance).
### SR
#### Fixed or modelled *σ* (variance)
##### Fixed *σ*
```{r SR_check_NO_no_sigma, echo=FALSE}
d <- info.rate.data[,c("SR","Sex","Text","Language","Family","Speaker")]; # use only the columns we actually need (avoid errors about NAs in other columns)
model.NO.no.modelled.sigma.SR <- gamlss(SR ~ 1 + Sex + random(Text) + random(Language) + random(Family) + random(Speaker),
data=d,
family=NO(mu.link="identity"),
control = gamlss.control(n.cyc = 800, trace = FALSE), i.control=glim.control(bf.cyc = 800))
plot(model.NO.no.modelled.sigma.SR)
wp(model.NO.no.modelled.sigma.SR, ylim.all=2)
cat("\nDeviance=",deviance(model.NO.no.modelled.sigma.SR, what = "G"),"\n");
cat("\nAIC=",AIC(model.NO.no.modelled.sigma.SR),"\n");
```
##### Modelled *σ*
```{r SR_check_NO, echo=FALSE}
d <- info.rate.data[,c("SR","Sex","Text","Language","Family","Speaker")]; # use only the columns we actually need (avoid errors about NAs in other columns)
model.NO.SR <- gamlss(SR ~ 1 + Sex + random(Text) + random(Language) + random(Family) + random(Speaker),
sigma.fo = ~ 1 + Sex + random(Text) + random(Language) + random(Family) + random(Speaker),
data=d,
family=NO(mu.link="identity"),
control = gamlss.control(n.cyc = 800, trace = FALSE), i.control=glim.control(bf.cyc = 800))
plot(model.NO.SR)
wp(model.NO.SR, ylim.all=0.4)
cat("\nDeviance=",deviance(model.NO.SR, what = "G"),"\n");
cat("\nAIC=",AIC(model.NO.SR),"\n");
```
The distribution of the residuals is less heteroscedastic than before and the fit to the data better.
The full summary of the model is:
```{r SR_summary, echo=FALSE}
summary(model.NO.SR)
```
#### Random effects
##### *μ*
**Text**
```{r SR_re_mu_text, echo=FALSE}
getSmo(model.NO.SR, "mu", which = 1)
```
**Language**
```{r SR_re_mu_lg, echo=FALSE}
getSmo(model.NO.SR, "mu", which = 2)
```
**Family**
```{r SR_re_mu_fam, echo=FALSE}
getSmo(model.NO.SR, "mu", which = 3)
```
**Speaker**
```{r SR_re_mu_spk, echo=FALSE}
getSmo(model.NO.SR, "mu", which = 4)
```
##### *σ*
**Text**
```{r SR_re_sigma_text, echo=FALSE}
getSmo(model.NO.SR, "sigma", which = 1)
```
**Language**
```{r SR_re_sigma_lg, echo=FALSE}
getSmo(model.NO.SR, "sigma", which = 2)
```
**Family**
```{r SR_re_sigma_fam, echo=FALSE}
getSmo(model.NO.SR, "sigma", which = 3)
```
**Speaker**
```{r SR_re_sigma_spk, echo=FALSE}
getSmo(model.NO.SR, "sigma", which = 4)
```
### IR
#### Fixed or modelled *σ* (variance)
##### Fixed *σ*
```{r IR_check_NO_no_sigma, echo=FALSE}
d <- info.rate.data[,c("IR","Sex","Text","Language","Family","Speaker")]; # use only the columns we actually need (avoid errors about NAs in other columns)
model.NO.no.modelled.sigma.IR <- gamlss(IR ~ 1 + Sex + random(Text) + random(Language) + random(Family) + random(Speaker),
data=d,
family=NO(mu.link="identity"),
control = gamlss.control(n.cyc = 800, trace = FALSE), i.control=glim.control(bf.cyc = 800))
plot(model.NO.no.modelled.sigma.IR)
wp(model.NO.no.modelled.sigma.IR, ylim.all=0.4)
cat("\nDeviance=",deviance(model.NO.no.modelled.sigma.IR, what = "G"),"\n");
cat("\nAIC=",AIC(model.NO.no.modelled.sigma.IR),"\n");
```
##### Modelled *σ*
```{r IR_check_NO, echo=FALSE}
d <- info.rate.data[,c("IR","Sex","Text","Language","Family","Speaker")]; # use only the columns we actually need (avoid errors about NAs in other columns)
model.NO.IR <- gamlss(IR ~ 1 + Sex + random(Text) + random(Language) + random(Family) + random(Speaker),
sigma.fo = ~ 1 + Sex + random(Text) + random(Language) + random(Family) + random(Speaker),
data=d,
family=NO(mu.link="identity"),
control = gamlss.control(n.cyc = 800, trace = FALSE), i.control=glim.control(bf.cyc = 800))
plot(model.NO.IR)
wp(model.NO.IR, ylim.all=0.4)
cat("\nDeviance=",deviance(model.NO.IR, what = "G"),"\n");
cat("\nAIC=",AIC(model.NO.IR),"\n");
```
Again, this is a better fit to the data.
The full summary of the model is:
```{r IR_summary, echo=FALSE}
summary(model.NO.IR)
```
#### Random effects
##### *μ*
**Text**
```{r IR_re_mu_text, echo=FALSE}
getSmo(model.NO.IR, "mu", which = 1)
```
**Language**
```{r IR_re_mu_lg, echo=FALSE}
getSmo(model.NO.IR, "mu", which = 2)
```
**Family**
```{r IR_re_mu_fam, echo=FALSE}
getSmo(model.NO.IR, "mu", which = 3)
```
**Speaker**
```{r IR_re_mu_spk, echo=FALSE}
getSmo(model.NO.IR, "mu", which = 4)
```
##### *σ*
**Text**
```{r IR_re_sigma_text, echo=FALSE}
getSmo(model.NO.IR, "sigma", which = 1)
```
**Language**
```{r IR_re_sigma_lg, echo=FALSE}
getSmo(model.NO.IR, "sigma", which = 2)
```
**Family**
```{r IR_re_sigma_fam, echo=FALSE}
getSmo(model.NO.IR, "sigma", which = 3)
```
**Speaker**
```{r IR_re_sigma_spk, echo=FALSE}
getSmo(model.NO.IR, "sigma", which = 4)
```
## Modelling the relationship between SR and ID
Let's model *SR* with *ID* as an additional predictor (fixed effect) interacting with *Sex*.
*N.B.* In this case, we must drop *Language* as a random effect, since each language has, by definition, only one value of *ID.*
```{r SR_ID_gamlss, echo=FALSE}
d <- info.rate.data[,c("SR","ID","Sex","Text","Family","Speaker")]; # use only the columns we actually need (avoid errors about NAs in other columns)
model.NO.SR.ID <- gamlss(SR ~ 1 + ID * Sex + random(Text) + random(Speaker) + random(Family),
sigma.fo = ~ 1 + ID + Sex + random(Text) + random(Speaker) + random(Family),
data=d,
family=NO(mu.link="identity"),
control = gamlss.control(n.cyc = 800, trace = FALSE), i.control=glim.control(bf.cyc = 800))
summary(model.NO.SR.ID)
plot(model.NO.SR.ID)
wp(model.NO.SR.ID, ylim.all=0.5)
cat("\nDeviance=",deviance(model.NO.SR.ID, what = "G"),"\n");
cat("\nAIC=",AIC(model.NO.SR.ID),"\n");
```
Adding *ID* as a predictor improves the fits (as judged by AIC).
There is a negative estimate for ID, but significance is difficult to assess with GAMLSS model involving smoothing functions.
However, also using a simple `lmer` model we have a significant effect of *ID*:
```{r SR_ID_lmer, echo=FALSE}
lmer.SR.ID <- lmer(SR ~ 1 + ID * Sex + (1 | Text) + (1 | Speaker) + (1 | Family), data=info.rate.data)
summary(lmer.SR.ID)
anova(lmer.SR.ID)
```
### Random effects
#### *μ*