首页 文章

如果缺少值,则按子组删除-tabstat-的摘要统计信息

提问于
浏览
2

我正在编写一个代码,使用 tabstat, by()esttab 在Latex中导出摘要统计表 .

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

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

输出将摘要统计信息显示为两个子表,每个子表对应一个数据集(由 rep2 定义) . 这两个数据集不一定具有相同的变量:第一个数据集中完全缺少 price .

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

我查看了 tabstat 的文档,但我不太清楚如何继续 . 我是否必须使用 estoutdrop() 选项?

非常感谢,S

1 回答

  • 3

    如您所述,您可以使用 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)
    

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

    注意我在分组变量的值中取出了空格 . 当打电话给_1266633时,这似乎很麻烦,但我留给你去探索 .

相关问题