Residuals of fitted generalized linear mixed-effects model
Specify optional pairs of arguments as
the argument name and
Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
Conditional — Indicator for conditional residuals
true (default) |
Indicator for conditional residuals, specified as the comma-separated
pair consisting of
'Conditional' and one of the
|Contributions from both fixed effects and random effects (conditional)|
|Contribution from only fixed effects (marginal)|
Conditional residuals include contributions from both fixed-
and random-effects predictors. Marginal residuals include contribution
from only fixed effects. To obtain marginal residual values,
the conditional mean of the response with the empirical Bayes predictor
vector of random effects, b, set to 0.
ResidualType — Residual type
'raw' (default) |
Residual type, specified as the comma-separated pair consisting
'ResidualType' and one of the following.
In each of these equations:
yi is the ith element of the n-by-1 response vector, y, where i = 1, ..., n.
g-1 is the inverse link function for the model.
xiT is the ith row of the fixed-effects design matrix X.
ziT is the ith row of the random-effects design matrix Z.
δi is the ith offset value.
σ2 is the dispersion parameter.
wi is the ith observation weight.
vi is the variance term for the ith observation.
μi is the mean of the response for the ith observation.
and are estimated values of β and b.
Raw residuals from a generalized linear mixed-effects model have nonconstant variance. Pearson residuals are expected to have an approximately constant variance, and are generally used for analysis.
Plot Residuals Versus Fitted Values
Load the sample data.
This simulated data is from a manufacturing company that operates 50 factories across the world, with each factory running a batch process to create a finished product. The company wants to decrease the number of defects in each batch, so it developed a new manufacturing process. To test the effectiveness of the new process, the company selected 20 of its factories at random to participate in an experiment: Ten factories implemented the new process, while the other ten continued to run the old process. In each of the 20 factories, the company ran five batches (for a total of 100 batches) and recorded the following data:
Flag to indicate whether the batch used the new process (
Processing time for each batch, in hours (
Temperature of the batch, in degrees Celsius (
Categorical variable indicating the supplier (
C) of the chemical used in the batch (
Number of defects in the batch (
The data also includes
temp_dev, which represent the absolute deviation of time and temperature, respectively, from the process standard of 3 hours at 20 degrees Celsius.
Fit a generalized linear mixed-effects model using
supplier as fixed-effects predictors. Include a random-effects term for intercept grouped by
factory, to account for quality differences that might exist due to factory-specific variations. The response variable
defects has a Poisson distribution, and the appropriate link function for this model is log. Use the Laplace fit method to estimate the coefficients. Specify the dummy variable encoding as
'effects', so the dummy variable coefficients sum to 0.
The number of defects can be modeled using a Poisson distribution
This corresponds to the generalized linear mixed-effects model
is the number of defects observed in the batch produced by factory during batch .
is the mean number of defects corresponding to factory (where ) during batch (where ).
, , and are the measurements for each variable that correspond to factory during batch . For example, indicates whether the batch produced by factory during batch used the new process.
and are dummy variables that use effects (sum-to-zero) coding to indicate whether company
B, respectively, supplied the process chemicals for the batch produced by factory during batch .
is a random-effects intercept for each factory that accounts for factory-specific variation in quality.
glme = fitglme(mfr,'defects ~ 1 + newprocess + time_dev + temp_dev + supplier + (1|factory)',... 'Distribution','Poisson','Link','log','FitMethod','Laplace','DummyVarCoding','effects');
Generate the conditional Pearson residuals and the conditional fitted values from the model.
r = residuals(glme,'ResidualType','Pearson'); mufit = fitted(glme);
Display the first ten rows of the Pearson residuals.
ans = 10×1 0.4530 0.4339 0.3833 -0.2653 0.2811 -0.0935 -0.2984 -0.2509 1.5547 -0.3027
Plot the Pearson residuals versus the fitted values, to check for signs of nonconstant variance among the residuals (heteroscedasticity).
figure scatter(mufit,r) title('Residuals versus Fitted Values') xlabel('Fitted Values') ylabel('Residuals')
The plot does not show a systematic dependence on the fitted values, so there are no signs of nonconstant variance among the residuals.