How i can obtain a distribution from a shape in R?

Hi everyone,
i need to obtain a distribution, for exmple under a gamma shape.
I already try to use rgamma(x,shape,rate), but when i see the result it is not what i want, i need somethings like in figure below, I drew this graph with paint, is only to give an idea

.
Does somebody can help me?

Thanks

I think you're looking for the dgamma function. In the example below, I specify a shape and rate. I then find the 99.9th percentile to set as the maximum x value

library(tidyverse)

shape <- 5
rate <- 1/3

maxx <- qgamma(.999, shape=shape, rate=rate) #99.9th percentile as max x to plot

datgamma <-tibble(x=seq(0, maxx, by=.1)) %>%
  mutate(y=dgamma(x, shape, rate=rate))

datgamma %>%
  ggplot(aes(x=x, y=y)) +
  geom_line()

Created on 2020-09-25 by the reprex package (v0.3.0)

Hi StatSteph
I already know this, but i need to know if exist a function that can do this:

x=1:5000 

y=dgamma(x,scale=1000,shape=1.5)

k=c()

for (i in x) {
  k[i]=runif(1,0,y[i])
}

plot(x,k)

thank you

I'm not aware of any function to do this but you can make your own. Note that R is vectorized so you don't need a loop here.



plot_rgamma <- function(scale, shape, maxx=NULL){
  if (is.null(maxx)){
    maxx <- qgamma(.999, shape=shape, scale=scale)  
  }
  
  x <- seq(0, maxx, length.out = 5000)
  y=dgamma(x, scale=scale, shape=shape)
  k <- runif(length(x), 0, y)
  
  plot(x,k)
}

plot_rgamma(1000, 1.5, 5000)

plot_rgamma(1000, 1.5)

Created on 2020-09-25 by the reprex package (v0.3.0)

3 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.