【问题标题】:How to Plot "Reverse" Cumulative Frequency Graph With ECDF如何使用 ECDF 绘制“反向”累积频率图
【发布时间】:2010-03-19 07:01:25
【问题描述】:

绘制以下累积频率图表没有问题 像这样。

     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))

但我想以值 > x 的“反向”累积频率的形式显示图表。 (即等同于 what="1-f")。

有办法吗?

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

【问题讨论】:

  • 嗨。我从您的两个问题中删除了 unix/linux 标签,因为它们独立于操作系统。

标签: r statistics


【解决方案1】:

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

【讨论】:

  • @Marek:正如我在 OP 或 Dirk 中提到的。我正在寻找的是反向累积“频率”图而不是“比例”。
【解决方案2】:

来自Hmisc 的更通用的Ecdf 函数有一个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.

因此我们可以修改答案from your earlier question并添加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), small-f。
【解决方案3】:
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)

【讨论】:

    【解决方案4】:

    假设你只有一个向量x,那么你可以这样做:

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

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 2021-06-02
      • 2020-08-23
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多