hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
321 stars 28 forks source link

can huxreg flip a regression table? #67

Closed randomgambit closed 6 years ago

randomgambit commented 6 years ago

Hello and thanks for this amazing package!

I wonder if huxtable can flip a regression table. For instance, consider this example

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)

stargazer cannot do that, and I was unable to do it with huxtable Am I missing something? Thanks!

hughjonesd commented 6 years ago

You can use t() to transpose a huxtable:


> hr <- huxreg(linear.1, linear.2, coefs = c('complaints', 'privileges'), note = NULL)
> hr
──────────────────────────────────────────
                   (1)           (2)      
             ─────────────────────────────
  complaints     0.692 ***     0.682 ***  
                (0.149)       (0.129)     
  privileges    -0.104        -0.103      
                (0.135)       (0.129)     
             ─────────────────────────────
  N             30            30          
  R2             0.715         0.715      
  logLik       -98.187       -98.206      
  AIC          210.375       206.412      
──────────────────────────────────────────

Column names: names, model1, model2
> t(hr)
│       complain             privileg             N    R2      logLik    AIC     │
│       ts                   es                                                  │
│ (1) │    0.692   (0.149)     -0.104   (0.135) │ 30   0.715   -98.187   210.375 │
│     │      ***                                │                                │
│ (2) │    0.682   (0.129)     -0.103   (0.129) │ 30   0.715   -98.206   206.412 │
│     │      ***                                │                                │

Obviously you'll need to faff around a bit with borders etc. to get what you want.

randomgambit commented 6 years ago

ok thanks! I will try