在Stata中的汇总统计表中添加一列差异

发布于 2024-12-06 01:44:50 字数 854 浏览 1 评论 0原文

如果我使用 table 在 Stata 中制作双向汇总统计表,我可以添加另一列作为其他两列的差异吗?

假设我有三个变量 (a, b, c)。我在 ab 上生成五分位数,然后在每个五分位数-五分位数交叉点生成 c 均值的双向表。我想生成第六列,它是 b 的每个五分位数的 b 的顶部和底部五分位数之间的平均值 c 的差异。

我可以为每个五分位数-五分位数交集生成均值 c 表,但我无法找出差异列。

* 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)

更新:这是我想要执行的操作的示例。在此处输入图像描述

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.enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2024-12-13 01:44:50

使用collapse 命令,您可以创建您想要的自定义表格。

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

然后你应该在输出中获得类似这样的内容:

  +-----------------------------------------+
  | 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
help subscripting

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文