Plot Cook’s distance for mixed effects model

3 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by