Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

residuals

Class: LinearMixedModel

Residuals of fitted linear mixed-effects model

Syntax

R = residuals(lme)
R = residuals(lme,Name,Value)

Description

example

R = residuals(lme) returns the raw conditional residuals from a fitted linear mixed-effects model lme.

example

R = residuals(lme,Name,Value) returns the residuals from the linear mixed-effects model lme with additional options specified by one or more Name,Value pair arguments.

For example, you can specify Pearson or standardized residuals, or residuals with contributions from only fixed effects.

Input Arguments

expand all

Linear mixed-effects model, returned as a LinearMixedModel object.

For properties and methods of this object, see LinearMixedModel.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

expand all

Indicator for conditional residuals, specified as the comma-separated pair consisting of 'Conditional' and one of the following.

TrueContribution from both fixed effects and random effects (conditional)
FalseContribution from only fixed effects (marginal)

Example: 'Conditional,'False'

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

Residual TypeConditionalMarginal
'Raw'

riC=[yXβ^Zb^]i

riM=[yXβ^]i

'Pearson'

priC=riC[Var^y,b(yXβZb)]ii

priM=riM[Var^y(yXβ)]ii

'Standardized'

stiC=riC[Var^y(rC)]ii

stiM=riM[Var^y(rM)]ii

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

Example: 'ResidualType','Standardized'

Output Arguments

expand all

Residuals of the fitted linear mixed-effects model lmereturned as an n-by-1 vector, where n is the number of observations.

Examples

expand all

Navigate to a folder containing sample data.

cd(matlabroot)
cd('help/toolbox/stats/examples')

Load the sample data.

load weight

weight contains 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. Define Subject and Program as categorical variables.

tbl = table(InitialWeight,Program,Subject,Week,y);
tbl.Subject = nominal(tbl.Subject);
tbl.Program = nominal(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)');

Compute the fitted values and raw residuals.

F = fitted(lme);
R = residuals(lme);

Plot the residuals versus the fitted values.

plot(F,R,'bx')
xlabel('Fitted Values')
ylabel('Residuals')

Now, plot the residuals versus the fitted values, grouped by program.

figure();
gscatter(F,R,Program)

The residuals seem to behave similarly across levels of the program as expected.

Load the sample data.

load carbig

Store the variables for miles per gallon (MPG), acceleration, horsepower, cylinders, and model year in a table.

tbl = table(MPG,Acceleration,Horsepower,Cylinders,Model_Year);

Fit a linear mixed-effects model for miles per gallon (MPG), with fixed effects for acceleration, horsepower, and the cylinders, and potentially correlated random effects for intercept and acceleration grouped by model year.

lme = fitlme(tbl,'MPG ~ Acceleration + Horsepower + Cylinders + (Acceleration|Model_Year)');

Compute the conditional Pearson residuals and display the first five residuals.

PR = residuals(lme,'ResidualType','Pearson');
PR(1:5)
ans =

   -0.0533
    0.0652
    0.3655
   -0.0106
   -0.3340

Compute the marginal Pearson residuals and display the first five residuals.

PRM = residuals(lme,'ResidualType','Pearson','Conditional',false);
PRM(1:5)
ans =

   -0.1250
    0.0130
    0.3242
   -0.0861
   -0.3006

Load the sample data.

load carbig

Store the variables for miles per gallon (MPG), acceleration, horsepower, cylinders, and model year in a table.

tbl = table(MPG,Acceleration,Horsepower,Cylinders,Model_Year);

Fit a linear mixed-effects model for miles per gallon (MPG), with fixed effects for acceleration, horsepower, and the cylinders, and potentially correlated random effects for intercept and acceleration grouped by model year.

lme = fitlme(tbl,'MPG ~ Acceleration + Horsepower + Cylinders + (Acceleration|Model_Year)');

Draw a histogram of the raw residuals with a normal fit.

r = residuals(lme);
histfit(r)

Normal distribution seems to be a good fit for the residuals.

Compute the conditional Pearson and standardized residuals and create box plots of all three types of residuals.

pr = residuals(lme,'ResidualType','Pearson');
st = residuals(lme,'ResidualType','Standardized');
X = [r pr st];
figure();
boxplot(X)

Red plus signs show the observations with residuals above or below $q3 + 1.5(q3 - q1)$ and $q1 - 1.5(q3 - q1)$, where $q1$ and $q3$ are the 25th and 75th percentiles, respectively.

Find the observations with residuals that are 2.5 standard deviations above and below the mean.

find(r > nanmean(r) + 2.5*nanstd(r))
ans =

    62
   252
   255
   330
   337
   341
   396

find(r < nanmean(r) - 2.5*nanstd(r))
ans =

   119
   324
   375

Definitions

expand all

Was this topic helpful?