使用Stargazer包按分类变量获取单独的摘要统计信息

问题描述 投票:7回答:4

我想使用为分组变量的每个类别生成摘要统计信息。我可以在单独的表中完成它,但我喜欢它在一个 - 如果这对于这个包没有不合理的挑战。

例如

library(stargazer)
stargazer(ToothGrowth, type = "text")
#> 
#> =========================================
#> Statistic N   Mean  St. Dev.  Min   Max  
#> -----------------------------------------
#> len       60 18.813  7.649   4.200 33.900
#> dose      60 1.167   0.629   0.500 2.000 
#> -----------------------------------------

提供ToothGrowth中连续变量的夏季统计数据。我想用supp中的分类变量ToothGrowth来分割那个夏天。

对期望结果的两点建议,

stargazer(ToothGrowth ~ supp, type = "text")
#> 
#> ==================================================
#> Statistic         N   Mean   St. Dev.  Min   Max  
#> --------------------------------------------------
#> OJ       len       30 16.963  8.266   4.200 33.900
#>          dose      30  1.167  0.634   0.500  2.000
#> VC       len       30 20.663  6.606   8.200 30.900
#>          dose      30  1.167  0.634   0.500  2.000 
#> --------------------------------------------------
#> 
 stargazer(ToothGrowth ~ supp, type = "text")
#> 
#> ==================================================
#> Statistic          N   Mean   St. Dev.  Min   Max  
#> --------------------------------------------------
#> len               
#>        _by VC     30 16.963  8.266   4.200 33.900
#>        _by VC     30  1.167  0.634   0.500  2.000
#> _tot              60 18.813  7.649   4.200 33.900
#> 
#> dose             
#>        _by OJ     30 20.663  6.606   8.200 30.900
#>        _by OJ     30  1.167  0.634   0.500  2.000 
#> _tot              60 1.167   0.629   0.500 2.000         
#> --------------------------------------------------
r summary stargazer
4个回答
7
投票

Solution

library(stargazer)
library(dplyr)
library(tidyr)

ToothGrowth %>%
    group_by(supp) %>%
    mutate(id = 1:n()) %>%
    ungroup() %>%
    gather(temp, val, len, dose) %>%
    unite(temp1, supp, temp, sep = '_') %>%
    spread(temp1, val) %>%
    select(-id) %>%
    as.data.frame() %>%
    stargazer(type = 'text')

Result

=========================================
Statistic N   Mean  St. Dev.  Min   Max  
-----------------------------------------
OJ_dose   30 1.167   0.634   0.500 2.000 
OJ_len    30 20.663  6.606   8.200 30.900
VC_dose   30 1.167   0.634   0.500 2.000 
VC_len    30 16.963  8.266   4.200 33.900
-----------------------------------------

Explanation

这解决了OP在对原始答案的评论中提到的问题,“我真正想要的是一个表格,其中汇总统计信息由分类变量分隔,而不是创建单独的表格。”我用stargazer看到的最简单的方法是使用gather()unite()spread()策略创建一个新数据框,其中包含每个组观察的变量。唯一的技巧是通过按组创建唯一标识符并在调用stargazer()之前删除该变量来避免重复标识符。


4
投票

三种可能的解决方一个使用,一个使用工具和,第三个使用解决方案。

First,

我想建议你看看有点离开,但我想你应该看看它,

# 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

在乳胶你得到这个不错的结果,Latex with reporttools

Second,

使用工具以及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,

独家

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] "-----------------------------------------"

1
投票
invisible(lapply(levels(ToothGrowth$supp),stargazer))

会这样做,但是如果你想在它们之间使用单独的\ subsection {},你很可能应该使用类似的东西

invisible(lapply(levels(ToothGrowth$supp),function(sg){
    cat("\\subsection{add your text here}\n")
    print(stargazer(sg)
  })

0
投票

你可以简单地使用subset和观星者。 *另外,请确保您的数据是使用as.data.frame的数据框,以便观星者产生输出。

library(stargazer)
# Descriptive statistics for Income of Org 1
stargazer(subset(mydata, mydata$org==1),
title="Income for Org 1", type = "html", out="stat_org1.html")
© www.soinside.com 2019 - 2024. All rights reserved.