I was trying to generate random numbers with a gamma distribution. In R the function is: rgamma(n, shape, rate = 1, scale = 1/rate). My question is that if X~gamma(alpha, beta) and I want to generate one random number where do I plug alpha and beta in rgamma? and, what is the meaning and use of rate? Thanks for your attention, Jorge [[alternative HTML version deleted]]
Icabalceta, Jorge L. wrote:> I was trying to generate random numbers with a gamma distribution. In R the > function is: > rgamma(n, shape, rate = 1, scale = 1/rate). My question is that if > X~gamma(alpha, beta) and I want to generate one random number where do I > plug alpha and beta in rgamma? and, what is the meaning and use of rate? > Thanks for your attention, > Jorge >Did you look at the help? From ?rgamma: <quote> Details: If 'scale' is omitted, it assumes the default value of '1'. The Gamma distribution with parameters 'shape' = a and 'scale' = s has density f(x)= 1/(s^a Gamma(a)) x^(a-1) e^-(x/s) for x > 0, a > 0 and s > 0. The mean and variance are E(X) = a*s and Var(X) = a*s^2. </quote> Then, depending how you define "alpha" and "beta" use the above to figure out how to use rgamma. -sundar
"Icabalceta, Jorge L." <Icabalceta_j at wlf.state.la.us> writes:> I was trying to generate random numbers with a gamma distribution. In R the > function is: > rgamma(n, shape, rate = 1, scale = 1/rate). My question is that if > X~gamma(alpha, beta) and I want to generate one random number where do I > plug alpha and beta in rgamma? and, what is the meaning and use of rate?Well, it depends on your definition of alpha and beta.... You need to match up your notation for the gamma density with that given on help(rgamma), which will also tell you what to do with them. The "rate" argument just allows you to specify the scale as its inverse. A large rate corresponds to a narrow distribution. I suspect this is popular notation for interarrival distributions in queuing theory. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Thu, 5 Feb 2004, Icabalceta, Jorge L. wrote:> I was trying to generate random numbers with a gamma distribution. In R the > function is: > rgamma(n, shape, rate = 1, scale = 1/rate). My question is that if > X~gamma(alpha, beta) and I want to generate one random number where do I > plug alpha and beta in rgamma? and, what is the meaning and use of rate?It depends on what you mean by gamma(alpha,beta). It could be rgamma(1,alpha,beta) or rgamma(1,alpha,1/beta) since both of these parameterisations are used. If you think the mean of gamma(alpha,beta) is alpha*beta, use the second one, if you think it is alpha/beta use the first one. -thomas
Jorge: If I have trouble understanding documentation with something like this, I make plots, e.g., of dgamma vs. x for different values for shape and rate or scale. hope this helps. spencer graves Peter Dalgaard wrote:>"Icabalceta, Jorge L." <Icabalceta_j at wlf.state.la.us> writes: > > > >>I was trying to generate random numbers with a gamma distribution. In R the >>function is: >>rgamma(n, shape, rate = 1, scale = 1/rate). My question is that if >>X~gamma(alpha, beta) and I want to generate one random number where do I >>plug alpha and beta in rgamma? and, what is the meaning and use of rate? >> >> > >Well, it depends on your definition of alpha and beta.... You need to >match up your notation for the gamma density with that given on >help(rgamma), which will also tell you what to do with them. > >The "rate" argument just allows you to specify the scale as its >inverse. A large rate corresponds to a narrow distribution. I suspect >this is popular notation for interarrival distributions in queuing >theory. > > >
Sorry to bother you. I am sort of confused with the random number generation from a gamma distribution. For some reason, if a variable X~gamma(shape, scale)I have been told that the mean mu can be eithe mu=shape/scale or mu=shape*scale. Why is that so? This is making me have doubt about the random generation I am using: 1.- The first variable I have is the precision (h) (the inverse of the variance)distributed as: h~gamma((T-2)/2, SSE/2) where T is the number of observations and SSE is the sum of square errors. How do I draw random number in R with this information. How do I plug this into rgamma(n,shape,scale)? 2.- The second variable is L^-1 ~ gamma(T+1, vi-ln(r*)^-1) (please, don't mind my no explanation for the terms used in the shape and scale parameters). Again, How do I plug this into rgamma(n,shape,scale)? 3.- I am having a problem putting the results of each iteration from for(i 1:11000) into samp. For some reason I get 10000 identical values for each column. What am I doing wrong? To see what the problem is you can try to run the program. Attached is the data I am using, the program with comments and without them. I appreciate your help. Jorge -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: trans3_1_4g1.txt Url: https://stat.ethz.ch/pipermail/r-help/attachments/20040206/e8b0538d/trans3_1_4g1.txt -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: stoch_fron.txt Url: https://stat.ethz.ch/pipermail/r-help/attachments/20040206/e8b0538d/stoch_fron.txt -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: stoch_fron-c.txt Url: https://stat.ethz.ch/pipermail/r-help/attachments/20040206/e8b0538d/stoch_fron-c.txt