Obtaining Separate Summary Statistics by Categorical Variable with Stargazer Package

后端 未结 4 1136
轮回少年
轮回少年 2020-12-16 02:37

I would like to use stargazer to produce summary statistics for each category of a grouping variable. I could do it in separate tables, but I\'d like it all in one – if that

4条回答
  •  半阙折子戏
    2020-12-16 03:11

    Three possible solution. One using reporttools and xtable, one using tidyverse tools along with stargazer, and third a base-r solution.

    First,

    I want to suggest you take a look at reporttools which is kinda leaving stargazer, but I think you should take a look at it,

    # install.packages("reporttools")  #Use this to install it, do this only once
    require(reporttools)
    
    vars <- ToothGrowth[,c('len','dose')]
    group <- ToothGrowth[,c('supp')]
    
    ## display default statistics, only use a subset of observations, grouped analysis
    tableContinuous(vars = vars, group = group, prec = 1, cap = "Table of 'len','dose' by 'supp' ", lab = "tab: descr stat")
    
    % latex table generated in R 3.3.3 by xtable 1.8-2 package
    \begingroup\footnotesize
    \begin{longtable}{llrrrrrrrrrr}
     \textbf{Variable} & \textbf{Levels} & $\mathbf{n}$ & \textbf{Min} & $\mathbf{q_1}$ & $\mathbf{\widetilde{x}}$ & $\mathbf{\bar{x}}$ & $\mathbf{q_3}$ & \textbf{Max} & $\mathbf{s}$ & \textbf{IQR} & \textbf{\#NA} \\ 
      \hline
    len & OJ & 30 & 8.2 & 15.5 & 22.7 & 20.7 & 25.7 & 30.9 & 6.6 & 10.2 & 0 \\ 
       & VC & 30 & 4.2 & 11.2 & 16.5 & 17.0 & 23.1 & 33.9 & 8.3 & 11.9 & 0 \\ 
       \hline
     & all & 60 & 4.2 & 13.1 & 19.2 & 18.8 & 25.3 & 33.9 & 7.6 & 12.2 & 0 \\ 
       \hline
    dose & OJ & 30 & 0.5 &  0.5 &  1.0 &  1.2 &  2.0 &  2.0 & 0.6 &  1.5 & 0 \\ 
       & VC & 30 & 0.5 &  0.5 &  1.0 &  1.2 &  2.0 &  2.0 & 0.6 &  1.5 & 0 \\ 
       \hline
     & all & 60 & 0.5 &  0.5 &  1.0 &  1.2 &  2.0 &  2.0 & 0.6 &  1.5 & 0 \\ 
       \hline
    \hline
    \caption{Table of 'len','dose' by 'supp' } 
    \label{tab: descr stat}
    \end{longtable}
    \endgroup
    

    in latex you get this nice result,

    Second,

    using tidyverse tools along with stargazer, inspired by this SO answer,

    # install.packages(c("tidyverse"), dependencies = TRUE)
    library(dplyr); library(purrr)
    #> ToothGrowth %>% split(. $supp) %>% walk(~ stargazer(., type = "text"))
    #> =========================================
    #> Statistic N   Mean  St. Dev.  Min   Max  
    #> -----------------------------------------
    #> len       30 20.663  6.606   8.200 30.900
    #> dose      30 1.167   0.634   0.500 2.000 
    #> -----------------------------------------
    #> =========================================
    #> Statistic N   Mean  St. Dev.  Min   Max  
    #> -----------------------------------------
    #> len       30 16.963  8.266   4.200 33.900
    #> dose      30 1.167   0.634   0.500 2.000 
    #> -----------------------------------------
    #> 
    

    Third,

    an exclusive base-r

    by(ToothGrowth, ToothGrowth$supp, stargazer, type = 'text')
        #> =========================================
        #> Statistic N   Mean  St. Dev.  Min   Max  
        #> -----------------------------------------
        #> len       30 20.663  6.606   8.200 30.900
        #> dose      30 1.167   0.634   0.500 2.000 
        #> -----------------------------------------
        #> 
        #> =========================================
        #> Statistic N   Mean  St. Dev.  Min   Max  
        #> -----------------------------------------
        #> len       30 16.963  8.266   4.200 33.900
        #> dose      30 1.167   0.634   0.500 2.000 
        #> -----------------------------------------
        #> ToothGrowth$supp: OJ
        #> [1] ""                                         
        #> [2] "========================================="
        #> [3] "Statistic N   Mean  St. Dev.  Min   Max  "
        #> [4] "-----------------------------------------"
        #> [5] "len       30 20.663  6.606   8.200 30.900"
        #> [6] "dose      30 1.167   0.634   0.500 2.000 "
        #> [7] "-----------------------------------------"
        #> --------------------------------------------------------------- 
        #> ToothGrowth$supp: VC
        #> [1] ""                                         
        #> [2] "========================================="
        #> [3] "Statistic N   Mean  St. Dev.  Min   Max  "
        #> [4] "-----------------------------------------"
        #> [5] "len       30 16.963  8.266   4.200 33.900"
        #> [6] "dose      30 1.167   0.634   0.500 2.000 "
        #> [7] "-----------------------------------------"
    

提交回复
热议问题