Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ecdf()和plyr::percent_rank()的不同百分位数_R_Data.table_Dplyr_Ecdf - Fatal编程技术网

使用ecdf()和plyr::percent_rank()的不同百分位数

使用ecdf()和plyr::percent_rank()的不同百分位数,r,data.table,dplyr,ecdf,R,Data.table,Dplyr,Ecdf,我一直试图根据大量的观察结果计算百分位数。我遇到了两种计算百分位数的不同方法。因为我正在处理面板数据集,所以我想将每个时间段的百分位数分组。为此,我使用了这个和这个问题 现在的问题是,这两个命令之间的百分比明显不同,我想知道这两个命令是否“正确”。 为了证明这一点: library(data.table) library(plyr) years = c(2006, 2006, 2006, 2006, 2001, 2001, 2001, 2001, 2001) scores = c(13, 65,

我一直试图根据大量的观察结果计算百分位数。我遇到了两种计算百分位数的不同方法。因为我正在处理面板数据集,所以我想将每个时间段的百分位数分组。为此,我使用了这个和这个问题

现在的问题是,这两个命令之间的百分比明显不同,我想知道这两个命令是否“正确”。 为了证明这一点:

library(data.table)
library(plyr)
years = c(2006, 2006, 2006, 2006, 2001, 2001, 2001, 2001, 2001)
scores = c(13, 65, 23, 34, 78, 56, 89, 98, 100)

dt <- data.table(years
                 , scores)

ddply(dt, .(years), transform, percentile = ecdf(scores)(scores)) 
ddply(dt, .(years), transform, percentile = round(percent_rank(scores), 4)) 
dt[, .( scores
      , ecdf.percentile = ecdf(scores)(scores)
      , p.rank.percentile = round(percent_rank(scores), 4) )
      , by = list(years)][order(years),]

查看
ecdf
percent\u rank
的定义。我认为您应该使用dplyr::cume\u dist。这将为您提供与ecdf相同的结果<代码>dt[,(分数,ecdf.percentile=ecdf(分数)(分数),p.rank.percentile=round(cume_dist(分数),4)),by=list(years)][顺序(years),]Ah ok,谢谢。但是我认为这些函数有一些混淆,在这个答案中也可以看到。我不知道这是否是计算四分位数的正确方法,这也是我的下一步。在这种情况下,我建议理解百分位数和分位数之间的差异。是啊,对不起,我昨天迟到了,不管怎样,谢谢。
   years scores ecdf.percentile p.rank.percentile
1:  2001     78            0.40            0.2500
2:  2001     56            0.20            0.0000
3:  2001     89            0.60            0.5000
4:  2001     98            0.80            0.7500
5:  2001    100            1.00            1.0000
6:  2006     13            0.25            0.0000
7:  2006     65            1.00            1.0000
8:  2006     23            0.50            0.3333
9:  2006     34            0.75            0.6667