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

    %% Plot Different Categorical Levels for Linear Model  
% Fit a mileage model to the |carsmall| data, including the |Year| categorical
% predictor. Superimpose fitted curves on a scatter plot of the data.   

%% 
% Load the data and fit a model. 
load carsmall
tbl = table(MPG,Weight);
tbl.Year = ordinal(Model_Year);
mdl = fitlm(tbl,'MPG ~ Year + Weight^2');  

%% 
% Create a scatter plot of the mileage versus weight. 
gscatter(tbl.Weight,tbl.MPG,tbl.Year);     

%% 
% Use |feval| to plot curves of the model predictions for the various years
% and weights. 
w = linspace(min(tbl.Weight),max(tbl.Weight))';
line(w,feval(mdl,w,'70'),'Color','r')
line(w,feval(mdl,w,'76'),'Color','g')
line(w,feval(mdl,w,'82'),'Color','b')