Gundala Viswanath
2008-Jul-07 03:24 UTC
[R] Plot Mixtures of Synthetically Generated Gamma Distributions
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. - Gundala Viswanath Jakarta - Indonesia
Jorge Ivan Velez
2008-Jul-07 03:37 UTC
[R] Plot Mixtures of Synthetically Generated Gamma Distributions
Dear Gundala, It's just a starting points. I'm sure it could be better. Try this: set.seed(123) x=c(rgamma(30,shape=.2,scale=14),rgamma(30,shape=12,scale=10),rgamma(30,shape=5,scale=6)) plot(density(x[1:30]),col=2,xlim=range(0,max(density(x)$x)),type='l',main="Density for your vector") points(density(x[31:60]),col=1,type='l') points(density(x[61:90]),col=4,type='l') legend("topright",paste('Type',1:3,sep=""),col=c(2,1,4),lty=1) HTH, Jorge On Sun, Jul 6, 2008 at 11:24 PM, Gundala Viswanath <gundalav@gmail.com> wrote:> 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. > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Jorge Ivan Velez
2008-Jul-07 03:42 UTC
[R] Plot Mixtures of Synthetically Generated Gamma Distributions
Dear Gundala, It's just a starting points and I'm sure completely sure it will be improved. Try this (for now): set.seed(123) x=c( rgamma(30,shape=.2,scale=14), rgamma(30,shape=12,scale=10), rgamma(30,shape=5,scale=6)) plot(density(x[1:30]),col=2,xlim=range(0,max(density(x)$x)),type='l',main="Density for your vector") points(density(x[31:60]),col=1,type='l') points(density(x[61:90]),col=4,type='l') legend("topright",paste('Type',1:3,sep=""),col=c(2,1,4),lty=1) Also, see ?density, ?points and ?plot for more information. HTH, Jorge On Sun, Jul 6, 2008 at 11:24 PM, Gundala Viswanath <gundalav@gmail.com> wrote:> 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. > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Moshe Olshansky
2008-Jul-07 03:57 UTC
[R] Plot Mixtures of Synthetically Generated Gamma Distributions
I know very little about graphics, so my primitive and brute force solution would be plot(density(x[1:30]),col="blue");lines(density(x[31:60]),col="red");lines(density(x[61:90]),col="green") --- On Mon, 7/7/08, Gundala Viswanath <gundalav at gmail.com> wrote:> From: Gundala Viswanath <gundalav at gmail.com> > Subject: [R] Plot Mixtures of Synthetically Generated Gamma Distributions > To: r-help at stat.math.ethz.ch > Received: Monday, 7 July, 2008, 1:24 PM > 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. > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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.
Stephen Tucker
2008-Jul-07 06:52 UTC
[R] Plot Mixtures of Synthetically Generated Gamma Distributions
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)) ----- Original Message ---- From: Gundala Viswanath <gundalav at gmail.com> To: r-help at stat.math.ethz.ch Sent: Sunday, July 6, 2008 8:24:06 PM Subject: [R] Plot Mixtures of Synthetically Generated Gamma Distributions 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. - Gundala Viswanath Jakarta - Indonesia ______________________________________________ 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.