Xing Zhang
Xing Zhang

Reputation: 175

Displaying the variable names according to the sequence appeared in the regression models using `texreg`

Using texreg, I am trying to display the variable names according to the sequence appeared in the regression models. For instance:

I create an interaction term of Sepal.Widthand Petal.Length.

iris$Interaction = iris$Sepal.Width * iris$Petal.Length

I run two regressions, one without and one with the interaction term:

OLS1 = lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = iris)
OLS2 = lm(Sepal.Length ~ Sepal.Width + Petal.Length + Interaction + Petal.Width, data = iris)
screenreg(list(OLS1, OLS2))

When exporting the regression table, texreg would throw the interaction term at the very bottom. However, it would be much easier for reader to check the results if the Interaction term appears right below the Sepal.Widthand Petal.Length in OLS2 instead of the bottom (considering that I have a very big regression table).

This is the output using texreg:

enter image description here

Is there any way to configure the texreg package so that the newly added variable would appear according to the order in the model instead of appearing at the bottom?

Upvotes: 2

Views: 586

Answers (1)

jay.sf
jay.sf

Reputation: 73352

You can use option reorder.coef of texreg::screenreg().

texreg::screenreg(list(OLS1, OLS2), reorder.coef = c(1:3, 5, 4))

Or, more general:

rrdrd.coef <- c(seq_along(names(coef(OLS2)))[-which(names(coef(OLS2)) == "Interaction")], 
  which(names(coef(OLS2)) == "Interaction"))

texreg::screenreg(list(OLS1, OLS2), reorder.coef = rrdrd.coef)

Yielding

====================================
              Model 1     Model 2   
------------------------------------
(Intercept)     1.86 ***    1.30 *  
               (0.25)      (0.51)   
Sepal.Width     0.65 ***    0.82 ***
               (0.07)      (0.15)   
Petal.Length    0.71 ***    0.87 ***
               (0.06)      (0.14)   
Interaction                -0.05    
                           (0.04)   
Petal.Width    -0.56 ***   -0.53 ***
               (0.13)      (0.13)   
------------------------------------
R^2             0.86        0.86    
Adj. R^2        0.86        0.86    
Num. obs.     150         150       
RMSE            0.31        0.31    
====================================
*** p < 0.001, ** p < 0.01, * p < 0.05

Upvotes: 2

Related Questions