5
$\begingroup$

I have a dataset with 3 groups and two variables. I want to do ANOVA test on the three groups for each variable. Does anyone know how to do this?

Needs["ANOVA`"]
ds = Dataset[{
<|"group" -> "1", "var" -> "var1", "value" -> 6.035053959`|>,
<|"group" -> "1", "var" -> "var1", "value" -> 6.193979765`|>,
<|"group" -> "1", "var" -> "var1", "value" -> Missing["UnknownData"]|>,
<|"group" -> "1", "var" -> "var1", "value" -> 3.3745613910000003`|>,
<|"group" -> "2", "var" -> "var1", "value" -> Missing["UnknownData"]|>,
<|"group" -> "2", "var" -> "var1", "value" -> 0.001071983`|>,
<|"group" -> "2", "var" -> "var1", "value" -> 0.00197519`|>,
<|"group" -> "2", "var" -> "var1", "value" -> 0.000850834`|>,
<|"group" -> "3", "var" -> "var1", "value" -> 0.001295309`|>,
<|"group" -> "3", "var" -> "var1", "value" -> 0.000994449`|>,
<|"group" -> "3", "var" -> "var1", "value" -> 0.003818416`|>,
<|"group" -> "3", "var" -> "var1", "value" -> 0.002378116`|>,
<|"group" -> "1", "var" -> "var2", "value" -> Missing["UnknownData"]|>,
<|"group" -> "1", "var" -> "var2", "value" -> 0.00677702`|>,
<|"group" -> "1", "var" -> "var2", "value" -> 0.001844996`|>,
<|"group" -> "1", "var" -> "var2", "value" -> 0.005386635`|>,
<|"group" -> "2", "var" -> "var2", "value" -> 1.320257649`|>,
<|"group" -> "2", "var" -> "var2", "value" -> 1.090243962`|>,
<|"group" -> "2", "var" -> "var2", "value" -> 1.6674887180000002`|>,
<|"group" -> "2", "var" -> "var2", "value" -> 1.88535474`|>,
<|"group" -> "3", "var" -> "var2", "value" -> 0.446011094`|>,
<|"group" -> "3", "var" -> "var2", "value" -> 0.46027675799999995`|>,
<|"group" -> "3", "var" -> "var2", "value" -> Missing["UnknownData"]|>,
<|"group" -> "3", "var" -> "var2", "value" -> 0.344685377`|>}]

I've tried this simple code, but it doesn't work:

ds[GroupBy[#var &], ANOVA["value"]] /@ {"var1", "var2"}

Thanks!

$\endgroup$
3
$\begingroup$

I think there are a few complications to handle. From the code you proposed, it seems to me that you want to run one-way ANOVA on the two subsets obtained by grouping the data according to the value of the "var" key in the full dataset.

However, to carry out one-way analysis, ANOVA accepts input as a list of lists of two elements, not three, as you can see from its documentation. In addition to that, my attempts indicate that ANOVA will also not handle the Missing data points, so they have to be removed.

Here is a sequence of steps I would suggest:

1) Remove missing data that can't be handled by ANOVA:

DeleteMissing[ds, 1, 1]

removed missing data placeholders

2) Split the original dataset into two datasets according to the value of the "var" column *)

GroupBy[#"var" &][%]

dataset of datasets, one per value of var

3) Extract only the first and third columns from each subsets, because the second column containing the "var" value is now the same for all data points in each subset and therefore superfluous:

%[All, #[[All, {1, 3}]] &]

middle column removed

4) Remove the data labels from the associations to prep the data for ANOVA:

%[All, All, Values]

data labels removed

5) Apply one-way ANOVA to each dataset:

Needs["ANOVA`"]
%[All, ANOVA]

ANOVA results in the dataset format

The above is shown as a dataset, which truncates some of the results. A more traditional result formatting can be obtained with Normal:

% // Normal

ANOVA results in traditional format

$\endgroup$
  • $\begingroup$ Thanks for taking the time! Your suggestion works for the most part, but the DeleteMissing[ds,1,1] does not work as you wrote it. I get an error message saying the the DeleteMissing only works with 1 or 2 arguments. However DeleteMissing[ds,1] does not do anything. Any suggestions? $\endgroup$ – AudunB May 25 '15 at 11:18
  • $\begingroup$ @AudunB That's weird. On second thought, a better choice of arguments would have been DeleteMissing[ds, 1, Infinity], but either this last one, or the original one I posted both work as intended on my end (I've just reloaded your dataset and tried again). DeleteMissing[ds, 1] wouldn't work because it only looks for Missing values at the first level, and yours are deeper than that. What version of MMA are you using? Could you try restarting the kernel just to see whether some leftover definitions are getting in the way? $\endgroup$ – MarcoB May 25 '15 at 11:44
  • $\begingroup$ I'm on version 10.0.1. I tried to restart but still get the error "too many arguments". DeleteMissing[ds,Infinity] sort of works, but it only removes the Missing["UnknownData"] and replaces it with KeyAbsent. $\endgroup$ – AudunB May 25 '15 at 11:54
  • $\begingroup$ That is really weird. Out of curiosity, this (i.imgur.com/GiqRb8V.png) is what I see when I type DeleteMissing in a notebook and then press CTRL+Shift+K to pull up the syntax templates. The last template is clearly the one I am using. I wonder whether you see it as well. $\endgroup$ – MarcoB May 25 '15 at 12:13
  • $\begingroup$ I see this [i.imgur.com/5MvZv48.png]. I clearly is missing the last option that you have. But when using Wolfram Programming Cloud I get the same options as you do. Guess I can use the programming cloud for that particular step. $\endgroup$ – AudunB May 25 '15 at 12:37

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.