繁体   English   中英

在R中建立经验累积分布函数和数据插值

[英]Building an empirical cumulative distribution function and data interpolation in R

这是我正在使用的示例数据框

 level    Income    cumpop
 1      17995.50  0.028405
 2      20994.75  0.065550
 3      29992.50  0.876185
 4      41989.50  2.364170
 5      53986.50  4.267305
 6      65983.50  6.323390
 7      77980.51  8.357625
 8      89977.50 10.238910
 9     101974.50 11.923545
10     113971.51 13.389680
11     125968.49 14.659165
12     137965.50 15.753850
13     149962.52 16.673735
14     161959.50 17.438485
15     173956.50 18.093985
16     185953.52 18.640235
17     197950.52 19.099085
18     209947.52 19.514235
19     221944.50 19.863835
20     233941.50 20.169735
21     251936.98 20.628585
22     275931.00 20.936670
23     383904.00 21.850000

该特定国家/地区的全部人口已按收入分类,并分为23个相应的“级别”。 Income变量是该级别所有成员的平均收入(这与说第10个百分位数的收入为17995.50非常重要,这是很重要的区别)。

但是每个级别的人口数量是不一致的(如果您看一下cumpop的差异(即累积人口),就会注意到这一点。 最终,我想构建一个10行的数据框,为变量Income给出插值的十分位数,这样,例如,我们可以说“最贫困的10%人口平均收入28,000”或“人口20至30%的平均收入为41,000英寸左右。 因此,我想有效地将​​这23个级别减少为10个等人口规模的级别(以cumpop [23]作为总人口),这需要一些插值。

我环顾四周,做这种经验累积分布函数生成/插值库,它似乎ecdf是非常有用的,但我不知道如何将它应用到Income受到cumpop如上所述。

将不胜感激这里的一些方向。

使用黄土插接的一种快速而肮脏的解决方案。 跨度设置得非常短,以确保基本完美的配合,可悲的是,这也使任何误差术语变得毫无意义。 尝试进行适当的回归可能值得。

incdist <- read.table("inc.txt", header=TRUE)

fit <- loess(incdist$Income~incdist$cumpop, span=0.2)
V2 <- predict(fit, seq(0, max(incdist$cumpop)*9/10, max(incdist$cumpop)/10))
V1 <- seq(0, max(incdist$cumpop)*9/10, max(incdist$cumpop)/10)
pred <- data.frame(V1, V2)

par(mar=c(5, 5.5, 4, 2) + 0.1)

plot(incdist$Income~incdist$cumpop, type="n", xaxt="n", yaxt="n",
    xlab="percentile", ylab=expression(frac("average income",1000)),
    main="income distribution")

abline(h=V2, v=V1[-1], col="grey")
points(incdist$Income~incdist$cumpop, col="grey")
lines(loess(incdist$Income~incdist$cumpop, span=0.2), col="red")
points(pred, col="blue", cex=1.5, pch=9)
axis(side=1, at=V1[-1], labels=c(1:9)*10)
axis(side=2, at=V2, labels=round(V2/1000), las=1)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM