[英]Extracting/Exporting the Data of the Empirical Cumulative Distribution Function in R (ecdf)
[英]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.