Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 是否有可能提前知道密度的最大值?_R_Probability Density_Density Plot - Fatal编程技术网

R 是否有可能提前知道密度的最大值?

R 是否有可能提前知道密度的最大值?,r,probability-density,density-plot,R,Probability Density,Density Plot,考虑以下示例图: mtcars %>% ggplot() + stat_bin(aes(x=wt, y=..density..), binwidth=0.2) 密度的最大值接近0.8。有可能知道它的精确值吗 根据stat_bin文档,.density..表示bin中点的密度,按比例积分为1,但这无助于我了解这些值在实践中是如何计算的,或者如何获得最大密度点(因为它还取决于binwidth集) 我越接近于: binwidth = 0.2 ((mtcars$wt %/% binwidth)*

考虑以下示例图:

mtcars %>% ggplot() + stat_bin(aes(x=wt, y=..density..), binwidth=0.2)
密度的最大值接近0.8。有可能知道它的精确值吗

根据
stat_bin
文档,
.density..
表示bin中点的密度,按比例积分为1,但这无助于我了解这些值在实践中是如何计算的,或者如何获得最大密度点(因为它还取决于
binwidth
集)

我越接近于:

binwidth = 0.2
((mtcars$wt %/% binwidth)*binwidth) %>% table %>% prop.table %>% max
但它确实返回在绘图中观察到的值

有什么想法吗?

不清楚“提前”指的是什么,但如果您正在寻找密度曲线的最大值,请尝试以下方法:

dens <- density(mtcars$wt)
plot(dens)

max(dens$y)
## [1] 0.5068726
dens你可以试试这个

binwidth=0.2
x=mtcars$wt
x_bins <- table(cut(x,breaks=seq(min(x),max(x),by = binwidth)),useNA = "ifany" )
max(x_bins)/(sum(x_bins)*binwidth )
#[1] 0.78125
binwidth=0.2
x=mtcars$wt

这些文档还解释了如何计算密度曲线,以及如何传入内核和带宽等参数。如果你想控制这些,你可以明确地设置它们。计算密度的向量值是我能想到的文档中提到的最接近的容器。或者,也可以使用命令
cut
。在任何情况下,向量中的最可能(即重复)值都不会与
stat\u bin
返回的较高条匹配,这让我感到困惑。我指的是在计算
stat\u bin
之前(或不计算它)。这确实回答了问题,但它不会返回条形图给出的~0.8值,我如何才能得到它?
gg_plot <- mtcars %>% ggplot() + stat_bin(aes(x=wt, y=..density..), binwidth=binwidth)
max(ggplot_build(gg_plot)$data[[1]][[1]])