Categories &

Functions List

Function Reference: anova1

statistics: p = anova1 (x)
statistics: p = anova1 (x, group)
statistics: p = anova1 (x, group, displayopt)
statistics: p = anova1 (x, group, displayopt, vartype)
statistics: [p, atab] = anova1 (x, …)
statistics: [p, atab, stats] = anova1 (x, …)

Perform a one-way analysis of variance (ANOVA) for comparing the means of two or more groups of data under the null hypothesis that the groups are drawn from distributions with the same mean. For planned contrasts and/or diagnostic plots, use anovan instead.

anova1 can take up to three input arguments:

  • x contains the data and it can either be a vector or matrix. If x is a matrix, then each column is treated as a separate group. If x is a vector, then the group argument is mandatory.
  • group contains the names for each group. If x is a matrix, then group can either be a cell array of strings of a character array, with one row per column of x. If you want to omit this argument, enter an empty array ([]). If x is a vector, then group must be a vector of the same length, or a string array or cell array of strings with one row for each element of x. x values corresponding to the same value of group are placed in the same group.
  • displayopt is an optional parameter for displaying the groups contained in the data in a boxplot. If omitted, it is ’on’ by default. If group names are defined in group, these are used to identify the groups in the boxplot. Use ’off’ to omit displaying this figure.
  • vartype is an optional parameter to used to indicate whether the groups can be assumed to come from populations with equal variance. When vartype is 'equal' the variances are assumed to be equal (this is the default). When vartype is 'unequal' the population variances are not assumed to be equal and Welch’s ANOVA test is used instead.

    vartype is an Octave extension: MATLAB’s anova1 takes no fourth argument. It does not error on one either, it accepts it and ignores it, returning the same p and F for 'unequal' as for 'equal'. Code written against this function and then run in MATLAB therefore gets the classic ANOVA silently, with no diagnostic of any kind. Note that anova2’s analogous fourth argument does make MATLAB error, so the silence here is particular to anova1.

anova1 can return up to three output arguments:

  • p is the p-value of the null hypothesis that all group means are equal.
  • atab is a cell array containing the results in a standard ANOVA table.
  • stats is a structure containing statistics useful for performing a multiple comparison of means with the MULTCOMPARE function.

A categorical group may declare levels that no observation uses. Such a level takes no part in the analysis and is dropped from every field of stats, so n, means and gnames always describe the same groups, in the same order, and can be indexed together.

This is a deliberate deviation from MATLAB, which drops an unused level from gnames but keeps it in n and means as a count of zero and a mean of NaN. Those fields then disagree in length and the group indices run past gnames, so MATLAB’s own multcompare reports comparisons against a group holding no observations and labels them with indices that its gnames cannot resolve.

If anova1 is called without any output arguments, then it prints the results in a one-way ANOVA table to the standard output. It is also printed when displayopt is ’on’.

Examples:

 
 x = meshgrid (1:6);
 x = x + normrnd (0, 1, 6, 6);
 anova1 (x, [], 'off');
 [p, atab] = anova1(x);
 
 x = ones (50, 4) .* [-2, 0, 1, 5];
 x = x + normrnd (0, 2, 50, 4);
 groups = {"A", "B", "C", "D"};
 anova1 (x, groups);

See also: anova2, anovan, multcompare

Source Code: anova1

 rng (42);
 x = meshgrid (1:6);
 x = x + normrnd (0, 1, 6, 6);
 anova1 (x, [], 'off');
                      ANOVA Table

Source        SS      df        MS       F      Prob>F
------------------------------------------------------
Groups    111.9398     5    22.3880    18.42    0.0000
Error      36.4629    30     1.2154
Total     148.4027    35
 rng (42);
 x = meshgrid (1:6);
 x = x + normrnd (0, 1, 6, 6);
 [p, atab] = anova1 (x);
                      ANOVA Table

Source        SS      df        MS       F      Prob>F
------------------------------------------------------
Groups    111.9398     5    22.3880    18.42    0.0000
Error      36.4629    30     1.2154
Total     148.4027    35
plotted figure

 rng (42);
 x = ones (50, 4) .* [-2, 0, 1, 5];
 x = x + normrnd (0, 2, 50, 4);
 groups = {'A', 'B', 'C', 'D'};
 anova1 (x, groups);
                      ANOVA Table

Source        SS      df        MS       F      Prob>F
------------------------------------------------------
Groups   1037.3570     3   345.7857    81.63    0.0000
Error     830.2136   196     4.2358
Total    1867.5706   199
plotted figure

 y = [54 87 45; 23 98 39; 45 64 51; 54 77 49; 45 89 50; 47 NaN 55];
 g = [1  2  3 ; 1  2  3 ; 1  2  3 ; 1  2  3 ; 1  2  3 ; 1  2  3 ];
 anova1 (y(:), g(:), 'on', 'unequal');
           Welch's ANOVA Table

Source        F     df     dfe     Prob>F
-----------------------------------------
Groups     15.52     2    7.58     0.0021
plotted figure