Search code examples
rxtablestargazer

how to flip a regression table in stargazer?


I would like to "flip" the row/columns in a regression table.

The latest stargazer manuals says that now the flip argument also works with regression tables (previously it only worked with summary stats).

However I am unable to make it work. Here is an example with stargazer 5.2.1

library(stargazer)
stargazer(attitude)

linear.1 <- lm(rating ~ complaints + privileges + learning + raises + critical, data=attitude)
linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)

## create an indicator dependent variable, and run a probit model

attitude$high.rating <- (attitude$rating > 70)
probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude, family = binomial(link = "probit"))

stargazer(linear.1, linear.2, probit.model, title= "Regression Results", type = 'text', flip = TRUE)

gives :

> stargazer(linear.1, linear.2, probit.model, title="Regression Results", type = 'text', flip = FALSE)
the condition has length > 1 and only the first element will be usednumber of rows of result is not a multiple of vector length (arg 2)number of rows of result is not a multiple of vector length (arg 2)
Regression Results
=============================================================================
                                       Dependent variable:                   
                    ---------------------------------------------------------
                                       rating                     high.rating
                                         OLS                        probit   
                             (1)                    (2)               (3)    
-----------------------------------------------------------------------------
complaints                 0.692***               0.682***                   
                           (0.149)                (0.129)                    

privileges                  -0.104                 -0.103                    
                           (0.135)                (0.129)                    

learning                    0.249                  0.238*          0.164***  
                           (0.160)                (0.139)           (0.053)  

raises                      -0.033                                       

The table is just the regular one with variables as columns. Instead, I am looking for something like

Regression Results
=====================================
            complaints   privileges
-------------------------------------
model1      0.692***      etc
            (0.149) 
model2      0.14**    
            (0.049)
model3      0.692    
            (0.149)

Am I missing something? Thanks!


Solution

  • Here you go, my friend. I hope this helps.

    stargazer(coef(summary(linear.1)), coef(summary(linear.2)), coef(summary(probit.model)), title= "Regression Results", type = 'text', flip = TRUE)
    

    Output:

    Regression Results
    ======================================================================
                (Intercept) complaints privileges learning raises critical
    ----------------------------------------------------------------------
    Estimate      11.011      0.692      -0.104    0.249   -0.033  0.015  
    Std. Error    11.704      0.149      0.135     0.160   0.202   0.147  
    t value        0.941      4.649      -0.769    1.560   -0.165  0.105  
    Pr(> | t| )    0.356      0.0001     0.450     0.132   0.870   0.917  
    ----------------------------------------------------------------------
    
    Regression Results
    ======================================================
                (Intercept) complaints privileges learning
    ------------------------------------------------------
    Estimate      11.258      0.682      -0.103    0.238  
    Std. Error     7.318      0.129      0.129     0.139  
    t value        1.538      5.296      -0.799    1.707  
    Pr(> | t| )    0.136     0.00002     0.432     0.100  
    ------------------------------------------------------
    
    Regression Results
    =================================================
                (Intercept) learning critical advance
    -------------------------------------------------
    Estimate      -7.476     0.164    -0.001  -0.062 
    Std. Error     3.570     0.053    0.044    0.042 
    z value       -2.094     3.079    -0.013  -1.472 
    Pr(> | z| )    0.036     0.002    0.990    0.141 
    -------------------------------------------------
    > 
    

    EDIT after the OPs clarification:

        options(stringsAsFactors = F, scipen=999)
        model1 <- data.frame(t(coef(summary(linear.1))))
    model1$stats <- rownames(model1)
    model2 <- data.frame(t(coef(summary(linear.2))))
    model2$stats <- rownames(model2)
    model3 <- data.frame(t(coef(summary(probit.model))))
    model3$stats <- rownames(model3)
    
    
    x <- dplyr::bind_rows(model1 = model1, model2=model2,
                     model3 = model3, .id = "Name")
    x <- x[,c(1,8,2:7,9 )] #rearranging the terms
    stargazer(x, type="text", summary=F)
    

    Output:

        =========================================================================================
        Name     stats    X.Intercept. complaints privileges learning raises critical advance
    -----------------------------------------------------------------------------------------
    1  model1  Estimate      11.011      0.692      -0.104    0.249   -0.033  0.015          
    2  model1 Std. Error     11.704      0.149      0.135     0.160   0.202   0.147          
    3  model1   t value      0.941       4.649      -0.769    1.560   -0.165  0.105          
    4  model1 Pr(> | t| )    0.356       0.0001     0.450     0.132   0.870   0.917          
    5  model2  Estimate      11.258      0.682      -0.103    0.238                          
    6  model2 Std. Error     7.318       0.129      0.129     0.139                          
    7  model2   t value      1.538       5.296      -0.799    1.707                          
    8  model2 Pr(> | t| )    0.136      0.00002     0.432     0.100                          
    9  model3  Estimate      -7.476                           0.164           -0.001  -0.062 
    10 model3 Std. Error     3.570                            0.053           0.044    0.042 
    11 model3   z value      -2.094                           3.079           -0.013  -1.472 
    12 model3 Pr(> | z| )    0.036                            0.002           0.990    0.141 
    -----------------------------------------------------------------------------------------