Documentation Center

  • Trials
  • Product Updates

residuals

Class: LinearMixedModel

Residuals of fitted linear mixed-effects model

Syntax

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

lme — Linear mixed-effects modelLinearMixedModel object

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.

'Conditional' — Indicator for conditional residualsTrue (default) | False

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'

'ResidualType' — Residual type'Raw' (default) | 'Pearson' | 'Standardized'

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

Residual TypeConditionalMarginal
'Raw'

'Pearson'

'Standardized'

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

R — Residualsn-by-1 vector

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

Plot Residuals vs. Fitted Values

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.

Compute Conditional and Marginal Pearson Residuals

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

Examine Residuals

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

Conditional and Marginal Residuals

Conditional residuals include contributions from both fixed and random effects, whereas marginal residuals include contribution from only fixed effects.

Suppose the linear mixed-effects model lmehas an n-by-p fixed-effects design matrix X and an n-by-q random-effects design matrix Z. Also, suppose the p-by-1 estimated fixed-effects vector is , and the q-by-1 estimated best linear unbiased predictor (BLUP) vector of random effects is . The fitted conditional response is

and the fitted marginal response is

residuals can return three types of residuals: raw, Pearson, and standardized. For any type, you can compute the conditional or the marginal residuals. For example, the conditional raw residual is

and the marginal raw residual is

For more information on other types of residuals, see the ResidualType name-value pair argument.

See Also

| | |

Was this topic helpful?