Peter Maclean
2014-Jan-18 07:37 UTC
[R] shading and estimating area abetween two cdf curves
I would like to shade the area between two curves generated by the ecdf function. There are examples such as those explained here http://www.alisonsinclair.ca/2011/03/shading-between-curves-in-r/ These examples do not apply to the objects generated by the ecdf function. ############# library(truncnorm) x? <- rtruncnorm(100, 3,? 25,? 15,? 5) y? <- rtruncnorm(100, 5,? 30,? 17,? 8) x <- ecdf(x) y <- ecdf(y) plot(x) lines(y) #This is not working polygon(x, y, xpd = xpd, col = "orange", lty = 2, lwd = 2, border = "red") #How to shade the area between the curve #calculate the area between the two curves Peter Maclean Department of Economics UDSM
William Dunlap
2014-Jan-18 17:16 UTC
[R] shading and estimating area abetween two cdf curves
> These examples do not apply to the objects generated by the ecdf function.Look at the objects generated by the ecdf function and you will see that they are functions, not vectors of numbers (the 'f' in ecdf stands for 'function'). You need to apply the functions to vectors of numbers to get what you want. plot(ecdf(x)) works because there is a special plotting method for the function objects made by ecdf() which essentially does plot(x, ecdf(x)(x)). To go beyond that you have to evaluate the function yourself, as in the second part of this example: data0 <- log2(1:16) data1 <- 10 / (1 + sqrt(1:15)) ecdf0 <- ecdf(data0) ecdf1 <- ecdf(data1) plot(ecdf0, xlim=range(data0,data1), col="blue") plot(ecdf1, add=TRUE, col="orange") tmpx <- seq(min(data0,data1), max(data0,data1), len=2001) p0 <- ecdf0(tmpx) p1 <- ecdf1(tmpx) segments(x0=tmpx, y0=p0, y1=p1, col=adjustcolor(ifelse(p0<p1, "red", "black"), alpha.f=0.1)) (There are better ways to fill the area between two lines, but using segments to make lots of vertical lines makes for a simple example.) Bill Dunlap TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Peter Maclean > Sent: Friday, January 17, 2014 11:37 PM > To: r-help at r-project.org > Subject: [R] shading and estimating area abetween two cdf curves > > I would like to shade the area between two curves generated by the ecdf function. > There are examples such as those explained here > http://www.alisonsinclair.ca/2011/03/shading-between-curves-in-r/ > These examples do not apply to the objects generated by the ecdf function. > ############# > library(truncnorm) > x? <- rtruncnorm(100, 3,? 25,? 15,? 5) > y? <- rtruncnorm(100, 5,? 30,? 17,? 8) > x <- ecdf(x) > y <- ecdf(y) > plot(x) > lines(y) > #This is not working > polygon(x, y, xpd = xpd, col = "orange", lty = 2, lwd = 2, border = "red") > #How to shade the area between the curve > #calculate the area between the two curves > > > Peter Maclean > Department of Economics > UDSM > > ______________________________________________ > 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.