grantmcdermott / ggfixest

Dedicated ggplot2 methods for fixest model objects
http://grantmcdermott.com/ggfixest/
44 stars 8 forks source link

ggiplot lines do not align with corresponding point estimates when plotting multiple models from a single feols object #40

Open JayCata opened 2 weeks ago

JayCata commented 2 weeks ago

Hi,

First of all, thanks for this library -- it's very useful as I am big fan of ggplot and fixest. I am encountering a plotting issue when running a dynamic DID/event study with multiple fixed effects specifications in a single feols call.

I can't provide my data, but hopefully this explanation is complete enough as I can't imagine how the underlying data affects this behavior. First I run feols:

est <- feols(did_form_1, df, cluster = ~County)

where the left-hand side of did_form_1 is a single dependent variable and the right-hand side uses sw0() (see here) to consider multiple fixed effects. This means my est object contains three dynamic estimations -- one for each set of fixed effects.

I then run the est object through ggiplot:

ggiplot(est, pt.join = TRUE, grid = FALSE, lty = "dashed", xlab = "Year", geom_style = "errorbar") + ggtitle("test")

Naturally, this approach plots my year-specific estimates for each of the specifications in a staggered fashion which is great. The strange this is that some of the lines it plots (due to pt.join = TRUE) do not match the corresponding points. Here is a picture of what I am talking about:

image

As you can see, the staggered lines do not align with the correct points. The y values are correct, but the blue line is staggered to the left (when it should be staggered to the right) and the teal line is staggered to the right (when it should be staggered to the left). iplot, however, plots these as expected (albeit with an uglier color scheme):

image

If you need me to generate an example with fake data, let me know.

Thanks in advance!

Josh

JayCata commented 2 weeks ago

Actually, I saw issue #39, and I realized you provided code that I can use:

library(ggfixest)
#> Loading required package: ggplot2
#> Loading required package: fixest
data('base_did', package = 'fixest')

base_did$y2 = base_did$y*1.5
base_did$y3 = base_did$y*2

mods = feols(c(y, y2, y3) ~ x1 + i(period, treat, 5) | id+period, base_did)
ggiplot(mods, pt.join = TRUE)

yields

image

In this example, all three lines are staggered incorrectly though in my particular situation, it was the fixed effects that were varied, not the dependent variables.

Looking at varying the fixed effects it appears order matters. The following example is fine

library(ggfixest)
#> Loading required package: ggplot2
#> Loading required package: fixest
data('base_did', package = 'fixest')

base_did$y2 = base_did$y*1.5
base_did$y3 = base_did$y*2

mods = feols(y ~ x1 + i(period, treat, 5) | sw0(id, period), base_did)
ggiplot(mods, pt.join = TRUE)

image

but if I switch period and id, it is not:

library(ggfixest)
#> Loading required package: ggplot2
#> Loading required package: fixest
data('base_did', package = 'fixest')

base_did$y2 = base_did$y*1.5
base_did$y3 = base_did$y*2

mods = feols(y ~ x1 + i(period, treat, 5) | sw0(period, id), base_did)
ggiplot(mods, pt.join = TRUE)

image

SO it appears as if the staggering of the line is order dependent but the y values aren't.

grantmcdermott commented 2 weeks ago

Hi @JayCata.

First up, thanks for putting together a reproducible example. That's a huge help.

There's definitely a bug here. I'm juggling a couple of things at work and across my other projects, but will try to address this as soon as I can.

P.S. If you want a nicer colour scheme for the base iplot version, you can always set the global base graphics colour palette with the palette function. E.g. palette("Dark 2")

JayCata commented 2 weeks ago

Thanks for the quick response and the palette function tip. I took a quick look through the codebase while work was slow, but I was unable to see what's going on for sure. If I find the time, I'll give it another go.