Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How define the order of variables in an interaction term? #120

Closed
inkrement opened this issue Apr 1, 2021 · 4 comments
Closed

Comments

@inkrement
Copy link

Is it possible to change the order of variables in an interaction term? Using the order argument of the etable command, it is possible to rearrange the variable names per see, but I cannot directly change the ordering within an interaction term. Consequently, I am not sure if this is a question (i.e., functionality already exists) or a feature request.

@inkrement inkrement changed the title Question: How define the order of variables in interaction term? Question: How define the order of variables in an interaction term? Apr 1, 2021
@lrberge
Copy link
Owner

lrberge commented Apr 1, 2021

Hi, what do you have in mind by "interaction"? It can be several things actually. Could you give an example in code of what you have in mind?

@inkrement
Copy link
Author

Hi, thanks for the fast response! With interaction, I mean a formula such as "y ~ x:m". Whereby, xm is an interaction term. It seems like the order of xm (or mx) does not always follow the definition (sometimes I write xm, but the results list m*x; which of course does not affect the result, but the label). The possibility of ordering also the variables within interaction terms would be a nice feature!

@lrberge
Copy link
Owner

lrberge commented Apr 6, 2021

Oh, I see!

Actually my algorithm only reorders when there would ba a consistency problem (i.e. considering two variables with different names as different when they are the same). Here's an example:

base = iris
names(base) = c("y", "x1", "x2", "x3", "species")

a = feols(y ~ x1:x2, base)
b = feols(y ~ x2:x1, base)

# No reordering
etable(a)
#>                                  a
#> Dependent Var.:                  y
#>                                   
#> (Intercept)      4.253*** (0.0694)
#> x1 x x2         0.1425*** (0.0056)
#> _______________ __________________
#> S.E. type                 Standard
#> Observations                   150
#> R2                         0.81221
#> Adj. R2                    0.81094

# No reordering
etable(b)
#>                                  b
#> Dependent Var.:                  y
#>                                   
#> (Intercept)      4.253*** (0.0694)
#> x2 x x1         0.1425*** (0.0056)
#> _______________ __________________
#> S.E. type                 Standard
#> Observations                   150
#> R2                         0.81221
#> Adj. R2                    0.81094

# REORDERING
etable(a, b)
#>                                  a                  b
#> Dependent Var.:                  y                  y
#>                                                      
#> (Intercept)      4.253*** (0.0694)  4.253*** (0.0694)
#> x1 x x2         0.1425*** (0.0056) 0.1425*** (0.0056)
#> _______________ __________________ __________________
#> S.E. type                 Standard           Standard
#> Observations                   150                150
#> R2                         0.81221            0.81221
#> Adj. R2                    0.81094            0.81094

d = feols(y ~ x1:species, base)
e = feols(y ~ species:x1, base)

# No reordering
etable(d)
#>                                         d
#> Dependent Var.:                         y
#>                                          
#> (Intercept)             3.358*** (0.3300)
#> x1 x speciessetosa     0.4833*** (0.0968)
#> x1 x speciesversicolor 0.9299*** (0.1198)
#> x1 x speciesvirginica   1.084*** (0.1117)
#> ______________________ __________________
#> S.E. type                        Standard
#> Observations                          150
#> R2                                0.72257
#> Adj. R2                           0.71687

# No reordering
etable(e)
#>                                         e
#> Dependent Var.:                         y
#>                                          
#> (Intercept)             3.358*** (0.3300)
#> speciessetosa x x1     0.4833*** (0.0968)
#> speciesversicolor x x1 0.9299*** (0.1198)
#> speciesvirginica x x1   1.084*** (0.1117)
#> ______________________ __________________
#> S.E. type                        Standard
#> Observations                          150
#> R2                                0.72257
#> Adj. R2                           0.71687

# REORDERING
etable(d, e)
#>                                         d                  e
#> Dependent Var.:                         y                  y
#>                                                             
#> (Intercept)             3.358*** (0.3300)  3.358*** (0.3300)
#> speciessetosa x x1     0.4833*** (0.0968) 0.4833*** (0.0968)
#> speciesversicolor x x1 0.9299*** (0.1198) 0.9299*** (0.1198)
#> speciesvirginica x x1   1.084*** (0.1117)  1.084*** (0.1117)
#> ______________________ __________________ __________________
#> S.E. type                        Standard           Standard
#> Observations                          150                150
#> R2                                0.72257            0.72257
#> Adj. R2                           0.71687            0.71687

But I think that what you are referring to is due to R's parser which sometimes (and rather inconsistently) reorders the interactions.

In any case, I see your point, I'll see what I can do.

@lrberge
Copy link
Owner

lrberge commented Sep 13, 2021

Hi, I've added the argument interaction.order to deal with this problem:

base = iris
names(base) = c("y", "x1", "x2", "x3", "species")
est = feols(y ~ i(species, x1) + i(species, x2), base)

etable(est, interaction.order = "species")
#>                                          est
#> Dependent Var.:                            y
#>                                             
#> (Intercept)                1.725*** (0.2781)
#> species = setosa x x1     0.7584*** (0.0974)
#> species = versicolor x x1   0.3133* (0.1579)
#> species = virginica x x1     0.1028 (0.1352)
#> species = setosa x x2       0.4627* (0.2212)
#> species = versicolor x x2 0.7838*** (0.1039)
#> species = virginica x x2  0.8222*** (0.0749)
#> _________________________ __________________
#> S.E. type                                IID
#> Observations                             150
#> R2                                   0.86873
#> Adj. R2                              0.86322

etable(est, interaction.order = c("x2", "species"))
#>                                          est
#> Dependent Var.:                            y
#>                                             
#> (Intercept)                1.725*** (0.2781)
#> species = setosa x x1     0.7584*** (0.0974)
#> species = versicolor x x1   0.3133* (0.1579)
#> species = virginica x x1     0.1028 (0.1352)
#> x2 x species = setosa       0.4627* (0.2212)
#> x2 x species = versicolor 0.7838*** (0.1039)
#> x2 x species = virginica  0.8222*** (0.0749)
#> _________________________ __________________
#> S.E. type                                IID
#> Observations                             150
#> R2                                   0.86873
#> Adj. R2                              0.86322

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants