-
Notifications
You must be signed in to change notification settings - Fork 0
/
EMS_Compact_v1_3_1.R
1323 lines (1042 loc) · 40.5 KB
/
EMS_Compact_v1_3_1.R
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
##
#
# E N T R O P Y
#
# of M O R P H O L O G I C A L
#
# S Y S T E M S
#
#
###
# EMS COMPACT
#
# v. Compact 1.3.1
#
# https://github.com/franfranz/Morphological_Systems_Entropy/tree/main/Compact
#
# this script contains the analysis of the EMS study:
# 1) the distribution of nouns across inflectional features is compared
# in all nouns and in the sample of animate nouns vs the maximum entropy of a reference distribution
# 2) the context entropy of nouns across inflectional features is compared
# in the sample of animate nouns and in a control sample randomly drawn from all noun
# careful! this script does not contain the preprocessing of text files and the calculation of context entropy
# please refer to the scripts of the EMS -FUll version for preprocessing :
# https://github.com/franfranz/Morphological_Systems_Entropy/tree/main/Full
###
# Entropy of Morphological Systems - EMS Compact
#
# S E T I N P U T S
#
#
#
###
# required packages: pryr, stringr, car, treemap, LaplacesDemon, kolmim, viridisLite, tidyverse
library(pryr)
## input required: graphical parameters
# palette
col_fp = "#00008B" # "blue4"
col_fs = "#00B2EE" # "deepskyblue2"
col_mp = "#CD2626" # "firebrick3"
col_ms = "#FF8C00" # "darkorange"
# use viridisLite for greyscale-resistant transformation
#col_fp = viridisLite::viridis(4)[1]
#col_fs = viridisLite::viridis(4)[2]
#col_mp = viridisLite::viridis(4)[3]
#col_ms = viridisLite::viridis(4)[4]
pal_01=c(col_fp, col_fs, col_mp, col_ms)
col_ref= "#4A4A4A" # "#008B45" # "springgreen4"
col_borders= "#FFFFFF" # "white"
myfavlinescol1="#4A4A4A" #"grey29"
# some common features of graphics
myfavcex=1.3
myfavpch=19
myfavlwd=2.7
# lty
myfavlty_fp=1
myfavlty_fs=1
myfavlty_mp=1
myfavlty_ms=1
myfavlty_ans=1
myfavlty_cont=2
myfavlty_ref=6
# line density and angle
myfavdensity=75
myfavlineangle=45
#
# some common features of graphics axes
ticks_h1=c(seq.int(1:4))
ticks_g1=c(seq.int(0,16,2))
mymostcommon_xlim=c(0, 16)
mymostcommon_ylim=c(0, 0.4)
# plots with box
#myaxesset=T
#rm(lightaxes)
#lightaxes=NULL
# plots with axes on left and bottom positions
myaxesset=F
lightaxes %<a-% {
axis(side=1, at=ticks, labels = T)
axis(side=2)}
# some common features of graphic grids
myfavgridcol="lightgray"
myfavgridlty="dotted"
# some common features of legends
myfavlegendcex=1.2
myfavlegendinset=0.02
legendcontent=c("Fem. Plur.","Fem. Sing.","Masc. Plur.", "Masc. Sing.")
# number of decimal places for rounded numbers
roundnum=3
## input directories
# this is the directory where this code is stored:
wd_code="PATH"
setwd(wd_code)
# this is the subdirectory of wd_code where the data are stored
# input data consist in list with all nouns, list with animate nouns
wd_in=paste0(wd_code, "\\wd_in")
# this is the subdirectory of wd_code to save graphics to
wd_graphs=paste0(wd_code, "\\Graphics")
#dir.create(wd_graphs)
###
# Entropy of Morphological Systems - EMS Compact
#
# I M P O R T D A T A
#
#
#
###
setwd(wd_in)
# Import all nouns
datallnouns_imported=read.csv("all_nouns_tagged_ITA.csv", sep=",", T)
# Import animate nouns
dat_anim= read.csv("Animate_sample_H_ITA.csv", T)
# subtract the animate sample from all nouns
datallnouns=datallnouns_imported[!datallnouns_imported$Form %in% dat_anim$Form, ]
# Import control nouns
dat_cont= read.csv("Control_sample_H_ITA.csv", T)
###
# Entropy of Morphological Systems - EMS Compact
# I:
# C O M P A R E
#
# N O U N S
#
# D I S T R I B U T I O N S
#
#
###
### Entropy of Morphological Systems - EMS Compact
# I: Compare Nouns Distributions
#
# 1 - R E F E R E N C E D I S T R I B U T I O N S
#
#
#
###
# generate uniform distribution, 4 discrete values
# size: approx the size of corpus = tokenfreq_total_alln
univec_length= 10000
uni_values=seq(1:5)
minval=min(uni_values)
maxval=max(uni_values)
uni_d=floor(runif(univec_length, minval, maxval))
str(uni_d)
ticks=c(minval:maxval)
histlim_uni=(((1/maxval)*univec_length)+((1/10)*univec_length))
hist(uni_d, ylim=c(0,histlim_uni),
border = col_borders,
main = "Uniform Categorial Distribution",
#col=col_ref, density = myfavdensity, angle = myfavlineangle,
axes=myaxesset,
)
lightaxes
uni_d=as.character(uni_d)
# entropy of the discrete uniform distribution
freqs_uni =table(uni_d)/length(uni_d)
entropy_uni= -sum(freqs_uni * log2(freqs_uni))
entropy_uni=round(entropy_uni, roundnum)
entropy_uni
refline=1/length(unique(uni_d))
freqs_uni4=as.data.frame(freqs_uni)
barplot(freqs_uni4$Freq,
border = col_borders,
#col=col_ref, density = myfavdensity, angle = myfavlineangle,
ylim = mymostcommon_ylim,
main = "Uniform Categorial Distribution"
)
abline(h=refline, col= col_ref, lty=myfavlty_ref, lwd=myfavlwd)
par(mfrow=c(1,1))
### Entropy of Morphological Systems - EMS Compact
# I: Compare Nouns Distributions Entropy of Morphological Systems - EMS Compact
#
# 2 - A L L N O U N S
#
#
#
###
# Import dataset with all nouns in Italian
#
# The dataset has been obtained by merging word frequencies collected from Itwac (Baroni et al., 2009)
# to a list of morphologically tagged words (Morph-it!, Zanchetta & Baroni, 2005).
# please refer to the first script of the Full version for the merging
#
# Split across INFLECTIONAL FEATURES - All nouns
#
# object with all Fp
alln_fp= datallnouns[datallnouns$POS=="NOUN-F:p", ]
summary(alln_fp)
alln_fp$POS=as.factor(as.character(alln_fp$POS))
# object with all Fs
alln_fs= datallnouns[datallnouns$POS=="NOUN-F:s", ]
summary(alln_fs)
str(alln_fs$FREQ)
alln_fs$POS=as.factor(as.character(alln_fs$POS))
# object with all Mp
alln_mp= datallnouns[datallnouns$POS=="NOUN-M:p", ]
summary(alln_mp)
alln_mp$POS=as.factor(as.character(alln_mp$POS))
# object with all Ms
alln_ms= datallnouns[datallnouns$POS=="NOUN-M:s", ]
summary(alln_ms)
alln_ms$POS=as.factor(as.character(alln_ms$POS))
# count: frequency observed in all inflection (token)
# sum partials
tokenfreq_alln_fs=sum(alln_fs$Freq)
tokenfreq_alln_fp=sum(alln_fp$Freq)
tokenfreq_alln_ms=sum(alln_ms$Freq)
tokenfreq_alln_mp=sum(alln_mp$Freq)
#check with total sum
tokenfreq_total_alln=sum(datallnouns$Freq)
tokenfreq_alln_fs+tokenfreq_alln_fp+tokenfreq_alln_ms+tokenfreq_alln_mp==tokenfreq_total_alln
#
# ENTROPY - Features All nouns
#
# entropy - type - allnouns
freq_types_all=table(datallnouns$POS)/length(datallnouns$POS)
entropy_type_alln=-sum(freq_types_all*log2(freq_types_all))
entropy_type_alln=round(entropy_type_alln, roundnum)
# entropy - token - allnouns
freq_tokens_alln=c(tokenfreq_alln_fp, tokenfreq_alln_fs,tokenfreq_alln_mp, tokenfreq_alln_ms)/tokenfreq_total_alln
entropy_token_alln= -sum(freq_tokens_alln*log2(freq_tokens_alln))
entropy_token_alln=round(entropy_token_alln, roundnum)
# Graph: proportion of types in each feature: all nouns #--------------- mygraph_prop_allnouns_types
library(treemap)
prop_allnouns_types=as.data.frame(freq_types_all)
prop_allnounscols=c("POS", "Freq")
colnames(prop_allnouns_types)<- prop_allnounscols
prop_allnouns_types$Freq=round(prop_allnouns_types$Freq, roundnum)
#prop_allnouns_types$label <- paste(prop_allnouns_types$POS, prop_allnouns_types$Freq, sep = "\n")
mygraph_prop_allnouns_types %<a-% {
prop_allnouns_types$label <- paste(legendcontent, " ", prop_allnouns_types$Freq)
treemap(prop_allnouns_types,
index=c("label"),
vSize="Freq",
type="index",
title="All nouns - Types",
palette=pal_01,
border.col=c("white"),
border.lwds=1,
fontsize.labels=20,
fontcolor.labels="white",
fontface.labels=1,
bg.labels=c("transparent"),
align.labels=c("left", "top"),
overlap.labels=0.5,
inflate.labels=F )
}
mygraph_prop_allnouns_types
# Graph: proportion of tokens in each feature: all nouns #--------------- mygraph_prop_allnouns_tokens
prop_allnouns_token=as.data.frame(freq_tokens_alln)
prop_allnouns_token$Freq=prop_allnouns_token$freq_tokens_alln
prop_allnouns_token$POS=legendcontent
prop_allnouns_token$freq_tokens_alln=NULL
prop_allnouns_token$Freq=round(prop_allnouns_token$Freq, roundnum)
mygraph_prop_allnouns_tokens %<a-% {
#prop_allnouns_token$label <- paste(prop_allnouns_token$POS, prop_allnouns_token$Freq, sep = "\n")
prop_allnouns_token$label <- paste(legendcontent, " ", prop_allnouns_token$Freq)
treemap(prop_allnouns_token,
# data
index=c("label"),
vSize="Freq",
type="index",
title="All nouns - Tokens",
palette=pal_01,
border.col=c("white"),
border.lwds=1,
fontsize.labels=20,
fontcolor.labels="white",
fontface.labels=1,
bg.labels=c("transparent"),
align.labels=c("left", "top"),
overlap.labels=0.5,
inflate.labels=F )
}
mygraph_prop_allnouns_tokens
#
# density
#
# token frequency - density - all nouns #--------------- mygraph_dens_allnouns
ticks=ticks_g1
mygraph_dens_allnouns %<a-% {
plot(density(alln_fp$logtoken),
col=col_fp,
lwd=myfavlwd,
lty=myfavlty_fp,
main="All nouns - Token Frequency",
xlab="Token frequency of occurrence (log)",
ylim=mymostcommon_ylim, xlim=mymostcommon_xlim,
axes=myaxesset)
lightaxes
lines(density(alln_fs$logtoken), col=col_fs, lwd=myfavlwd, lty=myfavlty_fs)
lines(density(alln_mp$logtoken), col=col_mp, lwd=myfavlwd, lty=myfavlty_mp)
lines(density(alln_ms$logtoken), col=col_ms, lwd=myfavlwd, lty=myfavlty_ms)
legend("topright", inset=myfavlegendinset,# title="Inflection",
# c("Fem. Plur.","Fem. Sing.","Masc. Plur.", "Masc. Sing."), fill=c("blue4","cyan3", "firebrick3","darkgoldenrod1"), bty="n", cex=0.8)
legend=legendcontent,
fill=pal_01,
bty="n",
cex=myfavlegendcex)
}
mygraph_dens_allnouns
### Entropy of Morphological Systems - EMS Compact
# I: Compare Nouns Distributions Entropy of Morphological Systems - EMS Compact
#
# 3 - A N I M A T E N O U N S
#
#
#
###
summary(dat_anim)
table(dat_anim$base)
summary(table(dat_anim$base)==4)
#remove possible duplicates
library(tidyverse)
dat_anim$notunique_forms=duplicated(dat_anim$Form)
animdup=dat_anim[which(dat_anim$notunique_forms==T), ]
summary(animdup)
dat_anim=dat_anim[which(dat_anim$notunique_forms==F), ]
dat_anim$notunique_forms=NULL
#
# Split across INFLECTIONAL FEATURES - Animate nouns
#
#object with all Fp
anim_fp= dat_anim[dat_anim$POS=="NOUN-F:p", ]
summary(anim_fp)
anim_fp$POS=as.factor(as.character(anim_fp$POS))
length(anim_fp$POS)
#object with all Fs
anim_fs= dat_anim[dat_anim$POS=="NOUN-F:s", ]
summary(anim_fs)
anim_fs$POS=as.factor(as.character(anim_fs$POS))
length(anim_fs$POS)
#object with all Mp
anim_mp= dat_anim[dat_anim$POS=="NOUN-M:p", ]
summary(anim_mp)
anim_mp$POS=as.factor(as.character(anim_mp$POS))
length(anim_mp$POS)
#object with all Ms
anim_ms= dat_anim[dat_anim$POS=="NOUN-M:s", ]
summary(anim_ms)
anim_ms$POS=as.factor(as.character(anim_ms$POS))
length(anim_ms$POS)
#
# ENTROPY - Features - Animate nouns
#
# count: frequency observed in animate inflection (type)
typefreq_anim=table(dat_anim$POS)/length(dat_anim$POS)
entropy_type_anim= -(sum((typefreq_anim)*log2(typefreq_anim)))
entropy_type_anim=round(entropy_type_anim, roundnum)
# count: frequency observed in animate inflection (token)
# sum partials
tokenfreq_anim_fp=sum(anim_fp$Freq)
tokenfreq_anim_fs=sum(anim_fs$Freq)
tokenfreq_anim_mp=sum(anim_mp$Freq)
tokenfreq_anim_ms=sum(anim_ms$Freq)
#check with total sum
tokenfreq_total_anim=sum(dat_anim$Freq)
tokenfreq_anim_fp+tokenfreq_anim_fs+tokenfreq_anim_mp+tokenfreq_anim_ms==tokenfreq_total_anim
# entropy: token - animate
freq_tokens_anim=c(tokenfreq_anim_fp, tokenfreq_anim_fs, tokenfreq_anim_ms, tokenfreq_anim_mp)/tokenfreq_total_anim
entropy_token_anim= -sum(freq_tokens_anim*log2(freq_tokens_anim))
entropy_token_anim=round(entropy_token_anim, roundnum)
# Graph: proportion of types in each feature: animate nouns #--------------- mygraph_prop_animnouns_types
prop_animnouns_types=as.data.frame(typefreq_anim)
prop_cols=c("POS", "Freq")
colnames(prop_animnouns_types)<- prop_cols
prop_animnouns_types$Freq=round(prop_animnouns_types$Freq, roundnum)
#prop_animnouns_types$label <- paste(prop_animnouns_types$POS, prop_animnouns_types$Freq, sep = "\n")
mygraph_prop_animnouns_types %<a-% {
prop_animnouns_types$label <- paste(legendcontent, " ", prop_animnouns_types$Freq)
treemap(prop_animnouns_types,
index=c("label"),
vSize="Freq",
type="index",
title="Animate nouns - Types",
palette=pal_01,
border.col=c("white"),
border.lwds=1,
fontsize.labels=20,
fontcolor.labels="white",
fontface.labels=1,
bg.labels=c("transparent"),
align.labels=c("left", "top"),
overlap.labels=0.5,
inflate.labels=F )
}
mygraph_prop_animnouns_types
# Graph: proportion of tokens in each feature: all nouns #--------------- mygraph_prop_animnouns_token
prop_animnouns_token=as.data.frame(freq_tokens_anim)
prop_animnouns_token$Freq=prop_animnouns_token$freq_tokens_anim
prop_animnouns_token$POS=legendcontent
prop_animnouns_token$freq_tokens_anim=NULL
prop_animnouns_token$Freq=round(prop_animnouns_token$Freq, 3)
mygraph_prop_animnouns_token %<a-% {
#prop_animnouns_token$label <- paste(prop_animnouns_token$POS, prop_animnouns_token$Freq, sep = "\n")
prop_animnouns_token$label <- paste(legendcontent, " ", prop_animnouns_token$Freq)
treemap(prop_animnouns_token,
index=c("label"),
vSize="Freq",
type="index",
title="Animate nouns - Tokens",
palette=pal_01,
border.col=c("white"),
border.lwds=1,
fontsize.labels=20,
fontcolor.labels="white",
fontface.labels=1,
bg.labels=c("transparent"),
align.labels=c("left", "top"),
overlap.labels=0.5,
inflate.labels=F )
}
mygraph_prop_animnouns_token
# token frequency - density - animate nouns #--------------- mygraph_dens_animnouns
ticks=ticks_g1
mygraph_dens_animnouns %<a-% {
plot(density(anim_fp$logtoken),
col=col_fp,
lwd=myfavlwd,
lty=myfavlty_fp,
main="Animate nouns - Token Frequency",
xlab="Token frequency of occurrence (log)",
ylim=mymostcommon_ylim, xlim=mymostcommon_xlim,
axes=myaxesset)
lightaxes
lines(density(anim_fs$logtoken), col=col_fs, lwd=myfavlwd, lty=myfavlty_fs)
lines(density(anim_mp$logtoken), col=col_mp, lwd=myfavlwd, lty=myfavlty_mp)
lines(density(anim_ms$logtoken), col=col_ms, lwd=myfavlwd, lty=myfavlty_ms)
legend("topright",
inset=myfavlegendinset,
# title="Inflection",
legend=legendcontent,
fill=pal_01,
bty="n",
cex=myfavlegendcex)
}
mygraph_dens_animnouns
### Entropy of Morphological Systems - EMS Compact
# Compare Nouns Distributions Entropy of Morphological Systems - EMS Compact
#
# 4 - C O N T R O L S A M P L E N O U N S
#
#
#
###
full_contsamp=dat_cont
control_fp= full_contsamp[full_contsamp$POS=="NOUN-F:p", ]
control_fs= full_contsamp[full_contsamp$POS=="NOUN-F:s", ]
control_mp= full_contsamp[full_contsamp$POS=="NOUN-M:p", ]
control_ms= full_contsamp[full_contsamp$POS=="NOUN-M:s", ]
# distance of sampled control nouns from all nouns, within each feature
wilcox.test(alln_fp$logtoken, control_fp$logtoken)
wilcox.test(alln_fs$logtoken, control_fs$logtoken)
wilcox.test(alln_mp$logtoken, control_mp$logtoken)
wilcox.test(alln_ms$logtoken, control_ms$logtoken)
# distribution of nouns in the control sample #--------------- mygraph_controlsample_features
mygraph_controlsample_features %<a-% {
par(mfrow=c(2,2))
plot(density(alln_fp$logtoken), col=col_fp, lwd=myfavlwd, ylim=mymostcommon_ylim,
main="",
axes=myaxesset)
lightaxes
lines(density(control_fp$logtoken), col=col_fp, lwd=myfavlwd, lty=myfavlty_cont )
legend("topleft", legend = "Fem. \nPlur.", text.col=col_fp, bty="n")
xpd=T
legend("topright", inset=-0.1,
c("All Nouns","Control Sample"), lwd=1.9, lty=c(myfavlty_ans, myfavlty_cont),
bty="n", cex=myfavlegendcex, ncol = 1)
xpd=F
plot(density(alln_fs$logtoken), col=col_fs, lwd=myfavlwd, ylim=mymostcommon_ylim,
main="",
axes=myaxesset)
lightaxes
lines(density(control_fs$logtoken), col=col_fs, lwd=myfavlwd, lty=myfavlty_cont)
legend("topleft", legend = "Fem. \nSing.", text.col=col_fs, bty="n")
plot(density(alln_mp$logtoken), col=col_mp, lwd=myfavlwd, ylim=mymostcommon_ylim,
main="",
axes=myaxesset)
lightaxes
lines(density(control_mp$logtoken), col=col_mp, lwd=myfavlwd, lty=myfavlty_cont )
legend("topleft", legend = "Masc. \nPlur.", text.col=col_mp, bty="n")
plot(density(alln_ms$logtoken), col=col_ms, lwd=myfavlwd, ylim=mymostcommon_ylim,
main="",
axes=myaxesset)
lightaxes
lines(density(control_ms$logtoken), col=col_ms, lwd=myfavlwd, lty=myfavlty_cont )
title("Frequency of nouns in the All nouns vs. the Control Sample", line = -1.1, outer = TRUE)
legend("topleft", legend = "Masc. \nSing.", text.col=col_ms, bty="n")
}
mygraph_controlsample_features
# another way to visualize the same comparison
# distribution of nouns in the control sample #--------------- mygraph_controlsample_features_superimposed
#plot graph with all the features of the control sample
par(mfrow=c(1,1))
mygraph_controlsample_features_superimposed %<a-% {
plot(density(control_fp$logtoken),
ylim=mymostcommon_ylim,
xlim=mymostcommon_xlim,
col= col_fp,
lwd=myfavlwd,
lty=myfavlty_cont,
main="Control Sample", xlab="Token frequency of occurrence (log)",
axes=myaxesset)
lightaxes
lines(density(control_fs$logtoken), col=col_fs, lwd=myfavlwd, lty=myfavlty_cont)
lines(density(control_mp$logtoken), col=col_mp, lwd=myfavlwd, lty=myfavlty_cont)
lines(density(control_ms$logtoken), col=col_ms, lwd=myfavlwd, lty=myfavlty_cont)
legend("topright", inset=.02, bty= "n", #title="Inflection",
legend = legendcontent, fill=pal_01, cex=myfavlegendcex)
}
mygraph_controlsample_features_superimposed
# call plot of token frequency -density - all nouns to compare
mygraph_dens_allnouns
# entropy in the control sample
# count: frequency observed in control sample (type)
typefreq_cont=table(full_contsamp$POS)/length(full_contsamp$POS)
entropy_type_cont= -(sum((typefreq_cont)*log2(typefreq_cont)))
entropy_type_cont=round(entropy_type_cont, roundnum)
# count: frequency observed in control sample (token)
# sum partials
tokenfreq_cont_fp=sum(control_fp$Freq)
tokenfreq_cont_fs=sum(control_fs$Freq)
tokenfreq_cont_mp=sum(control_mp$Freq)
tokenfreq_cont_ms=sum(control_ms$Freq)
#check with total sum
tokenfreq_total_cont=sum(full_contsamp$Freq)
tokenfreq_cont_fp+tokenfreq_cont_fs+tokenfreq_cont_mp+tokenfreq_cont_ms==tokenfreq_total_cont
# entropy: token - control sample
freq_tokens_cont=c(tokenfreq_cont_fp,
tokenfreq_cont_fs,
tokenfreq_cont_mp,
tokenfreq_cont_ms)/tokenfreq_total_cont
entropy_token_cont= -sum(freq_tokens_cont*log2(freq_tokens_cont))
entropy_token_cont=round(entropy_token_cont, roundnum)
### Entropy of Morphological Systems - EMS Compact
# I: Compare Nouns Distributions
#
# 5 - C O M P A R E D I S T R I B U T I O N S
#
#
###
#
# Token frequency across features
#
# animate sample
shapiro.test(anim_fp$logtoken)
shapiro.test(anim_fs$logtoken)
shapiro.test(anim_mp$logtoken)
shapiro.test(anim_ms$logtoken)
# quantile-quantile plots
library(car)
mygraph_qq_anim_nouns%<a-%{
par(mfrow=c(2,2))
qqPlot(anim_fp$logtoken, pch=myfavpch, col=col_fp, col.lines = myfavlinescol1, id=F, main = "Anim nouns \n Feminine Singular")
qqPlot(anim_fs$logtoken, pch=myfavpch, col=col_fs, col.lines = myfavlinescol1, id=F, main = "Anim nouns \n Feminine Plural")
qqPlot(anim_mp$logtoken, pch=myfavpch, col=col_mp, col.lines = myfavlinescol1, id=F, main = "Anim nouns \n Masculine Singular")
qqPlot(anim_ms$logtoken, pch=myfavpch, col=col_ms, col.lines = myfavlinescol1, id=F, main = "Anim nouns \n Masculine Plural")
}
par(mfrow=c(1,1))
mygraph_qq_anim_nouns
mygraph_qq_cont_nouns%<a-%{
par(mfrow=c(2,2))
qqPlot(control_fp$logtoken, pch=myfavpch, col=col_fp, col.lines = myfavlinescol1, id=F, main = "Control Sample \n Feminine Singular")
qqPlot(control_fs$logtoken, pch=myfavpch, col=col_fs, col.lines = myfavlinescol1, id=F, main = "Control Sample \n Feminine Plural")
qqPlot(control_mp$logtoken, pch=myfavpch, col=col_mp, col.lines = myfavlinescol1, id=F, main = "Control Sample \n Masculine Singular")
qqPlot(control_ms$logtoken, pch=myfavpch, col=col_ms, col.lines = myfavlinescol1, id=F, main = "Control Sample \n Masculine Plural")
}
par(mfrow=c(1,1))
mygraph_qq_cont_nouns
mygraph_qq_all_nouns%<a-%{
par(mfrow=c(2,2))
qqPlot(alln_fp$logtoken, pch=myfavpch, col=col_fp, col.lines = myfavlinescol1, id=F, main = "All nouns \n Feminine Singular")
qqPlot(alln_fs$logtoken, pch=myfavpch, col=col_fs, col.lines = myfavlinescol1, id=F, main = "All nouns \n Feminine Plural")
qqPlot(alln_mp$logtoken, pch=myfavpch, col=col_mp, col.lines = myfavlinescol1, id=F, main = "All nouns \n Masculine Singular")
qqPlot(alln_ms$logtoken, pch=myfavpch, col=col_ms, col.lines = myfavlinescol1, id=F, main = "All nouns \n Masculine Plural")
}
par(mfrow=c(1,1))
mygraph_qq_all_nouns
# distance within features, between samples
ks.test(control_fp$logtoken, anim_fp$logtoken)
ks.test(control_fs$logtoken, anim_fs$logtoken)
ks.test(control_mp$logtoken, anim_mp$logtoken)
ks.test(control_ms$logtoken, anim_ms$logtoken)
# pairwise comparison within features, between samples
wilcox.test(alln_fp$logtoken, anim_fp$logtoken)
wilcox.test(alln_fs$logtoken, anim_fs$logtoken)
wilcox.test(alln_mp$logtoken, anim_mp$logtoken)
wilcox.test(alln_ms$logtoken, anim_ms$logtoken)
# frequency: median
#
medtoken_alln_fp=median(alln_fp$logtoken)
medtoken_alln_fs=median(alln_fs$logtoken)
medtoken_alln_mp=median(alln_mp$logtoken)
medtoken_alln_ms=median(alln_ms$logtoken)
medtoken_anim_fp=median(anim_fp$logtoken)
medtoken_anim_fs=median(anim_fs$logtoken)
medtoken_anim_mp=median(anim_mp$logtoken)
medtoken_anim_ms=median(anim_ms$logtoken)
medtoken_cont_fp=median(control_fp$logtoken)
medtoken_cont_fs=median(control_fs$logtoken)
medtoken_cont_mp=median(control_mp$logtoken)
medtoken_cont_ms=median(control_ms$logtoken)
median_logtoken= rbind(medtoken_alln_fp,
medtoken_alln_fs,
medtoken_alln_mp,
medtoken_alln_ms,
medtoken_anim_fp,
medtoken_anim_fs,
medtoken_anim_mp,
medtoken_anim_ms,
medtoken_cont_fp,
medtoken_cont_fs,
medtoken_cont_mp,
medtoken_cont_ms)
colnames(median_logtoken)= "median_token_frequency"
median_logtoken=round(median_logtoken, roundnum)
# frequency: sd
#
sdtoken_alln_fp=sd(alln_fp$logtoken)
sdtoken_alln_fs=sd(alln_fs$logtoken)
sdtoken_alln_mp=sd(alln_mp$logtoken)
sdtoken_alln_ms=sd(alln_ms$logtoken)
sdtoken_anim_fp=sd(anim_fp$logtoken)
sdtoken_anim_fs=sd(anim_fs$logtoken)
sdtoken_anim_mp=sd(anim_mp$logtoken)
sdtoken_anim_ms=sd(anim_ms$logtoken)
sdtoken_cont_fp=sd(control_fp$logtoken)
sdtoken_cont_fs=sd(control_fs$logtoken)
sdtoken_cont_mp=sd(control_mp$logtoken)
sdtoken_cont_ms=sd(control_ms$logtoken)
sd_logtoken= rbind(sdtoken_alln_fp,
sdtoken_alln_fs,
sdtoken_alln_mp,
sdtoken_alln_ms,
sdtoken_anim_fp,
sdtoken_anim_fs,
sdtoken_anim_mp,
sdtoken_anim_ms,
sdtoken_cont_fp,
sdtoken_cont_fs,
sdtoken_cont_mp,
sdtoken_cont_ms)
colnames(sd_logtoken)= "sd_token_frequency"
sd_logtoken=round(sd_logtoken, roundnum)
# Df with median and sd
#
logtoken_distr=cbind.data.frame(median_logtoken, sd_logtoken)
logtoken_distr$feature=str_sub((rownames(logtoken_distr)), -7, -1)
rownames(logtoken_distr)=NULL
#
# Summary of distributions
#
# allnouns - type -
count_allnouns_type=c(length(alln_fp$Form), length(alln_fs$Form), length(alln_mp$Form), length(alln_ms$Form))
probs_allnouns_type=round(count_allnouns_type/length(datallnouns$Form), roundnum)
# allnouns - token -
count_allnouns_token=c(tokenfreq_alln_fp, tokenfreq_alln_fs,tokenfreq_alln_mp, tokenfreq_alln_ms)
probs_allnouns_token=round((count_allnouns_token/tokenfreq_total_alln), roundnum)
# animate nouns - type -
count_animnouns_type=c(length(anim_fp$Form), length(anim_fs$Form), length(anim_mp$Form), length(anim_ms$Form))
probs_animnouns_type= round(count_animnouns_type/length(dat_anim$Form), roundnum)
# animate nouns - token -
count_animnouns_token=c(tokenfreq_anim_fp, tokenfreq_anim_fs, tokenfreq_anim_ms, tokenfreq_anim_mp)
probs_animnouns_token=round((count_animnouns_token/tokenfreq_total_anim), roundnum)
# control sample - type
count_contsamp_type=c(length(control_fp$Form), length(control_fs$Form), length(control_mp$Form), length(control_ms$Form))
probs_contsamp_type=round(count_contsamp_type/length(full_contsamp$Form), roundnum)
# control sample - token
count_contsamp_token=c(tokenfreq_cont_fp, tokenfreq_cont_fs, tokenfreq_cont_mp, tokenfreq_cont_ms)
probs_contsamp_token=round((count_contsamp_token/tokenfreq_total_cont), roundnum)
# discrete uniform distribution
freqs_uni=round(freqs_uni, roundnum)
count_freqs_uni=freqs_uni*univec_length
# create a df collecting the raw counts of all samples
count_tab_samples= rbind(count_freqs_uni,
count_allnouns_type, count_allnouns_token,
count_animnouns_type, count_animnouns_token,
count_contsamp_type, count_contsamp_token)
count_tab_samples=as.data.frame(count_tab_samples)
colnames(count_tab_samples)= legendcontent
# create a df collecting the probability distributions of all samples
probs_tab_samples=rbind(freqs_uni,
probs_allnouns_type, probs_allnouns_token,
probs_animnouns_type, probs_animnouns_token,
probs_contsamp_type, probs_contsamp_token
)
probs_tab_samples=as.data.frame(probs_tab_samples)
colnames(probs_tab_samples)=legendcontent
#
# entropy in different distributions
#
# uniform - token
entropy_uni
# all nouns - type
entropy_type_alln
# all nouns - token
entropy_token_alln
# animate - type
entropy_type_anim
# animate - token
entropy_token_anim
# control_sample_type
entropy_type_cont
# control_sample_token
entropy_token_cont
# create a df collecting the entropy of all distibutions
entropy_b=rbind(entropy_uni,
entropy_type_alln,
entropy_token_alln,
entropy_type_anim,
entropy_token_anim,
entropy_type_cont,
entropy_token_cont)
colnames(entropy_b)= "entropy"
# visualize results
logtoken_distr
count_tab_samples
probs_tab_samples
entropy_b
#
# Kullback-Leibler Divergence
#
# compare distr of all nouns to reference
# type
distalln__type_ent=LaplacesDemon::KLD(freqs_uni, probs_allnouns_type)
# token
distalln_token_ent=LaplacesDemon::KLD(freqs_uni, probs_allnouns_token)
# compare distr of animate nouns to reference
# type
distanim_type_ent=LaplacesDemon::KLD(freqs_uni, probs_animnouns_type)
# token
distanim_token_ent=LaplacesDemon::KLD(freqs_uni, probs_animnouns_token)
#collect distance in a df
collected_kld=cbind(distalln__type_ent$intrinsic.discrepancy,
distalln_token_ent$intrinsic.discrepancy,
distanim_type_ent$intrinsic.discrepancy,
distanim_token_ent$intrinsic.discrepancy
)
colnames(collected_kld)=list("allnouns_type", "allnouns_token", "animnouns_type", "animnouns_token")
#
# Sum-up graph of logtoken distributions
#
par(mfrow=c(1,1))
par(xpd=F)
sumgraph_data= as.matrix (t(probs_tab_samples[2:5, ])) #--------------- mygraph_sumup_logtoken_distr
mygraph_sumup_toktyp_all %<a-% {
colnames(sumgraph_data) <- c("All nouns \n Types","All nouns \n Tokens","Anim nouns \n Types", "Anim nouns \n Tokens")
# Grouped barplot
barplot(sumgraph_data,
col=pal_01 ,
border=col_borders,
beside=T,
)
abline(h=refline, col= col_ref, lty=myfavlty_ref, lwd=myfavlwd)
legend("topleft", inset=.02, bty= "n",
legend = legendcontent, fill=pal_01, cex=1)
}
mygraph_sumup_toktyp_all
###
# Entropy of Morphological Systems - EMS Compact
# II:
# C O M P A R E
#
# C O N T E X T
#
# E N T R O P Y
#
#
###
# bind anim and control df into a single df
dat_nouns=rbind.data.frame(dat_cont, dat_anim)
library(kolmim)
#library(multcomp)
library(car)
# Check for normality (Shapiro-Wilk)
# control sample
shapiro.test(control_fp$entropy)
shapiro.test(control_fs$entropy)
shapiro.test(control_mp$entropy)
shapiro.test(control_ms$entropy)
# quantile-quantile plots
mygraph_qq_cont_nouns%<a-%{
par(mfrow=c(2,2))
qqPlot(control_fp$entropy, pch=myfavpch, col=col_fp, col.lines = myfavlinescol1, id=F, main = "Control Sample \n Feminine Singular")
qqPlot(control_fs$entropy, pch=myfavpch, col=col_fs, col.lines = myfavlinescol1, id=F, main = "Control Sample \n Feminine Plural")
qqPlot(control_mp$entropy, pch=myfavpch, col=col_mp, col.lines = myfavlinescol1, id=F, main = "Control Sample \n Masculine Singular")