How to calculate the "inverse" cumulative frequency diagram with ECDF

I have no problem plotting an aggregate frequency graph like this.

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

But I want to show the graph in the form of a "reciprocal" cumulative frequency of values> x. (i.e., something equivalent to = "1-f").

Is there any way to do this?

Other suggestions in R, besides using Hmisc, are also very welcome.

+3
source share
4 answers

Using the Musa offer:

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)

Proportions

You can set some parameters, such as name, legend, etc.

If you need frequency instead of proportion, then a simple solution would be:

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

+3
source

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.

, 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))
+5
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)
+2
source

Suppose you have only one vector x, then you can do the following:

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

Source: https://habr.com/ru/post/1737648/


All Articles