r2d2/0000755000176200001440000000000012316320575011027 5ustar liggesusersr2d2/NAMESPACE0000644000176200001440000000055012316270660012245 0ustar liggesusersimportFrom(KernSmooth, dpik, bkde2D) importFrom(MASS, bandwidth.nrd, kde2d) importFrom(sp, over, Polygon, Polygons, SpatialPoints, SpatialPolygons) export(conf2d, conf2d.default, conf2d.formula, conf2d_int, freq2d, freq2d.default, freq2d.formula) S3method(conf2d, default) S3method(conf2d, formula) S3method(freq2d, default) S3method(freq2d, formula) r2d2/NEWS0000644000176200001440000000031512316270660011524 0ustar liggesusers-------------------------------------------------------------------------------- r2d2 1.0-0 (2014-03-31) -------------------------------------------------------------------------------- o Initial release. r2d2/data/0000755000176200001440000000000012316270705011737 5ustar liggesusersr2d2/data/saithe.rda0000644000176200001440000001635312316270705013714 0ustar liggesusers]\Uօ$@]$ :  'H> 38wO깿W՝ SZSW{>{sޣ`3oÆgX9&=c ]Ծgd&9gq͏ {w&զmv![P;/n+#bn| *Ş_lKk܍OmɮS8Ο8ˉW+~)aoI.zyZֽqW2UŚ}}R~woJqä|r&?s?Y/,n]|.+IU9,L<[|=_ )͋/{'t{!);{e֯odO.vNߎOJbnII̷+l$]t=~̘?YJoz~W{u}q{&Ik׌YCcGK7;`9ހ4iΟ_+v=#m|mNZY.7ksOxⱻזRTz7p_^%۸EaV 77 _#$wOfvyxߚIʔ>^P5/싉tn\/Rڰ=wpT]ecIk6z/ ()Ol&}Sri]AFI#Yuأ'7盈@]~G?gs*BUM0m&1.dɨ$;ΥmH7./8'_i6Qīhgo(&6}A )\#-ť֘rftf|,F2>RN6 qm#O6^T:U)`I𰄄5nƒQojpPf®+/dވODb;,9IvaR֛Pt)t3q3~}/{-%1RZ8:[b=n1qd-R:!I'b|*ٲxWI6󊻴=xҤWKIxv9[I[y`;$?/)9xu?3Q4+,%9tהOόs(,pq[%ergǔ_KmwI |i[:t+aO#f?^Xr.&PvT5GwMLBijoI]I$s+%yM$9<[n@y'^ 4RܽDdީﺆ.Km+>)[(%>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seMethod("conf2d") } conf2d.formula <- function(formula, data, subset, ...) { m <- match.call(expand.dots=FALSE) if(is.matrix(eval(m$data,parent.frame()))) m$data <- as.data.frame(data) m$... <- NULL m[[1L]] <- as.name("model.frame") mf <- eval(m, parent.frame()) conf2d.default(mf[2:1], ...) } conf2d.default <- function(x, y, level=0.95, n=200, method="wand", shape=1, smooth=50, plot=TRUE, add=FALSE, xlab=NULL, ylab=NULL, col.points="gray", col="black", lwd=2, ...) { method <- match.arg(tolower(method), c("wand","mass")) ## 1 Extract data if(is.matrix(x)) x <- as.data.frame(x) if(is.list(x)) # data.frame or list { xlab <- if(is.null(xlab)) names(x)[1] else xlab ylab <- if(is.null(ylab)) names(x)[2] else ylab y <- x[[2]] x <- x[[1]] } ## 2 Plot xy scatter if(plot && !add) { if(is.null(xlab)) xlab <- deparse(substitute(x)) if(is.null(ylab)) ylab <- deparse(substitute(y)) plot(x, y, xlab=xlab, ylab=ylab, lwd=1, col=col.points, ...) } ## 3 Compute surface matrix smooth <- rep(smooth, length.out=2) if(method == "wand") { bandwidth <- c(dpik(x), dpik(y)) surf <- bkde2D(cbind(x,y), bandwidth=1.3*bandwidth*shape, gridsize=c(smooth,smooth)) } else # mass { bandwidth <- c(bandwidth.nrd(x), bandwidth.nrd(y)) lx <- range(x) + c(-0.05,0.05)*diff(range(x)) ly <- range(y) + c(-0.05,0.05)*diff(range(y)) surf <- kde2d(x, y, h=bandwidth*shape, n=5*smooth/shape, lims=c(lx,ly)) } ## 4 Find best region output <- conf2d_int(x, y, surf, level, n) if(abs(level-output$prop) > 0.01) warning("appropriate region not found (desired level=", level, ", but actual prop=", round(output$prop,3), ")") ## 5 Overlay polygon if(plot || add) { polygon(output, lwd=lwd, border=col, col=NA, ...) return(invisible(output)) } else { return(output) } } conf2d_int <- function(x, y, surf, level, n) { ## 1 Slice surface into candidate regions cl <- contourLines(surf[[1]], surf[[2]], surf[[3]], nlevels=n) # candidate regions as contour lines ## 2 Find best region by counting the points inside pts <- SpatialPoints(cbind(x,y)) spols <- list() # spatial polygons representing candidate regions pin <- numeric(length(cl)) # number of points inside each region for(i in seq_len(length(cl))) { spol <- tryCatch(Polygon(cbind(cl[[i]]$x,cl[[i]]$y)), error=function(...) NA) spol <- tryCatch(Polygons(list(spol),ID=" "), error=function(...) NA) spol <- tryCatch(SpatialPolygons(list(spol)), error=function(...) NA) pin[i] <- tryCatch(sum(!is.na(over(pts,spol))), error=function(...) 0) spols[[i]] <- spol } pin <- pin / length(x) best <- which.min(abs(pin-level)) ## 3 Extract statistics best.spol <- spols[[best]]@polygons[[1]]@Polygons[[1]] xcoords <- best.spol@coords[,1] ycoords <- best.spol@coords[,2] inside <- !is.na(over(pts,spols[[best]])) area <- best.spol@area prop <- pin[best] output <- list(x=xcoords, y=ycoords, inside=inside, area=area, prop=prop) return(output) } r2d2/R/freq2d.R0000644000176200001440000000474212316270660012544 0ustar liggesusersfreq2d <- function(x, ...) { UseMethod("freq2d") } freq2d.formula <- function(formula, data, subset, ...) { m <- match.call(expand.dots=FALSE) if(is.matrix(eval(m$data,parent.frame()))) m$data <- as.data.frame(data) m$... <- NULL m[[1L]] <- as.name("model.frame") mf <- eval(m, parent.frame()) freq2d.default(mf[2:1], ...) } freq2d.default <- function(x, y, n=20, pad=0, layout=1, print=TRUE, dnn=NULL, ...) { method <- match.arg(as.character(layout), c("1","2","3")) dnn <- if(!is.null(dnn)) rep(dnn,length.out=2) else NULL xname <- dnn[1] yname <- dnn[2] ## 1 Extract data if(is.matrix(x)) x <- as.data.frame(x) if(is.list(x)) # data.frame or list { xname <- if(is.null(xname)) names(x)[1] else xname yname <- if(is.null(yname)) names(x)[2] else yname y <- x[[2]] x <- x[[1]] } ## 2 Create grid n <- rep(n, length.out=2) xmid <- pretty(x, n=n[1]) xstep <- diff(xmid)[1] xgrid <- c(xmid-0.5*xstep, max(xmid)+0.5*xstep) ymid <- pretty(y, n=n[2]) ystep <- diff(ymid)[1] ygrid <- c(ymid-0.5*ystep, max(ymid)+0.5*ystep) ## 3 Map data on grid xfac <- cut(x, xgrid, include.lowest=TRUE, labels=format(xmid)) if(is.null(xname)) xname <- deparse(substitute(x)) yfac <- cut(y, ygrid, include.lowest=TRUE, labels=format(ymid)) if(is.null(yname)) yname <- deparse(substitute(y)) z <- table(xfac, yfac, dnn=c(xname,yname)) ## 4 Remove existing edges with only zeros z <- z[cumsum(rowSums(z))>0, cumsum(colSums(z))>0] z <- z[rev(cumsum(rev(rowSums(z))))>0, rev(cumsum(rev(colSums(z))))>0] ## 5 Add edges with only zeros for(i in seq_len(pad)) { tmp <- cbind(0, rbind(0, z, 0), 0) rownames(tmp)[c(1,nrow(tmp))] <- as.numeric(rownames(z)[c(1,nrow(z))]) + c(-xstep,xstep) colnames(tmp)[c(1,ncol(tmp))] <- as.numeric(colnames(z)[c(1,ncol(z))]) + c(-xstep,xstep) names(dimnames(tmp)) <- names(dimnames(z)) z <- tmp } ## 5 Prepare output xnum <- as.numeric(rownames(z)) ynum <- as.numeric(colnames(z)) if(layout == 1) { output <- t(z)[ncol(z):1,] if(print) { print.table(output, zero.print=".") return(invisible(output)) } else { return(output) } } else if(layout == 2) { output <- list(x=xnum, y=ynum, z=z) return(output) } else # layout 3 { output <- data.frame(x=rep(xnum,length(ynum)), y=rep(ynum,each=length(xnum)), z=c(z)) names(output) <- make.names(c(xname,yname,"Freq"), unique=TRUE) return(output) } } r2d2/MD50000644000176200001440000000106512316320575011341 0ustar liggesusers6a39bc8d7a60c71906f3bf1b0fc88d11 *DESCRIPTION d2e8721365686d928b7f216f71da653c *NAMESPACE 9a2e47fbb805257e174ee47575135cce *NEWS fd242c6d5d1dec4d88cbe9a3b7bd8986 *R/conf2d.R cc18cd427cbd05fc23623e1e6ffb67b5 *R/freq2d.R ad3b1db67fc6b6dffbba9b3c11514b99 *data/Ushape.rda fa282497b11e9e6cf9ebcd1fac29e02a *data/saithe.rda 87d865b3b578ce618c48d4bd690ed4cb *man/Ushape.Rd 12a277321ff96d7fb4ff30751462545e *man/conf2d.Rd 841739748ac1d92dc2b0a3473a7ef953 *man/freq2d.Rd fdef20a29c14094f5f65d006d8f69a85 *man/r2d2-package.Rd 731694614ea98b5712830c650c212095 *man/saithe.Rd r2d2/DESCRIPTION0000644000176200001440000000155212316320575012540 0ustar liggesusersPackage: r2d2 Version: 1.0-0 Date: 2014-03-31 Title: Bivariate (Two-Dimensional) Confidence Region and Frequency Distribution Authors@R: c(person("Arni", "Magnusson", role="aut"), person("Julian", "Burgos", role=c("aut","cre"), email="julian@hafro.is"), person(c("Gregory","R."), "Warnes", role="ctb")) Author: Arni Magnusson [aut], Julian Burgos [aut, cre], Gregory R. Warnes [ctb] Maintainer: Julian Burgos Imports: KernSmooth, MASS, sp Suggests: cluster, lattice LazyData: yes Description: This package provides generic functions to analyze the distribution of two continuous variables: 'conf2d' to calculate a smooth empirical confidence region, and 'freq2d' to calculate a frequency distribution. License: GPL (>= 2) Packaged: 2014-03-31 13:46:45 UTC; julian NeedsCompilation: no Repository: CRAN Date/Publication: 2014-03-31 19:10:21 r2d2/man/0000755000176200001440000000000012316270660011601 5ustar liggesusersr2d2/man/conf2d.Rd0000644000176200001440000001120312316270660013240 0ustar liggesusers\name{conf2d} \alias{conf2d} \alias{conf2d.default} \alias{conf2d.formula} \alias{conf2d_int} \title{Bivariate (Two-Dimensional) Confidence Region} \description{ Calculate an empirical confidence region for two variables, and optionally overlay the smooth polygon on a scatterplot. } \usage{ conf2d(x, \dots) \method{conf2d}{formula}(formula, data, subset, \dots) \method{conf2d}{default}(x, y, level=0.95, n=200, method="wand", shape=1, smooth=50, plot=TRUE, add=FALSE, xlab=NULL, ylab=NULL, col.points="gray", col="black", lwd=2, \dots) conf2d_int(x, y, surf, level, n) # internal function } \arguments{ \item{x}{a vector of x values, or a data frame whose first two columns contain the x and y values.} \item{y}{a vector of y values.} \item{formula}{a \code{\link{formula}}, such as \code{y~x}.} \item{data}{a \code{data.frame}, \code{matrix}, or \code{list} from which the variables in \code{formula} should be taken.} \item{subset}{an optional vector specifying a subset of observations to be used.} \item{level}{the proportion of points that should be inside the region.} \item{n}{the number of regions to evaluate, before choosing the region that matches \code{level} best.} \item{method}{kernel smoothing function to use: \code{"wand"} or \code{"mass"}.} \item{shape}{a bandwidth scaling factor, affecting the polygon shape.} \item{smooth}{the number of bins (scalar or vector of length 2), affecting the polygon smoothness.} \item{plot}{whether to plot a scatterplot and overlay the region as a polygon.} \item{add}{whether to add a polygon to an existing plot.} \item{xlab}{a label for the x axis.} \item{ylab}{a label for the y axis.} \item{col.points}{color of points.} \item{col}{color of polygon.} \item{lwd}{line width of polygon.} \item{\dots}{further arguments passed to \code{plot} and \code{polygon}.} \item{surf}{a list whose first three elements are x coordinates, y coordinates, and a surface matrix.} } \details{ This function constructs a large number (\code{n}) of smooth polygons, and then chooses the polygon that comes closest to containing a given proportion (\code{level}) of the total points. The default \code{method="wand"} calls the \code{\link[KernSmooth]{bkde2D}} kernel smoother from the \pkg{KernSmooth} package, while \code{method="mass"} calls \code{\link[MASS]{kde2d}} from the \pkg{MASS} package. The \code{conf2d} function calls \code{bkde2D} or \code{kde2d} to compute a smooth surface from \code{x} and {y}. If users already have a smoothed surface to work from, the internal \code{conf2d_int} can be used directly to find the empirical confidence region that matches \code{level} best. } \value{ List containing five elements: \item{x}{x coordinates defining the region.} \item{y}{y coordinates defining the region.} \item{inside}{logical vector indicating which of the original data coordinates are inside the region.} \item{area}{area inside the region.} \item{prop}{actual proportion of points inside the region.} } \note{ The \code{area} of a bivariate region is analogous to the range of a univariate interval. This allows a quantitative comparison of different confidence regions. Ellipses are a more restrictive approach to calculate an empirical bivariate confidence region. Smooth polygons make fewer assumptions about how x and y covary. The \code{conf2d} and \code{freq2d} functions are closely related. The advantage of \code{conf2d} is that it returns a region as a smooth polygon. The advantage of \code{freq2d} is that it returns a set that is guaranteed to contain the correct proportion of points, even for spatially complex datasets. } \author{ Arni Magnusson and Julian Burgos, based on an earlier function by Gregory R. Warnes. } \seealso{ \code{\link{quantile}} is the corresponding univariate equivalent. The \pkg{distfree.cr} package uses a different smoothing algorithm to calculate bivariate empirical confidence regions. \code{\link[gplots]{ci2d}} in the \pkg{gplots} package is a predecessor of \code{conf2d}. \code{\link{freq2d}} calculates a discrete frequency distribution for two continuous variables. \code{\link{r2d2-package}} gives an overview of the package. } \examples{ conf2d(Ushape)$prop conf2d(saithe, pch=16, cex=1.2, col.points=rgb(0,0,0,0.1), lwd=3) # First surface, then region plot(saithe, col="gray") surf <- MASS::kde2d(saithe$Bio, saithe$HR, h=0.25, n=100) region <- conf2d_int(saithe$Bio, saithe$HR, surf, level=0.95, n=200) polygon(region, lwd=2) } % Graphics \keyword{dplot} % Statistics \keyword{distribution} \keyword{multivariate} \keyword{smooth} r2d2/man/freq2d.Rd0000644000176200001440000000555112316270660013261 0ustar liggesusers\name{freq2d} \alias{freq2d} \alias{freq2d.default} \alias{freq2d.formula} \title{Bivariate (Two-Dimensional) Frequency Distribution} \description{ Calculate a frequency distribution for two continuous variables. } \usage{ freq2d(x, \dots) \method{freq2d}{formula}(formula, data, subset, \dots) \method{freq2d}{default}(x, y, n=20, pad=0, layout=1, print=TRUE, dnn=NULL, \dots) } \arguments{ \item{x}{a vector of x values, or a data frame whose first two columns contain the x and y values.} \item{y}{a vector of y values.} \item{formula}{a \code{\link{formula}}, such as \code{y~x}.} \item{data}{a \code{data.frame}, \code{matrix}, or \code{list} from which the variables in \code{formula} should be taken.} \item{subset}{an optional vector specifying a subset of observations to be used.} \item{n}{the desired number of bins for the output, a scalar or a vector of length 2.} \item{pad}{number of rows and columns to add to each margin, containing only zeros.} \item{layout}{one of three layouts for the output: \code{1}, \code{2}, or \code{3}.} \item{print}{whether to display the resulting matrix on the screen using dots for zeros.} \item{dnn}{the names to be given to the dimensions in the result.} \item{\dots}{named arguments to be passed to the default method.} } \details{ The exact number of bins is determined by the \code{\link{pretty}} function, based on the value of \code{n}. Padding the margins with zeros can be helpful for subsequent analysis, such as smoothing. The \code{print} logical flag only has an effect when \code{layout=1}. } \value{ The \code{layout} argument specifies one of the following formats for the binned frequency output: \enumerate{ \item{\code{matrix} that is easy to read, aligned like a scatterplot.} \item{\code{list} with three elements (x, y, matrix) that can be passed to various plotting functions.} \item{\code{data.frame} with three columns (x, y, frequency) that can be analyzed further.} } } \author{Arni Magnusson.} \seealso{ \code{\link{cut}}, \code{\link{table}}, and \code{\link{print.table}} are the basic underlying functions. \code{\link[gplots]{hist2d}} in the \pkg{gplots} package is a related function with graphical capabilities. \code{\link{conf2d}} calculates a bivariate empirical confidence region, a smooth polygon. \code{\link{r2d2-package}} gives an overview of the package. } \examples{ freq2d(Ushape) freq2d(quakes$long, quakes$lat, dnn="") freq2d(lat~long, quakes, n=c(10,20), pad=1) # Supress display freq2d(saithe) range(freq2d(saithe, print=FALSE)) # Layout, plot freq2d(saithe, layout=2) freq2d(saithe, layout=3) contour(freq2d(saithe, layout=2)) lattice::contourplot(Freq~Bio+HR, freq2d(saithe,layout=3)) } % Graphics \keyword{dplot} % Basics \keyword{manip} % Statistics \keyword{distribution} \keyword{multivariate} r2d2/man/r2d2-package.Rd0000644000176200001440000000250412316270660014233 0ustar liggesusers\name{r2d2-package} \alias{r2d2-package} \alias{r2d2} \docType{package} \title{ Bivariate (Two-Dimensional) Confidence Region and Frequency Distribution } \description{ This package provides generic functions to analyze the distribution of two continuous variables. } \details{ \emph{Bivariate calculations:} \tabular{ll}{ \code{\link{conf2d}} \tab empirical confidence region, a smooth polygon\cr \code{\link{freq2d}} \tab frequency distribution, a table } \emph{Examples:} \tabular{ll}{ \code{\link{saithe}} \tab MCMC results in two columns\cr \code{\link{Ushape}} \tab U-shaped cloud in two columns } } \author{ Arni Magnusson and Julian Burgos, based on earlier functions by Gregory R. Warnes. } \references{ Bivand, R. S., Pebesma, E., and Gomez-Rubio, V. (2013) \emph{Applied Spatial Data Analysis with R}. Second edition. New York: Springer. Venables, W. N. and Ripley, B. D. (2002) \emph{Modern Applied Statistics with S}. Fourth edition. New York: Springer. Wand, M. P. and Jones, M. C. (1995) \emph{Kernel Smoothing}. London: Chapman and Hall. } \seealso{ Combines existing tools from the \pkg{KernSmooth}, \pkg{MASS}, and \pkg{sp} packages. } % Graphics \keyword{dplot} % Basics \keyword{manip} % Statistics \keyword{distribution} \keyword{multivariate} \keyword{smooth} r2d2/man/saithe.Rd0000644000176200001440000000167612316270660013357 0ustar liggesusers\name{saithe} \alias{saithe} \docType{data} \title{MCMC Results from Saithe Assessment} \description{ Markov chain Monte Carlo results from the analysis of the saithe (\emph{Pollachius virens}) fishery in Icelandic waters. } \usage{saithe} \format{ Data frame containing 1000 rows and 2 columns: \tabular{ll}{ \code{Bio}\tab population biomass in 2013, relative to the expected long-term biomass under optimal harvest rate.\cr \code{HR}\tab harvest rate in 2013, relative to the optimal harvest rate. } } \references{ Magnusson, A. (2013) Icelandic saithe. In: \emph{Report of the North Western Working Group (NWWG)}. ICES CM 2013/ACOM:07, pp. 231--252. Magnusson, A., Punt, A. E., and Hilborn, R. (2013) Measuring uncertainty in fisheries stock assessment: the delta method, bootstrap, and MCMC. \emph{Fish and Fisheries} \bold{14}, 325--342. } \examples{ conf2d(saithe, level=0.9) freq2d(saithe) } \keyword{datasets} r2d2/man/Ushape.Rd0000644000176200001440000000060112316270660013312 0ustar liggesusers\name{Ushape} \alias{Ushape} \docType{data} \title{U-Shaped Cloud} \description{ Bivariate scatter shaped like an open circle, for testing spatial algorithms. } \usage{Ushape} \format{ Matrix containing 1000 rows and 2 columns: \tabular{ll}{ \code{x}\tab x coordinates.\cr \code{y}\tab y coordinates. } } \examples{ freq2d(Ushape) conf2d(Ushape) } \keyword{datasets}