检查向量的出现间隔值,并在R中汇总结果

时间:2015-12-29 20:26:07

标签: r intervals

我使用hdr包中的hdr.denhdrcde函数来计算KDE和KDE的峰值:

require(hdrcde)
df<-faithful


hdregions<-hdr(den=density(df$eruptions,bw=bw.SJ(df$eruptions,method="dpi")*0.5),all.modes=T)


hdr.den(den=density(df$eruptions,bw=bw.SJ(df$eruptions,method="dpi")*0.5))

The red lines mark the peaks. The bars represent the probability blue=99%, red=95%, green=50%

获得以下结果:

hdregions
$hdr
        [,1]     [,2]     [,3]     [,4]     [,5]    [,6]
99% 1.549814 2.716431 2.743981 2.966948 3.226652 5.15715
95% 1.617094 2.487255 3.353519 5.046328       NA      NA
50% 1.756702 2.009904 4.058298 4.675423       NA      NA

$mode
[1] 1.860365 2.869094 4.143690 4.503392

$falpha
        1%         5%        50% 
0.03428318 0.09972730 0.48195456 

在这种情况下,hdregions$mode代表峰值,每对hdregions$hdr代表“高密度区域”(hdr)间隔。

所以间隔为:

99% are :  (1.549814 2.716431) (2.743981 2.966948) (3.226652 5.15715)

for 95%: (1.617094 2.487255) (3.353519 5.046328)

and so on.

所以我需要的东西或多或少是hdregions$hdr我希望列表(或data.frame)的另一种方式,显示概率内的相应模式(99%) ,95%,50%)看起来像这样:

99% 1.860365 2.869094 4.143690 4.503392
95% 1.860365 4.143690 4.503392
50% 1.860365 4.143690 4.503392

到目前为止我尝试了什么:

interval<-NULL
for (i in row.names(hdregions$hdr)) {
  interval <- rbind(interval, data.frame(LowLimit = hdregions$hdr[i,][seq(1,to=length(hdregions$hdr[i,][!is.na(hdregions$hdr[i,])]),by=2)], UpLimit = hdregions$hdr[i,][seq(2,to=length(hdregions$hdr[i,][!is.na(hdregions$hdr[i,])]),by=2)], ID=i))
}


for (i in hdregions$mode){

  interval[,as.character(i)]<-cbind( interval[,1] <= i & interval[,2] >= i)

}

“结果”如何:

  LowLimit  UpLimit  ID 1.86036518021136 2.8690942707769 4.14368994335195 4.50339178960013
1 1.553237 2.689358 99%             TRUE           FALSE            FALSE            FALSE
2 2.767980 2.956136 99%            FALSE            TRUE            FALSE            FALSE
3 3.231168 5.152859 99%            FALSE           FALSE             TRUE             TRUE
4 1.617957 2.485883 95%             TRUE           FALSE            FALSE            FALSE
5 3.357559 5.044552 95%            FALSE           FALSE             TRUE             TRUE
6 1.754505 2.014823 50%             TRUE           FALSE            FALSE            FALSE
7 4.049855 4.682700 50%            FALSE           FALSE             TRUE             TRUE

我很感激任何帮助,帮助我按照我开始的方式获得所需的结果,或任何其他(我猜可能更短)导致结果的方式。

0 个答案:

没有答案