-1

I would like to plot a gamma distribution in R where the scale paramter (alpha) stays the same but the convolution paramter changes (all in 1 graph). I know dgamma but don't really know what the x argument means. I would like to just draw the function by providing alpha and beta. Is this doable?

The output of your solution is shapes <- c(2,5,6,12)

> plot(dgamma.wrapper, from=0, to=10)
Error in plot(dgamma.wrapper, from = 0, to = 10) : 
  object 'dgamma.wrapper' not found
> for (i in seq_along(shapes))
+   lines(dgamma(x,shape= shapes[i] , scale = 1) , from=0, to=10, col=i)
Error in dgamma(x, shape = shapes[i], scale = 1) : object 'x' not found
> 
user3069326
  • 575
  • 1
  • 7
  • 10
  • A more general remark: You have asked already 24 questions. Most of them received one or more answers. Please inform youself by reading [what to do when someone answers your question](http://stackoverflow.com/help/someone-answers) – Jaap Mar 10 '14 at 21:05

2 Answers2

1

You can do this:

dgamma.wrapper <- function(x)
   dgamma(x,shape= 2 , scale = 1) 
plot(dgamma.wrapper, from=0, to=10)

EDIT

For more than one shape :

shapes <- c(2,5,6,12)
plot(dgamma.wrapper, from=0, to=10)
for (i in seq_along(shapes))
  curve(dgamma(x,shape= shapes[i] , scale = 1) , from=0, to=10, col=i,add=TRUE)
Pop
  • 12,135
  • 5
  • 55
  • 68
  • Hey if I try this I get an error message saying Error in x(x) : argument "shape" is missing, with no default – user3069326 Mar 10 '14 at 13:58
  • could you explain how I woudl add more than 1 gamma distribution to it? Can i just enter lines below the dgamma line to display 2,3,4... curves in the plot? – user3069326 Mar 10 '14 at 14:04
  • if I enter only the latter part of your code I still get error messages I am afraid. I edit the questionw itht e output – user3069326 Mar 10 '14 at 14:11
  • My apologies... I hadn't tested my code. You have to use the function `plot` with the option `add=TRUE` and not the function `lines`. See the edit – Pop Mar 10 '14 at 14:16
  • Could you just change it for the scale to change not the shapes otherwise it works now... – user3069326 Mar 10 '14 at 14:17
  • I do not understand your question sorry – Pop Mar 10 '14 at 14:19
  • could you add that I could superimpose another function over this graph with the gamma distributions...like f(x)=2x? – user3069326 Mar 10 '14 at 14:21
  • You can add any another function like this: `plot(function(x) 2*x, add=TRUE)` – Pop Mar 10 '14 at 14:24
  • I get still an error after the plot(dgamma.wrapper line . Do I have to initialise dgamma.wrapper at first as dgamma.wrapper <- function(x) dgamma(x,shape= 2 , scale = 1) – user3069326 Mar 10 '14 at 14:40
0

I think this is what you are looking for.

x <- seq(0,20, .1)
plot(x, dgamma(x, scale=2, shape=1), type="l", ylim=c(0,.5), ylab="y")
for(shape in 2:8){
  lines(x, dgamma(x, scale=2, shape=shape), col=shape)
}

EDIT - Quote from wikipedia: In probability theory and statistics, the cumulative distribution function (CDF), or just distribution function, describes the probability that a real-valued random variable X with a given probability distribution will be found at a value less than or equal to x. In the case of a continuous distribution, it gives the area under the probability density function from minus infinity to x.

That is what x represents in this example. Here you show the density function. I just defined a sequence of values for which the plot looks reasonably interesting. Feel free to change it to something more appropriate for your application.

shadow
  • 21,823
  • 4
  • 63
  • 77
  • how did you come up with the value for x? What does it represent? – user3069326 Mar 10 '14 at 14:14
  • If I try to vary the shape parameter in your line for(shape in 2:8) with for(shape in seq(0.5,1,0.1){ I only get 1 curve as plot – user3069326 Mar 10 '14 at 14:36
  • You have to change the color argument as well, if you change the shape. Use e.g `shapes=seq(0.5, 1, .1)` and then `for (i in seq_along(shapes))` and `shape=shapes[i]` and `col=i`. – shadow Mar 10 '14 at 14:45