lrberge / fixest

Fixed-effects estimations
https://lrberge.github.io/fixest/
361 stars 59 forks source link

Customizing Column Order in etable Output After 2SLS #463

Closed Oravishayrizi closed 5 months ago

Oravishayrizi commented 6 months ago

Hello,

I am using the fixest package for 2SLS regression analysis and have encountered an issue with the etable function. Specifically, I'm unsure how to customize the order of the columns in the etable output, particularly for the first stage of the 2SLS procedure.

Here's an example of my code:

base = iris
names(base) = c("y1", "y2", "x1", "x2", "species")
base$z <- rnorm(nrow(base))

res_multi = feols(y1 ~ 0 | species | x1 + z ~ x2 + y2, base)

etable(res_multi, stage = 1:2)

This produces the following output, where the first stage results are displayed with 'x1' before 'z':

                    res_multi.1      res_multi.2      res_multi.3
IV stages                 First            First           Second
Dependent Var.:              x1                z               y1
[...output truncated for brevity...]

I would like to change the order of the columns in the first stage of the output, specifically to display the results for 'z' before 'x1'. I have checked the documentation , but could not find a solution.

Is there a way to specify the order of the columns for the first-stage results in the `etable' output? Any guidance or suggestions would be greatly appreciated.

Thank you for your time and assistance.

lrberge commented 6 months ago

Hi Or, this is not currently possible within etable.

There are two solutions however:

base = setNames(iris, c("y1", "y2", "x1", "x2", "species"))
base$z <- rnorm(nrow(base))

res_iv = feols(y1 ~ 0 | species | x1 + z ~ x2 + y2, base)

# Two solutions

#
# A) transform the IV estimation into a multiple estimation before hand
#

res_multi = summary(res_iv, stage = 1:2)

# => this is a plain fixest_multi object, you can reorder it
# ... using numbers
etable(res_multi[iv = c(2, 1, 3)])
#>                 res_multi[iv ..1 res_multi[iv..2 res_multi[i..3
#> Dependent Var.:                z              x1             y1
#> 
#> x2              -0.6169 (0.5614) 0.8281 (0.4562)
#> y2              -0.2493 (0.3738) 0.2440 (0.1719)
#> x1                                               -6.050 (37.18)
#> [etc...]

# ... or using regular expressions
etable(res_multi[iv = c("z", ".")], 
       headers = .(Stage = .(First = 2, Second = 1)))
#>                 res_multi[iv ..1 res_multi[iv..2 res_multi[i..3
#> Stage                      First           First         Second
#> Dependent Var.:                z              x1             y1
#> 
#> x2              -0.6169 (0.5614) 0.8281 (0.4562)
#> y2              -0.2493 (0.3738) 0.2440 (0.1719)
#> x1                                               -6.050 (37.18)
#> [etc...]

## Note that you lose the header on the stage, so it needs to be manually added as above

#
# B) order the variables in the estimation stage
#

# Since reordering the variables is easier than reordering the columns,
# you order the endogenous regressors at the right column order at estimation time
# and order the variables at exportation time

# estimation with the "right" column order
res_iv_bis = feols(y1 ~ 0 | species | z + x1 ~ x2 + y2, base)

# reordering the variables (putting z last)
etable(res_iv_bis, stage = 1:2, order = "!z")
#>                     res_iv_bis.1    res_iv_bis.2   res_iv_bis.3
#> IV stages                  First           First         Second
#> Dependent Var.:                z              x1             y1
#> 
#> x2              -0.6169 (0.5614) 0.8281 (0.4562)
#> y2              -0.2493 (0.3738) 0.2440 (0.1719)
#> x1                                               -6.050 (37.18)
#> z                                                -8.723 (46.90)
#> [etc...]

I am not sure adding a built-in solution in etable for reordering the columns is desirable, I don't see a simple syntax for this.

I hope this helps!

lrberge commented 5 months ago

Is that OK? Or could you detail if it isn't?

Oravishayrizi commented 5 months ago

Hi Laurent,

Sorry for not seeing your message sooner. I ended up using a different formula for my calculation:

res_multi = feols(y1 ~ 0 | species | z + x1 ~ x2 + y2, base)

Instead of the one I first mentioned: res_multi = feols(y1 ~ 0 | species | x1 + z ~ x2 + y2, base)

It looks like the way the columns get sorted depends on how they are listed in the "IV part" of the formula. So, the columns ended up in the order I was hoping for.

Thanks again for your help, and sorry for replying late.

lrberge commented 5 months ago

Of course, no worries at all for the delay :-) I thought you knew of the solution you used so didn't mention it. Closing then.