[R-gregmisc-users] SF.net SVN: r-gregmisc:[1474] trunk/gplots
Brought to you by:
warnes
|
From: <wa...@us...> - 2011-08-25 03:10:11
|
Revision: 1474
http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1474&view=rev
Author: warnes
Date: 2011-08-25 03:10:05 +0000 (Thu, 25 Aug 2011)
Log Message:
-----------
- Add 'ci2d()' to compute 2-dimensional emipirical confidence interval.
- Fix parse error in help page for 'rich.colors()'.
Modified Paths:
--------------
trunk/gplots/DESCRIPTION
trunk/gplots/NAMESPACE
trunk/gplots/man/rich.colors.Rd
Added Paths:
-----------
trunk/gplots/R/ci2d.R
trunk/gplots/man/ci2d.Rd
Modified: trunk/gplots/DESCRIPTION
===================================================================
--- trunk/gplots/DESCRIPTION 2011-08-16 01:17:20 UTC (rev 1473)
+++ trunk/gplots/DESCRIPTION 2011-08-25 03:10:05 UTC (rev 1474)
@@ -4,7 +4,7 @@
Depends: gtools, gdata, stats, caTools, grid
Recommends: datasets, grid, MASS
Suggests: gtools
-Version: 2.8.1
+Version: 2.9.0
Date: 2011-08-15
Author: Gregory R. Warnes. Includes R source code and/or documentation
contributed by (in alphabetical order):
Modified: trunk/gplots/NAMESPACE
===================================================================
--- trunk/gplots/NAMESPACE 2011-08-16 01:17:20 UTC (rev 1473)
+++ trunk/gplots/NAMESPACE 2011-08-25 03:10:05 UTC (rev 1474)
@@ -4,6 +4,7 @@
barplot2,
bluered,
boxplot.n,
+ ci2d,
col2hex,
colorpanel,
greenred,
Added: trunk/gplots/R/ci2d.R
===================================================================
--- trunk/gplots/R/ci2d.R (rev 0)
+++ trunk/gplots/R/ci2d.R 2011-08-25 03:10:05 UTC (rev 1474)
@@ -0,0 +1,64 @@
+## first(...) selects the first element of which(...)
+first <- function(x,...)
+ {
+ w <- which(x,...)
+ if(length(x)>1)
+ w[1]
+ else
+ w
+}
+
+## first(...) selects the first element of which(...)
+last <- function(x,...)
+ {
+ w <- which(x,...)
+ if(length(x)>1)
+ rev(w)[1]
+ else
+ w
+}
+
+## non-parametric 2 dimensional approximate confidence interval
+## based on 2 dimensional histogram
+ci2d <- function(x,
+ y = NULL,
+ nbins=25,
+ ci.levels=c(0.50,0.75,0.90,0.95,0.975),
+ show=c("filled.contour","contour","image","none"),
+ xlab=deparse(substitute(x)),
+ ylab=deparse(substitute(y)),
+ col=topo.colors(length(breaks)-1),
+ ...)
+ {
+
+ show=match.arg(show)
+ breaks <- unique(c(0, ci.levels, 1.0))
+
+ h2d <- hist2d(x,y, show=FALSE, nbins=nbins, ...)
+ h2d$density <- h2d$counts / sum(h2d$counts, na.rm=TRUE)
+
+ uniqueVals <- rev(unique(sort(h2d$density)))
+ cumProbs <- sapply( uniqueVals, function(val) sum( h2d$density[h2d$density>=val] ) )
+ names(cumProbs) <- uniqueVals
+ h2d$cumDensity <- matrix(nrow=nrow(h2d$density), ncol=ncol(h2d$density))
+ h2d$cumDensity[] <- cumProbs[as.character(h2d$density)]
+
+ if(show=="image")
+ {
+ image( h2d$x, h2d$y, h2d$cumDensity, xlab=xlab, ylab=ylab, breaks=breaks, col=col)
+ }
+ else if(show=="filled.contour")
+ {
+ filled.contour(h2d$x, h2d$y, h2d$cumDensity,
+ levels=breaks,
+ col=col,
+ key.axes={axis(4, at=breaks); title("\nCI Level")}
+ )
+ }
+ else if(show=="contour")
+ contour(h2d$x, h2d$y, h2d$cumDensity, levels=breaks, nlevels=length(breaks))
+
+ h2d$contours <- contourLines(h2d$x, h2d$y, h2d$cumDensity, levels=breaks, nlevels=length(breaks))
+ names
+ invisible(h2d)
+ }
Added: trunk/gplots/man/ci2d.Rd
===================================================================
--- trunk/gplots/man/ci2d.Rd (rev 0)
+++ trunk/gplots/man/ci2d.Rd 2011-08-25 03:10:05 UTC (rev 1474)
@@ -0,0 +1,76 @@
+\name{ci2d}
+\alias{ci2d}
+\title{
+ Create 2-dimensional empirical confidence regions
+}
+\description{
+ Create 2-dimensional empirical confidence regions based on a
+ 2-dimensoinal histogram.
+}
+\usage{
+ci2d(x, y=NULL,
+ nbins=25,
+ ci.levels=c(0.5, 0.75, 0.9, 0.95, 0.975),
+ show=c("filled.contour", "contour", "image", "none"),
+ xlab=deparse(substitute(x)),
+ ylab=deparse(substitute(y)),
+ col=topo.colors(length(breaks) - 1),
+ ...)
+}
+\arguments{
+ \item{x}{either a vector containing the x coordinates
+ or a matrix with 2 columns. }
+ \item{y}{a vector contianing the y coordinates, not required if `x'
+ is matrix}
+ \item{nbins}{number of bins in each dimension. May be a scalar or a
+ 2 element vector. Defaults to 25.}
+ \item{ci.levels}{Confidence level(s) to use for plotting
+ data. Defaults to \code{c(0.5, 0.75, 0.9, 0.95, 0.975)} }
+ \item{show}{Plot type to be displaed. One of "filled.contour",
+ "contour", "image", or "none". Defaults to "filled.contour".}
+ \item{xlab, ylab}{Axis labels}
+ \item{col}{Colors to use for plots.}
+ \item{\dots}{Additional arguments passed to \code{hist2d}. }
+}
+\details{
+ This function utilizes \code{hist2d} to create a 2-dimensional
+ histogram of the data passed as an argument. This data is then
+ converted into densities and used to create and display confidence
+ regions.
+}
+\value{
+ A list containing 3 elements:
+ \item{counts}{Matrix containing the number of points falling into each
+ bin}
+ \item{x}{lower x limit of each bin}
+ \item{y}{lower y limit of each bin}
+ \item{density}{Matrix containing the probability density of each bin (count in bin/total
+ count)}
+ \item{cumDensity}{Matrix where each element contains the cumulative probability density
+ of all elements with the same density (used to create the confidence
+ region plots) }
+ \item{contours}{Contours of each confidence region}
+}
+%\references{
+%}
+\author{ Gregory R. Warnes \email{gr...@wa...}}
+\seealso{
+ \code{\link{hist2d}}
+}
+\examples{
+ # example data, bivariate normal, no correlation
+ x <- rnorm(2000, sd=4)
+ y <- rnorm(2000, sd=1)
+
+ # 2-d confidence intervals based on 2d histogram
+ ci2d(x,y)
+
+ # same scale for each axis, this looks oval
+ ci2d(x,y, same.scale=TRUE)
+
+}
+% Add one or more standard keywords, see file 'KEYWORDS' in the
+% R documentation directory.
+\keyword{dplot}
+\keyword{hplot}
+\keyword{nonparametric}
Modified: trunk/gplots/man/rich.colors.Rd
===================================================================
--- trunk/gplots/man/rich.colors.Rd 2011-08-16 01:17:20 UTC (rev 1473)
+++ trunk/gplots/man/rich.colors.Rd 2011-08-25 03:10:05 UTC (rev 1474)
@@ -15,7 +15,7 @@
black-blue-white.}
\item{alpha}{alpha transparency, from 0 (fully transparent) to 1
(opaque).}
- \item{rgb}{if ‘TRUE’ then a matrix of RGBA values is included as an
+ \item{rgb}{if \code{TRUE} then a matrix of RGBA values is included as an
attribute.}
\item{plot}{whether to plot a descriptive color diagram.}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|