简体   繁体   English

到 plot 理论密度超过经验密度

[英]To plot a theoretical density over an empirical density

If I want to plot an empirical density, I would go:如果我想要 plot 经验密度,我会 go:

library(ggplot2) ggplot() + geom_density(aes(x = rbeta(100,3,1)))

or或者

library(ggplot2)
ggplot() +
  geom_histogram(aes(x = rbeta(100,3,1)))

where rbeta(100,3,1) can be any vector.其中rbeta(100,3,1)可以是任何向量。

If I want to plot a theoretical density, I could go:如果我想要 plot 一个理论密度,我可以 go:

library(ggplot2)
ggplot(data = data.frame(x = c(0, 1)), mapping = aes(x = x)) +
  stat_function(fun = dbeta, args = c(3,1), n = 100)

But when I try to plot the first curve over the second:但是当我尝试 plot 时,第一条曲线超过第二条曲线:

library(ggplot2)
ggplot(data = data.frame(x = c(0, 1)), mapping = aes(x = x)) +
  stat_function(fun = dbeta, args = c(3,1), n = 100) +
  geom_histogram(aes(x = rbeta(100,3,1)))

I will get an error.我会得到一个错误。

How can I plot an empirically determined density over a theoretical?我怎样才能 plot 根据经验确定密度而不是理论密度?

This seems to work.这似乎有效。
Plot the histogram first, then the density. Plot 首先是直方图,然后是密度。 And with more data points the histogram fits the theoretical density better.并且随着数据点的增加,直方图更符合理论密度。

library(ggplot2)
library(gridExtra)

set.seed(2022)

p1 <- ggplot() +
  geom_histogram(aes(x = rbeta(100,3,1), y = ..density..), bins = 30) +
  stat_function(fun = dbeta, args = c(3,1), n = 100)

p2 <- ggplot() +
  geom_histogram(aes(x = rbeta(10000,3,1), y = ..density..), bins = 30) +
  stat_function(fun = dbeta, args = c(3,1), n = 100)

grid.arrange(p1, p2, ncol=2)

Created on 2022-02-20 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-02-20

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

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