www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@xreglinear/AnovaTable.m

    function [anova,R2,R2adj] = AnovaTable(m)
%ANOVATABLE calculate ANOVA table for regression
%
% [anova,R2,R2adj] = AnovaTable(m)

%   Copyright 2006 The MathWorks, Inc.

y = m.Store.y;
yhat = eval(m,m.Store.D);

r = y - yhat;
df = dferror(m);
nobs= length(y);
sse = sum(r.^2);  % Residual sum of squares.

if IncludeConst(m)
    ym= mean(y);
    sst = sum((y-ym).^2);     % Total sum of squares
    ssr = sum((yhat-ym).^2);
    dfT= nobs-1;
else
    sst = sum(y.^2);     % Total sum of squares doesn't include mean
    ssr = sum(yhat.^2);
    dfT= nobs;
end
% regression degrees of freedom
dfR= dfT-df;

if df <= 0
    mse = 0;
else
    mse = sse/df;
end

if dfR <= 0
    prob = 1;
    R2   = 0;
    R2adj = 0;
    F    = 0;
    ssr  = 0;
    msr  = 0;
elseif mse == 0
    msr= ssr/dfR;
    F = Inf;
    prob = 0;
    R2 = 1;
    R2adj = 1;
else
    msr= ssr/dfR;
    if sst>0
        R2   = ssr/sst;
        R2adj = 1-(mse/(sst/dfT));
    else
        R2adj = 1 - dfT/df;
        R2  = 1;
    end
    
    F    = msr/mse;
    % Significance probability for regression
    prob=-1;
    % don't calculate F here as this gets called lots of times
    %   prob = 1 - fcdf(F,p-1,nobs-p);   
end

% for rols or ridge ssr and sse not orthoganal so F not valid
anova= [ssr dfT-df msr F prob
    sse df mse 0 0
    sst dfT 0 0 0];