-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPCA_PC1_BAAP.Rmd
1734 lines (1439 loc) · 68.5 KB
/
FPCA_PC1_BAAP.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: "Using Functional PCA for articulatory dynamics: L1 Japanese speakers’ production of English liquids (BAAP2024)"
author: "Takayuki Nagamine"
date: "`r Sys.Date()`"
output:
html_document:
toc: true
number_sections: true
---
This document outlines functional Principal Component analysis on dynamic changes on the **PC1** scores from the PCA analysis.
# Preliminaries
## loading packages and machine setting
```{r message=FALSE}
library(fdapace)
library(ggplot2)
library(tidyverse)
library(brms)
library(scales)
library(grid)
library(gridExtra)
library(ggpubr)
library(ggsci)
library(emuR)
library(emmeans)
theme_set(theme_classic())
options(mc.cores = parallel::detectCores())
```
## loading data
```{r}
# Tongue spline tracking data
load(file = "data/int.350.rda")
# Participant info
load(file = "data/par.rda")
```
## participant info
```{r}
# select participants that are included in the analysis
sp <- int.350 %>%
select(speaker)
sp <- merge(sp, par, by.x = "speaker")
# N of speakers by L1
sp %>%
group_by(L1) %>%
summarise(n = n_distinct(speaker)) %>%
ungroup()
# Country
sp %>%
group_by(L1, country) %>%
summarise(n = n_distinct(speaker)) %>%
ungroup()
# fluency rating
sp %>%
mutate(
primary_lang = case_when(
L1 == "Japanese" ~ "Japanese",
TRUE ~ "English"
)
) %>%
group_by(primary_lang) %>%
summarise(
mean_fluency = mean(fluency),
sd_fluency = sd(fluency),
mean_use = mean(use),
sd_use = sd(use),
mean_familiarity = mean(familiarity),
sd_familiarity = sd(familiarity)
) %>%
ungroup()
## Japanese data
sp.jp <- sp %>%
filter(L1 == "Japanese")
## English study
sp.jp %>%
rename(
overseas = `overseas (month: 1wk = 0.25m)`
) %>%
summarise(mean_study = mean(as.numeric(English_study)),
sd_study = sd(as.numeric(English_study)),
mean_month_overseas = mean(as.numeric(overseas)),
sd_month_overseas = sd(as.numeric(overseas)))
```
## some descriptive statistics
```{r}
# matching tongue data with participant info
int.350 <- merge(int.350, par, by.x = "speaker", by.y = "speaker") %>%
select(-number, -L1.y) %>%
rename(
L1 = L1.x
)
# N of speaker = 43
int.350 %>%
group_by(L1) %>%
summarise(speaker = n_distinct(speaker),
mean_age = mean(age),
sd_age = sd(age)) %>%
ungroup()
# N of prompts
int.350 %>%
group_by(L1, segment, vowel) %>%
summarise(n = n_distinct(exclude_key)) %>%
ungroup()
int.350 %>%
group_by(L1, segment) %>%
summarise(n = n_distinct(exclude_key)) %>%
ungroup()
```
# analysis 1: principal component analysis
## running PCA
```{r}
# PCA: -350ms onset ----------------------------------------------------------------
## Data Preparation
int.350 <- int.350 %>%
select(speaker:country)
### Convert data frame into a PCA-friendly format
int.350.xy <- int.350 %>%
group_by(speaker) %>%
mutate(
X_z = scale(X),
Y_z = scale(Y)
) %>%
ungroup() %>%
dplyr::select(-X, -Y) %>%
pivot_wider(
names_from = point_number,
values_from = c(X_z, Y_z)
)
### Check 1
# int.350.xy %>%
# filter(speaker == "3wy8us",
# prompt == "ram",
# repetition == "1") %>%
# group_by(speaker, prompt, repetition, time, interval_350, phone) %>%
# summarise() %>%
# ungroup() %>%
# print(n = Inf)
### Check 2
# int.350.xy %>%
# filter(speaker == "2d57ke") %>%
# group_by(speaker, prompt, repetition) %>%
# summarise() %>%
# ungroup() %>%
# print(n = Inf)
### Check 3
# int.350.xy %>%
# group_by(speaker) %>%
# summarise() %>%
# ungroup() %>%
# print(n = Inf)
### check column names
# colnames(int.350.xy)
### Remove and save meta data
int.350.pca <- int.350.xy %>%
dplyr::select(-speaker, -rec_date, -time, -prompt, -L1, -frame_number, -spline_number, -repetition, -segment, -phone, -position, -interval_350, -vowel, -exclude_key, -start_time, -end_time, -total_duration, -proportional_time, -vowel_start, -vowel_start_prop, -acoustic_start, -acoustic_start_prop, -gender, -age, -country)
# remove the meta data that we don't need for PCA
meta.350 <- int.350.xy %>%
dplyr::select(speaker, rec_date, time, prompt, L1, frame_number, spline_number, repetition, segment, phone, position, interval_350, vowel, exclude_key, start_time, end_time, total_duration, proportional_time, vowel_start, vowel_start_prop, acoustic_start, acoustic_start_prop, gender, age, country)
# separate and save the meta data for later
### Check if there are any NAs
table(is.na(int.350.pca))
# int.350.pca <- drop_na(int.350.pca) # run this only when the table(is.na()) returns TRUE values
### Run PCA on all the liquid tokens
pca.350 <- princomp(int.350.pca)
summary(pca.350)
# Summarise to see how much variation each PCA accounts for
```
## variation explained by each PC
```{r}
### Plotting the variance explained (optional)
var.explained.350 <- pca.350$sdev^2 / sum(pca.350$sdev^2)
# making var_explained as a tibble and add colname
var.explained.350 <- as_tibble(var.explained.350)
var.explained.350 <- var.explained.350 %>%
as_tibble() %>%
mutate(
PC = row_number()
)
# create a plot
var.explained.350.PC10 <- var.explained.350 %>%
filter(PC < 11) # only plot PC10 or below
var.plot.350 <- var.explained.350.PC10 %>%
ggplot(mapping = aes(x = PC, y = value)) +
geom_line() +
geom_text(data = subset(var.explained.350.PC10, PC < 5), aes(label = round(value, digits = 5)), nudge_x = 0.8) +
geom_label(data = subset(var.explained.350.PC10, PC < 5), aes(label = PC), label.padding = unit(0.40, "lines")) +
geom_point(data = subset(var.explained.350.PC10, PC > 5)) +
geom_hline(yintercept = 0.05, linetype = 'dotted') +
xlab("Principal Component") +
ylab("Variance Explained") +
ggtitle("Proportion of Variance explained by each PC") +
# ylim(0, 0.6) +
theme_classic() +
theme(plot.title = element_text(size = 18, face = "bold"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 15),
strip.text.x = element_text(size = 15),
strip.text.y = element_text(size = 15, angle = 0),
legend.text = element_text(size = 12),
legend.position = "bottom",
legend.key.width = unit(3, "cm")
# legend.title = element_blank()
)
var.plot.350
# the plot shows that the first 4 PCs account for more than 5% of the variation in the data
ggsave(var.plot.350, filename = "figure/varplot_350ms.png", width = 10, height = 5, dpi = 1000)
```
## preparing for PCA plot
```{r}
## Preparing PCA plots
# Get the results of the PCA which are useful
pca.number.350 <- pca.350$scores
# Put it into a sensible format as some variables come out weird
pca.number.350 <- as_tibble(pca.number.350)
# Combine with non-numeric information from earlier
pca.result.350 <- cbind(meta.350, pca.number.350)
# normalise PCs by speaker for comparison
pca.result.350 = pca.result.350 %>%
group_by(speaker) %>%
mutate(
PC1z = scale(Comp.1),
PC2z = scale(Comp.2),
PC3z = scale(Comp.3),
PC4z = scale(Comp.4)
)
## Work out parameters of variation in first 3 PCs
# Mean values from the output of the PCA
mean.pca.350 <- tibble::enframe(pca.350$center)
## make subsettable variable
mean.pca.350 <- mean.pca.350 %>%
mutate(axis = substr(mean.pca.350$name, 1, 1))
## subset data to make into a matrix of x and y values
X <- subset(mean.pca.350, mean.pca.350$axis == 'X')
Y <- subset(mean.pca.350, mean.pca.350$axis == 'Y')
mean.pca.350 <- cbind(X, Y)
## changing colnames
colnames(mean.pca.350) = c("number1", "mean.x", "axis1", "number2", "mean.y", "axis2")
## get loadings - eigenvectors
loadings.350 <- as.table(pca.350$loadings)
# PC1 ---------------------------------------------------------------------
## get loadings for PC1 in a sensible format
PC1.l.350 <- as.data.frame(loadings.350) %>%
filter(Var2 == "Comp.1")
PC1.l.350 <- PC1.l.350 %>%
mutate(axis = substr(PC1.l.350$Var1, 1, 1))
PC1.l.350.x <- subset(PC1.l.350, PC1.l.350$axis == 'X')
PC1.l.350.y <- subset(PC1.l.350, PC1.l.350$axis == 'Y')
PC1.l.350 <- cbind(PC1.l.350.x, PC1.l.350.y)
colnames(PC1.l.350) = c("useless", "useless2", "PC1.l.350.x", "useless3", "useless4", "useless5", "PC1.l.350.y", "useless6")
PC1.l.350$useless <- NULL
PC1.l.350$useless2 <- NULL
PC1.l.350$useless3 <- NULL
PC1.l.350$useless4 <- NULL
PC1.l.350$useless5 <- NULL
PC1.l.350$useless6 <- NULL
# PC2 ---------------------------------------------------------------------
## get loadings for PC1 in a sensible format
PC2.l.350 <- as.data.frame(loadings.350) %>%
filter(Var2 == "Comp.2")
PC2.l.350 <- PC2.l.350 %>%
mutate(axis = substr(PC2.l.350$Var1, 1, 1))
PC2.l.350.x <- subset(PC2.l.350, PC2.l.350$axis == 'X')
PC2.l.350.y <- subset(PC2.l.350, PC2.l.350$axis == 'Y')
PC2.l.350 <- cbind(PC2.l.350.x, PC2.l.350.y)
colnames(PC2.l.350) = c("useless", "useless2", "PC2.l.350.x", "useless3", "useless4", "useless5", "PC2.l.350.y", "useless6")
PC2.l.350$useless <- NULL
PC2.l.350$useless2 <- NULL
PC2.l.350$useless3 <- NULL
PC2.l.350$useless4 <- NULL
PC2.l.350$useless5 <- NULL
PC2.l.350$useless6 <- NULL
# PC3 ---------------------------------------------------------------------
## get loadings for PC3 in a sensible format
PC3.l.350 <- as.data.frame(loadings.350) %>%
filter(Var2 == "Comp.3")
PC3.l.350 <- PC3.l.350 %>%
mutate(axis = substr(PC3.l.350$Var1, 1, 1))
PC3.l.350.x <- subset(PC3.l.350, PC3.l.350$axis == 'X')
PC3.l.350.y <- subset(PC3.l.350, PC3.l.350$axis == 'Y')
PC3.l.350 <- cbind(PC3.l.350.x, PC3.l.350.y)
colnames(PC3.l.350) = c("useless", "useless2", "PC3.l.350.x", "useless3", "useless4", "useless5", "PC3.l.350.y", "useless6")
PC3.l.350$useless <- NULL
PC3.l.350$useless2 <- NULL
PC3.l.350$useless3 <- NULL
PC3.l.350$useless4 <- NULL
PC3.l.350$useless5 <- NULL
PC3.l.350$useless6 <- NULL
# PC4 ---------------------------------------------------------------------
## get loadings for PC4 in a sensible format
PC4.l.350 <- as.data.frame(loadings.350) %>%
filter(Var2 == "Comp.4")
PC4.l.350 <- PC4.l.350 %>%
mutate(axis = substr(PC4.l.350$Var1, 1, 1))
PC4.l.350.x <- subset(PC4.l.350, PC4.l.350$axis == 'X')
PC4.l.350.y <- subset(PC4.l.350, PC4.l.350$axis == 'Y')
PC4.l.350 <- cbind(PC4.l.350.x, PC4.l.350.y)
colnames(PC4.l.350) = c("useless", "useless2", "PC4.l.350.x", "useless3", "useless4", "useless5", "PC4.l.350.y", "useless6")
PC4.l.350$useless <- NULL
PC4.l.350$useless2 <- NULL
PC4.l.350$useless3 <- NULL
PC4.l.350$useless4 <- NULL
PC4.l.350$useless5 <- NULL
PC4.l.350$useless6 <- NULL
## Plotting the meaning of PCs
# bind together all of the above
loadings.350 <- cbind(PC1.l.350, PC2.l.350, PC3.l.350, PC4.l.350)
# get sds of first 4 PCs
sd.350 <- tibble::enframe(pca.350$sdev)
sd_PC1.350 <- as.numeric(sd.350[1,2])
sd_PC2.350 <- as.numeric(sd.350[2,2])
sd_PC3.350 <- as.numeric(sd.350[3,2])
sd_PC4.350 <- as.numeric(sd.350[4,2])
# calculate estimated values including sd
# midpoint model
estimate.350 <- cbind(mean.pca.350, loadings.350)
estimate.350$PC1.max.x <- estimate.350$mean.x + sd_PC1.350*estimate.350$PC1.l.350.x
estimate.350$PC1.min.x <- estimate.350$mean.x - sd_PC1.350*estimate.350$PC1.l.350.x
estimate.350$PC1.max.y <- estimate.350$mean.y + sd_PC1.350*estimate.350$PC1.l.350.y
estimate.350$PC1.min.y <- estimate.350$mean.y - sd_PC1.350*estimate.350$PC1.l.350.y
estimate.350$PC2.max.x <- estimate.350$mean.x + sd_PC2.350*estimate.350$PC2.l.350.x
estimate.350$PC2.min.x <- estimate.350$mean.x - sd_PC2.350*estimate.350$PC2.l.350.x
estimate.350$PC2.max.y <- estimate.350$mean.y + sd_PC2.350*estimate.350$PC2.l.350.y
estimate.350$PC2.min.y <- estimate.350$mean.y - sd_PC2.350*estimate.350$PC2.l.350.y
estimate.350$PC3.max.x <- estimate.350$mean.x + sd_PC3.350*estimate.350$PC3.l.350.x
estimate.350$PC3.min.x <- estimate.350$mean.x - sd_PC3.350*estimate.350$PC3.l.350.x
estimate.350$PC3.max.y <- estimate.350$mean.y + sd_PC3.350*estimate.350$PC3.l.350.y
estimate.350$PC3.min.y <- estimate.350$mean.y - sd_PC3.350*estimate.350$PC3.l.350.y
estimate.350$PC4.max.x <- estimate.350$mean.x + sd_PC4.350*estimate.350$PC4.l.350.x
estimate.350$PC4.min.x <- estimate.350$mean.x - sd_PC4.350*estimate.350$PC4.l.350.x
estimate.350$PC4.max.y <- estimate.350$mean.y + sd_PC4.350*estimate.350$PC4.l.350.y
estimate.350$PC4.min.y <- estimate.350$mean.y - sd_PC4.350*estimate.350$PC4.l.350.y
```
## plotting PCA
```{r fig.height = 6, fig.width = 10}
# Make figures ------------------------------------------------------------
# PC1
PC1.350.plot <- ggplot() +
geom_path(data = estimate.350, aes(x = mean.x, y = mean.y), linewidth = 1.5) +
geom_path(data = estimate.350, aes(x = PC1.max.x, y = PC1.max.y), linewidth = 1, alpha = 0.5, linetype = "dashed") +
geom_path(data = estimate.350, aes(x = PC1.min.x, y = PC1.min.y), linewidth = 1, alpha = 0.5, linetype = "dotted") +
geom_point(data = estimate.350, aes(x = PC1.max.x, y = PC1.max.y), shape = 3, size = 3, stroke = 2) +
geom_point(data = estimate.350, aes(x = PC1.min.x, y = PC1.min.y), shape = "\u2212", size = 5, stroke = 8) +
xlab("X") + ylab("Y") +
ggtitle("PC1") +
theme_classic() +
# ylim(-35, 25) +
theme(plot.title = element_text(size = 15, hjust = 0.5, vjust = 1.5, face = "bold"),
legend.position = "top",
legend.title = element_blank(),
axis.text = element_text(size = 15),
axis.title = element_text(size = 15),
strip.text.x = element_text(size = 15)
)
PC2.350.plot <- ggplot() +
geom_path(data = estimate.350, aes(x = mean.x, y = mean.y), linewidth = 1.5) +
geom_path(data = estimate.350, aes(x = PC2.max.x, y = PC2.max.y), linewidth = 1, alpha = 0.5, linetype = "dashed") +
geom_path(data = estimate.350, aes(x = PC2.min.x, y = PC2.min.y), linewidth = 1, alpha = 0.5, linetype = "dotted") +
geom_point(data = estimate.350, aes(x = PC2.max.x, y = PC2.max.y), shape = 3, size = 3, stroke = 2) +
geom_point(data = estimate.350, aes(x = PC2.min.x, y = PC2.min.y), shape = "\u2212", size = 5, stroke = 8) +
xlab("X") + ylab("Y") +
ggtitle("PC2") +
theme_classic() +
# ylim(-35, 25) +
theme(plot.title = element_text(size = 15, hjust = 0.5, vjust = 1.5, face = "bold"),
legend.position = "top",
legend.title = element_blank(),
axis.text = element_text(size = 15),
axis.title = element_text(size = 15),
strip.text.x = element_text(size = 15)
)
PC3.350.plot <- ggplot() +
geom_path(data = estimate.350, aes(x = mean.x, y = mean.y), linewidth = 1.5) +
geom_path(data = estimate.350, aes(x = PC3.max.x, y = PC3.max.y), linewidth = 1, alpha = 0.5, linetype = "dashed") +
geom_path(data = estimate.350, aes(x = PC3.min.x, y = PC3.min.y), linewidth = 1, alpha = 0.5, linetype = "dotted") +
geom_point(data = estimate.350, aes(x = PC3.max.x, y = PC3.max.y), shape = 3, size = 3, stroke = 2) +
geom_point(data = estimate.350, aes(x = PC3.min.x, y = PC3.min.y), shape = "\u2212", size = 5, stroke = 8) +
xlab("X") + ylab("Y") +
ggtitle("PC3") +
theme_classic() +
# ylim(-35, 25) +
theme(plot.title = element_text(size = 15, hjust = 0.5, vjust = 1.5, face = "bold"),
legend.position = "top",
legend.title = element_blank(),
axis.text = element_text(size = 15),
axis.title = element_text(size = 15),
strip.text.x = element_text(size = 15)
)
PC4.350.plot <- ggplot() +
geom_path(data = estimate.350, aes(x = mean.x, y = mean.y), linewidth = 1.5) +
geom_path(data = estimate.350, aes(x = PC4.max.x, y = PC4.max.y), linewidth = 1, alpha = 0.5, linetype = "dashed") +
geom_path(data = estimate.350, aes(x = PC4.min.x, y = PC4.min.y), linewidth = 1, alpha = 0.5, linetype = "dotted") +
geom_point(data = estimate.350, aes(x = PC4.max.x, y = PC4.max.y), shape = 3, size = 3, stroke = 2) +
geom_point(data = estimate.350, aes(x = PC4.min.x, y = PC4.min.y), shape = "\u2212", size = 5, stroke = 8) +
xlab("X") + ylab("Y") +
ggtitle("PC4") +
theme_classic() +
# ylim(-35, 25) +
theme(plot.title = element_text(size = 15, hjust = 0.5, vjust = 1.5, face = "bold"),
legend.position = "top",
legend.title = element_blank(),
axis.text = element_text(size = 15),
axis.title = element_text(size = 15),
strip.text.x = element_text(size = 15)
)
# Publication plot
pca_meaning_all.350 <- grid.arrange(PC1.350.plot, PC2.350.plot, PC3.350.plot, PC4.350.plot, ncol = 2)
```
# analysis 2: functional principal component analysis
## FPCA using ```fdapace```
```{r}
# IDs = token column; tVec = time column; yVec = variable column(s)
input.PC1 <- fdapace::MakeFPCAInputs(IDs = pca.result.350$exclude_key, tVec = pca.result.350$proportional_time, yVec = pca.result.350$PC1z)
# Check if there's any issues with the data
fdapace::CheckData(input.PC1$Ly, input.PC1$Lt)
# No errors have been returned, so let's now run fPCA on the dynamic PC1 trajectory
PC1 <- fdapace::FPCA(Ly = input.PC1$Ly, Lt = input.PC1$Lt)
# saving the FPC1 output
save(PC1, file = "data/PC1_FPCA_BAAP.rda")
# understanding FPC1/PC1
## eigenvalues
PC1$lambda
## the cumulative percentage of variance explained by the eigenvalue
PC1$cumFVE
## fPC1: 0.5793283
## fPC2: 0.2456436
## fPC3: 0.1133626
## fPC4: 0.048286
## PC scores -> each row is 1 token, each column is one PC
# PC1$xiEst
## plot
plot(PC1)
## scree plot
# CreateScreePlot(PC1)
## path plot
# CreatePathPlot(PC1, xlab = "normalised time", ylab = "PC1 (tongue body movement)")
# the input data to FPCA() (just in case you want to check the specific input data you used)
# PC1$inputData$Lt
```
## join PC scores with data + plot
```{r fig.height = 6, fig.width = 10}
# load the fPCA results for PC1
load(file = "data/PC1_FPCA_BAAP.rda")
# function: get PC scores + return data frame with PCs for each token
get_pc_scores <- function(fpcaObj){
pcs <- data.frame(fpcaObj$xiEst)
token <- names(fpcaObj$inputData$Lt)
df <- cbind(token, pcs)
n_pcs <- length(fpcaObj$lambda) # get number of PCs
pc_names <- paste0("PC", 1:n_pcs) # create colnames for PCs
names(df) <- c("exclude_key", pc_names) # add colnames for token + PCs
return(df)
}
# get PC scores w/ token info
pc1_df <- get_pc_scores(PC1)
# join PCs (dat) with selected cols from original data frame
## store meta info
meta <- pca.result.350 %>%
select(speaker, L1, prompt, segment, vowel, exclude_key)
## merge the list and meta data - unique(meta) because otherwise there would be lots of duplicates
dat.PC1 <- left_join(pc1_df, unique(meta), by = "exclude_key")
# PC scores clustering per group
PC1.scatter <- dat.PC1 %>%
filter(segment %in% c("/l/", "/ɹ/")) %>%
# filter(group %in% c("advanced", "English")) %>%
ggplot2::ggplot() +
aes(x = PC1, y = PC2, colour = vowel, shape = vowel) +
geom_point(alpha = 0.3, size = 2, show.legend = FALSE) +
stat_ellipse(aes(color = vowel, linetype = vowel), level = 0.95, lwd = 1.2) +
facet_grid(segment ~ L1) +
scale_color_manual(values = c("blue4", "brown4", "darkolivegreen")) +
# ggtitle("Tongue body movement (PC1) for English and Japanese liquids") +
guides(linetype = "none") +
theme_classic() +
theme(legend.text = element_text(size = 30),
legend.key.size = unit(1, 'cm'),
legend.position = "bottom",
legend.title = element_text(size = 30),
axis.text = element_text(size = 15),
axis.title = element_text(size = 20),
plot.title = element_text(size = 20, hjust = 0.5, face = "bold"),
strip.text.x = element_text(size = 30),
strip.text.y = element_text(angle = 0, size = 30)
) +
# ylim(c(-4, 4)) +
labs(x = "FPC1", y = "FPC2", colour = "Adjacent vowel")
PC1.scatter
ggsave(PC1.scatter, filename = "figure/PC1_scatter.png", width = 15, height = 10, dpi = 1000)
```
## tracking dynamic pca
```{r warning=FALSE, message=FALSE, fig.height = 6, fig.width = 10}
## PC1
PC1.dyn.350 <- pca.result.350 %>%
group_by(speaker, prompt, repetition) %>%
ggplot() +
geom_path(aes(x = proportional_time, y = PC1z, colour = vowel, group = rec_date), alpha = 0.03) +
# geom_smooth(colour = "black", linewidth = 3, se = FALSE, show.legend = TRUE) +
geom_smooth(aes(x = proportional_time, y = PC1z, colour = vowel, group = vowel), linewidth = 2, se = FALSE, show.legend = TRUE) +
stat_smooth(aes(x = proportional_time, y = PC1z, colour = vowel, group = vowel), method = "gam", geom = "ribbon", fill = NA, linewidth = 0.5, linetype = 3, show.legend = FALSE) +
# facet_wrap(segment ~ vowel, ncol = 2) +
facet_grid(segment ~ L1) +
# ggtitle("PC1 (Onset: -350 ms)") +
geom_hline(yintercept = 0, linetype = 1, linewidth = 0.1) +
geom_vline(aes(xintercept = mean(vowel_start_prop)), linetype = 2, linewidth = 0.5) +
# geom_text(aes(x = mean(vowel_start_prop), y = 1.3), label = "vowel\nonset", colour = "Black", size = 5) +
geom_vline(aes(xintercept = mean(acoustic_start_prop)), linetype = 2, linewidth = 0.5) +
# geom_text(aes(x = mean(acoustic_start_prop), y = 1.3), label = "liquid\nonset", colour = "Black", size = 5) +
scale_color_manual(values = c("blue4", "brown4", "darkolivegreen")) +
theme_classic() +
theme(legend.text = element_text(size = 30),
legend.key.size = unit(1, 'cm'),
legend.position = "bottom",
legend.title = element_text(size = 30),
axis.text = element_text(size = 15),
axis.title = element_text(size = 20),
plot.title = element_text(size = 20, hjust = 0.5, face = "bold"),
strip.text.x = element_text(size = 20),
strip.text.y = element_text(angle = 0, size = 20)
) +
ylim(c(-4, 4)) +
labs(x = "Proportional time (%)", y = "PC1 (z-score)", colour = "Adjacent vowel")
PC1.dyn.350
ggsave(PC1.dyn.350, filename = "figure/dynamic_PC1.jpg", width = 15, height = 10, dpi = 1000)
```
## plotting FPC scores
```{r}
# function: define perturbation function (±Q = ±sd, k = PC number)
perturbation <- function(fpcaObj, Q, k){
Q * sqrt(fpcaObj$lambda[k]) * fpcaObj$phi[,k] + fpcaObj$mu
}
# function: create perturbation object with mean and ±Q sd as a data frame (for one PC only)
# can validate against fdapace::GetMeanCurve and fdapace::CreateModeOfVarPlot
perturbation_object <- function(fpcaObj, Q, k){
time <- fpcaObj$workGrid # grid of time values
mean <- fpcaObj$mu # mean trajectory
Qplus <- perturbation(fpcaObj, Q, k) # +Q sd
Qminus <- perturbation(fpcaObj, -Q, k) # -Q sd
df <- cbind(time, mean, Qplus, Qminus)
colnames(df) <- c("time", "mean", "Qplus", "Qminus")
df <- data.frame(df)
df$PC <- paste0("PC", k) # add PC colname
return(df)
}
# function: create perturbation data frame with mean and ±Q sd (for all PCs)
# to do: add ability to pass list of Q values for gradient perturbation function
get_perturbation <- function(fpcaObj, Q){
n_pcs <- length(fpcaObj$lambda)
k <- 1:n_pcs
df <- lapply(k, perturbation_object, fpcaObj=fpcaObj, Q=Q)
df <- dplyr::bind_rows(df) # unnest lists into single df
return(df)
}
# get mean trajectory and ±2 sd for all PCs
p_PC1 <- get_perturbation(PC1, Q = 2)
```
## perturbation plot
```{r fig.height = 6, fig.width = 10}
# Manually calculating proportional time for liquid onset and offset for plotting
pca.result.350 %>%
ungroup() %>%
summarise(mean_start = mean(acoustic_start_prop),
mean_end = mean(vowel_start_prop))
# plot data, perturbation + PC scores ------------------------------------
# perturbation plot
pc1_perturbation <- p_PC1 %>%
filter(PC %in% c("PC1", "PC2")) %>%
mutate(
fPC = case_when(
PC == "PC1" ~ "fPC1",
PC == "PC2" ~ "fPC2"
)
) %>%
ggplot2::ggplot() +
aes(x = time, y = mean) +
geom_path() +
geom_point(aes(y = Qplus), shape = 3, size = 3, colour = "red") +
geom_point(aes(y = Qminus), shape = 95, size = 5, colour = "blue") +
xlab("Proportional Time (%)") +
ylab("PC") +
geom_vline(data = pca.result.350, aes(xintercept = mean(acoustic_start_prop)), linetype = 2) +
geom_vline(data = pca.result.350, aes(xintercept = mean(vowel_start_prop)), linetype = 2) +
facet_wrap(~ fPC, ncol = 2) +
scale_color_manual(values = c("blue4", "brown4", "darkolivegreen")) +
theme_classic() +
theme(legend.text = element_text(size = 30),
legend.key.size = unit(1, 'cm'),
legend.position = "bottom",
legend.title = element_text(size = 30),
axis.text = element_text(size = 15),
axis.title = element_text(size = 20),
plot.title = element_text(size = 20, hjust = 0.5, face = "bold"),
strip.text.x = element_text(size = 20),
strip.text.y = element_text(angle = 0, size = 20)
) +
labs(x = "Proportional Time(%)", y = "fPC")
pc1_perturbation
ggsave(pc1_perturbation, filename = "figure/perturbation_PC1.jpg", width = 15, height = 5, dpi = 1000)
```
## FPCA reconstruction
### preparation
```{r fig.height = 6, fig.width = 10}
# mean fPC1 trajectory
# pc1_mean_curve <- fdapace::GetMeanCurve(Ly = input.PC1$Ly, Lt = input.PC1$Lt, optns = list(plot = TRUE))
pc1_mu_values <- data.frame(PC1$mu) # mean curve values
pc1_mu_time <- data.frame(PC1$workGrid) # timepoints used for estimating the curve
pc1_phi <- data.frame(PC1$phi) # eigenfunction at each timepoint: workGrid * nlambda (e.g., 255 = 51 workGrid * 5 lambda)
pc1_lambda <- data.frame(PC1$lambda) # PC loadings for each PC: currently 5
# create a data frame containing mean curve, time and eigenfunctions assocaited with each PC at each time point
## add an extra column 'col_number' as a common index across the data frames - useful when merging everything together later on
### mean curve
pc1_mu_values <- pc1_mu_values %>%
mutate(
col_number = row_number()
)
### sampling time points
pc1_mu_time <- pc1_mu_time %>%
mutate(
col_number = row_number()
)
### eigenfunction
pc1_phi <- pc1_phi %>%
mutate(
col_number = row_number()
)
### pc loadings
pc1_lambda <- pc1_lambda %>%
mutate(
PC = str_c("PC", row_number()),
PC = str_c(PC, "lambda", sep = "_")
) %>%
pivot_wider(names_from = "PC", values_from = "PC1.lambda") %>%
slice(rep(1:n(), each = 51)) %>%
mutate(
col_number = row_number()
)
## merging all data together one by one
PC1.rec <- left_join(pc1_mu_values, pc1_mu_time, by = "col_number")
PC1.rec <- left_join(PC1.rec, pc1_phi, by = "col_number")
PC1.rec <- left_join(PC1.rec, pc1_lambda, by = "col_number")
## tidying up some column names
PC1.rec <- PC1.rec %>%
select(col_number, PC1.workGrid, PC1.mu, X1, X2, X3, X4, X5, PC1_lambda, PC2_lambda, PC3_lambda, PC4_lambda, PC5_lambda) %>%
rename(
mean = PC1.mu,
time = PC1.workGrid,
PC1_eigen = X1,
PC2_eigen = X2,
PC3_eigen = X3,
PC4_eigen = X4,
PC5_eigen = X5
)
## plotting the eigenfunctions - this should match with a sub-plot in bottom right created with plot(PC1)
PC1.rec %>%
ggplot() +
# geom_path(aes(x = time, y = mean)) +
geom_path(aes(x = time, y = PC1_eigen), colour = "black", linewidth = 1.5) +
geom_path(aes(x = time, y = PC2_eigen), colour = "red", linetype = 2, linewidth = 1.5) +
geom_path(aes(x = time, y = PC3_eigen), colour = "darkgreen", linetype = 3, linewidth = 1.5) +
# geom_path(aes(x = time, y = value, colour = pc)) +
geom_hline(yintercept = 0) +
labs(x = "time", y = "eigenfunctions", title = "First 3 eigenfunctions")
## check if this matches plot(PC1)
plot(PC1)
```
```{r}
# PC scores -> each row is 1 token, each column is one PC
# head(PC1$xiEst)
# PC scores have already been added to the main data set
# head(dat.PC1)
# duplicate each row by 51 times
dat.PC1.time <- dat.PC1 %>%
slice(rep(1:n(), each = 51))
# add col_names to merge with the other data frame
dat.PC1.time <- dat.PC1.time %>%
group_by(exclude_key) %>%
mutate(
col_number = row_number()
) %>%
ungroup()
# merge
dat.PC1.time <- left_join(dat.PC1.time, PC1.rec, by = "col_number")
```
### visualisation: reconstructed PC1 curves based on FPC1
```{r message=FALSE, warning=FALSE, fig.height = 6, fig.width = 10}
pca.result.350.BAAP <- pca.result.350 %>%
filter(segment %in% c("/l/", "/ɹ/"))
rec.PC1.fPC1 <- dat.PC1.time %>%
mutate(
PC1_reconstruct = PC1 * PC1_eigen + mean,
PC2_reconstruct = PC2 * PC2_eigen + mean,
PC3_reconstruct = PC3 * PC3_eigen + mean,
PC4_reconstruct = PC4 * PC4_eigen + mean,
PC5_reconstruct = PC5 * PC5_eigen + mean,
) %>%
mutate(
language = case_when(
L1 == "English" ~ "L1 English",
L1 == "Japanese" ~ "L1 Japanese"
)
) %>%
# group_by(exclude_key) %>%
filter(segment %in% c("/l/", "/ɹ/")) %>%
ggplot() +
geom_path(aes(x = time, y = PC1_reconstruct, group = exclude_key, colour = vowel), alpha = 0.2, show.legend = TRUE) +
# geom_smooth(aes(x = time, y = PC1_reconstruct, group = vowel, colour = vowel)) +
scale_color_manual(values = c("blue3", "brown2", "darkolivegreen")) +
labs(x = "Proportional Time (%)", y = "Reconstructed PC1 scores from FPC1") +
labs(title = "FPC1 for PC1") +
geom_hline(yintercept = 0, linetype = 1, linewidth = 0.1) +
geom_vline(data = pca.result.350.BAAP, aes(xintercept = mean(vowel_start_prop)), linetype = 2, linewidth = 0.5) +
# geom_text(data = pca.result.350.BAAP, aes(x = mean(vowel_start_prop)+9, y = 1.5), label = "vowel\nonset", colour = "Black", size = 10) +
geom_vline(data = pca.result.350.BAAP, aes(xintercept = mean(acoustic_start_prop)), linetype = 2, linewidth = 0.5) +
# geom_text(data = pca.result.350.BAAP, aes(x = mean(acoustic_start_prop)-9, y = 1.5), label = "liquid\nonset", colour = "Black", size = 10) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
facet_grid(segment ~ language) +
theme_classic() +
theme(legend.text = element_text(size = 30),
legend.key.size = unit(2, 'cm'),
legend.position = "bottom",
legend.title = element_text(size = 30),
axis.text = element_text(size = 15),
axis.title = element_text(size = 20),
# plot.title = element_text(size = 20, hjust = 0, face = "bold"),
plot.title = element_blank(),
strip.text.x = element_text(size = 30),
strip.text.y = element_text(angle = 0, size = 30)
)
ggsave(rec.PC1.fPC1, filename = "figure/reconstructed_PC1_fPC1.jpg", width = 15, height = 10, dpi = 300)
```
```{r message=FALSE, warning=FALSE, fig.height = 10, fig.width = 20}
# raw data and reconstructed trajectories side by side
raw.rec <- ggpubr::ggarrange(PC1.dyn.350, rec.PC1.fPC1, common.legend = TRUE, legend = "bottom")
raw.rec
ggsave(raw.rec, filename = "figure/traj_side.jpg", width = 25, height = 10, dpi = 500)
```
### visualisation: with Japanese tap
```{r message=FALSE, warning=FALSE, fig.height = 10, fig.width = 20}
# raw
## PC1
PC1.dyn.350.jp <- pca.result.350 %>%
# filter(segment %in% c("/l/", "/ɹ/")) %>%
group_by(speaker, prompt, repetition) %>%
ggplot() +
geom_path(aes(x = proportional_time, y = PC1z, colour = vowel, group = rec_date), alpha = 0.03) +
# geom_smooth(colour = "black", linewidth = 3, se = FALSE, show.legend = TRUE) +
geom_smooth(aes(x = proportional_time, y = PC1z, colour = vowel, group = vowel), linewidth = 2, se = FALSE, show.legend = TRUE) +
stat_smooth(aes(x = proportional_time, y = PC1z, colour = vowel, group = vowel), method = "gam", geom = "ribbon", fill = NA, linewidth = 0.5, linetype = 3, show.legend = FALSE) +
# facet_wrap(segment ~ vowel, ncol = 2) +
facet_grid(segment ~ L1) +
# ggtitle("PC1 (Onset: -350 ms)") +
geom_hline(yintercept = 0, linetype = 1, linewidth = 0.1) +
geom_vline(aes(xintercept = mean(vowel_start_prop)), linetype = 2, linewidth = 0.5) +
# geom_text(aes(x = mean(vowel_start_prop), y = 1.3), label = "vowel\nonset", colour = "Black", size = 5) +
geom_vline(aes(xintercept = mean(acoustic_start_prop)), linetype = 2, linewidth = 0.5) +
# geom_text(aes(x = mean(acoustic_start_prop), y = 1.3), label = "liquid\nonset", colour = "Black", size = 5) +
scale_color_manual(values = c("blue4", "brown4", "darkolivegreen")) +
theme_classic() +
theme(legend.text = element_text(size = 30),
legend.key.size = unit(1, 'cm'),
legend.position = "bottom",
legend.title = element_text(size = 30),
axis.text = element_text(size = 15),
axis.title = element_text(size = 20),
plot.title = element_text(size = 30, hjust = 0.5, face = "bold"),
strip.text.x = element_text(size = 30),
strip.text.y = element_text(angle = 0, size = 30)
) +
ylim(c(-4, 4)) +
labs(x = "Proportional time (%)", y = "PC1 (z-score)", colour = "Adjacent vowel")
# FPC1 reconstruction
rec.PC1.fPC1.jp <- dat.PC1.time %>%
mutate(
PC1_reconstruct = PC1 * PC1_eigen + mean,
PC2_reconstruct = PC2 * PC2_eigen + mean,
PC3_reconstruct = PC3 * PC3_eigen + mean,
PC4_reconstruct = PC4 * PC4_eigen + mean,
PC5_reconstruct = PC5 * PC5_eigen + mean,
) %>%
mutate(
language = case_when(
L1 == "English" ~ "L1 English",
L1 == "Japanese" ~ "L1 Japanese"
)
) %>%
# group_by(exclude_key) %>%
# filter(segment %in% c("/l/", "/ɹ/")) %>%
ggplot() +
geom_path(aes(x = time, y = PC1_reconstruct, group = exclude_key, colour = vowel), alpha = 0.2, show.legend = TRUE) +
# geom_smooth(aes(x = time, y = PC1_reconstruct, group = vowel, colour = vowel)) +
scale_color_manual(values = c("blue3", "brown2", "darkolivegreen")) +
labs(x = "Proportional Time (%)", y = "Reconstructed PC1 scores from FPC1") +
labs(title = "FPC1 for PC1") +
geom_hline(yintercept = 0, linetype = 1, linewidth = 0.1) +
geom_vline(data = pca.result.350, aes(xintercept = mean(vowel_start_prop)), linetype = 2, linewidth = 0.5) +
# geom_text(data = pca.result.350.BAAP, aes(x = mean(vowel_start_prop)+9, y = 1.5), label = "vowel\nonset", colour = "Black", size = 10) +
geom_vline(data = pca.result.350, aes(xintercept = mean(acoustic_start_prop)), linetype = 2, linewidth = 0.5) +
# geom_text(data = pca.result.350.BAAP, aes(x = mean(acoustic_start_prop)-9, y = 1.5), label = "liquid\nonset", colour = "Black", size = 10) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
facet_grid(segment ~ language) +
theme_classic() +
theme(legend.text = element_text(size = 30),
legend.key.size = unit(2, 'cm'),
legend.position = "bottom",
legend.title = element_text(size = 30),
axis.text = element_text(size = 15),
axis.title = element_text(size = 20),
# plot.title = element_text(size = 20, hjust = 0, face = "bold"),
plot.title = element_blank(),
strip.text.x = element_text(size = 30),
strip.text.y = element_text(angle = 0, size = 30)
)
# raw data and reconstructed trajectories side by side
raw.rec.jp <- ggpubr::ggarrange(PC1.dyn.350.jp, rec.PC1.fPC1.jp, common.legend = TRUE, legend = "bottom")
raw.rec.jp
ggsave(raw.rec.jp, filename = "figure/traj_side_jp.jpg", width = 25, height = 10, dpi = 500)
```
# Other visualisation
## PCA tongue plot
```{r fig.height = 10, fig.width = 6}
# PC1
PC1.350.plot.BAAP <- ggplot() +
geom_path(data = estimate.350, aes(x = mean.x, y = mean.y), linewidth = 1.5) +
geom_path(data = estimate.350, aes(x = PC1.max.x, y = PC1.max.y), linewidth = 1, alpha = 0.5, linetype = "dashed") +
geom_path(data = estimate.350, aes(x = PC1.min.x, y = PC1.min.y), linewidth = 1, alpha = 0.5, linetype = "dotted") +
geom_point(data = estimate.350, aes(x = PC1.max.x, y = PC1.max.y), shape = 3, size = 3, stroke = 2) +
geom_point(data = estimate.350, aes(x = PC1.min.x, y = PC1.min.y), shape = "\u2212", size = 5, stroke = 8) +
xlab("X") + ylab("Y") +
ggtitle("PC1") +
theme_classic() +
# ylim(-35, 25) +
theme(plot.title = element_text(size = 30, face = "bold"),
legend.position = "top",
legend.title = element_blank(),
axis.text = element_text(size = 15),
axis.title = element_text(size = 15),
strip.text.x = element_text(size = 15)
)
# PC2
PC2.350.plot.BAAP <- ggplot() +
geom_path(data = estimate.350, aes(x = mean.x, y = mean.y), linewidth = 1.5) +
geom_path(data = estimate.350, aes(x = PC2.max.x, y = PC2.max.y), linewidth = 1, alpha = 0.5, linetype = "dashed") +
geom_path(data = estimate.350, aes(x = PC2.min.x, y = PC2.min.y), linewidth = 1, alpha = 0.5, linetype = "dotted") +
geom_point(data = estimate.350, aes(x = PC2.max.x, y = PC2.max.y), shape = 3, size = 3, stroke = 2) +
geom_point(data = estimate.350, aes(x = PC2.min.x, y = PC2.min.y), shape = "\u2212", size = 5, stroke = 8) +
xlab("X") + ylab("Y") +
ggtitle("PC2") +
theme_classic() +
# ylim(-35, 25) +
theme(plot.title = element_text(size = 30, face = "bold"),