如果缺少值,则从 -tabstat- by subgroup 中省略汇总统计信息

omitting summary statistics from -tabstat- by subgroup if they are missing values

我正在编写代码,使用 tabstat, by()esttab 在 Latex 中导出摘要统计信息 table。

这里有一个复制我的数据结构的玩具示例:

cls
clear all
set more off

use auto, clear

// Create two groups to be used in the -by()- option
gen rep2="First dataset" if rep78>=3
replace rep2="Second dataset" if rep78<3 

// Recode "price" as completely missing in the first group
replace price=. if rep2=="First dataset"  

// Table
eststo: estpost tabstat weight price mpg trunk, ///
    column(statistics) statistics(count mean median sd) by(rep2) nototal

local sum_statistics "count(label(Observations)) mean(label(Mean) fmt(2)) p50(label(Median)) sd(label(Standard deviation) fmt(2))"

esttab using "table1.tex", replace type ///
    title("Summary Statistics")  ///
    cells("`sum_statistics'") ///   
    noobs nonum booktabs

输出将汇总统计信息显示为两个子 table,每个数据集一个(由 rep2 定义)。这两个数据集不一定具有相同的变量:price 在第一个数据集中完全缺失。

我只想完全省略 "First dataset" 的 price 的汇总统计行(留给 "Second dataset")。 这是因为 "First dataset" 缺少变量 price,因此它的所有摘要统计信息都缺少值。 这相当于在 "Observations" 等于特定按组中的 0 的情况下省略整行汇总统计信息。

我查看了 tabstat 的文档,但我不太确定如何继续。我必须使用 estoutdrop() 选项吗?

非常感谢,S

如您所述,您可以使用 drop() 选项:

clear all
set more off

sysuse auto, clear

// Create two groups to be used in the -by()- option
gen rep2="First" if rep78>=3
replace rep2="Second" if rep78<3 

// Recode "price" as completely missing in the first group
replace price=. if rep2=="First dataset"  

// Table
eststo: estpost tabstat weight price mpg trunk, ///
    column(statistics) statistics(count mean median sd) by(rep2) nototal

local sum_statistics "count(label(Observations)) mean(label(Mean) fmt(2)) p50(label(Median)) sd(label(Standard deviation) fmt(2))"

esttab, replace type ///
    title("Summary Statistics")  ///
    cells("`sum_statistics'") ///   
    noobs nonum booktabs drop(First:price)

这涉及使用 全名 而不仅仅是变量名。

注意我去掉了分组变量值中的空白space。调用esttab好像有点麻烦,留给大家自己去探索吧