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

    %% Hypothesis Tests for Fixed-Effects Coefficients  

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

%%
% |weight| contains data from a longitudinal study, where 20 subjects are
% randomly assigned to 4 exercise programs, and their weight loss is recorded
% over six 2-week time periods. This is simulated data.  

%% 
% Store the data in a table. Define |Subject| and |Program| as categorical
% variables. 
tbl = table(InitialWeight,Program,Subject,Week,y);
tbl.Subject = nominal(tbl.Subject);
tbl.Program = nominal(tbl.Program);  

%% 
% Fit a linear mixed-effects model where the initial weight, type of program,
% week, and the interaction between the week and type of program are the
% fixed effects. The intercept and week vary by subject. 
lme = fitlme(tbl,'y ~ InitialWeight + Program*Week + (Week|Subject)')  

%% 
% Test for the significance of the interaction between |Program| and |Week|. 
H = [0 0 0 0 0 0 1 0 0; 
     0 0 0 0 0 0 0 1 0;
     0 0 0 0 0 0 0 0 1];
pVal = coefTest(lme,H) 

%%
% The high $p$-value indicates that the interaction between |Program| and
% |Week| is not statistically significant.  

%% 
% Now, test whether all coefficients involving |Program| are 0. 
H = [0 0 1 0 0 0 0 0 0;
     0 0 0 1 0 0 0 0 0;
     0 0 0 0 1 0 0 0 0;
     0 0 0 0 0 0 1 0 0; 
     0 0 0 0 0 0 0 1 0;
     0 0 0 0 0 0 0 0 1];
C = [0;0;0;0;0;0];
pVal = coefTest(lme,H,C) 

%%
% The $p$-value of 0.0274 indicates that not all coefficients involving
% |Program| are zero.