Stata:在变量内但在个体之间进行比较

时间:2013-07-10 16:44:43

标签: comparison stata

我有一个变量ranking,它由许多不同大小的组组成,这些组都是排名的。因此,下一个1-4和下一个1-52可能是1-6组。

我知道想要创建两个变量,分别对他和他下面和下面的所有个体之间的差异进行求和。

对于一组5个人和个人1,这意味着我想要

UP:SUM(1-1)= 0 DOWN:SUM((1-5)+(1-4)+(1-3)+(1-2))= -10

2 个答案:

答案 0 :(得分:3)

这里似乎需要一些相当大的猜测。总结排名差异似乎不太可能是你想要的,因为那些只是几个算术进展,没有提供有关数据的信息。

以下内容可以重现并可能有所帮助。

. sysuse auto, clear
. bysort rep78 (mpg) : gen rank = _n
. bysort rep78 (rank) : gen cuscore = sum(mpg)
. bysort rep78 (rank) : gen above = cuscore - mpg 
. bysort rep78 (rank) : gen below = cuscore[_N] - cuscore

答案 1 :(得分:0)

谢谢尼克,

这实际上非常有帮助。 我做了一些小修改以获得我想要的东西,并检查它在数据窗口中是否有效。 我很抱歉没有提及我的问题,也没有提供玩具示例。

如果将来有人通过这篇文章发表评论,那么这是我修改后的答案

sysuse auto, clear
*just to have a better overview in the data window
keep mpg rep78
*creates ranking first by rep78 and within that by mpg
bysort rep78 (mpg) : gen rank = _n
*Sums mpg by rep78 and then by rank
bysort rep78 (rank) : gen cuscore = sum(rank)
*Create up as sum of the ranks minus individual rank 
bysort rep78 (rank) : gen up= cuscore - rank 
bysort rep78 (rank) : gen down= cuscore[_N] - cuscore