1

Previous question asked how the coefficients can be plotted in coefplot in descending order. The answer was to include sort = 'magnitude'

However, I then discovered that this method does not work when plotting multiple models using multiplot:

data(tips, package = "reshape2")
mod1 <- lm(tip ~ day + sex + smoker, data = tips)
mod2 <- lm(tip ~ day + sex + smoker + size, data = tips)
multiplot(mod1, mod2, decreasing = TRUE, sort = "magnitude")

Is there a way to do this with multiplot? I realize it might not be straightforward. I just wonder if I am missing something.

Community
  • 1
  • 1
invictus
  • 1,821
  • 3
  • 25
  • 30
  • 1
    general question: how should sorting in decreasing order work when there's more than one model which might have different coefficient rank orders? Sort by coefficients of the first model? – Ben Bolker Oct 23 '16 at 01:29

1 Answers1

4

I don't know how to do this with coefplot(), but I can offer a solution with the similar dotwhisker package:

Fit models:

data(tips, package = "reshape2")
mod1 <- lm(tip ~ day + sex + smoker, data = tips)
mod2 <- lm(tip ~ day + sex + smoker + size, data = tips)

library(dotwhisker)
## figure out order
ov <- names(sort(coef(mod2),decreasing=TRUE))
dwplot(list(mod1=mod1,mod2=mod2),order_vars=ov)+
    theme_bw()+
    geom_vline(xintercept=0,lty=2)

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • I haven't checked, but are you saying (1) there's a bug in `dwplot()` so that the coefficients of mod1 aren't matched up right with the labels or (2) the label-matching is correct? If the latter, then I don't see how it's logically possible to have *both* sorted and in the same plot. `order(coef(mod1))` is `6 4 5 2 3 1`; `order(coef(mod2))` is `4 2 3 5 6 7 1` ... (This is the same issue as my comment above.) – Ben Bolker Oct 23 '16 at 01:54
  • 1
    Certainly not claiming there is a bug. If anything your second point is spot-on: there is a bug in my thinking that an ordered coefficient plot for two models is possible. In retrospect my question was pretty stupid. – invictus Oct 23 '16 at 01:57