简体   繁体   English

R 上 ecdf() 和 Ecdf() 的差异

[英]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()我试过 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()在这个答案之后,我尝试了 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().结果看起来非常不同 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()')

Ecdf returns a list while ecdf returns a function: Ecdf返回一个列表,而ecdf返回一个函数:

> 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. 总结下面的评论... ECDF的列表表示需要与功能表示不同的访问方法。 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() . 因此,至少一个错误是在处理Ecdf()的返回对象时在cdf$y[df$mids]中使用df $ mids。 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. base :: ecdf函数的优点在于它可以直接处理X值,而列表表示形式将需要使用诸如findInterval()类的函数来构造适当的索引。

Dear friends I have similar problem while running frostfreqs function in agroclim package.亲爱的朋友,我在 agroclim 包中运行frostfreqs 函数时遇到了类似的问题。 It returns the following:它返回以下内容:

 frostFreqs(mn = tm, dates = seq.Date(as.Date('1987-01-01'),
+                                      as.Date('2017-12-31'), by ='day'))
Error in ecdf(ff) : 'x' must have 1 or more non-missing values
In addition: Warning message:
In type.convert.default(X[[i]], ...) :
  'as.is' should be specified by the caller; using TRUE


~~~~~~~~
Any one having solution for the problem please?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM