在 R 中叠加核分布

发布于 2024-12-03 04:27:24 字数 1390 浏览 1 评论 0原文

我试图在图中放置 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")

,我得到了 在此处输入图像描述

y 轴上的密度值不应该 <0 吗? 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"

I am trying to place 3 density functions in plot using

plot(density(all_noise),xlim=c(-1,1),ylim=c(0,10))
lines(density(max_nearby),col="blue")
lines(density(max_repeats),col="red")

and I got
enter image description here

Shouldn't the density value on the y axis be < 1? Are there better methods for superimposing
kernel distributions?

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拧巴小姐 2024-12-10 04:27:24

密度曲线下的面积为1,但它们可以超过1。我认为你这样做没有任何问题。出于我自己的目的,我要做的唯一更改是用值初始化绘图窗口,以便所有密度都在绘图窗口的范围内。

另外,关于之前的答案(我还不能发表评论)请注意, ylim 是 plot() 的参数,而不是 Density() 的参数> --- 它没有告诉 密度() 做任何事情。

The area under the density curves is 1, but they can exceed 1. I see nothing wrong with how you're doing this. For my own purposes about the only change I'd make would be to initialize the plot window with values so that all densities are in the bounds of the plot window.

Also, regarding the previous answer (I can't comment yet) notice that ylim is an argument to plot(), not to density() --- it's not telling density() to do anything.

垂暮老矣 2024-12-10 04:27:24

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

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

kernel density plot is not a histogram. here is an example: take a look at the min and max of the density function and real min max of the data.

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文