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

    %% F-Tests for Fixed Effects   

%% 
% Load the sample data. 
load(fullfile(matlabroot,'examples','stats','shift.mat'))

%%
% The data shows the deviations from the target quality characteristic measured
% from the products that five operators manufacture during three shifts:
% morning, evening, and night. This is a randomized block design, where
% the operators are the blocks. The experiment is designed to study the
% impact of the time of shift on the performance. The performance measure
% is the deviation of the quality characteristics from the target value.
% This is simulated data.  

%% 
% |Shift| and |Operator| are nominal variables. 
shift.Shift = nominal(shift.Shift);
shift.Operator = nominal(shift.Operator);  

%% 
% Fit a linear mixed-effects model with a random intercept grouped by operator
% to assess if performance significantly differs according to the time of
% the shift. Use the restricted maximum likelihood method and |'effects'|
% contrasts. 
%
% |'effects'| contrasts indicate that the coefficients sum to 0, and |fitlme|
% creates two contrast-coded variables in the fixed-effects design matrix,
% $X$1 and $X$2, where 
%
% $$Shift{\rm{\_}}Evening = \left\{ {\begin{array}{*{20}{c}}
% {0,\quad if\;Morning}\\
% {1,\quad if\;Evening}\\
% { -1,\quad if\;Night}
% \end{array}}\right.$$
%
% and
%
% $$Shift{\rm{\_}}Morning = \left\{ {\begin{array}{*{20}{c}}
% {1,\quad if\;Morning}\\
% {0,\quad if\;Evening}\\
% { - 1,\quad if\;Night }
% \end{array}}\right..$$
%
% The model corresponds to 
%
% $$\begin{array}{l}
% {\rm{Morning Shift: }}QCDe{v_{im}} = {\beta _0} + {\beta _2}Shift{\rm{\_}}Mornin{g_i} + {b_{0m}} + {\varepsilon _{im}},\quad m = 1,2,...,5,\\
% {\rm{Evening Shift: }}QCDe{v_{im}} = {\beta _0} + {\beta _1}Shift{\rm{\_}}Evenin{g_i} + {b_{0m}} + {\varepsilon _{im}},\\
% {\rm{Night Shift:  }}\quad QCDe{v_{im}} = {\beta _0} - {\beta _1}Shift{\rm{\_}}Evenin{g_i} - {\beta _2}Shift{\rm{\_}}Mornin{g_i} + {b_{0m}} + {\varepsilon _{im}},
% \end{array}$$
%
% where $b$ ~ N(0, $\sigma^{2}_{b}$ ) and $\epsilon$
% ~ N(0, $\sigma^{2}$ ). 
lme = fitlme(shift,'QCDev ~ Shift + (1|Operator)',...
'FitMethod','REML','DummyVarCoding','effects')  

%% 
% Perform an $F$-test to determine if all fixed-effects coefficients are 0. 
anova(lme) 

%%
% The $p$-value for the constant term, 0.0021832, is the same as in the
% coefficient table in the |lme| display. The $p$-value of 0.0018721 for
% |Shift| measures the combined significance for both coefficients representing
% |Shift|.