Order variables with interaction in stargazer regression output
Asked Answered
G

3

5

I have the following regressions:

fit1 <- lm(y ~ x1, data = data)
fit2 <- lm(y ~ x1 + x2, data = data)
fit3 <- lm(y ~ x3 * x1, data = data)

I want in the output the explanatory variables to be order (x1, x2, x3 and finally the interaction). I try the following with order but still the interaction appears second after x1. What may be wrong here?

stargazer(fit1, fit2, fit3, align=TRUE, table.placement="H",omit.stat=c("f", "ser"), order=c("x1", "x2", "x3", "x3:x1"))
Genni answered 4/9, 2015 at 18:59 Comment(0)
A
6

The help-file for order states "order - a vector of regular expressions (or of numerical indexes) that indicates the order in which variables will appear in the output". So your strings were parsed as regex, and x1 matches x3:x1. You could do a numeric order:

stargazer(fit1, fit2, fit3, align=TRUE, 
 table.placement="H",omit.stat=c("f", "ser"), order=c(2,3,1,4,5))
Alboin answered 4/9, 2015 at 19:25 Comment(2)
Why are there five elements in your order vector?Engud
Because there's a constantAlboin
O
7

The numeric order works fine if you have only a few variables. If you change the explanatory variables often you might want to set the order in an automated way.

Suppose you have a vector of variables names, already in the right order. You can transform them into "exact match regular expressions":

    vars.order <- c("x1", "x2", "x3", "x3:x1")
    stargazer(fit1, fit2, fit3, align=TRUE, 
    table.placement="H",omit.stat=c("f", "ser"), order=paste0("^", vars.order , "$")

This has worked for me so far, but you might have some issues if the variable names include special characters, e.g.: "." or "*".

Orabelle answered 28/3, 2018 at 20:47 Comment(0)
A
6

The help-file for order states "order - a vector of regular expressions (or of numerical indexes) that indicates the order in which variables will appear in the output". So your strings were parsed as regex, and x1 matches x3:x1. You could do a numeric order:

stargazer(fit1, fit2, fit3, align=TRUE, 
 table.placement="H",omit.stat=c("f", "ser"), order=c(2,3,1,4,5))
Alboin answered 4/9, 2015 at 19:25 Comment(2)
Why are there five elements in your order vector?Engud
Because there's a constantAlboin
N
2

In case you come along here in search for answers too, here is what else I noticed:

First of all I agree with tino_ladino. the numerical ordering is not practical if you have many variables. Therefore it is more of a matter to get the RegEx right imho. His comment with the exact match is already very valuable ("^ $") where ^ denotes the beginning, $ the end of the term).

But caveat, if your interaction variable is categorical (e.g. value is TRUE / FALSE) the output of Stargazer will not show the value in the output, but to order it, you need to call it like in the interaction terms (with the value, like ModeratorTRUE). So:

lm <- lm(DV ~ IV * Moderator) #Moderator being either TRUE or FALSE

will produce a stargazer Output like this:

IV
Moderator
IV:ModeratorTRUE
Constant

now to order the Moderator you cannot just call "^Moderator$"

stargazer(lm, type ="latex", out = "file.tex", order = c("^Constant$", "^Moderator$", "^IV:Moderator$"))

it will not catch the Moderator as you'd expect because what stargazer creates seemingly differs from how it handles the variable internally. To order it, you need to call "^ModeratorTRUE$":

stargazer(lm, type ="latex", out = "file.tex", order = c("^Constant$", "^ModeratorTRUE$", "^IV:Moderator$"))

Another note: You can also make use of the RegEx way if you e.g. have many interaction terms that you ant to show all underneath each other in the end, just call them all with the ":", of course here you cannot call for the exact match ("^ $")

stargazer(lm, type ="latex", out = "file.tex", order = c("^Constant$", "^ModeratorTRUE$", ":"))

And if you want to put the Interaction beneath the main effect, you could only denote the end of the variable. Then the Interaction would be shown underneacth, because in the format ModeratorTRUE:IV it would also fit "IV$". In this example it obviously doesn't make sense with only one row left, but you get the point.

stargazer(lm, type ="latex", out = "file.tex", order = c("^Constant$", "^ModeratorTRUE$", "IV$"))
Nucleotidase answered 22/11, 2018 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.