Re: [R] Plot Mixtures of Synthetically Generated Gamma Distributions

From: Stephen Tucker <brown_emu_at_yahoo.com>
Date: Sun, 06 Jul 2008 23:52:35 -0700 (PDT)


Are you trying to look at the difference in the gamma distributions due to variations in the shape and scale parameters? In this case, the following approach might be more straightforward:

## assign parameter values

params <- list(curve1=c(1,1),curve2=c(1,2),curve3=c(1,3))

## define function

gammafun <- function(shape,scale) {

  p <- seq(0.001,0.999,length=1000)
  x <- qgamma(p,shape,scale)
  y <- dgamma(x,shape,scale)

  list(x=x,y=y)
}

## apply function to parameters

dat <- lapply(params,function(p) gammafun(p[1],p[2]))

## plot lines

xlim <- range(sapply(dat,`[[`,"x"))
ylim <- range(sapply(dat,`[[`,"y"))
plot.new()
plot.window(xlim,ylim)
for( i in 1:3 ) lines(dat[[i]],col=i)
lapply(1:2,axis)
box()
legend("topright",lty=1,col=1:3,legend=names(dat))

Hi,

I have the following vector
which is created from 3 distinct distribution (three components) of gamma:

x=c(rgamma(30,shape=.2,scale=14),rgamma(30,shape=12,scale=10),rgamma(30,shape=5,scale=6))

I want to plot the density curve of X, in a way that it shows a distinct 3 curves that represent each component.

How can I do that?

I tried this but doesn't work:

lines(density(x))

Please advise.


R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 07 Jul 2008 - 06:57:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 07 Jul 2008 - 07:31:57 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive