Search code examples
rstargazer

Use stargazer(..., order = c(...)) for as.factor(var)


I have a regression model like this:

Call:
glm(formula = DV ~ as.factor(IV), family = "binomial", data = df)

[...]
Coefficients:
                                       Estimate Std. Error z value Pr(>|z|)    
(Intercept)                          -2.104e+01  3.956e+03  -0.005   0.9958    
as.factor(IV)AAA                      2.626e-01  2.962e-01   0.887   0.3752    
as.factor(IV)BBB                      4.150e-01  3.176e-01   1.307   0.1913    
as.factor(IV)CCC                      8.060e-01  3.551e-01   2.270   0.0232 *  
as.factor(IV)DDD                      2.640e+00  3.210e-01   8.222  < 2e-16 ***

Using stargazer, I would like to reorder the output. I want to change the order of entries to "as.factor(IV)BBB", "as.factor(IV)AAA", "as.factor(IV)DDD", "as.factor(IV)CCC" and rename them to c("BBB", "AAA", "DDD", "CCC"); I have tried with this and similar calls:

stargazer(model, type = "html", out = "./out/test.html", 
      title = "Title", order = c("as.factor(IV)BBB", "as.factor(IV)AAA",
                                 "as.factor(IV)DDD", "as.factor(IV)CCC")

Unfortunately, I can neither get it to work nor find help in the documentation.


Solution

  • You can use the numeric indexes to change the order and then rename using covariate.labels

    stargazer(model, type = "html", out = "./out/test.html", 
          title = "Title", order = c(2, 1, 4, 3), covariate.labels = c("BBB", "AAA", "DDD", "CCC"))