R:在数据直方图上叠加泊松分布 [英] R: Overlay Poisson distribution over histogram of data

查看:125
本文介绍了R:在数据直方图上叠加泊松分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些离散数据,已在直方图中绘制.我想覆盖一个Poisson分布,以显示数据大致为Poisson分布.想象一下下面代码中的两个图合并为一个图,这就是我想要实现的.

I have some discrete data, which I have plotted in a histogram. I'd like to overlay a Poisson distribution to show the data is roughly Poisson distributed. Imagine the two plots from the code below merging into one plot, that is what I'd like to achieve.

# Read data
data <- read.csv("data.csv")

# Plot data
hist(data, prob=TRUE)

# Plot Poisson
c <- c(0:7)
plot(c, dpois(c, mean(data)), type="l")

我尝试了曲线函数:

curve(c, dpois(x=c, lambda=mean(data)), add=T)

但是我得到的只是这个:

But all I get is this:

泊松曲线似乎突然停止,但我希望它遵循直方图的形状.

The Poisson curve just seems to abruptly stop, but I would expect it to follow the shape of the histogram.

我希望它看起来像这样(不一定具有颜色或多个数据集):

I'd like it to look like this (not necessarily with colours or multiple data sets):

推荐答案

下面的代码可以满足您的需求.

The code below does what you want.

set.seed(12111978)
vec <- rpois(50, 3)
hist(vec, prob=TRUE, ylim = c(0, .25)) # may need to tweak the y axis.
lines(0:max(vec), dpois(0:max(vec), mean(vec)), col = 'red')

这篇关于R:在数据直方图上叠加泊松分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆