# ANOVA on data with different lengths

I have data in the following form:

{{2.749, 2.649, 2.689, 2.679, 2.679, 2.699, 2.699, 2.699, 2.749,
2.749, 2.779, 2.799, 2.799, 2.799, 2.799, 2.799, 2.799, 2.799,
2.799}, {2.849, 2.9465, 2.899, 2.899, 2.899, 2.849, 2.824, 2.859,
2.899, 2.897, 2.899, 2.849, 2.824, 2.8485, 2.899, 2.8485, 2.92,
2.874, 2.859, 2.891, 2.899, 2.899, 2.899, 2.899, 2.899, 2.899,
2.899, 2.899, 2.899, 2.899, 2.949, 2.899, 2.899, 2.899,
2.979}, {2.998, 2.998, 2.999, 2.999, 2.999, 2.999, 2.999, 2.999,
2.999, 3.097, 3.099, 3.099, 3.099, 3.299, 3.199, 3.199, 3.399}}


These are three different groups of data and I wanted to do an analysis of variance (ANOVA) on them, but I couldn't figure out how to use the ANOVA function in the ANOVA package and after struggling with LinearModelFit I decided to call for help.

It seems that I need to transform the data into something like {{2.749,2.849,2.998},...} in order to use these methods. The trouble is that the (sub)lists are of unequal length, so I don't know how to "fill the gaps".

So, what should I do? Is it possible to run ANOVA in this kind of data? Note that I can easily run ANOVA in R with the exact same data, so why is Mathematica designed in this (maybe unnecessarily complicated) way?

• I really don't mean to start a flame war. I just one to know if I'm missing something like maybe these methods shouldn't really be run on these kind of data. Feb 9, 2015 at 0:51

Yes, you are just inputing the data wrong. ANOVA in mathematica needs to labeled.

I assume you are just placing the list

list ={{2.749, 2.649, 2.689, 2.679, 2.679, 2.699, 2.699, 2.699, 2.749,
2.749, 2.779, 2.799, 2.799, 2.799, 2.799, 2.799, 2.799, 2.799,
2.799}, {2.849, 2.9465, 2.899, 2.899, 2.899, 2.849, 2.824, 2.859,
2.899, 2.897, 2.899, 2.849, 2.824, 2.8485, 2.899, 2.8485, 2.92,
2.874, 2.859, 2.891, 2.899, 2.899, 2.899, 2.899, 2.899, 2.899,
2.899, 2.899, 2.899, 2.899, 2.949, 2.899, 2.899, 2.899,
2.979}, {2.998, 2.998, 2.999, 2.999, 2.999, 2.999, 2.999, 2.999,
2.999, 3.097, 3.099, 3.099, 3.099, 3.299, 3.199, 3.199, 3.399}}

ANOVA[list]


You need to label the data. Assuming each of the sublists is from a separate factor (i.e. "Treatement") then this should do the trick,

labeledlist=Flatten[MapIndexed[{First@#2, #1} &,
{{2.749, 2.649, 2.689, 2.679,
2.679, 2.699, 2.699, 2.699, 2.749, 2.749, 2.779, 2.799, 2.799,
2.799, 2.799, 2.799, 2.799, 2.799, 2.799}, {2.849, 2.9465, 2.899,
2.899, 2.899, 2.849, 2.824, 2.859, 2.899, 2.897, 2.899, 2.849,
2.824, 2.8485, 2.899, 2.8485, 2.92, 2.874, 2.859, 2.891, 2.899,
2.899, 2.899, 2.899, 2.899, 2.899, 2.899, 2.899, 2.899, 2.899,
2.949, 2.899, 2.899, 2.899, 2.979}, {2.998, 2.998, 2.999, 2.999,
2.999, 2.999, 2.999, 2.999, 2.999, 3.097, 3.099, 3.099, 3.099,
3.299, 3.199, 3.199, 3.399}},   {2}], 1]]


{{1, 2.749}, {1, 2.649}, {1, 2.689}, {1, 2.679}, {1, 2.679}, {1, 2.699}, {1, 2.699}, {1, 2.699}, {1, 2.749}, {1, 2.749}, {1, 2.779}, {1, 2.799}, {1, 2.799}, {1, 2.799}, {1, 2.799}, {1, 2.799}, {1, 2.799}, {1, 2.799}, {1, 2.799}, {2, 2.849}, {2, 2.9465}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.849}, {2, 2.824}, {2, 2.859}, {2, 2.899}, {2, 2.897}, {2, 2.899}, {2, 2.849}, {2, 2.824}, {2, 2.8485}, {2, 2.899}, {2, 2.8485}, {2, 2.92}, {2, 2.874}, {2, 2.859}, {2, 2.891}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.949}, {2, 2.899}, {2, 2.899}, {2, 2.899}, {2, 2.979}, {3, 2.998}, {3, 2.998}, {3, 2.999}, {3, 2.999}, {3, 2.999}, {3, 2.999}, {3, 2.999}, {3, 2.999}, {3, 2.999}, {3, 3.097}, {3, 3.099}, {3, 3.099}, {3, 3.099}, {3, 3.299}, {3, 3.199}, {3, 3.199}, {3, 3.399}}

Then putting this in ANOVA gives you the one-way analysis.

ANOVA[labeledlist]


If you want to make sure it is doing it correctly you can check with LinearModelFit[]

test=LinearModelFit[labeledlsit, x, x, NominalVariables -> x]
test["ANOVATable"]


Note that if you do not use NominalVariables, it treats the labels as continuous variables and therefore the statistics are a bit different (However it shouldn't change too much) But this should allow you more control over the model you want to check against.

• Thank you! What about a with LinearModelFit? Feb 9, 2015 at 10:46
• The above labels should also work with LinearModelFit. You may need to set which variables are "Categorical" for the ANOVA test. Feb 9, 2015 at 10:56
• I'm trying this: LinearModelFit[labeledList, {x, y}, {x, y}, NominalVariables -> x] and getting this error: "Number of coordinates (3) is not equal to the number of variables (4).". In any way, thank very much for your help, sir! Feb 9, 2015 at 10:59
• I added the LinearModelFit case, yes, the current data only has one variable, x which can take the values: 1,2 or 3). I encourage you to check the documentation. It is a bit confusing with the labels for ANOVA, but it contain plenty of examples. Feb 9, 2015 at 11:05
• I realized my mistake shortly before your answer. I did check the documentation, but to be honest I find Mathematica's documentation quite confusing sometimes, like this one. Thank you for your help! Marking as correct. Feb 9, 2015 at 11:08