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

    %% ANOVA for Fixed-Effects in LME Model  

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

%%
% The dataset array includes data from a split-plot experiment, where soil
% is divided into three blocks based on the soil type: sandy, silty, and
% loamy. Each block is divided into five plots, where five types of tomato
% plants (cherry, heirloom, grape, vine, and plum) are randomly assigned
% to these plots. The tomato plants in the plots are then divided into subplots,
% where each subplot is treated by one of four fertilizers. This is simulated
% data.  

%% 
% Store the data in a dataset array called |ds|, for practical purposes,
% and define |Tomato|, |Soil|, and |Fertilizer| as categorical variables. 
ds = fertilizer;
ds.Tomato = nominal(ds.Tomato);
ds.Soil = nominal(ds.Soil);
ds.Fertilizer = nominal(ds.Fertilizer);  

%% 
% Fit a linear mixed-effects model, where |Fertilizer| and |Tomato| are
% the fixed-effects variables, and the mean yield varies by the block (soil
% type) and the plots within blocks (tomato types within soil types) independently.
% Use the |'effects'| contrasts when fitting the data for the type III sum
% of squares. 
lme = fitlme(ds,'Yield ~ Fertilizer * Tomato + (1|Soil) + (1|Soil:Tomato)',...
'DummyVarCoding','effects')  

%% 
% Perform an analysis of variance to test for the fixed-effects. 
anova(lme) 

%%
% The $p$-value for the constant term, 5.9086e-30, is the same as in the
% coefficient table in the |lme| display. The $p$-values of 0.00018935,
% 1.0024e-14, and 0.19804 for |Tomato|, |Fertilizer|, and |Tomato:Fertilizer|
% represent the combined significance for all tomato coefficients, fertilizer
% coefficients, and coefficients representing the interaction between the
% tomato and fertilizer, respectively. The $p$-value of 0.19804 indicates
% that the interaction between tomato and fertilizer is not significant.