Leverage Plots for regression diagnostics

These plots are diagnostic plots for multiple linear regression. The documentation for the leveragePlot function seems straightforward, but I can't get the function to produce anything. Let's do a simple model with mtcars.

model1=lm(mpg ~ cyl + disp + hp, data=mtcars)

From what I can tell, one of the following should produce a plot

leveragePlot(model1, cyl)


leveragePlot(model1, ~cyl)

but neither does. I consistently get ~cyl is not a term in the model and similar errors when I try other terms or combinations of terms. I've also tried explicitly using terms= to no avail.

I imagine this is just my inability to read the help documentation syntax correctly, but I'm at a wall and need some help.

A reproducible example, called a reprex is really helpful in avoiding assumptions. Here, I have to assume you mean car::leveragePlots.

The example from the help page produces

#> Loading required package: carData
leveragePlots(lm(prestige~(income+education)*type, data=Duncan))

Created on 2019-12-27 by the reprex package (v0.3.0)

But when we reduce the argument to

model1 <- lm(prestige~(income+education)*type, data=Duncan))
leveragePlots(model1 ~ prestige)
Error in eval(predvars, data, env) : object 'prestige' not found

it throws your error.

I am quite familiar with reproducible examples and thought I was providing one when I provided a specific data set and the things that I had tried. Thanks for catching the typo, though. I, of course, meant leveragePlots, although there is a leveragePlot function in car as well that seems to map to the same code.

I too was able to run the example in the help file, but was not able to reduce it to a simpler example, although as you see I tried several things. The example in the help runs the lm() inside the leveragePlots() function itself, which is certainly not the way that users would use the plot. Since it's a diagnostic plot, most people would have run a linear model and would then want to produce the plot afterward, as I am trying to do.

Your reduced example may not work as you've requested the Y variable (prestige) rather than one of the X variables (income, education, etc), and leverage plots are used on the X variables. That may be one reason you are getting an error.

I very much appreciate the help but am not sure what your post is trying to tell me -- that the help page works but other things don't?

It seems that changing from LeveragePlot to LeveragePlots allows me to enter a model as the argument and I will get one plot for each effect, that is, all the possible leverage plots for the model. the LeveragePlot function is (I think) where you ask for a specific term in the model, rather than all terms, and it is that function that I cannot get to work.

You were missing information about the used libraries, i.e. library calls, this is fundamental to ensure the reproducibility of the code, otherwise people would have to guess which packages are you using, as technocrat did.

Ah, yes, I see that now. Thanks.

Uh, @leecreighton, I have the same type of lapses (probably more often) as typing LeveragePlot (again) for LeveragePlots. That is why a reprex is useful: it catches us our when we do that.

leveragePlots takes an lm model as argument with one-sided terms to specify which numeric regressors, factors and interactions to include. The default is all. To specify a single regressor

leveragePlots(lm(prestige~income, data=Duncan))

Let's focus in your example again, is this not the output you expected? what would be your expected output?


model1 <- lm(mpg ~ cyl + disp + hp, data = mtcars)

leveragePlots(model = model1, terms = ~ cyl)

Created on 2019-12-27 by the reprex package (v0.3.0.9000)

1 Like

That’s exactly what I was expecting. I didn’t try the syntax you used (obviously) but I thank you for finding it.

Somehow I thought LeveragePlot() [no s] would give me a single plot, but I see now using LeveragePlots() is the way to go.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.