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

    %% Assess Fit of Model Using F-statistic  
% This example shows how to use assess the fit of the model and the significance
% of the regression coefficients using F-statistic.   

% Copyright 2015 The MathWorks, Inc.


%% 
% Load the sample data. 
load carbig
tbl = table(Acceleration,Cylinders,Weight,MPG);
tbl.Cylinders = ordinal(Cylinders);  

%% 
% Fit a linear regression model. 
mdl = fitlm(tbl,'MPG~Acceleration*Weight+Cylinders+Weight^2') 

%%
% The F-statistic of the linear fit versus the constant model is 139, with
% a _p_-value of 2.94e-109. The model is significant at the 5% significance
% level. The R-squared value of 0.741 means the model explains about 74%
% of the variability in the response.  

%% 
% Display the ANOVA table for the fitted model. 
anova(mdl,'summary') 

%%
% This display separates the variability in the model into linear and nonlinear
% terms. Since there are two non-linear terms (|Weight^2| and the interaction
% between |Weight| and |Acceleration|), the nonlinear degrees of freedom
% in the |DF| column is 2. There are six linear terms in the model (four
% |Cylinders| indicator variables, |Weight|, and |Acceleration|). The corresponding
% F-statistics in the |F| column are for testing the significance of the
% linear and nonlinear terms as separate groups. 

%%
% The residual term is also separated into two parts; first is the error
% due to the lack of fit, and second is the pure error independent from
% the model, obtained from the replicated observations. The corresponding
% F-statistics in the |F| column are for testing the lack of fit, that is,
% whether the proposed model is an adequate fit or not.  

%% 
% Display the ANOVA table for the model terms. 
anova(mdl) 

%%
% This display decomposes the ANOVA table into the model terms. The corresponding
% F-statistics in the |F| column are for assessing the statistical significance
% of each term. The F-test for |Cylinders| test whether at least one of
% the coefficients of indicator variables for cylinders categories is different
% from zero or not. That is, whether different numbers of cylinders have
% a significant effect on |MPG| or not. The degrees of freedom for each
% model term is the numerator degrees of freedom for the corresponding F-test.
% Most of the terms have 1 degree of freedom, but the degrees of freedom
% for |Cylinders| is 4. Because there are four indicator variables for this
% term.