包含因素和连续变量的摘要统计表

时间:2020-06-11 00:07:42

标签: r tidyverse stargazer sjplot

我正在尝试创建一个简单的汇总统计表(最小值,最大值,平均值,n等),该表既可以处理因子变量又可以处理连续变量,即使存在多个因子变量也是如此。我正在尝试产生美观的HTML输出,例如stargazerhuxtable输出。

对于一个简单的可重现示例,我将使用mtcars,但将两个变量更改为factor,并简化为三个变量。

library(tidyverse)
library(stargazer)

mtcars_df <- mtcars
mtcars_df <- mtcars_df %>% 
  mutate(vs = factor(vs),
         am = factor(am)) %>% 
  select(mpg, vs, am)
head(mtcars_df)

因此,数据具有两个因子变量vsammpg保留为双精度:

#>    mpg vs am
#>  <dbl> <fctr> <fctr>
#> 1 21.0  0  1
#> 2 21.0  0  1
#> 3 22.8  1  1
#> 4 21.4  1  0
#> 5 18.7  0  0
#> 6 18.1  1  0

我想要的输出看起来像这样(仅格式,数字对am0并不都是正确的):

======================================================
Statistic N   Mean  St. Dev. Min Pctl(25) Pctl(75) Max
------------------------------------------------------
mpg       32 20.091  6.027   10    15.4     22.8   34 
vs0       32 0.562   0.504    0     0        1      1 
vs1       32 0.438   0.504    0     0        1      1 
am0       32 0.594   0.499    0     0        1      1 
am1       32 0.406   0.499    0     0        1      1 
------------------------------------------------------

直接调用stargazer不能处理因素(但下面有一个汇总一个因素的解决方案)

# this doesn't give factors
stargazer(mtcars_df, type = "text")
======================================================
Statistic N   Mean  St. Dev. Min Pctl(25) Pctl(75) Max
------------------------------------------------------
mpg       32 20.091  6.027   10    15.4     22.8   34 
------------------------------------------------------

@ jake-fisher的上一个答案很好地总结了一个因素变量。 https://stackoverflow.com/a/26935270/8742237

上一个答案的以下代码给出了第一个因子vs的两个值,即vs0vs1,但是当涉及第二个因子am时,它仅列出am的一个值的摘要统计信息:

  • am0丢失。

我确实意识到这是因为我们希望在建模时避免虚拟变量陷阱,但是我的问题不是关于建模,而是要创建一个包含所有因子变量的所有值的汇总表。

options(na.action = "na.pass")  # so that we keep missing values in the data
X <- model.matrix(~ . - 1, data = mtcars_df)
X.df <- data.frame(X)  # stargazer only does summary tables of data.frame objects
#names(X) <- colnames(X)
stargazer(X.df, type = "text")

======================================================
Statistic N   Mean  St. Dev. Min Pctl(25) Pctl(75) Max
------------------------------------------------------
mpg       32 20.091  6.027   10    15.4     22.8   34 
vs0       32 0.562   0.504    0     0        1      1 
vs1       32 0.438   0.504    0     0        1      1 
am1       32 0.406   0.499    0     0        1      1 
------------------------------------------------------

虽然最好使用stargazerhuxtable,但如果有一种更简单的方法来使用其他库生成这种汇总表,那还是很有帮助的。

1 个答案:

答案 0 :(得分:1)

最后,不是使用{em}设计的model.matrix()来创建虚拟变量时放弃基本情况,而是使用mlr::createDummyFeatures()来创建虚拟变量所有值(甚至是基本情况)的虚拟对象。

library(tidyverse)
library(stargazer)
library(mlr)

mtcars_df <- mtcars
mtcars_df <- mtcars_df %>% 
  mutate(vs = factor(vs),
         am = factor(am)) %>% 
  select(mpg, vs, am)
head(mtcars_df)


X <- mlr::createDummyFeatures(obj = mtcars_df)
X.df <- data.frame(X)  # stargazer only does summary tables of data.frame objects
#names(X) <- colnames(X)
stargazer(X.df, type = "text")

可提供所需的输出:

======================================================
Statistic N   Mean  St. Dev. Min Pctl(25) Pctl(75) Max
------------------------------------------------------
mpg       32 20.091  6.027   10    15.4     22.8   34 
vs.0      32 0.562   0.504    0     0        1      1 
vs.1      32 0.438   0.504    0     0        1      1 
am.0      32 0.594   0.499    0     0        1      1 
am.1      32 0.406   0.499    0     0        1      1 
------------------------------------------------------