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

    %% Examine Residuals of a Poisson Model
% This example shows how to use residual plots to help you discover errors,
% outliers, or correlations in the model.
%%
% Randomly generate the sample data from Poisson distribution.

% Copyright 2015 The MathWorks, Inc.

rng default; % For reproducibility
X = randn(100,5);
mu = exp(X(:,[1 4 5])*[2;1;.5]);
y = poissrnd(mu);
%%
% The data construction has two out of five predictors not affecting the
% response, and no intercept term.
%%
% Fit a Poisson model.
mdl = fitglm(X,y,'linear','Distribution','poisson');
%%
% Examine the residuals.
plotResiduals(mdl)
%%
% While most residuals cluster near 0, there are several near -18 and +18.
% Examine a different residuals plot.
plotResiduals(mdl,'fitted')
%%
% The large residuals don't seem to have much to do with the sizes of the
% fitted values.
%%
% Create a probability plot.
plotResiduals(mdl,'probability')
%%
% Now it is clear. The residuals do not follow a normal distribution.
% Instead, they have fatter tails, much as an underlying Poisson
% distribution.