代码之家  ›  专栏  ›  技术社区  ›  bill999

带平均数的esttab中的子群

  •  1
  • bill999  · 技术社区  · 6 年前

    以下是我尝试过的Stata代码:

    eststo clear
    sysuse auto, clear
    eststo Dom: estpost sum rep78 mpg turn trunk weight length if foreign==0
    eststo For: estpost sum rep78 mpg turn trunk weight length if foreign==1
    esttab Dom For, cells("mean(fmt(2))" "sd") ///
        nonumber nodepvars noobs se collabels(none) mlabels(, lhs("Var") title)
    

    --------------------------------------
    Var                   Dom          For
    --------------------------------------
    rep78                3.02         4.29
                         0.84         0.72
    mpg                 19.83        24.77
                         4.74         6.61
    turn                41.44        35.41
                         3.97         1.50
    trunk               14.75        11.41
                         4.31         3.22
    weight            3317.12      2315.91
                       695.36       433.00
    length             196.13       168.55
                        20.05        13.68
    --------------------------------------
    

    这样做的目的是计算几个变量的平均值和标准差,使用 summarize . 这是根据一个条件分别进行的(一次用于国外观察,一次用于非国外观察)。

    esttab . 我最终会希望在LaTeX中得到这个结果,但是为了简单起见,这个例子展示了Stata的结果。

    我有两个问题:

    1. 我怎样才能得到括号里的标准差呢?

    2. 是否可以在变量之间包含任何行来分隔两个不同的组?

    --------------------------------------
    Var                   Dom          For
    --------------------------------------
    Variable Group 1:
    --------------------------------------
    rep78                3.02         4.29
                        (0.84)       (0.72)
    mpg                 19.83        24.77
                        (4.74)       (6.61)
    turn                41.44        35.41
                        (3.97)       (1.50)
    --------------------------------------
    Variable Group 2:
    --------------------------------------
    trunk               14.75        11.41
                       (4.31)       (3.22)
    weight            3317.12      2315.91
                     (695.36)      (433.00)
    length             196.13       168.55
                      (20.05)       (13.68)
    --------------------------------------
    

    我想用 eststo 等,如果可能的话。我更希望它尽可能自动化,但我愿意将矩阵从Stata导出到LaTeX或使用片段(如果需要的话)。如果这是不可能的,我也愿意接受其他解决办法。

    1 回复  |  直到 6 年前
        1
  •  3
  •   user8682794 user8682794    6 年前

    关于第一个问题,您需要指定选项 par 在里面 sd 在内部 cells() :

    sysuse auto, clear
    
    eststo clear
    
    eststo Dom: estpost sum rep78 mpg turn trunk weight length if foreign==0
    eststo For: estpost sum rep78 mpg turn trunk weight length if foreign==1
    esttab Dom For, cells("mean(fmt(2))" "sd(par)") ///
        nonumber nodepvars noobs se collabels(none) mlabels(, lhs("Var") title)
    

    eststo clear
    
    eststo Dom: estpost sum rep78 mpg turn if foreign==0
    eststo For: estpost sum rep78 mpg turn if foreign==1
    esttab Dom For using output.txt, cells("mean(fmt(2))" "sd(par)") ///
        nonumber nodepvars noobs collabels(none) mlabels(, lhs("Vars") title) ///
        posthead("@hline" "Variable Group 1:" "@hline" ) postfoot(" ") replace
    
    eststo clear
    
    eststo Dom: estpost sum trunk weight length if foreign==0
    eststo For: estpost sum trunk weight length if foreign==1
    esttab Dom For using output.txt, cells("mean(fmt(2))" "sd(par)") ///
        nonumber nodepvars noobs collabels(none) mlabels(none)  ///
        prehead("@hline" "Variable Group 2:") append
    

    这将产生所需的输出:

    type output.txt
    
    --------------------------------------
    Vars                  Dom          For
    --------------------------------------
    Variable Group 1:
    --------------------------------------
    rep78                3.02         4.29
                       (0.84)       (0.72)
    mpg                 19.83        24.77
                       (4.74)       (6.61)
    turn                41.44        35.41
                       (3.97)       (1.50)
    
    --------------------------------------
    Variable Group 2:
    --------------------------------------
    trunk               14.75        11.41
                       (4.31)       (3.22)
    weight            3317.12      2315.91
                     (695.36)     (433.00)
    length             196.13       168.55
                      (20.05)      (13.68)
    --------------------------------------