如何使用ECDF绘制“反向”累积频率图

5
我没有问题绘制以下累积频率图表,就像这样。
     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))

我想以“反向”值累积频率的形式显示图形(即相当于 what="1-f")。

有没有方法可以做到这一点?

在R中使用Hmisc以外的其他建议也非常受欢迎。


你好。我从你的两个问题中删除了Unix/Linux标签,因为它们与操作系统无关。 - Marek
4个回答

6
使用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)

比例

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

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

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)

Frequencies


@Marek:正如我在OP或Dirk中提到的那样,我要找的是反累积“频率”图,而不是“比例”。 - neversaint

5

Ecdf函数是来自Hmisc的更一般的函数,它有一个what=选项:

Arguments:

   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.
所以,我们可以修改你之前提出的问题的答案,并添加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))

@Dirk:我知道“1-F”选项,但我想要的是反向“频率”,而不是比例。因此我提到了(1-f),小写f。 - neversaint

2
df <- data.frame(x, g)
df$y <- apply(df, 1, function(v){nrow(subset(df, g == v[2] & x >= v[1]))})
library(ggplot2)
qplot(x, y, data=df, geom='line', colour=g)

1

假设你只有一个向量x,那么你可以执行以下操作:

f <- ecdf(x)
plot(1-f(x),x)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接