2015-10-31 53 views
1

這裏是我與建立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

會非常感謝這裏的某些方向。

回答

1

一個快速和骯髒的解決方案,使用黃土interploation。 跨度設置非常短以確保完美匹配,可悲的是這也使得任何錯誤詞彙都毫無意義。可能值得嘗試一個適當的迴歸。

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) 

enter image description here