【问题标题】:Fixed-width format with commas for summary statistics with estpost summarize in Stata带逗号的固定宽度格式,用于在 Stata 中使用 estpost 汇总统计信息
【发布时间】:2023-03-29 14:36:01
【问题描述】:

estpost 和 esttab 命令来自 SSC 上的 estout 包。

我使用estpost summarize 制作汇总统计表。 有人告诉我,读者更喜欢小数作为固定宽度/精度。 比如说,前导零和三个小数位。 选项fmt(%9.3f) 给出前导零和固定宽度, 但报告大量不带逗号。 选项fmt(%12.3gc) 给出逗号, 但不报告固定宽度小数。

有没有办法让fmt() 在esttab 中更细化? 比如说,对超过某个阈值的数字使用%12.3gc,否则使用%9.3f? 这可能会产生我没有预料到的问题。

这是我如何使用estpost summarize 的示例以及每种方法的缺点。

sysuse auto, clear
expand 1000

eststo clear
estpost summarize price rep78, detail
eststo

/* gives mean/median/SD with fixed number of decimals */
/* but must manually add commas to large numbers (price) */
local rightwidth_nocommas ///
    label noobs nonumbers nomtitles ///
    cell("count(fmt(%9.3gc) label(Obs.)) mean(fmt(%9.3f) label(Mean)) p50(fmt(%9.3f) label(Median)) sd(fmt(%9.3f) label(Std. Dev.))")
esttab, `rightwidth_nocommas'

. esttab, `rightwidth_nocommas'

------------------------------------------------------------------------
                             Obs.         Mean       Median    Std. Dev.
------------------------------------------------------------------------
Price                      74,000     6165.257     5006.500     2929.519
Repair Record 1978         69,000        3.406        3.000        0.983
------------------------------------------------------------------------



/* gives commas to large numbers (price) */
/* but must pad mean/median/SD to get a fixed number of decimals */
local commas_wrongwidth ///
    label noobs nonumbers nomtitles ///
    cell("count(fmt(%9.3gc) label(Obs.)) mean(fmt(%9.3gc) label(Mean)) p50(fmt(%9.3gc) label(Median)) sd(fmt(%9.3gc) label(Std. Dev.))")
esttab, `commas_wrongwidth'

. esttab, `commas_wrongwidth'

------------------------------------------------------------------------
                             Obs.         Mean       Median    Std. Dev.
------------------------------------------------------------------------
Price                      74,000        6,165        5,007        2,930
Repair Record 1978         69,000         3.41            3         .983
------------------------------------------------------------------------

【问题讨论】:

  • 那么您是否尝试过类似%9.3fc 的方法来获取小数点右侧3 位的数字,以及小数点左侧的大数逗号?
  • @WilliamLisowski 谢谢。这(大部分)在这种情况下有效。我之前在stats() 选项中为N 尝试过%9.3fc,但它不起作用(即,没有逗号,但固定小数)。所以对于计数,esttab 中的%9.3fc 给出XXXX.XXX(无逗号),但对于非计数,esttab 中的%9.3fc 给出X,XXX.XXX,这就是我想要的。 TLDR 将9.3gc 用于计数,%9.3fc 用于其他所有内容。
  • @WilliamLisowski 如果您想要声誉,请将此作为答案,我将标记为正确。否则,我可以回答。

标签: stata


【解决方案1】:

这是一个使用fc 处理所有内容的演示,计数不需要gc,并使用一组括号而不是单元格参数周围的引号将四个项目强制放在一行中。

. esttab, ///
>     label noobs nonumbers nomtitles               ///
>     cell((                                        ///
>            count(fmt(%9.0fc)  label(Obs.))        ///
>             mean(fmt(%10.3fc) label(Mean))        ///
>              p50(fmt(%8.1fc)  label(Median))      ///
>               sd(fmt(%10.3fc) label(Std. Dev.))   ///
>          ))

------------------------------------------------------------------------
                             Obs.         Mean       Median    Std. Dev.
------------------------------------------------------------------------
Price                      74,000    6,165.257      5,006.5    2,929.519
Repair Record 1978         69,000        3.406          3.0        0.983
------------------------------------------------------------------------

【讨论】:

  • 知道为什么%9.0fc 用逗号表示计数,而%9.3fc 没有吗?
  • 因为 74,000.000 的长度是 10 个字符,所以 Stata 做了一些事情使它适合 9 个字符。将 9 更改为 10 并返回逗号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 2020-12-29
相关资源
最近更新 更多