leeper/margins

Label mismatch between margins() and plot(margins())?

PrisonRodeo opened this issue · 1 comments

Hi -

I'm seeing mismatched labels between margins(foo) and plot(margins(foo)), in this case for a logit (no interactions). I hope I'm not doing something wrong. Reproduceable minimal example below. The issue seems to arise when the variables in the fitted model appear in a different order than they do in the data frame.

Thanks,

-- CZ (zorn@psu.edu, @PrisonRodeo)

====================================
Please specify whether your issue is about:

  • a possible bug
  • a question about package functionality
  • a suggested code or documentation change, improvement to the code, or feature request

Code!

library("margins")
set.seed(7222009)
df<-data.frame(X1=rbinom(400,1,0.5),
               X2=runif(400,0,1),
               X3=rbinom(400,1,0.2))
df$Y<-rbinom(400,1,plogis(1*df$X1+3*df$X2-3*df$X3))
hat<-glm(Y~X1+X2+X3,df,family="binomial")
margins(hat)
plot(margins(hat))  # <- works OK
hat2<-glm(Y~X3+X2+X1,df,family="binomial")
margins(hat2)
plot(margins(hat2)) # <- mismatches labels to AMEs

## session info for your system
sessionInfo()

I've encountered the same, it appears that the labels are in the order of the fitted mode, but the plots are in alphabetical order of variable names.