Plot Cook’s distance for mixed effects model
Ältere Kommentare anzeigen
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 Kommentare
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
am 26 Jul. 2019
Adam Fitchett
am 26 Jul. 2019
Bearbeitet: Adam Fitchett
am 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
am 26 Jul. 2019
Adam Fitchett
am 26 Jul. 2019
Adam Fitchett
am 26 Jul. 2019
Bearbeitet: Adam Fitchett
am 26 Jul. 2019
Adam Danz
am 26 Jul. 2019
Copy the directory that stores LinearModel.m and add that path using addpath('...'). Then run the which plotDiagnostics -all again.
If that fixes it, then somehow the matlab path got messed up.
Adam Fitchett
am 26 Jul. 2019
Oh right, because method directories are not allowed to be added that way. Do this instead (make sure this path is the same on your system); then run the which command
addpath(genpath('C:\Program Files\MATLAB\R2019a\toolbox\stats'))
Adam Fitchett
am 26 Jul. 2019
xLon
am 2 Mär. 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.
@Xuelong Fan what is returned by the following commands?
license('test','Statistics_toolbox')
which LinearModel
Katharina
am 2 Mär. 2021
I do have the same problem and would be grateful for help!
>> license('test','Statistics_toolbox')
ans =
1
>> which LinearModel
/Applications/MATLAB_R2020b.app/toolbox/stats/classreg/@LinearModel/LinearModel.m % LinearModel constructor
>>
Adam Danz
am 2 Mär. 2021
Please share
- the line of code you're using when calling plotDiagnostics.
- the full error message you receive after calling the function.
xLon
am 3 Mär. 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?
Adam Danz
am 3 Mär. 2021
Could you share the line of code you used that led to this error? Ideally, you should share the code and the inputs so we can reproduce the error or trace the stack on our end.
Katharina
am 3 Mär. 2021
for same lme, I got this error this time:
Undefined function 'plotDiagnostics' for input arguments of type 'LinearMixedModel'.
xLon
am 4 Mär. 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
am 5 Mär. 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.
Antworten (1)
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
help LinearModel.plotDiagnostics
The which function only works when the LinearModel class is constructed.
clear all
which plotDiagnostics
fitlm(1:5,1:5);
which plotDiagnostics
4 Kommentare
Adam Danz
am 4 Mär. 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.
Katharina
am 4 Mär. 2021
Thanks, Adam
xLon
am 5 Mär. 2021
Now I know!
Thank you!
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!