[英]Calculate income and wealth shares in R
I have a simple question, but I am confused with how deciles, quantiles, percentiles are defined. 我有一个简单的问题,但是我对十分位,分位数,百分位数的定义感到困惑。
My purpose is to compute various income and wealth shares. 我的目的是计算各种收入和财富份额。 That is the share of x% per cent of the population from total income or wealth. 这就是总收入或财富占总人口x%的份额。
So, say that one wants to compute how much wealth the top 10% own. 因此,有人说要计算前10%的人拥有多少财富。
How I can do this on R? 我如何在R上做到这一点? Are my below calculation correct? 我的以下计算正确吗?
MWE MWE
w<-rgamma(10000, 3, scale = 1/3)
per <- quantile(w, c(0.1, 0.9))
top_1_percent <- (per[2]/sum(w))*100
bottom_90_percent <-per[1]/sum(w))*100
The top 10% should be: 前10%应该是:
sum(w[w > per[2]])/sum(w)
Alternately: 交替:
sum(tail(sort(w), .1 * length(w))) / sum(w)
The bottom 90% is 1 - top 10%. 最低的90%是1-最高的10%。
If I understand the question correctly, the following will do it. 如果我正确理解了该问题,则可以执行以下操作。
set.seed(1234) # Make the results reproducible
w <- rgamma(10000, 3, scale = 1/3)
per <- quantile(w, c(0.1, 0.9))
Now get an index i1
on the top 10% and sum their wealth. 现在获得前10%的指数i1
并求和他们的财富。
i1 <- w >= per[2]
sum(w[i1])
#[1] 2196.856
And the same for the bottom 10%, with index i2
. 底部10%的索引相同,索引为i2
。
i2 <- w <= per[1]
sum(w[i2])
#[1] 254.6375
Note that I am using >=
and <=
. 请注意,我正在使用>=
和<=
。 See the help page ?quantile
to see the types of quantile computations R can do. 请参阅帮助页面?quantile
以查看R可以执行的分位数计算的类型。 This is given by argument type
. 这由参数type
给定。
Edit. 编辑。
To compute proportions and percentages of wealth of the top 10% and bottom 10%, divide by the total wealth and multiply by 100
. 要计算前10%和后10%的财富比例和百分比,除以总财富再乘以100
。
top10 <- sum(w[i1])/sum(w)
top10
#[1] 0.221291
100*top10
#[1] 22.1291
bottom10 <- sum(w[i2])/sum(w)
bottom10
#[1] 0.02564983
100*bottom10
#[1] 2.564983
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.