如何根据 x 轴上的一个变量的百分位数和根据 y 轴上的百分位数绘制另一个值的平均值的图?

How to make a plot with percentiles of one variable on the x axis and the means of another value according to the percentiles on the y-axis?

大家好,

我有一个在我看来很基本的问题,但我无法找到快速解决问题的方法。

我的最终目标是绘制如下图:x 轴上的收入分布百分位数和另一个变量的均值,比方说年龄,根据 y 轴上的收入百分位数进行组织.

在我看来命令 collapse 应该能够做到这一点,但我找不到在任何地方指定崩溃(按收入百分位数)的方法。

我现在需要的是一行年龄和一行收入的 100 个百分位值之一,它们匹配并代表同一个人,然后我可以通过(我的新变量百分位)和然后绘图。

我很欣赏这可能是一个基本问题,但我似乎找不到解决方法!

非常感谢。

您可以考虑计算百分位数并在折叠数据之前按每 1 个百分位数范围对它们进行分组:

*Clear and Create Sample Data

clear *
set obs 10000
gen age = floor(uniform()*100)
gen inc = round((uniform()*100000),5)

* Create Percentiles, group by each 1 pctile range

sort inc
pctile P_inc = inc, nq(10000) genp(percent)

replace percent = ceil(percent)

* Now collapse data, Make desired plot

collapse (mean) age, by(percent)

或者,如果您想要与每个百分位数关联的收入值,您可以尝试手动执行此操作:

*picking up after sort inc:

gen P = _n/100
replace P = ceil(P)

gen incPct = (inc[_n]+inc[_n+1])/2 if P[_n] < P[_n+1]
replace incPct = inc if _n == _N

collapse (mean) age (mean) incPct, by(P)

请注意,无论这两种方法如何,年龄将在每个百分位数 "group"(0-1、1-2 等)内的所有观察值中取平均值,因为每个百分位数只是一个点在更大的数据集中。