将一列差异添加到Stata中的汇总统计表中 [英] Add a column of differences to tables of summary statistics in Stata
问题描述
如果我使用 table
在Stata中创建一个双向汇总统计表,我可以添加另一列,它是另外两列的区别 ?
假设我有三个变量( a,b,c
)。我在 a
和 b
上生成五元组,然后生成一个双向表格 c
在每个五等分交叉点。我想生成第六列,它是 b
的顶部和底部五分位数之间的平均值 c
的差值每个五分位数 a
。
我可以生成平均值表 c
为每个五等分交叉点,但我无法找出差异列。
*生成数据
清除
set obs 2000
生成a = rnormal()
生成b = rnormal()
生成c = rnormal()
*生成a和b
的分位数b btile a_q = a,nquantiles(5)
xtile b_q = b,nquantiles(5)
*计算每个五分位交点的平均值
表a_q b_q,c(均值c)
* if如果b_q == 1 |,我想要顶部和底部b分位数
表a_q b_q b_q == 5,c(mean c)
更新:以下是我想要做的一个示例。使用 collapse
命令,您可以创建自定义表格,如下所示: 你必须记住。
保存
折叠(均值)c,由(a_q b_q)
保留如果inlist(b_q,1,5)
重塑宽c,我(a_q)j(b_q)
gen c5_c1 = c5 - c1
set obs`= _N + 1'$ b $如果mi(a_q)
替换c5 = c5 [`= _N - 1'] - c5 [1]如果mi(a_q)$替换c1 = c1 [`= _N - 1'] - c1 [1] b $ b替换c5_c1 = c5_c1 [`= _N - 1'] - c5_c1 [1] if mi(a_q)
list,sep(0)noobs
restore
然后,您应该在输出中获得如下所示的内容:
+ ----------------------------------------- +
| a_q c1 c5 c5_c1 |
| ----------------------------------------- |
| 1 .2092651 .1837719 -.0254932 |
| 2 .0256483 -.0118134 -.0374617 |
| 3 .022957 .0586441 .0356871 |
| 4 .0431809 .0876745 .0444935 |
| 5 -.0859874 .0199202 .1059076 |
| 。 -.2952525 -.1638517 .1314008 |
+ ----------------------------------------- +
如果您对Stata不太熟悉,以下帮助页面可能对理解代码很有用
help _variables
帮助下标
If I make a two way summary statistics table in Stata using table
, can I add another column that is the difference of two other columns?
Say that I have three variables (a, b, c
). I generate quintiles on a
and b
then generate a two-way table of means of c
in each quintile-quintile intersection. I would like to generate a sixth column that is the difference of mean c
between the top and bottom quintiles of b
for each quintile of a
.
I can generate the table of mean c
for each quintile-quintile intersection, but I can't figure out the difference column.
* generate data
clear
set obs 2000
generate a = rnormal()
generate b = rnormal()
generate c = rnormal()
* generate quantiles for for a and b
xtile a_q = a, nquantiles(5)
xtile b_q = b, nquantiles(5)
* calculate the means of each quintile intersection
table a_q b_q, c(mean c)
* if I want the top and bottom b quantiles
table a_q b_q if b_q == 1 | b_q == 5, c(mean c)
Update: Here's an example of what I would like to do.
With the collapse
command you can create customized tables like the one you have in mind.
preserve
collapse (mean) c, by(a_q b_q)
keep if inlist(b_q, 1, 5)
reshape wide c, i(a_q) j(b_q)
gen c5_c1 = c5 - c1
set obs `=_N + 1'
replace c1 = c1[`=_N - 1'] - c1[1] if mi(a_q)
replace c5 = c5[`=_N - 1'] - c5[1] if mi(a_q)
replace c5_c1 = c5_c1[`=_N - 1'] - c5_c1[1] if mi(a_q)
list, sep(0) noobs
restore
Then you should obtain something like this in your output:
+-----------------------------------------+
| a_q c1 c5 c5_c1 |
|-----------------------------------------|
| 1 .2092651 .1837719 -.0254932 |
| 2 .0256483 -.0118134 -.0374617 |
| 3 .022957 .0586441 .0356871 |
| 4 .0431809 .0876745 .0444935 |
| 5 -.0859874 .0199202 .1059076 |
| . -.2952525 -.1638517 .1314008 |
+-----------------------------------------+
If you are not very familiar with Stata, the following help pages might be useful in understanding the code
help _variables
help subscripting
这篇关于将一列差异添加到Stata中的汇总统计表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!