开发者

Add a column of differences to tables of summary statistics in Stata

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.

Add a column of differences to tables of summary statistics in Stata


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜