Plot Cook’s distance for mixed effects model

조회 수: 2 (최근 30일)
Adam Fitchett
Adam Fitchett 2019년 7월 26일
댓글: Adam Danz 2021년 3월 5일
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
Adam Danz
Adam Danz 2021년 3월 4일
@Katharina thanks for the sample of your data. It was helpful to identify the problem.
I'll add an answer to this in a few minutes.
Adam Danz
Adam Danz 2021년 3월 5일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2021년 3월 4일
편집: Adam Danz 2021년 3월 5일
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
Katharina
Katharina 2021년 3월 4일
Thanks, Adam
Xuelong Fan
Xuelong Fan 2021년 3월 5일
Now I know!
Thank you!

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by