www.gusucode.com > stats 源码程序 matlab案例代码 > stats/PerformOneWayANOVAExample.m

    %% Perform One-Way ANOVA
% This example shows how to perform one-way ANOVA to determine whether data
% from several groups have a common mean.
%%
% Load and display the sample data.
load hogg
hogg
%%
% The data comes from a Hogg and Ledolter (1987) study on bacteria
% counts in shipments of milk. The columns of the matrix |hogg| represent
% different shipments. The rows are bacteria counts from cartons of milk
% chosen randomly from each shipment. 
%%
% Test if some shipments have higher counts than others. By default,
% <docid:stats_ug.bulav5i anova1> returns two figures. One is the standard
% ANOVA table, and the other one is the box plots of data by group.
[p,tbl,stats] = anova1(hogg);
p
%%
% The small _p_-value of about 0.0001 indicates that the
% bacteria counts from the different shipments are not the same.
%%
% You can get some graphical assurance that the means are different by
% looking at the box plots. The notches, however, compare the medians, not
% the means. For more information on this display, see
% <docid:stats_ug.bu180jd boxplot>.
%%
% View the standard ANOVA table. |anova1| saves the standard ANOVA table as a cell array in the output
% argument |tbl|.
tbl
%%
% Save the _F_-statistic value in the variable |Fstat|.
Fstat = tbl{2,5}
%%
% View the statistics necessary to make a multiple pairwise
% comparison of group means. |anova1| saves these statistics in the structure |stats|.
stats
%%
% ANOVA rejects the null hypothesis that all group means are equal, so you
% can use the multiple comparisons to determine which
% group means are different  from others. To conduct multiple
% comparison tests, use the function <docid:stats_ug.bujc800 multcompare>,
% which accepts |stats| as an input argument. In this example, |anova1|
% rejects the null hypothesis that the mean bacteria counts from all four
% shipments are equal to each other, i.e., $H_{0}: \mu_{1} = \mu_{2} =
% \mu_{3} = \mu_{4}$. 
%% 
% Perform a multiple comparison test to determine which shipments are
% different than the others in terms of mean bacteria counts.
multcompare(stats)
%%
% The first two columns show which group means are compared with each
% other. For example, the first row compares the means for groups 1 and 2.
% The last column shows the _p_-values for the tests. The _p_-values
% 0.0059, 0.0013, and 0.0001 indicate that the mean bacteria counts in the milk
% from the first shipment is different from the ones from the second,
% third, and fourth shipments. The _p_-value of 0.0292 indicates that the mean
% bacteria counts in the milk from the fourth shipment is different from
% the ones from the fifth. The procedure fails to reject the hypotheses
% that the other group means are different from each other.
%%
% The figure also illustrates the same result. The blue bar shows the
% comparison interval for the first group mean, which does not overlap with
% the comparison intervals for the second, third, and fourth group means,
% shown in red. The comparison interval for the mean of fifth
% group, shown in gray, overlaps with the comparison interval for
% the first group mean. Hence, the group means for the first and fifth
% groups are not significantly different from each other.