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

    %% Test Fixed-Effects Coefficients for Categorical Data  

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

%%
% The data shows the absolute deviations from the target quality characteristic
% measured from the products that five operators manufacture during three
% different 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 absolute 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 there is significant difference in the performance according
% to the time of the shift. 
lme = fitlme(shift,'QCDev ~ Shift + (1|Operator)')  

%% 
% Test if all fixed-effects coefficients except for the intercept are 0. 
pVal = coefTest(lme) 

%%
% The small $p$-value indicates that not all fixed-effects coefficients are 0.  

%% 
% Test the significance of the |Shift| term using a contrast matrix. 
H = [0 1 0; 0 0 1];
pVal = coefTest(lme,H)  

%% 
% Test the significance of the |Shift| term using the |anova| method. 
anova(lme) 

%%
% The $p$-value for |Shift|, 0.00075956, is the same as the $p$-value of
% the previous hypothesis test.  

%% 
% Test if there is any difference between the evening and morning shifts. 
pVal = coefTest(lme,[0 1 -1]) 

%%
% This small $p$-value indicates that the performance of the operators are
% not the same in the morning and the evening shifts.