Wiggly confidence interval with marginaleffects::plot_slopes

69 views Asked by At

I want to plot the marginal effects of an independent variable 'Shannon' estimated with a linear (lm) model. The variable appears both linearly and quadratically in the model, so the marginal effect changes along the range of 'Shannon' values.

To do this I use marginaleffects::plot_slopes(model = MODEL, variables = "Shannon", condition = 'Shannon', conf_level = 0.95)

I am very confused as to why my confidence interval is wiggly? I was expecting a smooth band around my estimated marginal effects line.

enter image description here

1

There are 1 answers

1
Vincent On BEST ANSWER

This appears to be an artefact of numerical instability when computing derivatives. One solution is to select a different step size manually using the eps argument:

library(marginaleffects)
plot_slopes(
  model = MODEL,
  variables = "Shannon",
  condition = 'Shannon',
  eps = 0.01)

FYI, I am the author of the package and exchanged emails with the original poster. This solved their problem.