R 关于伽马密度模拟

R 关于伽马密度模拟,r,random,statistics,simulation,gamma-distribution,R,Random,Statistics,Simulation,Gamma Distribution,我在模拟一些伽马随机数 plot(density(rgamma(10000,8.1,rate=0.00510)),lwd=2,las=1,cex.axis=0.75, main=expression(paste("Gamma Distribution with",' scale ',alpha," and rate ",beta))) plot(density(rgamma(10000,2.1,rate=0.00110)),lwd=2,las=1,cex.axis=0.75, main=

我在模拟一些伽马随机数

plot(density(rgamma(10000,8.1,rate=0.00510)),lwd=2,las=1,cex.axis=0.75,
main=expression(paste("Gamma Distribution with",' scale ',alpha," and  rate 
",beta)))

plot(density(rgamma(10000,2.1,rate=0.00110)),lwd=2,las=1,cex.axis=0.75,
 main=expression(paste("Gamma Distribution with",' scale ',alpha," and  rate 
",beta)))


plot(density(rgamma(10000,2.1,rate=110)),lwd=2,las=1,cex.axis=0.75,
 main=expression(paste("Gamma Distribution with",' scale ',alpha," and  rate 
",beta)))

我需要用一些尾巴来模拟这种伽马射线,平均值在1200左右。考虑到gamma分布的期望值和方差的定义,我一直在选择随机数以获得该值,但在第一种情况下,我得到的是负数,我不希望这样。在第二种情况下,相同,但在两个图中,y轴的概率非常低,我想增加这个概率,但我不知道如何选择适当的参数来获得它


另一方面,第三个图中给出的参数给出了一个奇怪的密度,因为y轴上的概率大于1,我可以得到大于1的值。我不明白这一点。

在第一种情况下,你不会得到负数

sum(rgamma(10000,8.1,rate=0.00510)<0)

# [1] 0

这是使用
ggplot2
绘制的图

ggplot(data.frame(x,y), aes(x,y)) + 
  geom_line()

你没有得到负数。尝试使用
dgamma
而不是
rgamma
。例如尝试,
x曲线的高度是密度,而不是概率。概率是在曲线下的区域中找到的。请注意,您可以使用
from=0
来防止密度被计算为负值——但是计算硬边界附近的内核密度很棘手……谢谢,@BenBolker。我记得我在编辑了我的答案后马上发表了评论,但不知怎么的,评论不见了。
ggplot(data.frame(x,y), aes(x,y)) + 
  geom_line()