How to extract Ecdf value from the Ecdf() return?

不羁的心 提交于 2019-12-11 05:38:03

问题


The answer for This question here suggest a way by applying ecdf.

However I am using Ecdf() from package Hmisc for it provides a convenient way to do a ccdf(Complementary Cumulative Distribution Function) plot. (by setting the what option to '1-F')

By default, Ecdf() does the plot and return a nested list containing x and y.

How can I extract the y value of a certain x value? and then plot it on the original plot?

FYI:

 > str(Ecdf(rnorm(20), lwd = 2))
    List of 2
     $ x: num [1:21] -1.46 -1.46 -1.18 -1.17 -1.16 ...
     $ y: num [1:21] 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 ...
     - attr(*, "N")=List of 2
      ..$ n: num 20
      ..$ m: num 0

At first i am considering convert this list to a data.frame using methods suggested from R List to Data Frame, but my data is huge and the rbind seems really slow.


回答1:


Ecdf returns a list whereas ecdf returns a function. It's a lot easier to use the R-stats function ecdf than it is to use something tortured like: Ecdf(.)$y[ min(which(Ecdf(.)$x>val))]. If you want the value of ecdf(x=0) from an Ecdf-object then this should work:

 ecdf( Ecdf(rnorm(20), lwd = 2)$x ) (v=0)
[1] 0.5238095

(It turns out that the formal parameter for the function returned by ecdf is "v".) But if you want the less elegant method and you already have assigned the result to an object named 'oneEcdf':

oneEcdf <- Ecdf(rnorm(20), lwd = 2)
oneEcdf$y[ min( which(oneEcdf$x > 0 ))]
[1] 0.6


来源:https://stackoverflow.com/questions/16816645/how-to-extract-ecdf-value-from-the-ecdf-return

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!