具有因子和连续变量的摘要统计表

问题描述 投票:0回答:1

我正在尝试创建一个简单的汇总统计表(最小值,最大值,平均值,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的先前答案很好地总结了[[one因素变量。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的使用是首选,但如果有一种更简单的方法来使用其他库生成这种汇总表,那仍然会非常有帮助。    
  • r tidyverse stargazer sjplot
    1个回答
    0
    投票
    最后,我发现代替model.matrix(),而不是[[应该在进行虚拟变量时放弃基本情况,一个简单的解决方法是使用mlr::createDummyFeatures(),然后发送到stargazer。] >

    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 
    ------------------------------------------------------
        
    © www.soinside.com 2019 - 2024. All rights reserved.