George
George

Reputation:

I am trying to generate Gamma data in R but it is not working

lambda=1;
n=100;
alpha=3;
y = dgamma(n, shape=alpha, scale=lambda)

I have this typed in but i just get a graph of one point. I need to generate one sample of size n = 100 of Gamma data with lambda = 1 and alpha = 3.

Upvotes: 1

Views: 317

Answers (1)

Siong Thye Goh
Siong Thye Goh

Reputation: 3586

As stated in the comment, use rgamma rather than dgamma:

lambda = 1
n = 100
alpha = 3
y = rgamma(n, shape = alpha, scale = lambda)
hist(y, freq = FALSE, ylim=c(0,0.3))
domain = seq(from = 0, to = 8, by = 0.01)
pdf = dgamma(domain, shape = alpha, scale = lambda)
lines(density(y), col='red')
lines(domain, pdf, col='blue')

Here's what the data generated looks like.

enter image description here

Upvotes: 2

Related Questions