One-way ANOVA for each sub-group in a melted data frame

ε祈祈猫儿з 提交于 2021-01-28 17:58:30

问题


I have a very large data set that requires individual one-way ANOVAs for multiple species on 4 different treatments with several measurements. Usually I just make separate excel spread sheets for each species and run ANOVAs on each, looping through each measurement column, but this is very time consuming. Is it possible to use a single spread sheet and run ANOVAs on the melted data? Or perhaps there is another reshape option I could use?

Data:

structure(list(Species = c("A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "D", 
"D", "D", "D", "D", "D", "D", "D", "D", "D"), TREATMENT = c(1, 
1, 1, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 1, 
1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4), 
`CLASS 1` = c(9.3993303987076, 8.14588087483834, 8.44889021858093, 
28.2773809415175, 9.49156649568952, 37.406663111623, 8.42458221212166, 
35.529904738035, 33.1401135085753, 8.26977630375797, 7.87786527094827, 
7.83020300515061, 35.1465417538538, 10.5560853720815, 7.54702433773332, 
7.15030081390987, 7.73624654623485, 33.6461639540039, 10.3098164094602, 
7.79017325570062, 8.47473750173462, 8.37179798600773, 31.7364310355766, 
2.00147496567679, 9.30194886619568, 7.87886829173726, 7.93445694220837, 
9.10020522660375, 8.81542855137005, 7.83313314713951, 7.84449591023115, 
23.6150030864875, 9.3452854347794, 8.91047098149179, 7.76031738257155, 
9.79467065201063, 24.7592334362831, 8.54842834366722, 7.60436112798701, 
8.93480758329653, 8.72406315335014, 9.49850179222777), `CLASS 2` = c(10.8069912074565, 
4.52426389123869, 8.13120921128287, 52.3870196313339, 7.17369219206721, 
45.7325224336886, 17.8345921677786, 38.4579761235057, 37.5916934855387, 
6.28803058195647, 3.72788988807285, 4.64744990904241, 29.7689968962103, 
7.08515103144071, 6.44277647222835, 5.71017728280462, 6.28290843412007, 
45.6123170472575, 6.98431855663527, 8.03809625184267, 4.76656440828616, 
6.74640254081232, 31.3243238213156, 45.1287867136276, 7.6308508343969, 
4.0127554151831, 9.11910102221636, 6.1658394708941, 10.4617259648364, 
6.07502685224869, 8.08373642262043, 48.588633863193, 10.2160085507338, 
7.52606530219909, 5.66373884014351, 8.51992766801391, 25.9109062123364, 
5.74498954209992, 5.56377323143979, 7.76698847227212, 7.05016373786876, 
7.99745310894107), `CLASS 3` = c(3.96856956332584, 2.76052305637364, 
6.92715392916015, 0.687821057043984, 2.30154255462355, 2.61089063893911, 
11.2199145273738, 10.7058533354417, 1.90691767773411, 3.93488282297868, 
1.7034110083142, 1.69310511636903, 1.54005861925764, 50.436990190291, 
3.93233520754151, 4.06684782901502, 6.10592204678281, 0.675086986967025, 
3.94018776658881, 5.74129993338595, 2.02845185559621, 4.10963382465756, 
14.9264019576272, 12.9672579626868, 5.1049208042632, 1.37282635713804, 
3.00088572108344, 4.78878116348504, 4.79564218319094, 3.03836532949481, 
3.48474205480686, 1.09218910757234, 6.2830307568812, 3.06784943090836, 
4.89376208853059, 6.6321148581705, 1.01356027363186, 3.15439940439419, 
40.8141653079423, 2.52825000616702, 1.65382018138259, 1.81173455682492
), `CLASS 4` = c(0, 13.4274810838142, 10.9876140536356, 3.15424686759082, 
15.2632739415738, 0, 0, 2.39525969535064, 4.19386122886851, 
13.15599261724, 14.5421891905919, 14.4542067660843, 0, 0, 
12.2276086827261, 12.7527880016103, 12.1436697242409, 3.79216208516423, 
0, 12.2283190622827, 16.0271803699645, 14.035876401479, 4.24556176551009, 
0, 0, 14.4993393432366, 13.6722412691012, 0, 14.0027443968931, 
13.7579074961889, 12.9935353616471, 4.66128854387559, 0, 
15.1941922851023, 11.6990009190362, 0, 7.99399142573694, 
14.5041748372822, 0, 15.0674109079436, 13.7134908002476, 
15.4194201146961), `CLASS 5` = c(7.82638584740367, 6.56112678542475, 
6.95253086439919, 2.06445951884762, 7.17086660532553, 2.58627258328855, 
7.83400556063298, 1.77053879587063, 2.65292759651742, 6.94701807830366, 
6.85309102458439, 6.71505104532983, 4.06818278652755, 5.79906266122279, 
6.62064468061089, 6.88365856613044, 7.68403751285005, 2.38479005191691, 
9.07405520739349, 7.65785587918449, 7.4385885335047, 7.30144390122309, 
2.41680929257195, 4.18258704279641, 7.8906816661241, 6.75678558060943, 
6.67150537517493, 8.24794113296791, 7.67443442992891, 6.89357008866252, 
6.45444668132533, 2.98342694785768, 8.704729108357, 7.14382850099481, 
7.15233553294014, 9.14001781571836, 3.98831954045444, 7.54093786042356, 
5.79029360470226, 6.82793163574773, 6.48049736162586, 7.18554914992982
), `CLASS 6` = c(20.6189597026452, 22.8728557858066, 23.0767150659042, 
4.99832103176657, 24.1726463550235, 5.56104550736533, 31.8124013284184, 
4.31653191057476, 8.4695331411828, 20.63468068931, 19.7369752322083, 
19.6902616040991, 11.6648564225744, 0, 25.2321582223958, 
22.2981543181678, 29.3198455372777, 5.88723409877159, 30.1474816315191, 
28.0835788057802, 24.0430626320328, 25.1446564854412, 5.78713327050339, 
30.6155806819949, 23.8853696442419, 20.1783872969561, 17.5935515655693, 
23.4169038776536, 21.1986239116884, 19.6931330316831, 22.2658181144794, 
7.38944654414811, 32.1897387187698, 23.6398829158785, 25.3561697324352, 
30.4118856020653, 12.6822088903071, 26.300118251779, 11.7338836812169, 
23.8624555097246, 20.1037712460599, 21.8478004507985), `CLASS 7` = c(15.9129851563051, 
15.2250454288061, 13.5577123002506, 2.9902563940573, 15.4408266617369, 
2.67511425705514, 8.17164465017573, 2.23047357314211, 4.01010767344732, 
13.4046459481448, 15.3008244637288, 15.3885729336047, 7.81496654756214, 
17.8194559247092, 12.7823202355514, 13.7684066964868, 15.378473991847, 
3.75026919344972, 18.2880822635935, 14.7412162942703, 17.5270089738067, 
16.799718650752, 4.33839497916674, 2.21937177530762, 15.0315149187176, 
15.3632530721031, 14.1580725482114, 16.4215442147509, 15.5113323256627, 
14.3349000132624, 12.8504657216928, 5.06281347160092, 15.5075336560533, 
15.9392345541138, 13.3981839319596, 16.6700105346756, 8.10398633871805, 
15.958090408468, 16.5733149488757, 15.1802203155931, 14.2236219296677, 
16.2095182295187), `CLASS 8` = c(19.9174685533413, 16.6755018156139, 
13.9892072522183, 3.35339208579287, 18.98558519396, 3.42749146804023, 
10.4801793890691, 2.97802997775506, 5.11270635117451, 17.0372757040089, 
18.7865491767228, 18.3992789502607, 9.99639697401416, 0, 
15.9270550696003, 17.1615519869107, 15.3488962066467, 4.25197658246908, 
21.2560581648095, 15.7194605175531, 19.6944057250743, 17.4904702096271, 
5.22494387772846, 2.88494085790995, 19.1038328534942, 19.0183655117756, 
17.533290326259, 19.92632149392, 17.5400682364295, 17.664926273487, 
16.3075864395099, 6.6071984352649, 17.7536737744256, 18.5784760293114, 
14.706720581834, 18.8313728693457, 9.73353207739478, 18.2488613518859, 
8.53356517614357, 19.8319355692553, 17.4801581342745, 20.0300225970631
), `CLASS 9` = c(11.5493095708147, 9.80732127808386, 7.92896710456816, 
2.08710247204941, 0, 0, 4.22268016442976, 1.61543185032431, 
2.92213933696131, 10.3276972542995, 11.4712047448286, 11.1818706700593, 
0, 8.30325482025479, 9.28807709161222, 10.2081145049644, 
0, 0, 0, 0, 0, 0, 0, 0, 12.0508804125665, 10.9194191312608, 
10.316895230176, 11.9324634197247, 0, 10.709037767833, 9.7151732936871, 
0, 0, 0, 9.36977099054923, 0, 5.81426180513736, 0, 3.38664292169246, 
0, 10.5704134555229, 0), `CLASS 10` = c(44.7938508721352, 
51.7310046920715, 57.5715824785637, 89.5047895292528, 58.4027215389776, 
91.3111216916161, 69.2914902356924, 91.4055258029079, 85.3021190418994, 
52.2833630152431, 47.5883305901355, 48.3152264007455, 78.1204536918961, 
68.0782265938132, 55.3819029226251, 51.9782682455077, 61.5885922886562, 
89.6129641721643, 51.3818043642034, 61.8814673089921, 55.3399967676143, 
58.4083672383978, 88.0198518505328, 90.713100323986, 45.9230901490977, 
47.942176704251, 51.3202365201787, 43.4717297386365, 59.2741650079789, 
50.3975658567551, 54.6723278637849, 85.3465611452765, 58.0340634611641, 
58.33846091558, 55.372988962717, 55.3585987802603, 72.3599002382954, 
58.2521103792226, 65.716183348586, 58.1599124794039, 51.2453091189091, 
56.5749100234884), `CLASS 11` = c(55.2061491278648, 48.2689953079285, 
42.4284175214362, 10.4952104707472, 41.5972784610224, 8.68887830838393, 
30.7085097643076, 8.59447419709211, 14.6978809581006, 47.7166369847569, 
52.4116694098645, 51.6847735992545, 21.8795463081039, 31.9217734061868, 
44.6180970773749, 48.0217317544923, 38.4114077113438, 10.3870358278357, 
48.6181956357966, 38.1185326910079, 44.6600032323857, 41.5916327616022, 
11.9801481494672, 9.28689967601398, 54.0769098509023, 52.0578232957489, 
48.6797634798213, 56.5282702613635, 40.7258349920211, 49.6024341432449, 
45.3276721362151, 14.6534388547235, 41.9659365388359, 41.66153908442, 
44.627011037283, 44.6414012197397, 27.6400997617046, 41.7478896207774, 
34.283816651414, 41.8400875205961, 48.7546908810909, 43.4250899765116
), `CLASS 12` = c(0.811392418775427, 1.07172325344784, 1.35691090645737, 
8.52815575054215, 1.40400342762093, 10.5089654211764, 2.25642633809048, 
10.6353831202186, 5.80370185913679, 1.09570511081795, 0.907972043744494, 
0.934805805194479, 3.57047868323309, 2.13265803649301, 1.24124305047309, 
1.08239054166649, 1.60339326148851, 8.62738568129464, 1.05684309531167, 
1.62339583767845, 1.23914000811097, 1.40432975000493, 7.34714218491929, 
9.76785617252635, 0.849218090969217, 0.920940862853288, 1.05424169822542, 
0.769026356858985, 1.45544382379371, 1.01603009463636, 1.20615785649631, 
5.82433666195463, 1.38288498357373, 1.40029538508808, 1.24079537651438, 
1.24007305478085, 2.61793194894868, 1.3953306600253, 1.91682810629766, 
1.39005236188319, 1.05108468934595, 1.30281618424025)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -42L), .Names = c("Species", 
"TREATMENT", "CLASS 1", "CLASS 2", "CLASS 3", "CLASS 4", "CLASS 5", 
"CLASS 6", "CLASS 7", "CLASS 8", "CLASS 9", "CLASS 10", "CLASS 11", 
"CLASS 12"))

library(Reshape2)
melt <- melt(example, id=c("TREATMENT","Species"), value.name="Percentage", variable.name = "Class")

回答1:


You can use the dplyr package to run an ANOVA on each combination of Species and Class.

library(reshape2)
library(dplyr)
library(broom)

datm <- melt(example, id=c("TREATMENT","Species"), value.name="Percentage", variable.name = "Class")

Now we run an ANOVA for each combination of Species and Class using the group_by function to break the data up into the desired groupings. This returns a data frame where the first two columns are the Species and Class and each element of the third column is a list with the aov model output.

res = datm %>% group_by(Species, Class) %>% 
  do(Model = aov(Percentage ~ TREATMENT, data=.))

Get results in a tidy data frame.

tidy(res, Model)
   Species    Class      term df        sumsq       meansq    statistic    p.value
1        A  CLASS 1 TREATMENT  1 1.660188e+02 166.01875057 0.9697826054 0.35357679
2        A  CLASS 1 Residuals  8 1.369534e+03 171.19171827           NA         NA
3        A  CLASS 2 TREATMENT  1 2.227081e+02 222.70809890 0.6192884560 0.45399125
4        A  CLASS 2 Residuals  8 2.876955e+03 359.61932885           NA         NA
...
93       D CLASS 11 TREATMENT  1 7.653510e+00   7.65350993 0.1968918980 0.66899748
94       D CLASS 11 Residuals  8 3.109731e+02  38.87163469           NA         NA
95       D CLASS 12 TREATMENT  1 2.799650e-02   0.02799650 0.1232651091 0.73458657
96       D CLASS 12 Residuals  8 1.816994e+00   0.22712429           NA         NA


来源:https://stackoverflow.com/questions/37902978/one-way-anova-for-each-sub-group-in-a-melted-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!