0
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – ivanmp Feb 9 '15 at 0:51
2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thank you! What about a with LinearModelFit? $\endgroup$ – ivanmp Feb 9 '15 at 10:46
  • $\begingroup$ The above labels should also work with LinearModelFit. You may need to set which variables are "Categorical" for the ANOVA test. $\endgroup$ – lalmei Feb 9 '15 at 10:56
  • $\begingroup$ 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! $\endgroup$ – ivanmp Feb 9 '15 at 10:59
  • $\begingroup$ 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. $\endgroup$ – lalmei Feb 9 '15 at 11:05
  • $\begingroup$ 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. $\endgroup$ – ivanmp Feb 9 '15 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.