在泊松的同一图中绘制直方图和 pdf

plot histogram and pdf in same plot for poisson

我有一个数据集需要用泊松回归分析。 我尝试在与 pdf 相同的图中绘制响应的直方图,但我没有找到如何..

我使用以下代码绘制直方图:

library(lattice) 
tot <- mydata[, 1] 
histogram(tot) 

并得到一个漂亮的直方图。要添加 pdf,我尝试了一些代码,如下所示:

xlines <-seq(min(tot),max(tot),length.out=100)  
lines(x = xlines,y=dpois(xlines,21))

我也尝试了其他一些代码,但找不到有效的代码...
有人有什么建议吗?

您可以开始处理此代码:

library(lattice) 
set.seed(1)
tot <- rpois(100,21)
xlines <- seq(min(tot),max(tot),by=1)
histogram(tot, type="density", 
  panel=function(x, ...){
    panel.histogram(x,...)
    panel.lines(x = xlines, y=dpois(xlines,21), lwd=2, col="red")
  }
) 

我认为你的问题一般来说是不正确的,因为将直方图和 pdf 一起绘制是没有意义的(频率直方图和密度图的坐标不同)。您可以做的是让 R 在概率坐标中绘制直方图。这是鸢尾花数据集的小例子。

data=iris
x=iris[, 1]
hist(x, freq=F)
lines(density(x))

是什么给了你:1

与您之前所做的比较:

data=iris
x <- iris[, 1]
hist(x, freq=T)
lines(density(x))

希望对您有所帮助!