使用 R 对变量进行多重比较的 Kruskal Wallis 测试的函数

Function to Conduct a Kruskal Wallis Tests for Multiple Comparison of Variables using R

问题:

我的数据集不符合进行方差分析的假设,因此我需要进行非参数替代,例如 Kruskal Wallis 检验。总的来说,我有七个参数,我想对它们进行多次成对比较。

我找到了进行此类分析的函数,但总体输出肯定不正确。

有人能帮忙吗?

如果有人能提供帮助,非常感谢。

R代码:

Outcomes <- c("Low.Freq", "High.Freq", "Peak.Freq", "Delta.Freq", "Delta.Time", "Peak.Time", 
              "Center.Freq", "Start.Freq", "End.Freq")

#Multiple Kruskall Wallis test the Raw Data for acoustic parameters
Multiple_Kruskal_Test<-sapply(Outcomes, function(my) {
                              f <- as.formula(paste(my, "~Species", sep=""))
                              summary(kruskal.test(f, data=New_Acoustic_Parameters))
                              })

不正确的输出:

      Low.Freq    High.Freq   Peak.Freq   Delta.Freq  Delta.Time  Peak.Time   Center.Freq Start.Freq  End.Freq   
 [1,] "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"        
 [2,] "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"        
 [3,] "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"        
 [4,] "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"        
 [5,] "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"         "1"        
 [6,] "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"   
 [7,] "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"   
 [8,] "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"   
 [9,] "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"   
[10,] "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"    "-none-"   
[11,] "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"  
[12,] "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"  
[13,] "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"  
[14,] "character" "character" "character" "character" "character" "character" "character" "character" "character"
[15,] "character" "character" "character" "character" "character" "character" "character" "character" "character"
> 

数据

   structure(list(ID = 1:150, Low.Freq = c(435L, 94103292L, 1L, 
2688L, 8471L, 28818L, 654755585L, 468628164L, 342491L, 2288474L, 
3915L, 411L, 267864894L, 3312618L, 5383L, 8989443L, 1894L, 534981L, 
9544861L, 3437614L, 475386L, 7550764L, 48744L, 2317845L, 5126197L, 
2445L, 8L, 557450L, 450259742L, 21006647L, 9L, 7234027L, 59L, 
9L, 605L, 9199L, 3022L, 30218156L, 46423L, 38L, 88L, 396396244L, 
28934316L, 7723L, 95688045L, 679354L, 716352L, 76289L, 332826763L, 
6L, 90975L, 83103577L, 9529L, 229093L, 42810L, 5L, 18175302L, 
1443751L, 5831L, 8303661L, 86L, 778L, 23947L, 8L, 9829740L, 2075838L, 
7434328L, 82174987L, 2L, 94037071L, 9638653L, 5L, 3L, 65972L, 
0L, 936779338L, 4885076L, 745L, 8L, 56456L, 125140L, 73043989L, 
516476L, 7L, 4440739L, 612L, 3966L, 8L, 9255L, 84127L, 96218L, 
5690L, 56L, 3561L, 78738L, 1803363L, 809369L, 7131L, 0L, 35502443L, 
88864L, 94L, 850L, 89628L, 261319L, 4848L, 97341913L, 75L, 82481L, 
806L, 14L, 7L, 88L, 912102577L, 48L, 169L, 6949L, 165L, 35969L, 
44L, 955L, 9L, 43090317L, 3L, 7905L, 596L, 7262304L, 4246710L, 
68L, 0L, 6859264L, 85725268L, 985L, 861158777L, 95L, 386L, 8773L, 
765L, 6798L, 18L, 9L, 32472L, 3L, 647698334L, 27918648L, 836L, 
188837805L, 968L, 88656L, 5823702L), High.Freq = c(6071L, 3210L, 
6L, 7306092L, 6919054L, 666399L, 78L, 523880161L, 4700783L, 4173830L, 
30L, 811L, 341014L, 780L, 44749L, 91L, 201620707L, 74L, 1L, 65422L, 
595L, 89093186L, 946520L, 6940919L, 655350L, 4L, 6L, 618L, 2006697L, 
889L, 1398L, 28769L, 90519642L, 984L, 0L, 296209525L, 487088392L, 
5L, 894L, 529L, 5L, 99106L, 2L, 926017L, 9078L, 1L, 21L, 88601017L, 
575770L, 48L, 8431L, 194L, 62324996L, 5L, 81L, 40634727L, 806901520L, 
6818173L, 3501L, 91780L, 36106039L, 5834347L, 58388837L, 34L, 
3280L, 6507606L, 19L, 402L, 584L, 76L, 4078684L, 199L, 6881L, 
92251L, 81715L, 40L, 327L, 57764L, 97668898L, 2676483L, 76L, 
4694L, 817120L, 51L, 116712L, 666L, 3L, 42841L, 9724L, 21L, 4L, 
359L, 2604L, 22L, 30490L, 5640L, 34L, 51923625L, 35544L, 59644L, 
306266049L, 27533545L, 61L, 2L, 5970L, 88728373L, 664355064L, 
30548207L, 254317L, 868L, 460L, 80L, 7003L, 86L, 916570L, 3124L, 
24L, 1217766L, 1L, 95912L, 59634328L, 0L, 761341L, 95L, 5L, 65269L, 
849L, 30408L, 99L, 9L, 78771L, 1288L, 70567L, 4L, 449L, 77L, 
90L, 4520867L, 7492578L, 825466852L, 349445L, 23L, 9094L, 20L, 
233144L, 21L, 82L, 216201L, 15988509L, 8594969L), Peak.Freq = c(87005561L, 
9102L, 994839015L, 42745869L, 32840L, 62737133L, 2722L, 24L, 
67404881L, 999242982L, 3048L, 85315406L, 703037627L, 331264L, 
8403609L, 3934064L, 50578953L, 370110665L, 3414L, 12657L, 40L, 
432L, 7707L, 214L, 68588962L, 69467L, 75L, 500297L, 704L, 1L, 
102659072L, 60896923L, 4481230L, 94124925L, 60164619L, 447L, 
580L, 8L, 172L, 9478521L, 20L, 53L, 3072127L, 2160L, 27301893L, 
8L, 4263L, 508L, 712409L, 50677L, 522433683L, 112844L, 193385L, 
458269L, 93578705L, 22093131L, 6L, 9L, 1690461L, 0L, 4L, 652847L, 
44767L, 21408L, 5384L, 304L, 721L, 651147L, 2426L, 586L, 498289375L, 
945L, 6L, 816L, 46207L, 39135L, 6621028L, 66905L, 26905085L, 
4098L, 0L, 14L, 88L, 530L, 97809006L, 90L, 6L, 260792844L, 9L, 
833205723L, 99467321L, 5L, 8455640L, 54090L, 2L, 309L, 299161148L, 
4952L, 454824L, 729805154L, 395358183L, 2643L, 314568967L, 80599L, 
34187604L, 304L, 86161503L, 8685L, 99892L, 13687L, 54L, 98470989L, 
7L, 55L, 879113565L, 106660L, 902524584L, 71116L, 43689323L, 
863L, 6930L, 5L, 965L, 70107282L, 29588L, 9L, 59L, 39159L, 64446665L, 
6034008L, 96L, 35850L, 670200431L, 17145L, 311804L, 31L, 7747522L, 
2270162L, 611L, 36121267L, 22900823L, 3L, 323L, 844738L, 956L, 
41433111L, 22694748L, 85899937L, 42L, 9497559L), Delta.Freq = c(5L, 
78L, 88553L, 794L, 5L, 3859122L, 782L, 36L, 8756801L, 243169338L, 
817789L, 8792384L, 7431L, 626921743L, 9206L, 95789L, 7916L, 8143453L, 
6L, 4L, 6363L, 181125L, 259618L, 6751L, 33L, 37960L, 0L, 2L, 
599582228L, 565585L, 19L, 48L, 269450424L, 70676581L, 7830566L, 
4L, 86484313L, 21L, 90899794L, 2L, 72356L, 574280L, 869544L, 
73418L, 6468164L, 2259L, 5938505L, 31329L, 1249L, 354L, 8817L, 
3L, 2568L, 82809L, 29836269L, 5230L, 37L, 33752014L, 79307L, 
1736L, 8522076L, 40L, 2289135L, 862L, 801448L, 8026L, 5L, 15L, 
4393771L, 405914L, 71098L, 950288L, 8319L, 1396973L, 832L, 70L, 
1746L, 61907L, 8709547L, 300750537L, 45862L, 91417085L, 79892L, 
47765L, 5477L, 18L, 4186L, 2860L, 754038591L, 375L, 53809223L, 
72L, 136L, 509L, 232325L, 13128104L, 1692L, 8581L, 23L, 7L, 5L, 
1269784L, 55451L, 614683290L, 645L, 1L, 16L, 93L, 19250054L, 
2695713L, 78271L, 7L, 6L, 691373L, 0L, 278L, 42857L, 8L, 94900646L, 
27796603L, 72716820L, 12816272L, 7738L, 596087L, 64091692L, 2261964L, 
371L, 978998L, 127180699L, 94L, 60969L, 23L, 66L, 13L, 26408810L, 
4L, 65L, 8687L, 72140L, 566656L, 263437292L, 9088L, 46496188L, 
215843L, 510L, 51989L, 16736709L, 147L, 8823L, 82L), Delta.Time = c(1361082L, 
7926L, 499L, 5004L, 3494530L, 213L, 64551179L, 70L, 797L, 5L, 
72588L, 86976L, 5163L, 635080L, 3L, 91L, 919806257L, 81443L, 
3135427L, 4410972L, 5810L, 8L, 46603718L, 422L, 1083626L, 48L, 
15699890L, 7L, 90167635L, 446459879L, 2332071L, 761660L, 49218442L, 
381L, 46L, 493197L, 46L, 798597155L, 45342274L, 6265842L, 6L, 
3445819L, 351L, 1761227L, 214L, 959L, 908996387L, 6L, 3855L, 
9096604L, 152664L, 7970052L, 32366926L, 31L, 5201618L, 114L, 
7806411L, 70L, 239L, 5065L, 2L, 1L, 14472831L, 122042249L, 8L, 
495604L, 29L, 8965478L, 2875L, 959L, 39L, 9L, 690L, 933626665L, 
85294L, 580093L, 95934L, 982058L, 65244056L, 137508L, 29L, 7621L, 
7527L, 72L, 2L, 315L, 6L, 2413L, 8625150L, 51298109L, 851L, 890460L, 
160736L, 6L, 850842734L, 2L, 7L, 76969113L, 190536L, 7855L, 3L, 
946706344L, 789239369L, 93L, 802838635L, 291929L, 55L, 52L, 6L, 
93601L, 5L, 179260L, 44006L, 220573L, 42152733L, 7L, 534618382L, 
3L, 768153L, 206921L, 59L, 324331L, 52641160L, 74563L, 24L, 4L, 
24L, 1046L, 862L, 71454L, 98894864L, 27686265L, 72584615L, 3L, 
106L, 2L, 67439L, 6408L, 2160359L, 862L, 998L, 453L, 26179L, 
5857483L, 7796517L, 659L, 261L, 0L, 25779L, 688592899L), Peak.Time = c(1465265L, 
452894L, 545076172L, 8226275L, 5040875L, 700530L, 1L, 3639L, 
20141L, 71712131L, 686L, 923L, 770569738L, 69961L, 737458636L, 
122403L, 199502046L, 6108L, 907L, 108078263L, 7817L, 4L, 6L, 
69L, 721L, 786353L, 87486L, 1563L, 876L, 47599535L, 79295722L, 
53L, 7378L, 591L, 6607935L, 954L, 6295L, 75514344L, 5742050L, 
25647276L, 449L, 328566184L, 4L, 2L, 2703L, 21367543L, 63429043L, 
708L, 782L, 909820L, 478L, 50L, 922L, 579882L, 7850L, 534L, 2157492L, 
96L, 6L, 716L, 5L, 653290336L, 447854237L, 2L, 31972263L, 645L, 
7L, 609909L, 4054695L, 455631L, 4919894L, 9L, 72713L, 9997L, 
84090765L, 89742L, 5L, 5028L, 4126L, 23091L, 81L, 239635020L, 
3576L, 898597785L, 6822L, 3798L, 201999L, 19624L, 20432923L, 
18944093L, 930720236L, 1492302L, 300122L, 143633L, 5152743L, 
417344L, 813L, 55792L, 78L, 14203776L, 853437L, 964147297L, 895424L, 
57L, 361L, 68512L, 5L, 9395L, 53562L, 83436598L, 6372L, 33387L, 
51L, 71L, 2340L, 246145L, 58519L, 15442937L, 362336019L, 213917L, 
5874213L, 4550L, 644668065L, 3712371L, 5928L, 8833L, 7L, 2186023L, 
61627221L, 37297L, 716427989L, 21387L, 26639L, 13428L, 66290997L, 
6657L, 16L, 52256745L, 1179L, 578L, 4602L, 7L, 5L, 23530306L, 
545453L, 6930L, 9120798L, 415L, 229600784L, 8862829L), Center.Freq = c(61907L, 
8709547L, 300750537L, 45862L, 91417085L, 79892L, 47765L, 5477L, 
18L, 4186L, 2860L, 754038591L, 375L, 53809223L, 72L, 136L, 4700783L, 
4173830L, 30L, 811L, 341014L, 780L, 44749L, 91L, 201620707L, 
74L, 1L, 65422L, 595L, 89093186L, 946520L, 6940919L, 48744L, 
2317845L, 5126197L, 2445L, 8L, 557450L, 450259742L, 21006647L, 
9L, 7234027L, 59L, 9L, 651547554L, 45554L, 38493L, 91055218L, 
38L, 1116474L, 2295482L, 3001L, 9L, 3270L, 141L, 53644L, 667983L, 
565598L, 84L, 971L, 555498297L, 60431L, 6597L, 856943893L, 607815536L, 
4406L, 79L, 4885076L, 745L, 8L, 56456L, 125140L, 73043989L, 516476L, 
7L, 4440739L, 754038591L, 375L, 53809223L, 72L, 136L, 509L, 232325L, 
13128104L, 1692L, 8581L, 23L, 5874213L, 4550L, 644668065L, 3712371L, 
5928L, 8833L, 7L, 2186023L, 61627221L, 37297L, 716427989L, 21387L, 
26639L, 13428L, 66290997L, 7747522L, 2270162L, 611L, 36121267L, 
7492578L, 825466852L, 349445L, 23L, 9094L, 20L, 233144L, 21L, 
82L, 7738L, 596087L, 64091692L, 2261964L, 371L, 978998L, 127180699L, 
94L, 60969L, 23L, 66L, 35969L, 44L, 955L, 9L, 43090317L, 3L, 
7905L, 596L, 7262304L, 4246710L, 68L, 0L, 6859264L, 85725268L, 
985L, 861158777L, 95L, 386L, 8773L, 602L, 2740L, 8065L, 61370968L, 
429953765L), Start.Freq = c(426355L, 22073538L, 680374L, 41771L, 
54L, 6762844L, 599171L, 108L, 257451851L, 438814L, 343045L, 4702L, 
967787L, 1937L, 18L, 89301735L, 366L, 90L, 954L, 7337732L, 70891703L, 
4139L, 10397931L, 940000382L, 7L, 38376L, 878528819L, 6287L, 
738366L, 31L, 47L, 5L, 6L, 77848L, 2366508L, 45L, 3665842L, 7252260L, 
6L, 61L, 3247L, 448348L, 1L, 705132L, 144L, 7423637L, 2L, 497L, 
844927639L, 78978L, 914L, 131L, 7089563L, 927L, 9595581L, 2774463L, 
1651L, 73509280L, 7L, 35L, 18L, 96L, 1L, 92545512L, 27354947L, 
7556L, 65019L, 7480L, 71835L, 8249L, 64792L, 71537L, 349389666L, 
280244484L, 82L, 6L, 40L, 353872L, 0L, 103L, 1255L, 4752L, 29L, 
76L, 81185L, 14L, 9L, 470775630L, 818361265L, 57947209L, 44L, 
24L, 41295L, 4L, 261449L, 9931404L, 773556640L, 930717L, 65007421L, 
341175L, 287376310L, 104L, 17L, 45616L, 48L, 0L, 34436L, 413591913L, 
799218L, 81L, 139678L, 87142L, 314040L, 1L, 486L, 30120565L, 
95L, 421252320L, 586616452L, 45L, 2L, 1470L, 391034L, 59182022L, 
75L, 267101L, 267L, 2L, 183169L, 814298L, 2L, 68720184L, 8256L, 
3L, 26054L, 391397L, 5724L, 61464405L, 40455L, 8066605L, 1L, 
474008L, 9577335L, 1L, 443959L, 2454751L, 2250L, 30385546L, 22091L, 
7973L), End.Freq = c(71000996L, 11613579L, 71377155L, 1942738L, 
8760748L, 79L, 455L, 374L, 8L, 5L, 2266932L, 597833L, 155488L, 
3020L, 4L, 554L, 4L, 16472L, 1945649L, 668181101L, 649780L, 22394365L, 
93060602L, 172146L, 20472L, 23558847L, 190513L, 22759044L, 44L, 
78450L, 205621181L, 218L, 69916344L, 23884L, 66L, 312148L, 7710564L, 
4L, 422L, 744572L, 651547554L, 45554L, 38493L, 91055218L, 38L, 
1116474L, 2295482L, 3001L, 9L, 3270L, 141L, 55595L, 38451L, 8660867L, 
14L, 96L, 345L, 6L, 44L, 8235824L, 910517L, 1424326L, 87102566L, 
53644L, 667983L, 565598L, 84L, 971L, 555498297L, 60431L, 6597L, 
856943893L, 607815536L, 4406L, 79L, 7L, 28978746L, 7537295L, 
6L, 633L, 345860066L, 802L, 1035131L, 602L, 2740L, 8065L, 61370968L, 
429953765L, 981507L, 8105L, 343787257L, 44782L, 64184L, 12981359L, 
123367978L, 818775L, 123745614L, 25345654L, 3L, 800889L, 3087787L, 
2L, 143387L, 552269L, 285146L, 86L, 95L, 95207L, 88122L, 999L, 
7L, 206341870L, 11725048L, 7398L, 9686L, 2913230L, 771L, 56634L, 
267L, 87834983L, 1910L, 79718L, 172348L, 5042905L, 47066L, 88747L, 
9668242L, 8175066L, 51L, 4578L, 359354L, 942L, 2244547L, 4799667L, 
44L, 245003L, 31132L, 25L, 58351180L, 1L, 24331L, 74483974L, 
46771553L, 46750L, 6L, 3L, 14L, 31554881L, 1L, 13L), Species = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L), Country = c("Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "France", "France", "France", 
"France", "France", "France", "France", "France", "France", "France", 
"France", "France", "France", "France", "France", "France", "France", 
"France", "France", "France", "France", "France", "France", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "France", 
"France", "France", "France", "France", "France", "France", "France", 
"France", "France", "France", "France", "France", "France", "France", 
"France", "France", "France", "France", "France", "France", "France", 
"France", "France", "France", "France", "France", "France", "France", 
"France", "France", "Holland", "Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland", "Holland", 
"France", "France", "France", "France", "France", "France", "France", 
"France", "France", "France", "France", "France", "France", "France", 
"France", "France", "France", "France", "France", "France", "Holland", 
"Holland", "Holland", "Holland", "Holland", "Holland")), class = "data.frame", row.names = c(NA, 
-150L))

我们可以使用 tidy

而不是 summary
library(purrr)
Outcomes <- setdiff(Outcomes, "Center.Freq") # Centre.Freq column not present
map_dfr(setNames(Outcomes, Outcomes), function(my) {
                     f <- as.formula(paste(my, "~Species", sep=""))
                     broom::tidy(kruskal.test(f, data=New_Acoustic_Parameters))
                              }, .id = "Outcomes")

-输出

# A tibble: 8 × 5
  Outcomes   statistic p.value parameter method                      
  <chr>          <dbl>   <dbl>     <int> <chr>                       
1 Low.Freq       2.03    0.362         2 Kruskal-Wallis rank sum test
2 High.Freq      0.252   0.881         2 Kruskal-Wallis rank sum test
3 Peak.Freq      3.53    0.171         2 Kruskal-Wallis rank sum test
4 Delta.Freq     1.66    0.436         2 Kruskal-Wallis rank sum test
5 Delta.Time     1.35    0.509         2 Kruskal-Wallis rank sum test
6 Peak.Time      2.41    0.299         2 Kruskal-Wallis rank sum test
7 Start.Freq     4.35    0.114         2 Kruskal-Wallis rank sum test
8 End.Freq       2.19    0.334         2 Kruskal-Wallis rank sum test