Skip to content

Overlay cdf

4 messages · beetle2, Bill Venables, Matthieu Dubois

#
Here are some ideas you might like to consider

par(mar = c(5,4,2,4)+0.1, yaxs = "r")
Sample <- rgamma(1000,2.5,.8)
hist(Sample, main = "", freq = FALSE, ylim = c(0,1))

pu <- par("usr")[1:2]
x <- seq(pu[1], pu[2], len = 5000)
y <- pgamma(x, 2.5, 0.8)
par(new = TRUE)
plot(x, y, type = "l", axes = FALSE, ann = FALSE, col = "red")
lines(x, dgamma(x, 2.5, 0.8), col = "darkgreen")

axis(4, col = "red")
mtext(side = 4, text = "Cumulative probability", col = "red", line = 2.5)

x0 <- c(0, sort(Sample))
p0 <- 0:1000/1000
lines(x0, p0, type = "S", col = "blue")


Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of beetle2
Sent: Wednesday, 13 May 2009 3:23 PM
To: r-help at r-project.org
Subject: [R] Overlay cdf


Hi,
Is it possible to  overlay a cummulative distribution function on a
histogram of a gamma distribuition.

I have a gamma function 

Sample = rgamma(1000,2.5,.8)+1.5
hist(Sample)

regards
#
Thanks alot I found the function

x0 <- c(0, sort(Sample))
p0 <- 0:1000/1000
lines(x0, p0, type = "S", col = "blue")


Very helpfull 
As it seems to plot an instantaneous representation of the variables in the
gamma distribution
Bill.Venables wrote:

  
    
#
You might also use ?curve

# same example as Bill's
par(mar = c(5,4,2,4)+0.1, yaxs = "r")
Sample <- rgamma(1000,2.5,.8)
hist(Sample, main = "", freq = FALSE, ylim = c(0,1))

curve(pgamma(x, 2.5, 0.8), add=T, col='red')
curve(dgamma(x, 2.5, 0.8), add=T, col='darkgreen')

axis(4, col = "red")
mtext(side = 4, text = "Cumulative probability", col = "red", line = 2.5)

x0 <- c(0, sort(Sample))
p0 <- 0:1000/1000
lines(x0, p0, type = "S", col = "blue")
 
Regards,

Matthieu