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

    %% Diagnostic Plots
% This example shows how to use diagnostic plots to examine your fit.
%%
% Load the sample data.

% Copyright 2015 The MathWorks, Inc.

load carsmall
%%
% Fit a model of |MPG| as a function of |Cylinders| (nominal) and |Weight| .
ds = dataset(Weight,MPG,Cylinders);
ds.Cylinders = ordinal(ds.Cylinders);
mdl = fitlm(ds,'MPG ~ Cylinders*Weight + Weight^2');
%%
% Make a leverage plot of the data and model.
plotDiagnostics(mdl)
%%
% There are a few points with high leverage. But this plot does not reveal
% whether the high-leverage points are outliers.
%%
% Look for points with large Cook's distance.
plotDiagnostics(mdl,'cookd')
%%
% There is one point with large Cook's distance. Identify it and remove it
% from the model. You can use the Data Cursor to click the outlier and
% identify it, or identify it programmatically.
[~,larg] = max(mdl.Diagnostics.CooksDistance);
mdl2 = fitlm(ds,'MPG ~ Cylinders*Weight + Weight^2',...
    'Exclude',larg);