1

I am simulating some gamma random numbers

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)))

The first plot

The second plot

enter image description here

I need to simulate this kind of gamma with some tail, the mean around 1200. I have been selecting random numbers in order to get that values considering the definition of expectation and variance for gamma distribution, but in the first case I get negative numbers, I don't want that. In the second case the same but also in both plots the probability in the y axis is so low, I would like to increase this probability but I don't know how to select the adequate parameters to get that.

On the other hand the parameters given in the third plot gives a density strange, because the probability in the y axis is greater than one I can get values greater than 1. I don't understand this.

user3483060
  • 337
  • 2
  • 13

1 Answers1

2

You are not getting negative numbers in the first case.

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

# [1] 0

As for the density exceeding 1, there is nothing wrong with that. It should be the area under the curve that should be exactly 1.

For plotting the distribution, you may also use dgamma instead of rgamma like so:

x <- seq(0,.12, length = 10000)
y <- dgamma(x,2.1,rate=110)
plot(x,y, type = "l")

enter image description here

And here is the plot using ggplot2.

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

enter image description here

hpesoj626
  • 3,529
  • 1
  • 17
  • 25
  • note that you can use `from=0` to prevent the density being computed for negative values -- but computing kernel densities near hard boundaries is tricky ... – Ben Bolker Mar 11 '18 at 01:13
  • Thanks, @BenBolker. I remember commenting right after I edited my answer but somehow the comment is missing. – hpesoj626 Mar 12 '18 at 01:22