如何用ECDF绘制"反向"累积频率图

nev*_*int 3 statistics r

我可以像这样绘制以下累积频率图.

     library(Hmisc)
     pre.test <- rnorm(100,50,10)
     post.test <- rnorm(100,55,10)
     x <- c(pre.test, post.test)
     g <- c(rep('Pre',length(pre.test)),rep('Post',length(post.test)))
     Ecdf(x, group=g, what="f", xlab='Test Results', label.curves=list(keys=1:2))
Run Code Online (Sandbox Code Playgroud)

但我希望以"反向"累积频率> x的形式显示图形.(即相当于什么="1-f"的东西).

有办法吗?

R中除了使用Hmisc之外的其他建议也非常受欢迎.

Mar*_*rek 5

使用 Musa 建议:

pre.ecdf <- ecdf(pre.test)
post.ecdf <- ecdf(post.test)

r <- range(pre.test,post.test)
curve(1-pre.ecdf(x), from=r[1], to=r[2], col="red", xlim=r)
curve(1-post.ecdf(x), from=r[1], to=r[2], col="blue", add=TRUE)
Run Code Online (Sandbox Code Playgroud)

比例

您可以设置一些参数,如标题、图例等。

如果你想要频率而不是比例,简单的解决方案是:

pre.ecdf <- ecdf(pre.test)
post.ecdf <- ecdf(post.test)

rx <- range(pre.test,post.test)
ry <- max(length(pre.test),length(post.test))
curve(length(pre.test)*(1-pre.ecdf(x)), from=rx[1], to=rx[2], col="red", xlim=rx, ylim=c(0,ry))
curve(length(post.test)*(1-post.ecdf(x)), from=rx[1], to=rx[2], col="blue", add=TRUE)
Run Code Online (Sandbox Code Playgroud)

频率


Dir*_*tel 5

Ecdf来自Hmisc的更一般的功能有一个what=选项:

参数:

   x: a numeric vector, data frame, or Trellis/Lattice formula

what: The default is ‘"F"’ which results in plotting the fraction
      of values <= x.  Set to ‘"1-F"’ to plot the fraction > x or
      ‘"f"’ to plot the cumulative frequency of values <= x.
Run Code Online (Sandbox Code Playgroud)

因此,我们可以修改您之前问题的答案并添加what="1-F":

 # Example showing how to draw multiple ECDFs from paired data
 pre.test <- rnorm(100,50,10)
 post.test <- rnorm(100,55,10)
 x <- c(pre.test, post.test)
 g <- c(rep('Pre',length(pre.test)),rep('Post',length(post.test)))
 Ecdf(x, group=g, what="1-F", xlab='Test Results', label.curves=list(keys=1:2))
Run Code Online (Sandbox Code Playgroud)