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

    %% Perform N-Way ANOVA
% This example shows how to perform N-way ANOVA on car data with mileage
% and other information on 406 cars made between 1970 and 1982.
%%
% Load the sample data.

% Copyright 2015 The MathWorks, Inc.

load carbig
%%
% The example focusses on four variables. |MPG| is the number of miles per
% gallon for each of 406 cars (though some have missing values coded as
% |NaN|). The other three variables are factors: |cyl4| (four-cylinder car
% or not), |org| (car originated in Europe, Japan, or the USA), and |when|
% (car was built early in the period, in the middle of the period, or late
% in the period).
%%
% Fit the full model, requesting up to three-way interactions and Type 3
% sums-of-squares.
varnames = {'Origin';'4Cyl';'MfgDate'};
anovan(MPG,{org cyl4 when},3,3,varnames)
%%
% Note that many terms are marked by a # symbol as not having full rank,
% and one of them has zero degrees of freedom and is missing a _p_-value.
% This can happen when there are missing factor combinations and the model
% has higher-order terms. In this case, the cross-tabulation below shows
% that there are no cars made in Europe during the early part of the period
% with other than four cylinders, as indicated by the 0 in |tbl(2,1,1)|.
[tbl,chi2,p,factorvals] = crosstab(org,when,cyl4)
%%
% Consequently it is impossible to estimate the three-way interaction
% effects, and including the three-way interaction term in the model makes
% the fit singular.
%%
% Using even the limited information available in the ANOVA table, you can
% see that the three-way interaction has a _p_-value of 0.699, so it is
% not significant.
%%
% Examine only two-way interactions.
[p,tbl2,stats,terms] = anovan(MPG,{org cyl4 when},2,3,varnames);
terms
%%
% Now all terms are estimable. The _p_-values for interaction term 4
% (|Origin*4Cyl|) and interaction term 6 (|4Cyl*MfgDate|) are much larger
% than a typical cutoff value of 0.05, indicating these terms are not
% significant. You could choose to omit these terms and pool their effects
% into the error term. The output |terms| variable returns a matrix of
% codes, each of which is a bit pattern representing a term.
%%
% Omit terms from the model by deleting their entries from |terms|.
terms([4 6],:) = []
%%
% Run |anovan| again, this time supplying the resulting vector as the
% model argument. Also return the statistics required for multiple
% comparisons of factors.
[~,~,stats] = anovan(MPG,{org cyl4 when},terms,3,varnames)
%%
% Now you have a more parsimonious model indicating that the mileage of
% these cars seems to be related to all three factors, and that the effect
% of the manufacturing date depends on where the car was made.
%%
% Perform multiple comparisons for Origin and Cylinder.
results = multcompare(stats,'Dimension',[1,2])