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

    %% Multiple Comparisons for Three-Way ANOVA
% Load the sample data.

% Copyright 2015 The MathWorks, Inc.

y = [52.7 57.5 45.9 44.5 53.0 57.0 45.9 44.0]';
g1 = [1 2 1 2 1 2 1 2];
g2 = {'hi';'hi';'lo';'lo';'hi';'hi';'lo';'lo'};
g3 = {'may';'may';'may';'may';'june';'june';'june';'june'};
%%
% |y| is the response vector and |g1|, |g2|, and |g3| are the grouping
% variables (factors). Each factor has two levels, and every observation in
% |y| is identified by a combination of factor levels. For example,
% observation |y(1)| is associated with level 1 of factor |g1|, level
% |'hi'| of factor |g2|, and level |'may'| of factor |g3|. Similarly,
% observation |y(6)| is associated with level 2 of factor |g1|, level
% |'hi'| of factor |g2|, and level |'june'| of factor |g3|.
%%
% Test if the response is the same for all factor levels. Also compute the
% statistics required for multiple comparison tests.
[~,~,stats] = anovan(y,{g1 g2 g3},'model','interaction',...
    'varnames',{'g1','g2','g3'});
%%
% The _p_-value of 0.2578 indicates that the mean responses for levels
% |'may'| and |'june'| of factor |g3| are not significantly different.
% The _p_-value of 0.0347 indicates that the mean responses for levels |1|
% and |2| of factor |g1| are significantly different. Similarly, the
% _p_-value of 0.0048 indicates that the mean responses for levels |'hi'|
% and |'lo'| of factor |g2| are significantly different.
%%
% Perform multiple comparison tests to find out which groups of the factors
% |g1| and |g2| are significantly different.
results = multcompare(stats,'Dimension',[1 2])
%%
% <docid:stats_ug.bujc800 multcompare> compares the combinations of groups
% (levels) of the two grouping variables, |g1| and |g2|. In the |results|
% matrix, the number 1 corresponds to the combination of level |1| of |g1|
% and level |hi| of |g2|, the number 2 corresponds to the combination of
% level |2| of |g1| and level |hi| of |g2|. Similarly, the number 3
% corresponds to the combination of level |1| of |g1| and level |lo| of
% |g2|, and the number 4 corresponds to the combination of level |2| of
% |g1| and level |lo| of |g2|. The last column of the matrix contains the
% _p_-values.
%
% For example, the first row of the matrix shows that the combination of
% level |1| of |g1| and level |hi| of |g2| has the same mean response
% values as the combination of level |2| of |g1| and level |hi| of |g2|.
% The _p_-value corresponding to this test is 0.0280, which indicates
% that the mean responses are significantly different. You can also see this result in the
% figure. The blue bar shows the comparison interval for the mean
% response for the combination of level |1| of |g1| and level |hi| of |g2|.
% The red bars are the comparison intervals for the mean response for
% other group combinations. None of the red bars overlap with the blue bar,
% which means the mean response for the combination of level |1| of |g1|
% and level |hi| of |g2| is significantly different from the mean response
% for other group combinations.
%
% You can test the other groups by clicking on the corresponding comparison
% interval for the group. The bar you click on turns to blue. The bars for
% the groups that are significantly different are red. The bars for the
% groups that are not significantly different are gray. For example, if you
% click on the comparison interval for the combination of level |1| of |g1|
% and level |lo| of |g2|, the comparison interval for the combination of
% level |2| of |g1| and level |lo| of |g2| overlaps, and is therefore gray.
% Conversely, the other comparison intervals are red, indicating
% significant difference.