1

我有以下数据集,其数量被称为(不同大小的)间隔:

Income              Numerosity
from 6000 to 7500       704790
from 7500 to 10000     1294784
from 10000 to 12000    1051902
from 12000 to 15000    1585132
from 15000 to 20000     704012
from 20000 to 25000     206901
from 25000 to 30000     156661

我想获得一个(近似的)数据集,如下所示:

Income  Numerosity
6000           ...
7000           ... 
8000           ...
...            ...
30000          ...

为此,我尝试了以下方法:首先,我使用sample(6000:7500, 704790, replace=TRUE)每一行并连接结果来创建rpop生成观察的向量。然后,我应用了函数density(我尝试了不同的参数值bw来平滑分布)

d=density(rpop,bw=2000,from=6000,to=30000,n=25)

d$x给出所需的收入水平,而数量与d$y

但是,我想知道是否有更好(更直接或更优雅)的方法来获得相同的结果。

4

1 回答 1

1

approx函数适用于这种插值。

例子:

> d <- read.table(header=T, text="Income     Numerosity
+ 6000       704790
+ 7500       1294784
+ 10000      1051902
+ 12000      1585132
+ 15000      704012
+ 20000      206901
+ 25000      156661")

> res <- approx(d$Income, d$Numerosity, seq(from=6000, to=30000, length.out=25))
> res
$x
 [1]  6000  7000  8000  9000 10000 11000 12000 13000 14000 15000 16000 17000
[13] 18000 19000 20000 21000 22000 23000 24000 25000 26000 27000 28000 29000
[25] 30000

$y
 [1]  704790.0 1098119.3 1246207.6 1149054.8 1051902.0 1318517.0 1585132.0
 [8] 1291425.3  997718.7  704012.0  604589.8  505167.6  405745.4  306323.2
[15]  206901.0  196853.0  186805.0  176757.0  166709.0  156661.0        NA
[22]        NA        NA        NA        NA
于 2013-11-10T21:25:10.963 回答