emanuele
emanuele

Reputation: 2589

Differences in ecdf() and Ecdf() on R

I am trying to plot this kind of behaviour of a data set. I have tried with Ecdf()

library(Hmisc)
dd<-read.table('critical.1.dat',head=F)
cdf<-Ecdf(dd$V1)
df<-hist(dd$V1)
ll<-((1-cdf$y[df$mids])/(df$density))
plot(df$mids,ll)

and seems all goes fine. After this answer i have tried with ecdf()

dd<-read.table('critical.1.dat',head=F)
cdf<-ecdf(dd$V1)
df<-hist(dd$V1)
ll<-(1-cdf(df$mids))/df$density
plot(df$mids,ll)

and the outcome looks very different Ecdf().

Where is my mistake? why they looks me different?

First Edit: My actual code is

library(Hmisc)
dd<-read.table('critical.1.dat',head=F)

cdf<-Ecdf(dd$V1)
ccdf<-ecdf(dd$V1)

df<-hist(dd$V1)

ll<-((1-cdf$y[df$mids])/(df$density))
llc<-(1-ccdf(df$mids))/df$density

par( mfrow = c( 2, 1 ) )

plot(df$mids,ll,ylab='Ecdf()')
plot(df$mids,llc,ylab='ecdf()')

Upvotes: 0

Views: 4729

Answers (1)

IRTFM
IRTFM

Reputation: 263342

Ecdf returns a list while ecdf returns a function:

> set.seed(1)
> ch <- rnorm(1000, 200, 40)
> Echol <- Ecdf(ch, xlab="Serum Cholesterol")
> e.chol <- ecdf(ch)
> str(Echol)
List of 2
 $ x: num [1:1001] 79.7 79.7 80.1 82.4 84.4 ...
 $ y: num [1:1001] 0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 ...
 - attr(*, "N")=List of 2
  ..$ n: num 1000
  ..$ m: num 0

> e.chol <- ecdf(ch)   
> str(e.chol)
function (v)  
 - attr(*, "class")= chr [1:3] "ecdf" "stepfun" "function"
 - attr(*, "call")= language ecdf(ch)

To summarize the comments below ... the list representation of an ECDF needs a different access method than the functional representation. So at least one of the the mistakes was in using df$mids in cdf$y[df$mids] when processing the return object from Ecdf(). The beauty of the base::ecdf function is that it can process an X value directly, whereas the list representation will need to use a function like findInterval() to construct an appropriate index.

Upvotes: 1

Related Questions