You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Plot Cook’s distance for mixed effects model
1 view (last 30 days)
Show older comments
Hello, I have a mixed effects regression model ‘lme’ and I want to plot the Cook’s distance. Apparently the code for this is ‘plotDiagnostics(lme, ‘cookd’)’ but when I enter that I get the error ‘Undefined function ‘plotDiagnostics’. Does anyone know what I am doing wrong here?
23 Comments
Adam Danz
on 26 Jul 2019
Edited: Adam Danz
on 26 Jul 2019
That command goes all the way back to r2012a and requires the Statistics and Machine Learning toolbox.
To confirm that you do not have the function/method,
which plotDiagnostics -all
It looks like you do have the toolbox since you seemed to have generated the model (lme).
Adam Fitchett
on 26 Jul 2019
Thank you Adam
I thought I did have the toolbox installed, but when I entered ‘which plotDiagnostics -all’ it said it wasn’t found
Adam Fitchett
on 26 Jul 2019
Edited: Adam Fitchett
on 26 Jul 2019
Indeed, I do have the toolbox as it is listed when I enter “ver” But plotDiagnostics is still not found And I have version 2019a, so should be there
Adam Danz
on 26 Jul 2019
Edited: Adam Danz
on 26 Jul 2019
Physically check that you have the file that contains this method. plotDiagnostics is a function stored in the classdef LinearModel.m.
In windows, on my system, that file can be found in
'C:\Program Files\MATLAB\R2019a\toolbox\stats\classreg\@LinearModel\LinearModel.m'
Do you have that file? If you open the file, can you find the plotDiagnostics function?
Adam Fitchett
on 26 Jul 2019
My version is 2019a. I have tried closing and reopening MATLAB, but that didn’t fix it. I tried restoredefaultpath, but that didn’t fix it. I don’t think anything has been deleted or renamed in the toolbox, but maybe it has happened somehow without my knowledge
Adam Fitchett
on 26 Jul 2019
Yes, I have the LinearModel.m file and when I search through it plotDiagnostics does seem to be there
Adam Fitchett
on 26 Jul 2019
Edited: Adam Fitchett
on 26 Jul 2019
Yes it’s in that exact directory. I followed through the directory you described in order to find it, and it was precisely there
Adam Fitchett
on 26 Jul 2019
Well that’s curious. I did what you said above, and MATLAB returns “Warning: Name is nonexistent or not a directory”. But I definitely entered the directory of the file.
Xuelong Fan
on 2 Mar 2021
Hi!
I have the same problem. And I tried addpath ... but it still says 'plotDiagnostics' not found.
I installed both 2019b and 2020b. But that shouldn't be a problem, should it?
I wonder if anyone has the same problem.
Adam Danz
on 2 Mar 2021
Edited: Adam Danz
on 2 Mar 2021
@Xuelong Fan what is returned by the following commands?
license('test','Statistics_toolbox')
which LinearModel
Xuelong Fan
on 3 Mar 2021
0.
I got something similar after running the codes:
ans =
1
C:\Program Files\MATLAB\R2020b\toolbox\stats\classreg\@LinearModel\LinearModel.m % LinearModel constructor
1&2.
And the full error message is:

lme is a linear mixed-effect model.
3.
After running addpath ...,
I got a warning like this:

maybe relevant?
Xuelong Fan
on 4 Mar 2021
Similar to Katharina.
It does not work on any lme.
What could be interesting to you is that:
1) when you type in the command line till the point "plotDiagnostics(", the hint of how to use this function can pop up.

But when you run it, it cannot be found:
>> Unrecognized function or variable 'plotDiagnostics'.
2) when you run addpath ... as you mentioned before, other functions such as plotResidual cannot be found neither (it can before).
I think it might have to do with conflict of function names??
Adam Danz
on 5 Mar 2021
This thread keeps getting attention now and then. I wonder where users get the idea that they can use plotDiagnostics with non-LinearModel objects. If there is a demo or some other souce that is leading people to this problem, please let us know here in the comments section.
Answers (1)
Adam Danz
on 4 Mar 2021
Edited: Adam Danz
on 5 Mar 2021
plotDiagnostics is a public method of the LinearModel class. The function is defined in LinearModel.m and recieves a LinearModel object as input.
plotDiagnostics is not defined in other model classes such as LinearMixedModel objects. Determine the class of your model using class(mdl) to confirm if it's a LinearModel object. If not, you cannot use plotDiagnostics with the model.
If the LinearModel class has not been constructed, help('plotDiagnostics') will not find the function unless you specify the class name,
clear all
help plotDiagnostics % r2020b
plotDiagnostics not found.
Use the Help browser search field to search the documentation, or
type "help help" for help command options, such as help for methods.
help LinearModel.plotDiagnostics
plotDiagnostics Plot diagnostics of fitted model
plotDiagnostics(LM,PLOTTYPE) plots diagnostics from LinearModel LM in
a plot of type PLOTTYPE. The default value of PLOTTYPE is 'leverage'.
Valid values for PLOTTYPE are:
'contour' residual vs. leverage with overlayed Cook's contours
'cookd' Cook's distance
'covratio' delete-1 ratio of determinant of covariance
'dfbetas' scaled delete-1 coefficient estimates
'dffits' scaled delete-1 fitted values
'leverage' leverage (diagonal of Hat matrix)
's2_i' delete-1 variance estimate
H = plotDiagnostics(...) returns handles to the lines in the plot.
The PLOTTYPE argument can be followed by parameter/value pairs to
specify additional properties of the primary line in the plot. For
example, plotDiagnostics(LM,'cookd','Marker','s') uses a square
marker.
The data cursor tool in the figure window will display the X and Y
values for any data point, along with the observation name or number.
It also displays the coefficient name for 'dfbetas'.
Example:
% Plot the leverage in a fitted regression model
load carsmall
d = dataset(MPG,Weight);
d.Year = ordinal(Model_Year);
lm = fitlm(d,'MPG ~ Year + Weight + Weight^2')
plotDiagnostics(lm,'leverage')
% Look at the data for the high-leverage points, and note that
% their Weight values are near the extremes
high = find(lm.Diagnostics.Leverage>0.11)
d(high,:)
See also LinearModel, plotResiduals.
Documentation for LinearModel/plotDiagnostics
doc LinearModel.plotDiagnostics
The which function only works when the LinearModel class is constructed.
clear all
which plotDiagnostics
'plotDiagnostics' not found.
fitlm(1:5,1:5);
which plotDiagnostics
/MATLAB/toolbox/stats/classreg/@LinearModel/LinearModel.m % LinearModel method
4 Comments
Adam Danz
on 4 Mar 2021
You'll have to compute it directly. There may be functions on the file exchange that could help out. Matlab's documentation shows how it's computed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)