Main Content

plotResiduals

Class:LinearMixedModel

情节残差的线性mixed-effects model

Description

example

plotResiduals(lme,plottype)plots the raw conditional residuals of the linear mixed-effects modellmein a plot of the type specified byplottype.

example

plotResiduals(lme,plottype,Name,Value)also plots the residuals of the linear mixed-effects modellmewith additional options specified by one or more name-value pair arguments. For example, you can specify the residual type to plot.

plotResidualsalso accepts some other name-value pair arguments that specify the properties of the primary line in the plot. For those name-value pairs, seeplot.

h= plotResiduals(___)returns a handle,h, to the lines or patches in the plot of residuals.

Input Arguments

expand all

Linear mixed-effects model, specified as aLinearMixedModelobject constructed usingfitlmeorfitlmematrix.

Type of residual plot, specified as one of the following.

'histogram' Default. Histogram of residuals
'caseorder' Residuals versus case (row) order
'fitted' Residuals versus fitted values
'lagged' Residuals versus lagged residual (r(t) versusr(t– 1))
'probability' Normal probability plot
'symmetry' Symmetry plot

Example:plotResiduals(lme,'lagged')

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis 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 encloseNamein quotes.

Residual type, specified by the comma-separated pair consisting ofResidualTypeand one of the following.

Residual Type Conditional Marginal
'Raw'

r i C = [ y X β ^ Z b ^ ] i

r i M = [ y X β ^ ] i

'Pearson'

p r i C = r i C [ V a r ^ y , b ( y X β Z b ) ] i i

p r i M = r i M [ V a r ^ y ( y X β ) ] i i

'Standardized'

s t i C = r i C [ V a r ^ y ( r C ) ] i i

s t i M = r i M [ V a r ^ y ( r M ) ] i i

For more information on the conditional and marginal residuals and residual variances, seeDefinitionsat the end of this page.

Example:'ResidualType','Standardized'

Output Arguments

expand all

Handle to the residual plot, returned as a handle.

Examples

expand all

Load the sample data.

load('weight.mat')

weightcontains data from a longitudinal study, where 20 subjects are randomly assigned to 4 exercise programs, and their weight loss is recorded over six 2-week time periods. This is simulated data.

Store the data in a table. DefineSubjectandProgramas categorical variables.

tbl = table(InitialWeight,Program,Subject,Week,y); tbl.Subject = categorical(tbl.Subject); tbl.Program = categorical(tbl.Program);

Fit a linear mixed-effects model where the initial weight, type of program, week, and the interaction between the week and type of program are the fixed effects. The intercept and week vary by subject.

lme = fitlme(tbl,'y ~ InitialWeight + Program*Week + (Week|Subject)');

Plot the histogram of the raw residuals.

plotResiduals(lme)

Figure contains an axes object. The axes object with title Histogram of residuals contains an object of type patch.

Plot the residuals versus the fitted values.

plotResiduals(lme,'fitted')

Figure contains an axes object. The axes object with title Plot of residuals vs. fitted values contains 2 objects of type line.

There is no obvious pattern, so there are no immediate signs of heteroscedasticity.

Create the normal probability plot of residuals.

plotResiduals(lme,'probability')

Figure contains an axes object. The axes object with title Normal probability plot of residuals contains 2 objects of type line.

Data appears to be normal.

Find the observation number for the data that appears to be an outlier to the right of the plot.

find(residuals(lme)>0.25)
ans = 101

Create a box plot of the raw, Pearson, and standardized residuals.

r = residuals(lme); pr = residuals(lme,'ResidualType','Pearson'); st = residuals(lme,'ResidualType','Standardized'); X = [r pr st]; boxplot(X,“标签”,{'Raw','Pearson','Standardized'})

Figure contains an axes object. The axes object contains 21 objects of type line.

All three box plots point out the outlier on the right tail of the distribution. The box plots of raw and Pearson residuals also point out a second possible outlier on the left tail. Find the corresponding observation number.

find(pr<-2)
ans = 10

Plot the raw residuals versus lagged residuals.

plotResiduals(lme,'lagged')

Figure contains an axes object. The axes object with title Plot of residuals vs. lagged residuals contains 3 objects of type line.

There is no obvious pattern in the graph. The residuals do not appear to be correlated.