【发布时间】:2013-04-05 15:57:33
【问题描述】:
是否有可能在Stata中获得一个变量中对应于另一个变量最大值的观察值? (类似于 SQL 中的this)。
编辑:你是对的,尼克。我对细节很轻。我的数据集看起来像 this(wfs Google Docs 表格;无法确定如何在此处格式化表格)。
我的目标是创建两个变量,其中“col”和“row”值分别对应于“压力”的最大值和条件。
【问题讨论】:
标签: stata
是否有可能在Stata中获得一个变量中对应于另一个变量最大值的观察值? (类似于 SQL 中的this)。
编辑:你是对的,尼克。我对细节很轻。我的数据集看起来像 this(wfs Google Docs 表格;无法确定如何在此处格式化表格)。
我的目标是创建两个变量,其中“col”和“row”值分别对应于“压力”的最大值和条件。
【问题讨论】:
标签: stata
这是相当笼统的,但简短的回答确实是肯定的。这里有两个具体的例子,一个是整体最大值,另一个是组内最大值。
. sysuse auto, clear
(1978 Automobile Data)
. su mpg , meanonly
. list weight if mpg == r(max)
+--------+
| weight |
|--------|
71. | 2,040 |
+--------+
. egen maxmpg = max(mpg), by(rep78)
. list rep78 maxmpg weight if mpg == maxmpg
+-------------------------+
| rep78 maxmpg weight |
|-------------------------|
7. | . 26 2,230 |
14. | 3 29 2,110 |
18. | 2 24 2,750 |
40. | 1 24 2,730 |
45. | . 26 2,520 |
|-------------------------|
52. | 2 24 2,690 |
63. | 4 30 1,980 |
71. | 5 41 2,040 |
+-------------------------+
一些一般说明:
注意平局,尤其是当数据是(保存为)整数时。
注意您的选择变量或任何其他变量上的缺失值。 (例如,缺失值是 sorted 到数据集的末尾。)
与最大值比较时要注意精度问题。 (此答案中未记录,但search precision, faq 引发了许多讨论。)
http://www.stata-journal.com/article.html?article=dm0055 讨论相关技术。
(更新)
听起来像
. bysort side condition (pressure) : gen rowmax = row[_N]
. bysort side condition (pressure) : gen colmax = col[_N]
有关by: 的教程,请参阅http://www.stata-journal.com/sjpdf.html?articlenum=pr0004 这是您应该阅读的免费.pdf 链接。和以前一样,如果您在pressure 上有缺失值,您将需要一些不同的东西,因为缺失值将被排序到side 和condition 的每个块的末尾。
【讨论】: