用于knitr的stargazer的多列输出

时间:2022-05-15 06:08:29

Here is MWE:

这是MWE:

library(pscl)

data("bioChemists", package = "pscl")

fm_pois <- glm(art ~ ., data = bioChemists, family = poisson)
fm_qpois <- glm(art ~ ., data = bioChemists, family = quasipoisson)
fm_nb <- glm.nb(art ~ ., data = bioChemists)
fm_zinb <- zeroinfl(art ~ . | 1, data = bioChemists, dist = "negbin")

library(stargazer)
stargazer(
  fm_pois, fm_qpois,  fm_nb, fm_zinb
  , type = "text"
)

=============================================================================
                                      Dependent variable:                    
                  -----------------------------------------------------------
                                              art                            
                   Poisson   glm: quasipoisson     negative     zero-inflated
                                link = log         binomial      count data  
                     (1)            (2)              (3)             (4)     
-----------------------------------------------------------------------------
femWomen          -0.225***      -0.225***        -0.216***       -0.216***  
                   (0.055)        (0.074)          (0.073)         (0.073)   

marMarried         0.155**        0.155*            0.150*         0.150*    
                   (0.061)        (0.083)          (0.082)         (0.082)   

kid5              -0.185***      -0.185***        -0.176***       -0.176***  
                   (0.040)        (0.054)          (0.053)         (0.053)   

phd                 0.013          0.013            0.015           0.015    
                   (0.026)        (0.036)          (0.036)         (0.036)   

ment               0.026***      0.026***          0.029***       0.029***   
                   (0.002)        (0.003)          (0.003)         (0.003)   

Constant           0.305***       0.305**           0.256*         0.256*    
                   (0.103)        (0.139)          (0.137)         (0.139)   

-----------------------------------------------------------------------------
Observations         915            915              915             915     
Log Likelihood    -1,651.056                      -1,561.958     -1,560.959  
theta                                          2.264*** (0.271)              
Akaike Inf. Crit. 3,314.113                       3,135.917                  
=============================================================================
Note:                                             *p<0.1; **p<0.05; ***p<0.01

I'm looking for multicolumn output like this:

我正在寻找像这样的多列输出:

=============================================================================
                                      Dependent variable:                    
                  -----------------------------------------------------------
                                              art                            
                           Poisson              Negative Binomial 

                    Poisson     QuasiPoisson         NB           ZINB

                     (1)            (2)              (3)             (4)     
-----------------------------------------------------------------------------
femWomen          -0.225***      -0.225***        -0.216***       -0.216***  
                   (0.055)        (0.074)          (0.073)         (0.073)   

marMarried         0.155**        0.155*            0.150*         0.150*    
                   (0.061)        (0.083)          (0.082)         (0.082)   

kid5              -0.185***      -0.185***        -0.176***       -0.176***  
                   (0.040)        (0.054)          (0.053)         (0.053)   

phd                 0.013          0.013            0.015           0.015    
                   (0.026)        (0.036)          (0.036)         (0.036)   

ment               0.026***      0.026***          0.029***       0.029***   
                   (0.002)        (0.003)          (0.003)         (0.003)   

Constant           0.305***       0.305**           0.256*         0.256*    
                   (0.103)        (0.139)          (0.137)         (0.139)   

-----------------------------------------------------------------------------
Observations         915            915              915             915     
Log Likelihood    -1,651.056                      -1,561.958     -1,560.959  
theta                                          2.264*** (0.271)              
Akaike Inf. Crit. 3,314.113                       3,135.917                  
=============================================================================
Note:                                             *p<0.1; **p<0.05; ***p<0.01
  1. First row should have the word Poisson for first two columns and Negative Binomial for next two columns.
  2. 第一行的前两列应为Poisson,后两列应为负二项。
  3. Second row should have columns names like Poisson, Quasi Poisson, Negative Binomial and Zero Inflated Negative Binomial.
  4. 第二行应具有列名称,如Poisson,Quasi Poisson,Negative Binomial和Zero Inflated Negative Binomial。

I found this link but could not figured out how to get this one. Any help will be highly appreciated. Thanks

我找到了这个链接,但无法弄清楚如何获得这个。任何帮助将受到高度赞赏。谢谢

1 个解决方案

#1


4  

Like Nick Kennedy I do not think that stargazer can produce your desired output directly.

像尼克肯尼迪一样,我认为观星者不能直接产生你想要的输出。

Therefore, here a workaround: Save the stargazer table in an object and add the desired lines manually. I hardcoded this here; with some more effort it should be possible to center the text above the respective columns automatically. Note that I slightly changed your stargazer call in order to hide the (wrong) model names.

因此,这里有一个解决方法:将stargazer表保存在对象中并手动添加所需的行。我在这里硬编码;通过更多的努力,应该可以自动将文本置于相应列的上方。请注意,为了隐藏(错误的)模型名称,我稍微更改了您的观星者调用。

library(pscl)
library(stargazer)

data("bioChemists", package = "pscl")

fm_pois <- glm(art ~ ., data = bioChemists, family = poisson)
fm_qpois <- glm(art ~ ., data = bioChemists, family = quasipoisson)
fm_nb <- glm.nb(art ~ ., data = bioChemists)
fm_zinb <- zeroinfl(art ~ . | 1, data = bioChemists, dist = "negbin")

byLine <- 
  do.call("c", 
          strsplit(
            capture.output(
              stargazer(fm_pois, fm_qpois,  fm_nb, fm_zinb, 
                        type = "text", model.names = FALSE)
              ),
            "\n"))

result <- append(
  byLine, 
  c(
  "                        Poisson               Negative Binomial",
  "",
  "                   Poisson  QuasiPoisson      NB          ZINB"
  ), 
  after = c(4, 5, 6))

cat(paste(result, collapse = "\n"))

# ==================================================================
#                                 Dependent variable:               
#                   ------------------------------------------------
#                                         art                       
#                         Poisson               Negative Binomial
# 
#                    Poisson  QuasiPoisson      NB          ZINB
#                      (1)        (2)          (3)           (4)    
# ------------------------------------------------------------------
# femWomen          -0.225***  -0.225***    -0.216***     -0.216*** 
#                    (0.055)    (0.074)      (0.073)       (0.073)  
#                                                                   
# marMarried         0.155**    0.155*        0.150*        0.150*  
#                    (0.061)    (0.083)      (0.082)       (0.082)  
#                                                                   
# kid5              -0.185***  -0.185***    -0.176***     -0.176*** 
#                    (0.040)    (0.054)      (0.053)       (0.053)  
#                                                                   
# phd                 0.013      0.013        0.015         0.015   
#                    (0.026)    (0.036)      (0.036)       (0.036)  
#                                                                   
# ment               0.026***  0.026***      0.029***      0.029*** 
#                    (0.002)    (0.003)      (0.003)       (0.003)  
#                                                                   
# Constant           0.305***   0.305**       0.256*        0.256*  
#                    (0.103)    (0.139)      (0.137)       (0.139)  
#                                                                   
# ------------------------------------------------------------------
# Observations         915        915          915           915    
# Log Likelihood    -1,651.056              -1,561.958    -1,560.959
# theta                                  2.264*** (0.271)           
# Akaike Inf. Crit. 3,314.113               3,135.917               
# ==================================================================
# Note:                                  *p<0.1; **p<0.05; ***p<0.01

#1


4  

Like Nick Kennedy I do not think that stargazer can produce your desired output directly.

像尼克肯尼迪一样,我认为观星者不能直接产生你想要的输出。

Therefore, here a workaround: Save the stargazer table in an object and add the desired lines manually. I hardcoded this here; with some more effort it should be possible to center the text above the respective columns automatically. Note that I slightly changed your stargazer call in order to hide the (wrong) model names.

因此,这里有一个解决方法:将stargazer表保存在对象中并手动添加所需的行。我在这里硬编码;通过更多的努力,应该可以自动将文本置于相应列的上方。请注意,为了隐藏(错误的)模型名称,我稍微更改了您的观星者调用。

library(pscl)
library(stargazer)

data("bioChemists", package = "pscl")

fm_pois <- glm(art ~ ., data = bioChemists, family = poisson)
fm_qpois <- glm(art ~ ., data = bioChemists, family = quasipoisson)
fm_nb <- glm.nb(art ~ ., data = bioChemists)
fm_zinb <- zeroinfl(art ~ . | 1, data = bioChemists, dist = "negbin")

byLine <- 
  do.call("c", 
          strsplit(
            capture.output(
              stargazer(fm_pois, fm_qpois,  fm_nb, fm_zinb, 
                        type = "text", model.names = FALSE)
              ),
            "\n"))

result <- append(
  byLine, 
  c(
  "                        Poisson               Negative Binomial",
  "",
  "                   Poisson  QuasiPoisson      NB          ZINB"
  ), 
  after = c(4, 5, 6))

cat(paste(result, collapse = "\n"))

# ==================================================================
#                                 Dependent variable:               
#                   ------------------------------------------------
#                                         art                       
#                         Poisson               Negative Binomial
# 
#                    Poisson  QuasiPoisson      NB          ZINB
#                      (1)        (2)          (3)           (4)    
# ------------------------------------------------------------------
# femWomen          -0.225***  -0.225***    -0.216***     -0.216*** 
#                    (0.055)    (0.074)      (0.073)       (0.073)  
#                                                                   
# marMarried         0.155**    0.155*        0.150*        0.150*  
#                    (0.061)    (0.083)      (0.082)       (0.082)  
#                                                                   
# kid5              -0.185***  -0.185***    -0.176***     -0.176*** 
#                    (0.040)    (0.054)      (0.053)       (0.053)  
#                                                                   
# phd                 0.013      0.013        0.015         0.015   
#                    (0.026)    (0.036)      (0.036)       (0.036)  
#                                                                   
# ment               0.026***  0.026***      0.029***      0.029*** 
#                    (0.002)    (0.003)      (0.003)       (0.003)  
#                                                                   
# Constant           0.305***   0.305**       0.256*        0.256*  
#                    (0.103)    (0.139)      (0.137)       (0.139)  
#                                                                   
# ------------------------------------------------------------------
# Observations         915        915          915           915    
# Log Likelihood    -1,651.056              -1,561.958    -1,560.959
# theta                                  2.264*** (0.271)           
# Akaike Inf. Crit. 3,314.113               3,135.917               
# ==================================================================
# Note:                                  *p<0.1; **p<0.05; ***p<0.01