在R中叠加核分布

时间:2011-09-07 00:27:34

标签: r plot kernel distribution

我正在尝试使用

在绘图中放置3个密度函数
plot(density(all_noise),xlim=c(-1,1),ylim=c(0,10))
lines(density(max_nearby),col="blue")
lines(density(max_repeats),col="red")
我得到了 enter image description here

y轴上的密度值不应该< 1?是否有更好的叠加方法 内核分发?

str(density(all_noise))
List of 7
$ x        : num [1:512] -0.629 -0.626 -0.624 -0.622 -0.62 ...
$ y        : num [1:512] 1.41e-06 8.22e-06 3.16e-05 7.85e-05 1.24e-04 ...
$ bw       : num 0.003
$ n        : int 1924150
$ call     : language density.default(x = all_noise)
$ data.name: chr "all_noise"
$ has.na   : logi FALSE
- attr(*, "class")= chr "density"

str(density(max_nearby))
List of 7
$ x        : num [1:512] 0.154 0.156 0.158 0.16 0.162 ...
$ y        : num [1:512] 0.00111 0.00125 0.0014 0.00157 0.00175 ...
$ bw       : num 0.0543
$ n        : int 250
$ call     : language density.default(x = max_nearby)
$ data.name: chr "max_nearby"
$ has.na   : logi FALSE
- attr(*, "class")= chr "density"

str(density(max_repeats ))
List of 7
$ x        : num [1:512] 0.272 0.274 0.275 0.277 0.279 ...
$ y        : num [1:512] 0.00507 0.00607 0.00722 0.00854 0.01011 ...
$ bw       : num 0.0261
$ n        : int 34
$ call     : language density.default(x = max_repeats)
$ data.name: chr "max_repeats"
$ has.na   : logi FALSE
- attr(*, "class")= chr "density"

2 个答案:

答案 0 :(得分:4)

密度曲线下的区域是1,但它们可以超过1.我认为你是怎么做的。出于我自己的目的,我所做的唯一改变是用值初始化绘图窗口,以便所有密度都在绘图窗口的范围内。

另外,关于上一个答案(我还不能发表评论),请注意ylimplot()的参数,而不是density() ---它没有告诉{{1}做任何事情。

答案 1 :(得分:0)

kernel density情节不是直方图。这是一个例子:看一下密度函数的最小值和最大值以及数据的实际最小值。

x <-rnorm(100)
min(x)
[1] -2.748188
max(x)
[1] 3.689254
density(x)
Call:
density.default(x = x)
Data: x (100 obs.); Bandwidth 'bw' = 0.4114

       x                 y            
 Min.   :-3.9823   Min.   :0.0001091  
 1st Qu.:-1.7559   1st Qu.:0.0079287  
 Median : 0.4705   Median :0.0612352  
 Mean   : 0.4705   Mean   :0.1121754  
 3rd Qu.: 2.6969   3rd Qu.:0.2267729  
 Max.   : 4.9234   Max.   :0.3439259 

plot(density(x))
相关问题